Skip to content

Commit 267d827

Browse files
refactor: Don't emit Occurrences for external files (#145)
1 parent f5e834a commit 267d827

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

indexer/Indexer.cc

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,11 @@ void MacroIndexer::emitExternalSymbols(bool deterministic,
259259
TuIndexer::TuIndexer(const clang::SourceManager &sourceManager,
260260
const clang::LangOptions &langOptions,
261261
const clang::ASTContext &astContext,
262-
SymbolFormatter &symbolFormatter)
262+
SymbolFormatter &symbolFormatter,
263+
GetStableFileId getStableFileId)
263264
: sourceManager(sourceManager), langOptions(langOptions),
264-
astContext(astContext), symbolFormatter(symbolFormatter), documentMap() {}
265+
astContext(astContext), symbolFormatter(symbolFormatter), documentMap(),
266+
getStableFileId(getStableFileId) {}
265267

266268
void TuIndexer::saveBindingDecl(const clang::BindingDecl &bindingDecl) {
267269
auto optSymbol = this->symbolFormatter.getBindingSymbol(bindingDecl);
@@ -669,38 +671,52 @@ void TuIndexer::emitDocumentOccurrencesAndSymbols(
669671
}
670672

671673
std::pair<FileLocalSourceRange, clang::FileID>
672-
TuIndexer::getTokenExpansionRange(clang::SourceLocation startLoc) const {
673-
startLoc = this->sourceManager.getExpansionLoc(startLoc);
674+
TuIndexer::getTokenExpansionRange(
675+
clang::SourceLocation startExpansionLoc) const {
674676
auto tokenLength = clang::Lexer::MeasureTokenLength(
675-
startLoc, this->sourceManager, this->langOptions);
677+
startExpansionLoc, this->sourceManager, this->langOptions);
676678
ENFORCE(tokenLength > 0);
677-
auto endLoc = startLoc.getLocWithOffset(tokenLength);
679+
auto endLoc = startExpansionLoc.getLocWithOffset(tokenLength);
678680
return FileLocalSourceRange::fromNonEmpty(this->sourceManager,
679-
{startLoc, endLoc});
681+
{startExpansionLoc, endLoc});
680682
}
681683

682684
void TuIndexer::saveReference(std::string_view symbol,
683685
clang::SourceLocation loc, int32_t extraRoles) {
686+
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
687+
auto fileId = this->sourceManager.getFileID(expansionLoc);
688+
auto optStableFileId = this->getStableFileId(fileId);
689+
if (!optStableFileId.has_value() || !optStableFileId->isInProject) {
690+
return;
691+
}
684692
ENFORCE((extraRoles & scip::SymbolRole::Definition) == 0,
685693
"use saveDefinition instead");
686-
(void)this->saveOccurrence(symbol, loc, extraRoles);
694+
(void)this->saveOccurrence(symbol, expansionLoc, extraRoles);
687695
}
688696

689697
void TuIndexer::saveDefinition(
690698
std::string_view symbol, clang::SourceLocation loc,
691699
std::optional<scip::SymbolInformation> &&optSymbolInfo,
692700
int32_t extraRoles) {
693-
auto &doc = this->saveOccurrence(symbol, loc,
694-
extraRoles | scip::SymbolRole::Definition);
695-
if (optSymbolInfo.has_value()) {
696-
doc.symbolInfos.emplace(symbol, std::move(optSymbolInfo.value()));
701+
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
702+
auto fileId = this->sourceManager.getFileID(expansionLoc);
703+
auto optStableFileId = this->getStableFileId(fileId);
704+
if (!optStableFileId.has_value()) {
705+
return;
706+
}
707+
if (optStableFileId->isInProject) {
708+
auto &doc = this->saveOccurrence(symbol, expansionLoc,
709+
extraRoles | scip::SymbolRole::Definition);
710+
if (optSymbolInfo.has_value()) {
711+
doc.symbolInfos.emplace(symbol, std::move(optSymbolInfo.value()));
712+
}
697713
}
698714
}
699715

700716
PartialDocument &TuIndexer::saveOccurrence(std::string_view symbol,
701-
clang::SourceLocation loc,
717+
clang::SourceLocation expansionLoc,
702718
int32_t allRoles) {
703-
auto [range, fileId] = this->getTokenExpansionRange(loc);
719+
auto [range, fileId] = this->getTokenExpansionRange(expansionLoc);
704720
scip::Occurrence occ;
705721
range.addToOccurrence(occ);
706722
occ.set_symbol(symbol.data(), symbol.size());

indexer/Indexer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ class TuIndexer final {
208208
SymbolFormatter &symbolFormatter;
209209
absl::flat_hash_map<llvm_ext::AbslHashAdapter<clang::FileID>, PartialDocument>
210210
documentMap;
211+
GetStableFileId getStableFileId;
211212

212213
public:
213214
TuIndexer(const clang::SourceManager &, const clang::LangOptions &,
214-
const clang::ASTContext &, SymbolFormatter &);
215+
const clang::ASTContext &, SymbolFormatter &, GetStableFileId);
215216

216217
// See NOTE(ref: emit-vs-save) for naming conventions.
217218
#define SAVE_DECL(DeclName) \

indexer/Worker.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,8 @@ class IndexerAstConsumer : public clang::SemaConsumer {
933933
};
934934
SymbolFormatter symbolFormatter{sourceManager, getStableFileId};
935935
TuIndexer tuIndexer{sourceManager, this->sema->getLangOpts(),
936-
this->sema->getASTContext(), symbolFormatter};
936+
this->sema->getASTContext(), symbolFormatter,
937+
getStableFileId};
937938

938939
IndexerAstVisitor visitor{stableFileIdMap, std::move(toBeIndexed),
939940
this->options.deterministic, tuIndexer};

0 commit comments

Comments
 (0)