Skip to content

Commit 583ba07

Browse files
committed
[clangd] Add xref for macros to FileIndex.
Summary: Adds macro references to the dynamic index. Tests added. Also exposed a new API to convert path to URI in URI.h Reviewers: hokein Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D71406
1 parent 880734a commit 583ba07

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

clang-tools-extra/clangd/index/FileIndex.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "FileIndex.h"
10+
#include "CollectMacros.h"
1011
#include "Logger.h"
1112
#include "ParsedAST.h"
1213
#include "SymbolCollector.h"
@@ -32,6 +33,7 @@ namespace clangd {
3233

3334
static SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
3435
llvm::ArrayRef<Decl *> DeclsToIndex,
36+
const MainFileMacros *MacroRefsToIndex,
3537
const CanonicalIncludes &Includes,
3638
bool IsIndexMainAST) {
3739
SymbolCollector::Options CollectorOpts;
@@ -59,6 +61,8 @@ static SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
5961

6062
SymbolCollector Collector(std::move(CollectorOpts));
6163
Collector.setPreprocessor(PP);
64+
if (MacroRefsToIndex)
65+
Collector.handleMacros(*MacroRefsToIndex);
6266
index::indexTopLevelDecls(AST, *PP, DeclsToIndex, Collector, IndexOpts);
6367

6468
const auto &SM = AST.getSourceManager();
@@ -68,6 +72,7 @@ static SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
6872
auto Syms = Collector.takeSymbols();
6973
auto Refs = Collector.takeRefs();
7074
auto Relations = Collector.takeRelations();
75+
7176
vlog("index AST for {0} (main={1}): \n"
7277
" symbol slab: {2} symbols, {3} bytes\n"
7378
" ref slab: {4} symbols, {5} refs, {6} bytes\n"
@@ -80,7 +85,8 @@ static SlabTuple indexSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
8085

8186
SlabTuple indexMainDecls(ParsedAST &AST) {
8287
return indexSymbols(AST.getASTContext(), AST.getPreprocessorPtr(),
83-
AST.getLocalTopLevelDecls(), AST.getCanonicalIncludes(),
88+
AST.getLocalTopLevelDecls(), &AST.getMacros(),
89+
AST.getCanonicalIncludes(),
8490
/*IsIndexMainAST=*/true);
8591
}
8692

@@ -89,7 +95,8 @@ SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
8995
std::vector<Decl *> DeclsToIndex(
9096
AST.getTranslationUnitDecl()->decls().begin(),
9197
AST.getTranslationUnitDecl()->decls().end());
92-
return indexSymbols(AST, std::move(PP), DeclsToIndex, Includes,
98+
return indexSymbols(AST, std::move(PP), DeclsToIndex,
99+
/*MainFileMacros=*/nullptr, Includes,
93100
/*IsIndexMainAST=*/false);
94101
}
95102

clang-tools-extra/clangd/index/FileIndex.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ using SlabTuple = std::tuple<SymbolSlab, RefSlab, RelationSlab>;
140140
/// Exposed to assist in unit tests.
141141
SlabTuple indexMainDecls(ParsedAST &AST);
142142

143-
/// Idex declarations from \p AST and macros from \p PP that are declared in
143+
/// Index declarations from \p AST and macros from \p PP that are declared in
144144
/// included headers.
145145
SlabTuple indexHeaderSymbols(ASTContext &AST, std::shared_ptr<Preprocessor> PP,
146146
const CanonicalIncludes &Includes);

clang-tools-extra/clangd/index/SymbolCollector.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,29 @@ bool SymbolCollector::handleDeclOccurrence(
346346
return true;
347347
}
348348

349+
void SymbolCollector::handleMacros(const MainFileMacros &MacroRefsToIndex) {
350+
assert(PP.get());
351+
const auto &SM = PP->getSourceManager();
352+
const auto *MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
353+
assert(MainFileEntry);
354+
355+
const auto MainFileURI = toURI(SM, MainFileEntry->getName(), Opts);
356+
// Add macro references.
357+
for (const auto &IDToRefs : MacroRefsToIndex.MacroRefs) {
358+
for (const auto &Range : IDToRefs.second) {
359+
Ref R;
360+
R.Location.Start.setLine(Range.start.line);
361+
R.Location.Start.setColumn(Range.start.character);
362+
R.Location.End.setLine(Range.end.line);
363+
R.Location.End.setColumn(Range.end.character);
364+
R.Location.FileURI = MainFileURI.c_str();
365+
// FIXME: Add correct RefKind information to MainFileMacros.
366+
R.Kind = RefKind::Reference;
367+
Refs.insert(IDToRefs.first, R);
368+
}
369+
}
370+
}
371+
349372
bool SymbolCollector::handleMacroOccurrence(const IdentifierInfo *Name,
350373
const MacroInfo *MI,
351374
index::SymbolRoleSet Roles,

clang-tools-extra/clangd/index/SymbolCollector.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOL_COLLECTOR_H
1010

1111
#include "CanonicalIncludes.h"
12+
#include "CollectMacros.h"
1213
#include "Index.h"
1314
#include "SymbolOrigin.h"
1415
#include "clang/AST/ASTContext.h"
@@ -108,6 +109,8 @@ class SymbolCollector : public index::IndexDataConsumer {
108109
index::SymbolRoleSet Roles,
109110
SourceLocation Loc) override;
110111

112+
void handleMacros(const MainFileMacros &MacroRefsToIndex);
113+
111114
SymbolSlab takeSymbols() { return std::move(Symbols).build(); }
112115
RefSlab takeRefs() { return std::move(Refs).build(); }
113116
RelationSlab takeRelations() { return std::move(Relations).build(); }

clang-tools-extra/clangd/unittests/FileIndexTests.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,40 @@ TEST(FileIndexTest, Refs) {
345345
FileURI("unittest:///test2.cc"))}));
346346
}
347347

348+
TEST(FileIndexTest, MacroRefs) {
349+
Annotations HeaderCode(R"cpp(
350+
#define $def1[[HEADER_MACRO]](X) (X+1)
351+
)cpp");
352+
Annotations MainCode(R"cpp(
353+
#define $def2[[MAINFILE_MACRO]](X) (X+1)
354+
void f() {
355+
int a = $ref1[[HEADER_MACRO]](2);
356+
int b = $ref2[[MAINFILE_MACRO]](1);
357+
}
358+
)cpp");
359+
360+
FileIndex Index;
361+
// Add test.cc
362+
TestTU Test;
363+
Test.HeaderCode = HeaderCode.code();
364+
Test.Code = MainCode.code();
365+
Test.Filename = "test.cc";
366+
auto AST = Test.build();
367+
Index.updateMain(Test.Filename, AST);
368+
369+
auto HeaderMacro = findSymbol(Test.headerSymbols(), "HEADER_MACRO");
370+
EXPECT_THAT(getRefs(Index, HeaderMacro.ID),
371+
RefsAre({AllOf(RefRange(MainCode.range("ref1")),
372+
FileURI("unittest:///test.cc"))}));
373+
374+
auto MainFileMacro = findSymbol(Test.headerSymbols(), "MAINFILE_MACRO");
375+
EXPECT_THAT(getRefs(Index, MainFileMacro.ID),
376+
RefsAre({AllOf(RefRange(MainCode.range("def2")),
377+
FileURI("unittest:///test.cc")),
378+
AllOf(RefRange(MainCode.range("ref2")),
379+
FileURI("unittest:///test.cc"))}));
380+
}
381+
348382
TEST(FileIndexTest, CollectMacros) {
349383
FileIndex M;
350384
update(M, "f", "#define CLANGD 1");

0 commit comments

Comments
 (0)