Author: akirtzidis Date: Fri May 19 23:11:33 2017 New Revision: 303484 URL: http://llvm.org/viewvc/llvm-project?rev=303484&view=rev Log: [index] Fix forward declarations interfering with USR generation of external source symbols
Patch by Nathan Hawes. https://reviews.llvm.org/D33346 Modified: cfe/trunk/include/clang/AST/DeclBase.h cfe/trunk/lib/AST/DeclBase.cpp cfe/trunk/lib/Index/IndexSymbol.cpp cfe/trunk/lib/Index/USRGeneration.cpp cfe/trunk/test/Index/Core/external-source-symbol-attr.m cfe/trunk/tools/libclang/CIndex.cpp Modified: cfe/trunk/include/clang/AST/DeclBase.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/include/clang/AST/DeclBase.h (original) +++ cfe/trunk/include/clang/AST/DeclBase.h Fri May 19 23:11:33 2017 @@ -34,6 +34,7 @@ class DeclarationName; class DependentDiagnostic; class EnumDecl; class ExportDecl; +class ExternalSourceSymbolAttr; class FunctionDecl; class FunctionType; enum Linkage : unsigned char; @@ -562,6 +563,10 @@ public: NextInContextAndBits.setInt(Bits); } + /// \brief Looks on this and related declarations for an applicable + /// external source symbol attribute. + ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const; + /// \brief Whether this declaration was marked as being private to the /// module in which it was defined. bool isModulePrivate() const { Modified: cfe/trunk/lib/AST/DeclBase.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/lib/AST/DeclBase.cpp (original) +++ cfe/trunk/lib/AST/DeclBase.cpp Fri May 19 23:11:33 2017 @@ -407,6 +407,27 @@ bool Decl::isExported() const { return false; } +ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const { + const Decl *Definition = nullptr; + if (auto ID = dyn_cast<ObjCInterfaceDecl>(this)) { + Definition = ID->getDefinition(); + } else if (auto PD = dyn_cast<ObjCProtocolDecl>(this)) { + Definition = PD->getDefinition(); + } else if (auto TD = dyn_cast<TagDecl>(this)) { + Definition = TD->getDefinition(); + } + if (!Definition) + Definition = this; + + if (auto *attr = Definition->getAttr<ExternalSourceSymbolAttr>()) + return attr; + if (auto *dcd = dyn_cast<Decl>(getDeclContext())) { + return dcd->getAttr<ExternalSourceSymbolAttr>(); + } + + return nullptr; +} + bool Decl::hasDefiningAttr() const { return hasAttr<AliasAttr>() || hasAttr<IFuncAttr>(); } Modified: cfe/trunk/lib/Index/IndexSymbol.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexSymbol.cpp?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/lib/Index/IndexSymbol.cpp (original) +++ cfe/trunk/lib/Index/IndexSymbol.cpp Fri May 19 23:11:33 2017 @@ -318,16 +318,7 @@ SymbolInfo index::getSymbolInfo(const De if (Info.Properties & (unsigned)SymbolProperty::Generic) Info.Lang = SymbolLanguage::CXX; - auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* { - if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) - return attr; - if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) { - if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>()) - return attr; - } - return nullptr; - }; - if (auto *attr = getExternalSymAttr(D)) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { if (attr->getLanguage() == "Swift") Info.Lang = SymbolLanguage::Swift; } Modified: cfe/trunk/lib/Index/USRGeneration.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/USRGeneration.cpp?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/lib/Index/USRGeneration.cpp (original) +++ cfe/trunk/lib/Index/USRGeneration.cpp Fri May 19 23:11:33 2017 @@ -49,7 +49,7 @@ static bool printLoc(llvm::raw_ostream & static StringRef GetExternalSourceContainer(const NamedDecl *D) { if (!D) return StringRef(); - if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { return attr->getDefinedIn(); } return StringRef(); Modified: cfe/trunk/test/Index/Core/external-source-symbol-attr.m URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/external-source-symbol-attr.m?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/test/Index/Core/external-source-symbol-attr.m (original) +++ cfe/trunk/test/Index/Core/external-source-symbol-attr.m Fri May 19 23:11:33 2017 @@ -4,6 +4,10 @@ #define GEN_DECL(mod_name) __attribute__((external_source_symbol(language="Swift", defined_in=mod_name, generated_declaration))) #define PUSH_GEN_DECL(mod_name) push(GEN_DECL(mod_name), apply_to=any(enum, objc_interface, objc_category, objc_protocol)) +// Forward declarations should not affect module namespacing below +@class I1; +@class I2; + // This should not be indexed. GEN_DECL("some_module") @interface I1 Modified: cfe/trunk/tools/libclang/CIndex.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=303484&r1=303483&r2=303484&view=diff ============================================================================== --- cfe/trunk/tools/libclang/CIndex.cpp (original) +++ cfe/trunk/tools/libclang/CIndex.cpp Fri May 19 23:11:33 2017 @@ -7487,16 +7487,7 @@ unsigned clang_Cursor_isExternalSymbol(C const Decl *D = getCursorDecl(C); - auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* { - if (auto *attr = D->getAttr<ExternalSourceSymbolAttr>()) - return attr; - if (auto *dcd = dyn_cast<Decl>(D->getDeclContext())) { - if (auto *attr = dcd->getAttr<ExternalSourceSymbolAttr>()) - return attr; - } - return nullptr; - }; - if (auto *attr = getExternalSymAttr(D)) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { if (language) *language = cxstring::createDup(attr->getLanguage()); if (definedIn) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits