Skip to content

Commit 5b8e89e

Browse files
refactor: Skip occurrences based on file check (#348)
1 parent 60972b7 commit 5b8e89e

File tree

5 files changed

+43
-26
lines changed

5 files changed

+43
-26
lines changed

indexer/AstConsumer.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ class IndexerAstVisitor : public clang::RecursiveASTVisitor<IndexerAstVisitor> {
2222
using Base = RecursiveASTVisitor;
2323

2424
const FileMetadataMap &fileMetadataMap;
25-
FileIdsToBeIndexedSet toBeIndexed;
25+
const FileIdsToBeIndexedSet &toBeIndexed;
2626
bool deterministic;
2727

2828
TuIndexer &tuIndexer;
2929

3030
public:
3131
IndexerAstVisitor(const FileMetadataMap &fileMetadataMap,
32-
FileIdsToBeIndexedSet &&toBeIndexed, bool deterministic,
33-
TuIndexer &tuIndexer)
34-
: fileMetadataMap(fileMetadataMap), toBeIndexed(std::move(toBeIndexed)),
32+
const FileIdsToBeIndexedSet &toBeIndexed,
33+
bool deterministic, TuIndexer &tuIndexer)
34+
: fileMetadataMap(fileMetadataMap), toBeIndexed(toBeIndexed),
3535
deterministic(deterministic), tuIndexer(tuIndexer) {}
3636

3737
// See clang/include/clang/Basic/DeclNodes.td for list of declarations.
@@ -198,15 +198,17 @@ void IndexerAstConsumer::HandleTranslationUnit(clang::ASTContext &astContext) {
198198
clangIdLookupMap, fileMetadataMap,
199199
toBeIndexed);
200200

201+
toBeIndexed.insert({astContext.getSourceManager().getMainFileID()});
202+
201203
SymbolFormatter symbolFormatter{sourceManager, fileMetadataMap};
202-
TuIndexer tuIndexer{sourceManager, this->sema->getLangOpts(),
203-
this->sema->getASTContext(), symbolFormatter,
204-
fileMetadataMap};
204+
TuIndexer tuIndexer{
205+
sourceManager, this->sema->getLangOpts(), this->sema->getASTContext(),
206+
toBeIndexed, symbolFormatter, fileMetadataMap};
205207

206208
this->saveIncludeReferences(toBeIndexed, macroIndexer, clangIdLookupMap,
207209
fileMetadataMap, tuIndexer);
208210

209-
IndexerAstVisitor visitor{fileMetadataMap, std::move(toBeIndexed),
211+
IndexerAstVisitor visitor{fileMetadataMap, toBeIndexed,
210212
this->options.deterministic, tuIndexer};
211213
visitor.TraverseAST(astContext);
212214

indexer/AstConsumer.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ struct IndexerAstConsumerOptions {
4343
bool deterministic;
4444
};
4545

46-
/// Type to track which files should be indexed.
47-
///
48-
/// For files that do not belong to this project; their symbols should be
49-
/// tracked in external symbols instead of creating a \c scip::Document.
50-
///
51-
/// Not every file that is part of this project will be part of this map.
52-
/// For example, if a file+hash was already indexed by another worker,
53-
/// then one shouldn't call insert(..) for that file.
54-
using FileIdsToBeIndexedSet =
55-
absl::flat_hash_set<llvm_ext::AbslHashAdapter<clang::FileID>>;
56-
5746
struct TuIndexingOutput {
5847
/// Index storing per-document output and external symbols
5948
/// for symbols that have definitions.
@@ -86,6 +75,9 @@ class IndexerAstConsumer : public clang::SemaConsumer {
8675
virtual void ForgetSema() override;
8776

8877
private:
78+
using FileIdsToBeIndexedSet =
79+
absl::flat_hash_set<llvm_ext::AbslHashAdapter<clang::FileID>>;
80+
8981
void computeFileIdsToBeIndexed(const clang::ASTContext &astContext,
9082
const EmitIndexJobDetails &emitIndexDetails,
9183
const ClangIdLookupMap &clangIdLookupMap,

indexer/Indexer.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,13 @@ void ForwardDeclMap::emit(bool deterministic, scip::ForwardDeclIndex &index) {
372372
TuIndexer::TuIndexer(const clang::SourceManager &sourceManager,
373373
const clang::LangOptions &langOptions,
374374
const clang::ASTContext &astContext,
375+
const FileIdsToBeIndexedSet &fileIdsToBeIndexed,
375376
SymbolFormatter &symbolFormatter,
376377
FileMetadataMap &fileMetadataMap)
377378
: sourceManager(sourceManager), langOptions(langOptions),
378-
astContext(astContext), symbolFormatter(symbolFormatter),
379-
approximateNameResolver(astContext), documentMap(),
380-
fileMetadataMap(fileMetadataMap), externalSymbols(),
379+
astContext(astContext), fileIdsToBeIndexed(fileIdsToBeIndexed),
380+
symbolFormatter(symbolFormatter), approximateNameResolver(astContext),
381+
documentMap(), fileMetadataMap(fileMetadataMap), externalSymbols(),
381382
forwardDeclarations() {}
382383

383384
void TuIndexer::saveSyntheticFileDefinition(clang::FileID fileId,
@@ -427,6 +428,9 @@ void TuIndexer::saveInclude(clang::SourceRange sourceRange,
427428
// ranges in macro expansions, directly call saveOccurrenceImpl.
428429
auto [range, fileId] =
429430
FileLocalSourceRange::fromNonEmpty(this->sourceManager, sourceRange);
431+
if (!this->fileIdsToBeIndexed.contains({fileId})) {
432+
return;
433+
}
430434
this->saveOccurrenceImpl(symbol, range, fileId, 0);
431435
}
432436

@@ -1032,6 +1036,9 @@ void TuIndexer::saveForwardDeclaration(SymbolNameRef symbol,
10321036
DocComment &&docComment) {
10331037
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
10341038
auto [range, fileId] = this->getTokenExpansionRange(expansionLoc);
1039+
if (!this->fileIdsToBeIndexed.contains({fileId})) {
1040+
return;
1041+
}
10351042
auto optStableFileId = this->fileMetadataMap.getStableFileId(fileId);
10361043
if (!optStableFileId.has_value() || !optStableFileId->isInProject) {
10371044
return;
@@ -1044,6 +1051,9 @@ void TuIndexer::saveReference(SymbolNameRef symbol, clang::SourceLocation loc,
10441051
RefersToForwardDecl fwdDecl, int32_t extraRoles) {
10451052
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
10461053
auto fileId = this->sourceManager.getFileID(expansionLoc);
1054+
if (!this->fileIdsToBeIndexed.contains({fileId})) {
1055+
return;
1056+
}
10471057
auto optStableFileId = this->fileMetadataMap.getStableFileId(fileId);
10481058
if (!optStableFileId.has_value() || !optStableFileId->isInProject) {
10491059
return;
@@ -1065,6 +1075,9 @@ void TuIndexer::saveDefinition(
10651075
int32_t extraRoles) {
10661076
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
10671077
auto fileId = this->sourceManager.getFileID(expansionLoc);
1078+
if (!this->fileIdsToBeIndexed.contains({fileId})) {
1079+
return;
1080+
}
10681081
auto optStableFileId = this->fileMetadataMap.getStableFileId(fileId);
10691082
if (!optStableFileId.has_value()) {
10701083
return;

indexer/Indexer.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,22 @@ class ForwardDeclMap final {
286286
void emit(bool deterministic, scip::ForwardDeclIndex &);
287287
};
288288

289+
/// Type to track which files should be indexed.
290+
///
291+
/// For files that do not belong to this project; their symbols should be
292+
/// tracked in external symbols instead of creating a \c scip::Document.
293+
///
294+
/// Not every file that is part of this project will be part of this map.
295+
/// For example, if a file+hash was already indexed by another worker,
296+
/// then one shouldn't call insert(..) for that file.
297+
using FileIdsToBeIndexedSet =
298+
absl::flat_hash_set<llvm_ext::AbslHashAdapter<clang::FileID>>;
299+
289300
class TuIndexer final {
290301
const clang::SourceManager &sourceManager;
291302
const clang::LangOptions &langOptions;
292303
[[maybe_unused]] const clang::ASTContext &astContext;
304+
const FileIdsToBeIndexedSet &fileIdsToBeIndexed;
293305
SymbolFormatter &symbolFormatter;
294306
ApproximateNameResolver approximateNameResolver;
295307

@@ -303,7 +315,8 @@ class TuIndexer final {
303315

304316
public:
305317
TuIndexer(const clang::SourceManager &, const clang::LangOptions &,
306-
const clang::ASTContext &, SymbolFormatter &, FileMetadataMap &);
318+
const clang::ASTContext &, const FileIdsToBeIndexedSet &,
319+
SymbolFormatter &, FileMetadataMap &);
307320

308321
/// Emit a fake 'definition' for a file, which can be used as a target
309322
/// of Go to definition from #include, as well as the source for

test/index/fwd_decl/myheader.snapshot.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
// definition [..] `<file>/myheader.h`/
33
/// Look, a function!
44
void f();
5-
// ^ reference [..] f(49f6e7a06ebc5aa8).
65
// ^ reference [..] f(49f6e7a06ebc5aa8).
76

87
/// Look, a struct!
98
struct S;
10-
// ^ reference [..] S#
119
// ^ reference [..] S#
1210

1311
struct C {
1412
// ^ definition [..] C#
1513
/// Look, a method!
1614
void m();
17-
// ^ reference [..] C#m(49f6e7a06ebc5aa8).
1815
// ^ reference [..] C#m(49f6e7a06ebc5aa8).
1916
};
2017

0 commit comments

Comments
 (0)