25
25
#include " clang/Lex/Lexer.h"
26
26
#include " clang/Lex/MacroInfo.h"
27
27
28
+ #include " proto/fwd_decls.pb.h"
28
29
#include " scip/scip.pb.h"
29
30
30
31
#include " indexer/AbslExtras.h"
@@ -68,15 +69,6 @@ FileLocalSourceRange::makeEmpty(const clang::SourceManager &sourceManager,
68
69
presumedLoc.getColumn ()};
69
70
}
70
71
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
-
80
72
std::string FileLocalSourceRange::debugToString () const {
81
73
return fmt::format (" {}:{}-{}:{}" , this ->startLine , this ->startColumn ,
82
74
this ->endLine , this ->endColumn );
@@ -105,7 +97,7 @@ void FileLocalMacroOccurrence::emitOccurrence(SymbolFormatter &symbolFormatter,
105
97
occ.set_syntax_kind (scip::SyntaxKind::IdentifierMacro);
106
98
break ;
107
99
}
108
- this ->range .addToOccurrence (occ);
100
+ this ->range .addTo (occ);
109
101
auto name =
110
102
symbolFormatter.getMacroSymbol (this ->defInfo ->getDefinitionLoc ()).value ;
111
103
occ.set_symbol (name.data (), name.size ());
@@ -315,19 +307,68 @@ void DocComment::replaceIfEmpty(DocComment &&other) {
315
307
}
316
308
}
317
309
318
- void DocComment::addTo (scip::SymbolInformation &symbolInfo ) {
310
+ void DocComment::addTo (std::string &slot ) {
319
311
auto stripped = absl::StripAsciiWhitespace (this ->contents );
320
312
if (stripped.empty ()) {
321
313
return ;
322
314
}
323
315
if (stripped.size () == this ->contents .size ()) {
324
- *symbolInfo. add_documentation () = std::move (this ->contents );
316
+ slot = std::move (this ->contents );
325
317
return ;
326
318
}
327
- *symbolInfo. add_documentation () = stripped;
319
+ slot = stripped;
328
320
this ->contents .clear ();
329
321
}
330
322
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
+
331
372
TuIndexer::TuIndexer (const clang::SourceManager &sourceManager,
332
373
const clang::LangOptions &langOptions,
333
374
const clang::ASTContext &astContext,
@@ -432,7 +473,8 @@ void TuIndexer::saveTypedefTypeLoc(
432
473
if (auto *typedefNameDecl = typedefTypeLoc.getTypedefNameDecl ()) {
433
474
if (auto optSymbol =
434
475
this ->symbolFormatter .getTypedefNameSymbol (*typedefNameDecl)) {
435
- this ->saveReference (*optSymbol, typedefTypeLoc.getNameLoc ());
476
+ this ->saveReference (*optSymbol, typedefTypeLoc.getNameLoc (),
477
+ NotForwardDecl);
436
478
}
437
479
}
438
480
}
@@ -441,7 +483,8 @@ void TuIndexer::saveUsingTypeLoc(const clang::UsingTypeLoc &usingTypeLoc) {
441
483
if (auto *usingShadowDecl = usingTypeLoc.getFoundDecl ()) {
442
484
if (auto optSymbol =
443
485
this ->symbolFormatter .getUsingShadowSymbol (*usingShadowDecl)) {
444
- this ->saveReference (*optSymbol, usingTypeLoc.getNameLoc ());
486
+ this ->saveReference (*optSymbol, usingTypeLoc.getNameLoc (),
487
+ NotForwardDecl);
445
488
}
446
489
}
447
490
}
@@ -459,7 +502,7 @@ void TuIndexer::saveFieldDecl(const clang::FieldDecl &fieldDecl) {
459
502
void TuIndexer::saveFieldReference (const clang::FieldDecl &fieldDecl,
460
503
clang::SourceLocation loc) {
461
504
if (auto optSymbol = this ->symbolFormatter .getFieldSymbol (fieldDecl)) {
462
- this ->saveReference (*optSymbol, loc);
505
+ this ->saveReference (*optSymbol, loc, NotForwardDecl );
463
506
}
464
507
}
465
508
@@ -575,7 +618,7 @@ void TuIndexer::trySaveTypeReference(const clang::Type *type,
575
618
return ;
576
619
}
577
620
if (auto optSymbol = this ->symbolFormatter .getNamedDeclSymbol (*namedDecl)) {
578
- this ->saveReference (*optSymbol, loc);
621
+ this ->saveReference (*optSymbol, loc, RefersToForwardDecl{*namedDecl} );
579
622
}
580
623
}
581
624
@@ -589,7 +632,8 @@ void TuIndexer::saveNestedNameSpecifierLoc(
589
632
// Don't use nameSpecLoc.getLocalSourceRange() as that may give
590
633
// two MacroID SourceLocations, in case the NestedNameSpecifier
591
634
// arises from a macro expansion.
592
- this ->saveReference (*optSymbol, nameSpecLoc.getLocalBeginLoc ());
635
+ this ->saveReference (*optSymbol, nameSpecLoc.getLocalBeginLoc (),
636
+ NotForwardDecl);
593
637
}
594
638
};
595
639
@@ -694,7 +738,8 @@ void TuIndexer::saveTagTypeLoc(const clang::TagTypeLoc &tagTypeLoc) {
694
738
}
695
739
if (auto optSymbol =
696
740
this ->symbolFormatter .getTagSymbol (*tagTypeLoc.getDecl ())) {
697
- this ->saveReference (optSymbol.value (), tagTypeLoc.getNameLoc ());
741
+ this ->saveReference (optSymbol.value (), tagTypeLoc.getNameLoc (),
742
+ RefersToForwardDecl{*tagTypeLoc.getDecl ()});
698
743
}
699
744
}
700
745
@@ -711,7 +756,8 @@ void TuIndexer::saveTemplateTypeParmTypeLoc(
711
756
const clang::TemplateTypeParmTypeLoc &templateTypeParmTypeLoc) {
712
757
if (auto optSymbol = this ->symbolFormatter .getTemplateTypeParmSymbol (
713
758
*templateTypeParmTypeLoc.getDecl ())) {
714
- this ->saveReference (*optSymbol, templateTypeParmTypeLoc.getNameLoc ());
759
+ this ->saveReference (*optSymbol, templateTypeParmTypeLoc.getNameLoc (),
760
+ NotForwardDecl);
715
761
}
716
762
}
717
763
@@ -740,7 +786,8 @@ void TuIndexer::saveTemplateSpecializationTypeLoc(
740
786
}
741
787
if (optSymbol.has_value ()) {
742
788
this ->saveReference (*optSymbol,
743
- templateSpecializationTypeLoc.getTemplateNameLoc ());
789
+ templateSpecializationTypeLoc.getTemplateNameLoc (),
790
+ NotForwardDecl);
744
791
}
745
792
break ;
746
793
}
@@ -749,7 +796,8 @@ void TuIndexer::saveTemplateSpecializationTypeLoc(
749
796
if (auto optSymbol =
750
797
this ->symbolFormatter .getUsingShadowSymbol (*usingShadowDecl)) {
751
798
this ->saveReference (*optSymbol,
752
- templateSpecializationTypeLoc.getTemplateNameLoc ());
799
+ templateSpecializationTypeLoc.getTemplateNameLoc (),
800
+ NotForwardDecl);
753
801
}
754
802
break ;
755
803
}
@@ -793,7 +841,8 @@ void TuIndexer::saveUsingShadowDecl(
793
841
if (auto *namedDecl = usingShadowDecl.getTargetDecl ()) {
794
842
if (auto optSymbol =
795
843
this ->symbolFormatter .getNamedDeclSymbol (*namedDecl)) {
796
- this ->saveReference (*optSymbol, usingShadowDecl.getLocation ());
844
+ this ->saveReference (*optSymbol, usingShadowDecl.getLocation (),
845
+ NotForwardDecl);
797
846
}
798
847
}
799
848
}
@@ -845,7 +894,8 @@ void TuIndexer::saveCXXConstructExpr(
845
894
}
846
895
if (auto optSymbol =
847
896
this ->symbolFormatter .getFunctionSymbol (*cxxConstructorDecl)) {
848
- this ->saveReference (*optSymbol, cxxConstructExpr.getBeginLoc ());
897
+ this ->saveReference (*optSymbol, cxxConstructExpr.getBeginLoc (),
898
+ NotForwardDecl);
849
899
}
850
900
}
851
901
}
@@ -872,7 +922,8 @@ void TuIndexer::saveDeclRefExpr(const clang::DeclRefExpr &declRefExpr) {
872
922
// ^ getLocation()
873
923
// ^^^^^^ getSourceRange()
874
924
// ^ getExprLoc()
875
- this ->saveReference (optSymbol.value (), declRefExpr.getLocation ());
925
+ this ->saveReference (optSymbol.value (), declRefExpr.getLocation (),
926
+ RefersToForwardDecl{*foundDecl});
876
927
// ^ TODO: Add read-write access to the symbol role here
877
928
}
878
929
@@ -892,7 +943,8 @@ void TuIndexer::saveMemberExpr(const clang::MemberExpr &memberExpr) {
892
943
if (!memberExpr.getMemberNameInfo ().getName ().isIdentifier ()) {
893
944
return ;
894
945
}
895
- this ->saveReference (optSymbol.value (), memberExpr.getMemberLoc ());
946
+ this ->saveReference (optSymbol.value (), memberExpr.getMemberLoc (),
947
+ RefersToForwardDecl{*namedDecl});
896
948
}
897
949
898
950
void TuIndexer::saveUnresolvedMemberExpr (
@@ -918,7 +970,8 @@ void TuIndexer::trySaveMemberReferenceViaLookup(
918
970
for (auto *namedDecl : namedDecls) {
919
971
auto optSymbol = this ->symbolFormatter .getNamedDeclSymbol (*namedDecl);
920
972
if (optSymbol) {
921
- this ->saveReference (*optSymbol, memberNameInfo.getLoc ());
973
+ this ->saveReference (*optSymbol, memberNameInfo.getLoc (),
974
+ RefersToForwardDecl{*namedDecl});
922
975
}
923
976
}
924
977
}
@@ -958,21 +1011,9 @@ void TuIndexer::emitExternalSymbols(bool deterministic,
958
1011
}));
959
1012
}
960
1013
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);
976
1017
}
977
1018
978
1019
std::pair<FileLocalSourceRange, clang::FileID>
@@ -989,18 +1030,18 @@ TuIndexer::getTokenExpansionRange(
989
1030
void TuIndexer::saveForwardDeclaration (SymbolNameRef symbol,
990
1031
clang::SourceLocation loc,
991
1032
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 ;
998
1038
}
999
- return ;
1039
+ this ->forwardDeclarations .insert (symbol, std::move (docComment),
1040
+ optStableFileId->path , range);
1000
1041
}
1001
1042
1002
1043
void TuIndexer::saveReference (SymbolNameRef symbol, clang::SourceLocation loc,
1003
- int32_t extraRoles) {
1044
+ RefersToForwardDecl fwdDecl, int32_t extraRoles) {
1004
1045
auto expansionLoc = this ->sourceManager .getExpansionLoc (loc);
1005
1046
auto fileId = this ->sourceManager .getFileID (expansionLoc);
1006
1047
auto optStableFileId = this ->fileMetadataMap .getStableFileId (fileId);
@@ -1009,6 +1050,12 @@ void TuIndexer::saveReference(SymbolNameRef symbol, clang::SourceLocation loc,
1009
1050
}
1010
1051
ENFORCE ((extraRoles & scip::SymbolRole::Definition) == 0 ,
1011
1052
" 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
+ }
1012
1059
(void )this ->saveOccurrence (symbol, expansionLoc, extraRoles);
1013
1060
}
1014
1061
@@ -1059,7 +1106,7 @@ PartialDocument &TuIndexer::saveOccurrenceImpl(SymbolNameRef symbol,
1059
1106
clang::FileID fileId,
1060
1107
int32_t allRoles) {
1061
1108
scip::Occurrence occ;
1062
- range.addToOccurrence (occ);
1109
+ range.addTo (occ);
1063
1110
occ.set_symbol (symbol.value .data (), symbol.value .size ());
1064
1111
occ.set_symbol_roles (allRoles);
1065
1112
auto &doc = this ->documentMap [{fileId}];
0 commit comments