Skip to content

Commit

Permalink
[clangd] Simplify "preferred" vs "definition" logic a bit in XRefs AS…
Browse files Browse the repository at this point in the history
…T code.

Summary:
Now Preferred is always the canonical (first) decl, Definition is always the def
if available.

In practice the index was already forcing this behaviour anyway, so there's no
change. (Unless you weren't using this index, in which case this patch makes
textDocument/declaration and toggling work as expected).

Reviewers: kadircet

Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D73369
  • Loading branch information
sam-mccall committed Mar 26, 2020
1 parent 1a27d63 commit 6324912
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 8 additions & 10 deletions clang-tools-extra/clangd/XRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,21 @@ locateASTReferent(SourceLocation CurLoc, const syntax::Token *TouchedIdentifier,
llvm::DenseMap<SymbolID, size_t> ResultIndex;

auto AddResultDecl = [&](const NamedDecl *D) {
const NamedDecl *Def = getDefinition(D);
const NamedDecl *Preferred = Def ? Def : D;

auto Loc = makeLocation(AST.getASTContext(), nameLocation(*Preferred, SM),
MainFilePath);
D = llvm::cast<NamedDecl>(D->getCanonicalDecl());
auto Loc =
makeLocation(AST.getASTContext(), nameLocation(*D, SM), MainFilePath);
if (!Loc)
return;

Result.emplace_back();
Result.back().Name = printName(AST.getASTContext(), *Preferred);
Result.back().Name = printName(AST.getASTContext(), *D);
Result.back().PreferredDeclaration = *Loc;
// Preferred is always a definition if possible, so this check works.
if (Def == Preferred)
Result.back().Definition = *Loc;
if (const NamedDecl *Def = getDefinition(D))
Result.back().Definition = makeLocation(
AST.getASTContext(), nameLocation(*Def, SM), MainFilePath);

// Record SymbolID for index lookup later.
if (auto ID = getSymbolID(Preferred))
if (auto ID = getSymbolID(D))
ResultIndex[*ID] = Result.size() - 1;
};

Expand Down
8 changes: 4 additions & 4 deletions clang-tools-extra/clangd/unittests/XRefsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,15 @@ TEST(LocateSymbol, All) {
)cpp",

R"cpp(// Forward class declaration
class Foo;
class [[Foo]] {};
class $decl[[Foo]];
class $def[[Foo]] {};
F^oo* foo();
)cpp",

R"cpp(// Function declaration
void foo();
void $decl[[foo]]();
void g() { f^oo(); }
void [[foo]]() {}
void $def[[foo]]() {}
)cpp",

R"cpp(
Expand Down

0 comments on commit 6324912

Please sign in to comment.