Index: include/clang/Index/IndexSymbol.h =================================================================== --- include/clang/Index/IndexSymbol.h +++ include/clang/Index/IndexSymbol.h @@ -16,6 +16,7 @@ namespace clang { class Decl; + class ExternalSourceSymbolAttr; class LangOptions; namespace index { @@ -132,6 +133,8 @@ bool isFunctionLocalSymbol(const Decl *D); +ExternalSourceSymbolAttr *getExternalSourceSymbolAttr(const Decl *D); + void applyForEachSymbolRole(SymbolRoleSet Roles, llvm::function_ref Fn); bool applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles, Index: lib/Index/IndexSymbol.cpp =================================================================== --- lib/Index/IndexSymbol.cpp +++ lib/Index/IndexSymbol.cpp @@ -80,6 +80,30 @@ return true; } +ExternalSourceSymbolAttr *index::getExternalSourceSymbolAttr(const Decl *D) { + if (!D) + return nullptr; + + const Decl *Definition = nullptr; + if (auto ID = dyn_cast(D)) { + Definition = ID->getDefinition(); + } else if (auto PD = dyn_cast(D)) { + Definition = PD->getDefinition(); + } else if (auto TD = dyn_cast(D)) { + Definition = TD->getDefinition(); + } + if (!Definition) + Definition = D; + + if (auto *attr = Definition->getAttr()) + return attr; + if (auto *dcd = dyn_cast(D->getDeclContext())) { + return dcd->getAttr(); + } + + return nullptr; +} + SymbolInfo index::getSymbolInfo(const Decl *D) { assert(D); SymbolInfo Info; @@ -318,16 +342,7 @@ 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 = getExternalSourceSymbolAttr(D)) { if (attr->getLanguage() == "Swift") Info.Lang = SymbolLanguage::Swift; } Index: lib/Index/USRGeneration.cpp =================================================================== --- lib/Index/USRGeneration.cpp +++ lib/Index/USRGeneration.cpp @@ -11,6 +11,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/DeclVisitor.h" +#include "clang/Index/IndexSymbol.h" #include "clang/Lex/PreprocessingRecord.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" @@ -47,9 +48,7 @@ } static StringRef GetExternalSourceContainer(const NamedDecl *D) { - if (!D) - return StringRef(); - if (auto *attr = D->getAttr()) { + if (auto *attr = getExternalSourceSymbolAttr(D)) { return attr->getDefinedIn(); } return StringRef(); Index: test/Index/Core/external-source-symbol-attr.m =================================================================== --- test/Index/Core/external-source-symbol-attr.m +++ 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