Skip to content

Commit 60972b7

Browse files
refactor: Separate out occurrences for forward decls (#346)
1 parent a9bf307 commit 60972b7

File tree

14 files changed

+296
-83
lines changed

14 files changed

+296
-83
lines changed

indexer/AstConsumer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "clang/Sema/SemaConsumer.h"
99
#include "llvm/ADT/StringRef.h"
1010

11+
#include "proto/fwd_decls.pb.h"
1112
#include "scip/scip.pb.h"
1213

1314
#include "indexer/IpcMessages.h"
@@ -59,7 +60,7 @@ struct TuIndexingOutput {
5960
scip::Index docsAndExternals;
6061
/// Index storing information about forward declarations.
6162
/// Only the external_symbols list is populated.
62-
scip::Index forwardDecls;
63+
scip::ForwardDeclIndex forwardDecls;
6364

6465
TuIndexingOutput() = default;
6566
TuIndexingOutput(const TuIndexingOutput &) = delete;

indexer/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cc_library(
1212
visibility = ["//visibility:public"],
1313
deps = [
1414
"//indexer/os",
15+
"//proto:fwd_decls",
1516
"@com_google_absl//absl/strings:str_format",
1617
"@com_google_absl//absl/functional:function_ref",
1718
"@com_google_absl//absl/algorithm:container",

indexer/Driver.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/ADT/StringMap.h"
3737
#include "llvm/Support/StringSaver.h"
3838

39+
#include "proto/fwd_decls.pb.h"
3940
#include "scip/scip.pb.h"
4041

4142
#include "indexer/CliOptions.h"
@@ -1028,7 +1029,7 @@ class Driver {
10281029
// a dependency on a library with a concurrent hash table.
10291030

10301031
auto readIndexShard = [](const AbsolutePath &path,
1031-
scip::Index &indexShard) -> bool {
1032+
auto &indexShard) -> bool {
10321033
TRACE_EVENT(tracing::indexIo, "(lambda readIndexShard)");
10331034
auto &shardPath = path.asStringRef();
10341035
std::ifstream inputStream(shardPath,
@@ -1089,13 +1090,13 @@ class Driver {
10891090
auto symbolToInfoMap = builder.populateSymbolToInfoMap();
10901091

10911092
for (auto &paths : this->shardPaths) {
1092-
scip::Index indexShard;
1093+
scip::ForwardDeclIndex indexShard;
10931094
if (!readIndexShard(paths.forwardDecls, indexShard)) {
10941095
continue;
10951096
}
10961097
TRACE_EVENT(tracing::indexMerging, "addForwardDeclarations", "size",
1097-
indexShard.external_symbols_size());
1098-
for (auto &forwardDeclSym : *indexShard.mutable_external_symbols()) {
1098+
indexShard.forward_decls_size());
1099+
for (auto &forwardDeclSym : *indexShard.mutable_forward_decls()) {
10991100
builder.addForwardDeclaration(*symbolToInfoMap,
11001101
std::move(forwardDeclSym));
11011102
}

indexer/Indexer.cc

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/Lex/Lexer.h"
2626
#include "clang/Lex/MacroInfo.h"
2727

28+
#include "proto/fwd_decls.pb.h"
2829
#include "scip/scip.pb.h"
2930

3031
#include "indexer/AbslExtras.h"
@@ -68,15 +69,6 @@ FileLocalSourceRange::makeEmpty(const clang::SourceManager &sourceManager,
6869
presumedLoc.getColumn()};
6970
}
7071

71-
void FileLocalSourceRange::addToOccurrence(scip::Occurrence &occ) const {
72-
occ.add_range(this->startLine - 1);
73-
occ.add_range(this->startColumn - 1);
74-
if (this->startLine != this->endLine) {
75-
occ.add_range(this->endLine - 1);
76-
}
77-
occ.add_range(this->endColumn - 1);
78-
}
79-
8072
std::string FileLocalSourceRange::debugToString() const {
8173
return fmt::format("{}:{}-{}:{}", this->startLine, this->startColumn,
8274
this->endLine, this->endColumn);
@@ -105,7 +97,7 @@ void FileLocalMacroOccurrence::emitOccurrence(SymbolFormatter &symbolFormatter,
10597
occ.set_syntax_kind(scip::SyntaxKind::IdentifierMacro);
10698
break;
10799
}
108-
this->range.addToOccurrence(occ);
100+
this->range.addTo(occ);
109101
auto name =
110102
symbolFormatter.getMacroSymbol(this->defInfo->getDefinitionLoc()).value;
111103
occ.set_symbol(name.data(), name.size());
@@ -315,19 +307,68 @@ void DocComment::replaceIfEmpty(DocComment &&other) {
315307
}
316308
}
317309

318-
void DocComment::addTo(scip::SymbolInformation &symbolInfo) {
310+
void DocComment::addTo(std::string &slot) {
319311
auto stripped = absl::StripAsciiWhitespace(this->contents);
320312
if (stripped.empty()) {
321313
return;
322314
}
323315
if (stripped.size() == this->contents.size()) {
324-
*symbolInfo.add_documentation() = std::move(this->contents);
316+
slot = std::move(this->contents);
325317
return;
326318
}
327-
*symbolInfo.add_documentation() = stripped;
319+
slot = stripped;
328320
this->contents.clear();
329321
}
330322

323+
void DocComment::addTo(scip::SymbolInformation &symbolInfo) {
324+
this->addTo(*symbolInfo.add_documentation());
325+
}
326+
327+
RefersToForwardDecl::RefersToForwardDecl(const clang::Decl &decl)
328+
: value(false) {
329+
auto canonicalDecl = decl.getCanonicalDecl();
330+
if (auto *varDecl = llvm::dyn_cast<clang::VarDecl>(canonicalDecl)) {
331+
this->value = !varDecl->isThisDeclarationADefinition();
332+
} else if (auto *tagDecl = llvm::dyn_cast<clang::TagDecl>(canonicalDecl)) {
333+
this->value = !tagDecl->isThisDeclarationADefinition();
334+
} else if (auto *functionDecl =
335+
llvm::dyn_cast<clang::FunctionDecl>(canonicalDecl)) {
336+
this->value = !functionDecl->isThisDeclarationADefinition();
337+
} else if (auto *functionTemplateDecl =
338+
llvm::dyn_cast<clang::FunctionTemplateDecl>(canonicalDecl)) {
339+
this->value = !functionTemplateDecl->isThisDeclarationADefinition();
340+
}
341+
}
342+
343+
void ForwardDeclMap::insert(SymbolNameRef symbol, DocComment &&docComment,
344+
RootRelativePathRef projectFilePath,
345+
FileLocalSourceRange occRange) {
346+
auto &entry = this->map[symbol]; // deliberate default initialization
347+
entry.docComment.replaceIfEmpty(std::move(docComment));
348+
entry.ranges.push_back({projectFilePath, occRange});
349+
}
350+
351+
void ForwardDeclMap::emit(bool deterministic, scip::ForwardDeclIndex &index) {
352+
TRACE_EVENT(tracing::indexing, "ForwardDeclMap::emit", "size",
353+
this->map.size());
354+
scip_clang::extractTransform(
355+
std::move(this->map), deterministic,
356+
absl::FunctionRef<void(SymbolNameRef &&, Value &&)>(
357+
[&](auto &&symbol, auto &&value) {
358+
scip::ForwardDecl fwdDecl{};
359+
fwdDecl.set_symbol(symbol.value.data(), symbol.value.size());
360+
value.docComment.addTo(*fwdDecl.mutable_documentation());
361+
for (auto [path, range] : value.ranges) {
362+
scip::ForwardDecl::Reference ref{};
363+
range.addTo(ref);
364+
auto p = path.asStringView();
365+
ref.set_relative_path(p.data(), p.size());
366+
*fwdDecl.add_references() = std::move(ref);
367+
}
368+
*index.add_forward_decls() = std::move(fwdDecl);
369+
}));
370+
}
371+
331372
TuIndexer::TuIndexer(const clang::SourceManager &sourceManager,
332373
const clang::LangOptions &langOptions,
333374
const clang::ASTContext &astContext,
@@ -432,7 +473,8 @@ void TuIndexer::saveTypedefTypeLoc(
432473
if (auto *typedefNameDecl = typedefTypeLoc.getTypedefNameDecl()) {
433474
if (auto optSymbol =
434475
this->symbolFormatter.getTypedefNameSymbol(*typedefNameDecl)) {
435-
this->saveReference(*optSymbol, typedefTypeLoc.getNameLoc());
476+
this->saveReference(*optSymbol, typedefTypeLoc.getNameLoc(),
477+
NotForwardDecl);
436478
}
437479
}
438480
}
@@ -441,7 +483,8 @@ void TuIndexer::saveUsingTypeLoc(const clang::UsingTypeLoc &usingTypeLoc) {
441483
if (auto *usingShadowDecl = usingTypeLoc.getFoundDecl()) {
442484
if (auto optSymbol =
443485
this->symbolFormatter.getUsingShadowSymbol(*usingShadowDecl)) {
444-
this->saveReference(*optSymbol, usingTypeLoc.getNameLoc());
486+
this->saveReference(*optSymbol, usingTypeLoc.getNameLoc(),
487+
NotForwardDecl);
445488
}
446489
}
447490
}
@@ -459,7 +502,7 @@ void TuIndexer::saveFieldDecl(const clang::FieldDecl &fieldDecl) {
459502
void TuIndexer::saveFieldReference(const clang::FieldDecl &fieldDecl,
460503
clang::SourceLocation loc) {
461504
if (auto optSymbol = this->symbolFormatter.getFieldSymbol(fieldDecl)) {
462-
this->saveReference(*optSymbol, loc);
505+
this->saveReference(*optSymbol, loc, NotForwardDecl);
463506
}
464507
}
465508

@@ -575,7 +618,7 @@ void TuIndexer::trySaveTypeReference(const clang::Type *type,
575618
return;
576619
}
577620
if (auto optSymbol = this->symbolFormatter.getNamedDeclSymbol(*namedDecl)) {
578-
this->saveReference(*optSymbol, loc);
621+
this->saveReference(*optSymbol, loc, RefersToForwardDecl{*namedDecl});
579622
}
580623
}
581624

@@ -589,7 +632,8 @@ void TuIndexer::saveNestedNameSpecifierLoc(
589632
// Don't use nameSpecLoc.getLocalSourceRange() as that may give
590633
// two MacroID SourceLocations, in case the NestedNameSpecifier
591634
// arises from a macro expansion.
592-
this->saveReference(*optSymbol, nameSpecLoc.getLocalBeginLoc());
635+
this->saveReference(*optSymbol, nameSpecLoc.getLocalBeginLoc(),
636+
NotForwardDecl);
593637
}
594638
};
595639

@@ -694,7 +738,8 @@ void TuIndexer::saveTagTypeLoc(const clang::TagTypeLoc &tagTypeLoc) {
694738
}
695739
if (auto optSymbol =
696740
this->symbolFormatter.getTagSymbol(*tagTypeLoc.getDecl())) {
697-
this->saveReference(optSymbol.value(), tagTypeLoc.getNameLoc());
741+
this->saveReference(optSymbol.value(), tagTypeLoc.getNameLoc(),
742+
RefersToForwardDecl{*tagTypeLoc.getDecl()});
698743
}
699744
}
700745

@@ -711,7 +756,8 @@ void TuIndexer::saveTemplateTypeParmTypeLoc(
711756
const clang::TemplateTypeParmTypeLoc &templateTypeParmTypeLoc) {
712757
if (auto optSymbol = this->symbolFormatter.getTemplateTypeParmSymbol(
713758
*templateTypeParmTypeLoc.getDecl())) {
714-
this->saveReference(*optSymbol, templateTypeParmTypeLoc.getNameLoc());
759+
this->saveReference(*optSymbol, templateTypeParmTypeLoc.getNameLoc(),
760+
NotForwardDecl);
715761
}
716762
}
717763

@@ -740,7 +786,8 @@ void TuIndexer::saveTemplateSpecializationTypeLoc(
740786
}
741787
if (optSymbol.has_value()) {
742788
this->saveReference(*optSymbol,
743-
templateSpecializationTypeLoc.getTemplateNameLoc());
789+
templateSpecializationTypeLoc.getTemplateNameLoc(),
790+
NotForwardDecl);
744791
}
745792
break;
746793
}
@@ -749,7 +796,8 @@ void TuIndexer::saveTemplateSpecializationTypeLoc(
749796
if (auto optSymbol =
750797
this->symbolFormatter.getUsingShadowSymbol(*usingShadowDecl)) {
751798
this->saveReference(*optSymbol,
752-
templateSpecializationTypeLoc.getTemplateNameLoc());
799+
templateSpecializationTypeLoc.getTemplateNameLoc(),
800+
NotForwardDecl);
753801
}
754802
break;
755803
}
@@ -793,7 +841,8 @@ void TuIndexer::saveUsingShadowDecl(
793841
if (auto *namedDecl = usingShadowDecl.getTargetDecl()) {
794842
if (auto optSymbol =
795843
this->symbolFormatter.getNamedDeclSymbol(*namedDecl)) {
796-
this->saveReference(*optSymbol, usingShadowDecl.getLocation());
844+
this->saveReference(*optSymbol, usingShadowDecl.getLocation(),
845+
NotForwardDecl);
797846
}
798847
}
799848
}
@@ -845,7 +894,8 @@ void TuIndexer::saveCXXConstructExpr(
845894
}
846895
if (auto optSymbol =
847896
this->symbolFormatter.getFunctionSymbol(*cxxConstructorDecl)) {
848-
this->saveReference(*optSymbol, cxxConstructExpr.getBeginLoc());
897+
this->saveReference(*optSymbol, cxxConstructExpr.getBeginLoc(),
898+
NotForwardDecl);
849899
}
850900
}
851901
}
@@ -872,7 +922,8 @@ void TuIndexer::saveDeclRefExpr(const clang::DeclRefExpr &declRefExpr) {
872922
// ^ getLocation()
873923
// ^^^^^^ getSourceRange()
874924
// ^ getExprLoc()
875-
this->saveReference(optSymbol.value(), declRefExpr.getLocation());
925+
this->saveReference(optSymbol.value(), declRefExpr.getLocation(),
926+
RefersToForwardDecl{*foundDecl});
876927
// ^ TODO: Add read-write access to the symbol role here
877928
}
878929

@@ -892,7 +943,8 @@ void TuIndexer::saveMemberExpr(const clang::MemberExpr &memberExpr) {
892943
if (!memberExpr.getMemberNameInfo().getName().isIdentifier()) {
893944
return;
894945
}
895-
this->saveReference(optSymbol.value(), memberExpr.getMemberLoc());
946+
this->saveReference(optSymbol.value(), memberExpr.getMemberLoc(),
947+
RefersToForwardDecl{*namedDecl});
896948
}
897949

898950
void TuIndexer::saveUnresolvedMemberExpr(
@@ -918,7 +970,8 @@ void TuIndexer::trySaveMemberReferenceViaLookup(
918970
for (auto *namedDecl : namedDecls) {
919971
auto optSymbol = this->symbolFormatter.getNamedDeclSymbol(*namedDecl);
920972
if (optSymbol) {
921-
this->saveReference(*optSymbol, memberNameInfo.getLoc());
973+
this->saveReference(*optSymbol, memberNameInfo.getLoc(),
974+
RefersToForwardDecl{*namedDecl});
922975
}
923976
}
924977
}
@@ -958,21 +1011,9 @@ void TuIndexer::emitExternalSymbols(bool deterministic,
9581011
}));
9591012
}
9601013

961-
void TuIndexer::emitForwardDeclarations(bool deterministic,
962-
scip::Index &forwardDeclIndex) {
963-
TRACE_EVENT(tracing::indexing, "TuIndexer::emitForwardDeclarations", "size",
964-
this->forwardDeclarations.size());
965-
scip_clang::extractTransform(
966-
std::move(this->forwardDeclarations), deterministic,
967-
absl::FunctionRef<void(SymbolNameRef &&, DocComment &&)>(
968-
[&](auto &&symbol, auto &&docComment) {
969-
scip::SymbolInformation symbolInfo{};
970-
docComment.addTo(symbolInfo);
971-
symbolInfo.set_symbol(symbol.value.data(), symbol.value.size());
972-
// Add a forward declaration SymbolRole here
973-
// once https://github.com/sourcegraph/scip/issues/131 is fixed.
974-
*forwardDeclIndex.add_external_symbols() = std::move(symbolInfo);
975-
}));
1014+
void TuIndexer::emitForwardDeclarations(
1015+
bool deterministic, scip::ForwardDeclIndex &forwardDeclIndex) {
1016+
this->forwardDeclarations.emit(deterministic, forwardDeclIndex);
9761017
}
9771018

9781019
std::pair<FileLocalSourceRange, clang::FileID>
@@ -989,18 +1030,18 @@ TuIndexer::getTokenExpansionRange(
9891030
void TuIndexer::saveForwardDeclaration(SymbolNameRef symbol,
9901031
clang::SourceLocation loc,
9911032
DocComment &&docComment) {
992-
this->saveReference(symbol, loc);
993-
auto it = this->forwardDeclarations.find(symbol);
994-
if (it == this->forwardDeclarations.end()) {
995-
this->forwardDeclarations.emplace(symbol, std::move(docComment));
996-
} else {
997-
it->second.replaceIfEmpty(std::move(docComment));
1033+
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
1034+
auto [range, fileId] = this->getTokenExpansionRange(expansionLoc);
1035+
auto optStableFileId = this->fileMetadataMap.getStableFileId(fileId);
1036+
if (!optStableFileId.has_value() || !optStableFileId->isInProject) {
1037+
return;
9981038
}
999-
return;
1039+
this->forwardDeclarations.insert(symbol, std::move(docComment),
1040+
optStableFileId->path, range);
10001041
}
10011042

10021043
void TuIndexer::saveReference(SymbolNameRef symbol, clang::SourceLocation loc,
1003-
int32_t extraRoles) {
1044+
RefersToForwardDecl fwdDecl, int32_t extraRoles) {
10041045
auto expansionLoc = this->sourceManager.getExpansionLoc(loc);
10051046
auto fileId = this->sourceManager.getFileID(expansionLoc);
10061047
auto optStableFileId = this->fileMetadataMap.getStableFileId(fileId);
@@ -1009,6 +1050,12 @@ void TuIndexer::saveReference(SymbolNameRef symbol, clang::SourceLocation loc,
10091050
}
10101051
ENFORCE((extraRoles & scip::SymbolRole::Definition) == 0,
10111052
"use saveDefinition instead");
1053+
if (fwdDecl.value) {
1054+
auto [range, _] = this->getTokenExpansionRange(expansionLoc);
1055+
this->forwardDeclarations.insert(symbol, DocComment{},
1056+
optStableFileId->path, range);
1057+
return;
1058+
}
10121059
(void)this->saveOccurrence(symbol, expansionLoc, extraRoles);
10131060
}
10141061

@@ -1059,7 +1106,7 @@ PartialDocument &TuIndexer::saveOccurrenceImpl(SymbolNameRef symbol,
10591106
clang::FileID fileId,
10601107
int32_t allRoles) {
10611108
scip::Occurrence occ;
1062-
range.addToOccurrence(occ);
1109+
range.addTo(occ);
10631110
occ.set_symbol(symbol.value.data(), symbol.value.size());
10641111
occ.set_symbol_roles(allRoles);
10651112
auto &doc = this->documentMap[{fileId}];

0 commit comments

Comments
 (0)