// * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
auto InTopLevelScope = hasDeclContext(
anyOf(namespaceDecl(), translationUnitDecl(), linkageSpecDecl()));
- if (match(decl(allOf(Opts.IndexMainFiles
- ? decl()
- : decl(unless(isExpansionInMainFile())),
+ if (match(decl(allOf(unless(isExpansionInMainFile()),
anyOf(InTopLevelScope,
hasDeclContext(enumDecl(InTopLevelScope,
unless(isScoped())))))),
// For symbols defined inside macros:
// * use expansion location, if the symbol is formed via macro concatenation.
// * use spelling location, otherwise.
-llvm::Optional<SymbolLocation>
-getSymbolLocation(const NamedDecl &D, SourceManager &SM,
- const SymbolCollector::Options &Opts,
- const clang::LangOptions& LangOpts,
- std::string &FileURIStorage) {
+llvm::Optional<SymbolLocation> getSymbolLocation(
+ const NamedDecl &D, SourceManager &SM, const SymbolCollector::Options &Opts,
+ const clang::LangOptions &LangOpts, std::string &FileURIStorage) {
SourceLocation SpellingLoc = SM.getSpellingLoc(D.getLocation());
if (D.getLocation().isMacroID()) {
std::string PrintLoc = SpellingLoc.printToString(SM);
return std::move(Result);
}
+// Checks whether \p ND is a definition of a TagDecl (class/struct/enum/union)
+// in a header file, in which case clangd would prefer to use ND as a canonical
+// declaration.
+// FIXME: handle symbol types that are not TagDecl (e.g. functions), if using
+// the the first seen declaration as canonical declaration is not a good enough
+// heuristic.
+bool isPreferredDeclaration(const NamedDecl &ND, index::SymbolRoleSet Roles) {
+ using namespace clang::ast_matchers;
+ return (Roles & static_cast<unsigned>(index::SymbolRole::Definition)) &&
+ llvm::isa<TagDecl>(&ND) &&
+ match(decl(isExpansionInMainFile()), ND, ND.getASTContext()).empty();
+}
+
} // namespace
SymbolCollector::SymbolCollector(Options Opts) : Opts(std::move(Opts)) {}
if (index::generateUSRForDecl(ND, USR))
return true;
+ const NamedDecl &OriginalDecl = *cast<NamedDecl>(ASTNode.OrigD);
auto ID = SymbolID(USR);
- const Symbol* BasicSymbol = Symbols.find(ID);
+ const Symbol *BasicSymbol = Symbols.find(ID);
if (!BasicSymbol) // Regardless of role, ND is the canonical declaration.
BasicSymbol = addDeclaration(*ND, std::move(ID));
+ else if (isPreferredDeclaration(OriginalDecl, Roles))
+ // If OriginalDecl is preferred, replace the existing canonical
+ // declaration (e.g. a class forward declaration). There should be at most
+ // one duplicate as we expect to see only one preferred declaration per
+ // TU, because in practice they are definitions.
+ BasicSymbol = addDeclaration(OriginalDecl, std::move(ID));
+
if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
- addDefinition(*cast<NamedDecl>(ASTNode.OrigD), *BasicSymbol);
+ addDefinition(OriginalDecl, *BasicSymbol);
}
return true;
}
std::tie(S.Scope, S.Name) = splitQualifiedName(QName);
S.SymInfo = index::getSymbolInfo(&ND);
std::string FileURI;
- // FIXME: we may want a different "canonical" heuristic than clang chooses.
- // Clang seems to choose the first, which may not have the most information.
if (auto DeclLoc =
getSymbolLocation(ND, SM, Opts, ASTCtx->getLangOpts(), FileURI))
S.CanonicalDeclaration = *DeclLoc;
}
MATCHER_P(QName, Name, "") { return (arg.Scope + arg.Name).str() == Name; }
MATCHER_P(DeclURI, P, "") { return arg.CanonicalDeclaration.FileURI == P; }
+MATCHER_P(DefURI, P, "") { return arg.Definition.FileURI == P; }
MATCHER_P(IncludeHeader, P, "") {
return arg.Detail && arg.Detail->IncludeHeader == P;
}
};
TEST_F(SymbolCollectorTest, CollectSymbols) {
- CollectorOpts.IndexMainFiles = true;
const std::string Header = R"(
class Foo {
void f();
inline void f2() {}
static const int KInt = 2;
const char* kStr = "123";
- )";
- const std::string Main = R"(
+
namespace {
void ff() {} // ignore
}
using bar::v2;
} // namespace foo
)";
- runSymbolCollector(Header, Main);
+ runSymbolCollector(Header, /*Main=*/"");
EXPECT_THAT(Symbols,
UnorderedElementsAreArray(
{QName("Foo"), QName("f1"), QName("f2"), QName("KInt"),
}
TEST_F(SymbolCollectorTest, Locations) {
- CollectorOpts.IndexMainFiles = true;
Annotations Header(R"cpp(
// Declared in header, defined in main.
extern int $xdecl[[X]];
void $printdef[[print]]() {}
// Declared/defined in main only.
- int $y[[Y]];
+ int Y;
)cpp");
runSymbolCollector(Header.code(), Main.code());
EXPECT_THAT(
DefRange(Main.offsetRange("clsdef"))),
AllOf(QName("print"), DeclRange(Header.offsetRange("printdecl")),
DefRange(Main.offsetRange("printdef"))),
- AllOf(QName("Z"), DeclRange(Header.offsetRange("zdecl"))),
- AllOf(QName("Y"), DeclRange(Main.offsetRange("y")),
- DefRange(Main.offsetRange("y")))));
+ AllOf(QName("Z"), DeclRange(Header.offsetRange("zdecl")))));
}
TEST_F(SymbolCollectorTest, SymbolRelativeNoFallback) {
- CollectorOpts.IndexMainFiles = false;
runSymbolCollector("class Foo {};", /*Main=*/"");
- EXPECT_THAT(Symbols,
- UnorderedElementsAre(AllOf(QName("Foo"), DeclURI(TestHeaderURI))));
+ EXPECT_THAT(Symbols, UnorderedElementsAre(
+ AllOf(QName("Foo"), DeclURI(TestHeaderURI))));
}
TEST_F(SymbolCollectorTest, SymbolRelativeWithFallback) {
- CollectorOpts.IndexMainFiles = false;
TestHeaderName = "x.h";
TestFileName = "x.cpp";
TestHeaderURI = URI::createFile(testPath(TestHeaderName)).toString();
#ifndef LLVM_ON_WIN32
TEST_F(SymbolCollectorTest, CustomURIScheme) {
- CollectorOpts.IndexMainFiles = false;
// Use test URI scheme from URITests.cpp
CollectorOpts.URISchemes.insert(CollectorOpts.URISchemes.begin(), "unittest");
TestHeaderName = testPath("test-root/x.h");
#endif
TEST_F(SymbolCollectorTest, InvalidURIScheme) {
- CollectorOpts.IndexMainFiles = false;
// Use test URI scheme from URITests.cpp
CollectorOpts.URISchemes = {"invalid"};
runSymbolCollector("class Foo {};", /*Main=*/"");
}
TEST_F(SymbolCollectorTest, FallbackToFileURI) {
- CollectorOpts.IndexMainFiles = false;
// Use test URI scheme from URITests.cpp
CollectorOpts.URISchemes = {"invalid", "file"};
runSymbolCollector("class Foo {};", /*Main=*/"");
}
TEST_F(SymbolCollectorTest, IncludeEnums) {
- CollectorOpts.IndexMainFiles = false;
const std::string Header = R"(
enum {
Red
}
TEST_F(SymbolCollectorTest, IgnoreNamelessSymbols) {
- CollectorOpts.IndexMainFiles = false;
const std::string Header = R"(
struct {
int a;
}
TEST_F(SymbolCollectorTest, SymbolFormedFromMacro) {
- CollectorOpts.IndexMainFiles = false;
Annotations Header(R"(
#define FF(name) \
DeclURI(TestHeaderURI))));
}
-TEST_F(SymbolCollectorTest, SymbolFormedFromMacroInMainFile) {
- CollectorOpts.IndexMainFiles = true;
-
- Annotations Main(R"(
- #define FF(name) \
- class name##_Test {};
-
- $expansion[[FF]](abc);
-
- #define FF2() \
- class $spelling[[Test]] {};
-
- FF2();
- )");
- runSymbolCollector(/*Header=*/"", Main.code());
- EXPECT_THAT(
- Symbols,
- UnorderedElementsAre(
- AllOf(QName("abc_Test"), DeclRange(Main.offsetRange("expansion")),
- DeclURI(TestFileURI)),
- AllOf(QName("Test"), DeclRange(Main.offsetRange("spelling")),
- DeclURI(TestFileURI))));
-}
-
TEST_F(SymbolCollectorTest, SymbolFormedByCLI) {
- CollectorOpts.IndexMainFiles = false;
-
Annotations Header(R"(
#ifdef NAME
class $expansion[[NAME]] {};
}
TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) {
- CollectorOpts.IndexMainFiles = false;
const std::string Header = R"(
class Foo {};
void f1();
UnorderedElementsAre(QName("Foo"), QName("f1"), QName("f2")));
}
-TEST_F(SymbolCollectorTest, IncludeSymbolsInMainFile) {
- CollectorOpts.IndexMainFiles = true;
- const std::string Header = R"(
- class Foo {};
- void f1();
- inline void f2() {}
- )";
- const std::string Main = R"(
- namespace {
- void ff() {} // ignore
- }
- void main_f() {}
- void f1() {}
- )";
- runSymbolCollector(Header, Main);
- EXPECT_THAT(Symbols, UnorderedElementsAre(QName("Foo"), QName("f1"),
- QName("f2"), QName("main_f")));
-}
-
TEST_F(SymbolCollectorTest, IgnoreClassMembers) {
const std::string Header = R"(
class Foo {
IncludeHeader("\"the/good/header.h\""))));
}
+TEST_F(SymbolCollectorTest, AvoidUsingFwdDeclsAsCanonicalDecls) {
+ CollectorOpts.CollectIncludePath = true;
+ Annotations Header(R"(
+ // Forward declarations of TagDecls.
+ class C;
+ struct S;
+ union U;
+
+ // Canonical declarations.
+ class $cdecl[[C]] {};
+ struct $sdecl[[S]] {};
+ union $udecl[[U]] {int x; bool y;};
+ )");
+ runSymbolCollector(Header.code(), /*Main=*/"");
+ EXPECT_THAT(Symbols,
+ UnorderedElementsAre(
+ AllOf(QName("C"), DeclURI(TestHeaderURI),
+ DeclRange(Header.offsetRange("cdecl")),
+ IncludeHeader(TestHeaderURI), DefURI(TestHeaderURI),
+ DefRange(Header.offsetRange("cdecl"))),
+ AllOf(QName("S"), DeclURI(TestHeaderURI),
+ DeclRange(Header.offsetRange("sdecl")),
+ IncludeHeader(TestHeaderURI), DefURI(TestHeaderURI),
+ DefRange(Header.offsetRange("sdecl"))),
+ AllOf(QName("U"), DeclURI(TestHeaderURI),
+ DeclRange(Header.offsetRange("udecl")),
+ IncludeHeader(TestHeaderURI), DefURI(TestHeaderURI),
+ DefRange(Header.offsetRange("udecl")))));
+}
+
+TEST_F(SymbolCollectorTest, ClassForwardDeclarationIsCanonical) {
+ CollectorOpts.CollectIncludePath = true;
+ runSymbolCollector(/*Header=*/"class X;", /*Main=*/"class X {};");
+ EXPECT_THAT(Symbols, UnorderedElementsAre(AllOf(
+ QName("X"), DeclURI(TestHeaderURI),
+ IncludeHeader(TestHeaderURI), DefURI(TestFileURI))));
+}
+
} // namespace
} // namespace clangd
} // namespace clang