From 088eaf5b7b121f70b2fe6b4328b7d8819c9f2aab Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sat, 20 May 2017 04:11:33 +0000 Subject: [PATCH] [index] Fix forward declarations interfering with USR generation of external source symbols Patch by Nathan Hawes. https://reviews.llvm.org/D33346 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@303484 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 5 +++++ lib/AST/DeclBase.cpp | 21 +++++++++++++++++++ lib/Index/IndexSymbol.cpp | 11 +--------- lib/Index/USRGeneration.cpp | 2 +- test/Index/Core/external-source-symbol-attr.m | 4 ++++ tools/libclang/CIndex.cpp | 11 +--------- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 29889fde96..c26e2d7bde 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -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 { diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index f6f8169261..81cd1cc426 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -407,6 +407,27 @@ bool Decl::isExported() const { return false; } +ExternalSourceSymbolAttr *Decl::getExternalSourceSymbolAttr() const { + const Decl *Definition = nullptr; + if (auto ID = dyn_cast(this)) { + Definition = ID->getDefinition(); + } else if (auto PD = dyn_cast(this)) { + Definition = PD->getDefinition(); + } else if (auto TD = dyn_cast(this)) { + Definition = TD->getDefinition(); + } + if (!Definition) + Definition = this; + + if (auto *attr = Definition->getAttr()) + return attr; + if (auto *dcd = dyn_cast(getDeclContext())) { + return dcd->getAttr(); + } + + return nullptr; +} + bool Decl::hasDefiningAttr() const { return hasAttr() || hasAttr(); } diff --git a/lib/Index/IndexSymbol.cpp b/lib/Index/IndexSymbol.cpp index 0bfa19346b..a108f86970 100644 --- a/lib/Index/IndexSymbol.cpp +++ b/lib/Index/IndexSymbol.cpp @@ -318,16 +318,7 @@ SymbolInfo index::getSymbolInfo(const Decl *D) { if (Info.Properties & (unsigned)SymbolProperty::Generic) Info.Lang = SymbolLanguage::CXX; - auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* { - if (auto *attr = D->getAttr()) - return attr; - if (auto *dcd = dyn_cast(D->getDeclContext())) { - if (auto *attr = dcd->getAttr()) - return attr; - } - return nullptr; - }; - if (auto *attr = getExternalSymAttr(D)) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { if (attr->getLanguage() == "Swift") Info.Lang = SymbolLanguage::Swift; } diff --git a/lib/Index/USRGeneration.cpp b/lib/Index/USRGeneration.cpp index 044edf715f..21054b099a 100644 --- a/lib/Index/USRGeneration.cpp +++ b/lib/Index/USRGeneration.cpp @@ -49,7 +49,7 @@ static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, static StringRef GetExternalSourceContainer(const NamedDecl *D) { if (!D) return StringRef(); - if (auto *attr = D->getAttr()) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { return attr->getDefinedIn(); } return StringRef(); diff --git a/test/Index/Core/external-source-symbol-attr.m b/test/Index/Core/external-source-symbol-attr.m index 49a075fc74..cdc5296697 100644 --- a/test/Index/Core/external-source-symbol-attr.m +++ b/test/Index/Core/external-source-symbol-attr.m @@ -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 diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index 9c795ae9c5..394ee0ec43 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -7487,16 +7487,7 @@ unsigned clang_Cursor_isExternalSymbol(CXCursor C, const Decl *D = getCursorDecl(C); - auto getExternalSymAttr = [](const Decl *D) -> ExternalSourceSymbolAttr* { - if (auto *attr = D->getAttr()) - return attr; - if (auto *dcd = dyn_cast(D->getDeclContext())) { - if (auto *attr = dcd->getAttr()) - return attr; - } - return nullptr; - }; - if (auto *attr = getExternalSymAttr(D)) { + if (auto *attr = D->getExternalSourceSymbolAttr()) { if (language) *language = cxstring::createDup(attr->getLanguage()); if (definedIn) -- 2.40.0