diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp --- a/clang-tools-extra/clangd/Hover.cpp +++ b/clang-tools-extra/clangd/Hover.cpp @@ -22,6 +22,7 @@ #include "clang/AST/Decl.h" #include "clang/AST/DeclBase.h" #include "clang/AST/DeclCXX.h" +#include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" @@ -60,12 +61,63 @@ return Policy; } +static llvm::StringRef getNameForObjCInterface(const ObjCInterfaceDecl *ID) { + return ID ? ID->getName() : "<>"; +} + +static std::string objcMethodLocalScope(const ObjCMethodDecl *Method) { + std::string Name; + llvm::raw_string_ostream OS(Name); + const ObjCInterfaceDecl *Class = Method->getClassInterface(); + + OS << (Method->isInstanceMethod() ? '-' : '+') << '[' + << getNameForObjCInterface(Class); + + if (const ObjCCategoryImplDecl *CID = + dyn_cast(Method->getDeclContext())) + OS << '(' << *CID << ')'; + + OS << ' ' << Method->getSelector().getAsString(); + if (Method->isVariadic()) + OS << ", ..."; + + OS << ']'; + return Name; +} + +static std::string objcContainerLocalScope(const ObjCContainerDecl *C) { + if (const ObjCCategoryDecl *Category = dyn_cast(C)) { + std::string Name; + llvm::raw_string_ostream OS(Name); + const ObjCInterfaceDecl *Class = Category->getClassInterface(); + OS << getNameForObjCInterface(Class) << '(' << Category->getName() << ')'; + return Name; + } + if (const ObjCCategoryImplDecl *CI = dyn_cast(C)) { + std::string Name; + llvm::raw_string_ostream OS(Name); + const ObjCInterfaceDecl *Class = CI->getClassInterface(); + OS << getNameForObjCInterface(Class) << '(' << *CI << ')'; + return Name; + } + return C->getNameAsString(); +} + /// Given a declaration \p D, return a human-readable string representing the /// local scope in which it is declared, i.e. class(es) and method name. Returns /// an empty string if it is not local. std::string getLocalScope(const Decl *D) { std::vector Scopes; const DeclContext *DC = D->getDeclContext(); + + // ObjC scopes won't have multiple components for us to join, instead: + // - Methods: "-[Class methodParam1:methodParam2]" + // - Classes, categories, and protocols: "MyClass(Category)" + if (const ObjCMethodDecl *MD = dyn_cast(DC)) + return objcMethodLocalScope(MD); + else if (const ObjCContainerDecl *CD = dyn_cast(DC)) + return objcContainerLocalScope(CD); + auto GetName = [](const TypeDecl *D) { if (!D->getDeclName().isEmpty()) { PrintingPolicy Policy = D->getASTContext().getPrintingPolicy(); @@ -92,6 +144,11 @@ std::string getNamespaceScope(const Decl *D) { const DeclContext *DC = D->getDeclContext(); + // ObjC does not have the concept of namespaces, so we return an empty string + // and instead support local scopes. + if (isa(DC) || isa(DC)) + return ""; + if (const TagDecl *TD = dyn_cast(DC)) return getNamespaceScope(TD); if (const FunctionDecl *FD = dyn_cast(DC)) diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp --- a/clang-tools-extra/clangd/unittests/HoverTests.cpp +++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp @@ -1988,7 +1988,8 @@ HI.Name = "data"; HI.Type = "char"; HI.Kind = index::SymbolKind::Field; - HI.NamespaceScope = "ObjC::"; // FIXME: fix it + HI.LocalScope = "ObjC::"; + HI.NamespaceScope = ""; HI.Definition = "char data"; }}, { @@ -2019,6 +2020,86 @@ HI.NamespaceScope = ""; HI.Definition = "@interface MYObject\n@end"; }}, + { + R"cpp( + @interface MYObject + @end + @interface MYObject (Private) + @property(nonatomic, assign) int privateField; + @end + + int someFunction() { + MYObject *obj = [MYObject sharedInstance]; + return obj.[[private^Field]]; + } + )cpp", + [](HoverInfo &HI) { + HI.Name = "privateField"; + HI.Kind = index::SymbolKind::InstanceProperty; + HI.LocalScope = "MYObject(Private)::"; + HI.NamespaceScope = ""; + HI.Definition = "@property(nonatomic, assign, unsafe_unretained, " + "readwrite) int privateField;"; + }}, + { + R"cpp( + @protocol MYProtocol + @property(nonatomic, assign) int prop1; + @end + + int someFunction() { + id obj = 0; + return obj.[[pro^p1]]; + } + )cpp", + [](HoverInfo &HI) { + HI.Name = "prop1"; + HI.Kind = index::SymbolKind::InstanceProperty; + HI.LocalScope = "MYProtocol::"; + HI.NamespaceScope = ""; + HI.Definition = "@property(nonatomic, assign, unsafe_unretained, " + "readwrite) int prop1;"; + }}, + {R"objc( + @interface Foo + @end + + @implementation Foo(Private) + + (int)somePrivateMethod { + int [[res^ult]] = 2; + return result; + } + @end + )objc", + [](HoverInfo &HI) { + HI.Name = "result"; + HI.Definition = "int result = 2"; + HI.Kind = index::SymbolKind::Variable; + HI.Type = "int"; + HI.LocalScope = "+[Foo(Private) somePrivateMethod]::"; + HI.NamespaceScope = ""; + HI.Value = "2"; + }}, + {R"objc( + @interface Foo + @end + + @implementation Foo + - (int)variadicArgMethod:(id)first, ... { + int [[res^ult]] = 0; + return result; + } + @end + )objc", + [](HoverInfo &HI) { + HI.Name = "result"; + HI.Definition = "int result = 0"; + HI.Kind = index::SymbolKind::Variable; + HI.Type = "int"; + HI.LocalScope = "-[Foo variadicArgMethod:, ...]::"; + HI.NamespaceScope = ""; + HI.Value = "0"; + }}, }; // Create a tiny index, so tests above can verify documentation is fetched.