7
7
#include " absl/container/flat_hash_map.h"
8
8
#include " absl/container/flat_hash_set.h"
9
9
#include " absl/functional/function_ref.h"
10
+ #include " absl/strings/strip.h"
10
11
#include " perfetto/perfetto.h"
11
12
#include " spdlog/fmt/fmt.h"
12
13
@@ -240,7 +241,7 @@ void MacroIndexer::emitDocumentOccurrencesAndSymbols(
240
241
macroOcc.emitOccurrence (symbolFormatter, occ);
241
242
switch (macroOcc.role ) {
242
243
case Role::Definition: {
243
- scip::SymbolInformation symbolInfo;
244
+ scip::SymbolInformation symbolInfo{} ;
244
245
*symbolInfo.add_documentation () =
245
246
scip::missingDocumentationPlaceholder;
246
247
ENFORCE (!occ.symbol ().empty ());
@@ -301,6 +302,28 @@ void MacroIndexer::forEachIncludeInFile(
301
302
}
302
303
}
303
304
305
+ void DocComment::replaceIfEmpty (DocComment &&other) {
306
+ if (!other.contents .empty ()) {
307
+ if (this ->contents .empty ()
308
+ || this ->contents == scip::missingDocumentationPlaceholder) {
309
+ this ->contents = std::move (other.contents );
310
+ }
311
+ }
312
+ }
313
+
314
+ void DocComment::addTo (scip::SymbolInformation &symbolInfo) {
315
+ auto stripped = absl::StripAsciiWhitespace (this ->contents );
316
+ if (stripped.empty ()) {
317
+ return ;
318
+ }
319
+ if (stripped.size () == this ->contents .size ()) {
320
+ *symbolInfo.add_documentation () = std::move (this ->contents );
321
+ return ;
322
+ }
323
+ *symbolInfo.add_documentation () = stripped;
324
+ this ->contents .clear ();
325
+ }
326
+
304
327
TuIndexer::TuIndexer (const clang::SourceManager &sourceManager,
305
328
const clang::LangOptions &langOptions,
306
329
const clang::ASTContext &astContext,
@@ -383,10 +406,8 @@ void TuIndexer::saveEnumConstantDecl(
383
406
}
384
407
auto symbol = optSymbol.value ();
385
408
386
- scip::SymbolInformation symbolInfo;
387
- for (auto &docComment : this ->tryGetDocComment (enumConstantDecl).lines ) {
388
- *symbolInfo.add_documentation () = std::move (docComment);
389
- }
409
+ scip::SymbolInformation symbolInfo{};
410
+ this ->getDocComment (enumConstantDecl).addTo (symbolInfo);
390
411
391
412
ENFORCE (enumConstantDecl.getBeginLoc () == enumConstantDecl.getLocation ());
392
413
this ->saveDefinition (symbol, enumConstantDecl.getLocation (),
@@ -426,9 +447,7 @@ void TuIndexer::saveFieldDecl(const clang::FieldDecl &fieldDecl) {
426
447
return ;
427
448
}
428
449
scip::SymbolInformation symbolInfo{};
429
- for (auto &docComment : this ->tryGetDocComment (fieldDecl).lines ) {
430
- *symbolInfo.add_documentation () = std::move (docComment);
431
- }
450
+ this ->getDocComment (fieldDecl).addTo (symbolInfo);
432
451
this ->saveDefinition (optSymbol.value (), fieldDecl.getLocation (), symbolInfo);
433
452
}
434
453
@@ -448,9 +467,7 @@ void TuIndexer::saveFunctionDecl(const clang::FunctionDecl &functionDecl) {
448
467
449
468
if (functionDecl.isPure () || functionDecl.isThisDeclarationADefinition ()) {
450
469
scip::SymbolInformation symbolInfo{};
451
- for (auto &docComment : this ->tryGetDocComment (functionDecl).lines ) {
452
- *symbolInfo.add_documentation () = std::move (docComment);
453
- }
470
+ this ->getDocComment (functionDecl).addTo (symbolInfo);
454
471
if (auto *cxxMethodDecl =
455
472
llvm::dyn_cast<clang::CXXMethodDecl>(&functionDecl)) {
456
473
for (auto &overridenMethodDecl : cxxMethodDecl->overridden_methods ()) {
@@ -478,7 +495,7 @@ void TuIndexer::saveFunctionDecl(const clang::FunctionDecl &functionDecl) {
478
495
std::move (symbolInfo));
479
496
} else {
480
497
this ->saveForwardDeclaration (symbol, functionDecl.getLocation (),
481
- this ->tryGetDocComment (functionDecl));
498
+ this ->getDocComment (functionDecl));
482
499
}
483
500
}
484
501
@@ -639,14 +656,12 @@ void TuIndexer::saveTagDecl(const clang::TagDecl &tagDecl) {
639
656
640
657
if (!tagDecl.isThisDeclarationADefinition ()) {
641
658
this ->saveForwardDeclaration (symbol, tagDecl.getLocation (),
642
- this ->tryGetDocComment (tagDecl));
659
+ this ->getDocComment (tagDecl));
643
660
return ;
644
661
}
645
662
646
- scip::SymbolInformation symbolInfo;
647
- for (auto &docComment : this ->tryGetDocComment (tagDecl).lines ) {
648
- *symbolInfo.add_documentation () = std::move (docComment);
649
- }
663
+ scip::SymbolInformation symbolInfo{};
664
+ this ->getDocComment (tagDecl).addTo (symbolInfo);
650
665
651
666
if (auto *cxxRecordDecl = llvm::dyn_cast<clang::CXXRecordDecl>(&tagDecl)) {
652
667
for (const clang::CXXBaseSpecifier &cxxBaseSpecifier :
@@ -754,9 +769,7 @@ void TuIndexer::saveTypedefNameDecl(
754
769
return ;
755
770
}
756
771
scip::SymbolInformation symbolInfo{};
757
- for (auto &docComment : this ->tryGetDocComment (typedefNameDecl).lines ) {
758
- *symbolInfo.add_documentation () = std::move (docComment);
759
- }
772
+ this ->getDocComment (typedefNameDecl).addTo (symbolInfo);
760
773
this ->saveDefinition (*optSymbol, typedefNameDecl.getLocation (),
761
774
std::move (symbolInfo));
762
775
}
@@ -767,9 +780,7 @@ void TuIndexer::saveUsingShadowDecl(
767
780
this ->symbolFormatter .getUsingShadowSymbol (usingShadowDecl)) {
768
781
if (auto *baseUsingDecl = usingShadowDecl.getIntroducer ()) {
769
782
scip::SymbolInformation symbolInfo{};
770
- for (auto &docComment : this ->tryGetDocComment (usingShadowDecl).lines ) {
771
- *symbolInfo.add_documentation () = std::move (docComment);
772
- }
783
+ this ->getDocComment (usingShadowDecl).addTo (symbolInfo);
773
784
this ->saveDefinition (*optSymbol, usingShadowDecl.getLocation (),
774
785
std::move (symbolInfo));
775
786
}
@@ -808,9 +819,7 @@ void TuIndexer::saveVarDecl(const clang::VarDecl &varDecl) {
808
819
return ;
809
820
}
810
821
scip::SymbolInformation symbolInfo{};
811
- for (auto &docComment : this ->tryGetDocComment (varDecl).lines ) {
812
- *symbolInfo.add_documentation () = std::move (docComment);
813
- }
822
+ this ->getDocComment (varDecl).addTo (symbolInfo);
814
823
this ->saveDefinition (optSymbol.value (), varDecl.getLocation (), symbolInfo);
815
824
}
816
825
}
@@ -952,9 +961,7 @@ void TuIndexer::emitForwardDeclarations(bool deterministic,
952
961
absl::FunctionRef<void (std::string_view &&, DocComment &&)>(
953
962
[&](auto &&symbol, auto &&docComment) {
954
963
scip::SymbolInformation symbolInfo{};
955
- for (auto &line : docComment.lines ) {
956
- *symbolInfo.add_documentation () = std::move (line);
957
- }
964
+ docComment.addTo (symbolInfo);
958
965
symbolInfo.set_symbol (symbol.data (), symbol.size ());
959
966
// Add a forward declaration SymbolRole here
960
967
// once https://github.com/sourcegraph/scip/issues/131 is fixed.
@@ -975,11 +982,13 @@ TuIndexer::getTokenExpansionRange(
975
982
976
983
void TuIndexer::saveForwardDeclaration (std::string_view symbol,
977
984
clang::SourceLocation loc,
978
- DocComment &&docComments ) {
985
+ DocComment &&docComment ) {
979
986
this ->saveReference (symbol, loc);
980
- auto [it, inserted] = this ->forwardDeclarations .emplace (symbol, docComments);
981
- if (!inserted && it->second .lines .empty () && !docComments.lines .empty ()) {
982
- it->second .lines = std::move (docComments.lines );
987
+ auto it = this ->forwardDeclarations .find (symbol);
988
+ if (it == this ->forwardDeclarations .end ()) {
989
+ this ->forwardDeclarations .emplace (symbol, std::move (docComment));
990
+ } else {
991
+ it->second .replaceIfEmpty (std::move (docComment));
983
992
}
984
993
return ;
985
994
}
@@ -1089,22 +1098,18 @@ checkIfCommentBelongsToPreviousEnumCase(const clang::Decl &decl,
1089
1098
1090
1099
namespace scip_clang {
1091
1100
1092
- DocComment TuIndexer::tryGetDocComment (const clang::Decl &decl) const {
1101
+ DocComment TuIndexer::getDocComment (const clang::Decl &decl) const {
1093
1102
auto &astContext = decl.getASTContext ();
1094
1103
// FIXME(def: hovers, issue:
1095
1104
// https://github.com/sourcegraph/scip-clang/issues/96)
1096
1105
if (auto *rawComment = astContext.getRawCommentForAnyRedecl (&decl)) {
1097
1106
if (::checkIfCommentBelongsToPreviousEnumCase (decl, *rawComment)) {
1098
- return {};
1099
- }
1100
- DocComment out{};
1101
- for (auto &line : rawComment->getFormattedLines (
1102
- this ->sourceManager , astContext.getDiagnostics ())) {
1103
- out.lines .emplace_back (std::move (line.Text ));
1107
+ return DocComment{};
1104
1108
}
1105
- return out;
1109
+ return DocComment{rawComment->getFormattedText (
1110
+ sourceManager, astContext.getDiagnostics ())};
1106
1111
}
1107
- return {};
1112
+ return DocComment {};
1108
1113
}
1109
1114
1110
1115
} // namespace scip_clang
0 commit comments