Skip to content

Commit 5ceb07c

Browse files
refactor: Tweak handling for doc comments (#324)
1 parent cf921e2 commit 5ceb07c

File tree

3 files changed

+62
-46
lines changed

3 files changed

+62
-46
lines changed

indexer/Indexer.cc

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "absl/container/flat_hash_map.h"
88
#include "absl/container/flat_hash_set.h"
99
#include "absl/functional/function_ref.h"
10+
#include "absl/strings/strip.h"
1011
#include "perfetto/perfetto.h"
1112
#include "spdlog/fmt/fmt.h"
1213

@@ -240,7 +241,7 @@ void MacroIndexer::emitDocumentOccurrencesAndSymbols(
240241
macroOcc.emitOccurrence(symbolFormatter, occ);
241242
switch (macroOcc.role) {
242243
case Role::Definition: {
243-
scip::SymbolInformation symbolInfo;
244+
scip::SymbolInformation symbolInfo{};
244245
*symbolInfo.add_documentation() =
245246
scip::missingDocumentationPlaceholder;
246247
ENFORCE(!occ.symbol().empty());
@@ -301,6 +302,28 @@ void MacroIndexer::forEachIncludeInFile(
301302
}
302303
}
303304

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+
304327
TuIndexer::TuIndexer(const clang::SourceManager &sourceManager,
305328
const clang::LangOptions &langOptions,
306329
const clang::ASTContext &astContext,
@@ -383,10 +406,8 @@ void TuIndexer::saveEnumConstantDecl(
383406
}
384407
auto symbol = optSymbol.value();
385408

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);
390411

391412
ENFORCE(enumConstantDecl.getBeginLoc() == enumConstantDecl.getLocation());
392413
this->saveDefinition(symbol, enumConstantDecl.getLocation(),
@@ -426,9 +447,7 @@ void TuIndexer::saveFieldDecl(const clang::FieldDecl &fieldDecl) {
426447
return;
427448
}
428449
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);
432451
this->saveDefinition(optSymbol.value(), fieldDecl.getLocation(), symbolInfo);
433452
}
434453

@@ -448,9 +467,7 @@ void TuIndexer::saveFunctionDecl(const clang::FunctionDecl &functionDecl) {
448467

449468
if (functionDecl.isPure() || functionDecl.isThisDeclarationADefinition()) {
450469
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);
454471
if (auto *cxxMethodDecl =
455472
llvm::dyn_cast<clang::CXXMethodDecl>(&functionDecl)) {
456473
for (auto &overridenMethodDecl : cxxMethodDecl->overridden_methods()) {
@@ -478,7 +495,7 @@ void TuIndexer::saveFunctionDecl(const clang::FunctionDecl &functionDecl) {
478495
std::move(symbolInfo));
479496
} else {
480497
this->saveForwardDeclaration(symbol, functionDecl.getLocation(),
481-
this->tryGetDocComment(functionDecl));
498+
this->getDocComment(functionDecl));
482499
}
483500
}
484501

@@ -639,14 +656,12 @@ void TuIndexer::saveTagDecl(const clang::TagDecl &tagDecl) {
639656

640657
if (!tagDecl.isThisDeclarationADefinition()) {
641658
this->saveForwardDeclaration(symbol, tagDecl.getLocation(),
642-
this->tryGetDocComment(tagDecl));
659+
this->getDocComment(tagDecl));
643660
return;
644661
}
645662

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);
650665

651666
if (auto *cxxRecordDecl = llvm::dyn_cast<clang::CXXRecordDecl>(&tagDecl)) {
652667
for (const clang::CXXBaseSpecifier &cxxBaseSpecifier :
@@ -754,9 +769,7 @@ void TuIndexer::saveTypedefNameDecl(
754769
return;
755770
}
756771
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);
760773
this->saveDefinition(*optSymbol, typedefNameDecl.getLocation(),
761774
std::move(symbolInfo));
762775
}
@@ -767,9 +780,7 @@ void TuIndexer::saveUsingShadowDecl(
767780
this->symbolFormatter.getUsingShadowSymbol(usingShadowDecl)) {
768781
if (auto *baseUsingDecl = usingShadowDecl.getIntroducer()) {
769782
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);
773784
this->saveDefinition(*optSymbol, usingShadowDecl.getLocation(),
774785
std::move(symbolInfo));
775786
}
@@ -808,9 +819,7 @@ void TuIndexer::saveVarDecl(const clang::VarDecl &varDecl) {
808819
return;
809820
}
810821
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);
814823
this->saveDefinition(optSymbol.value(), varDecl.getLocation(), symbolInfo);
815824
}
816825
}
@@ -952,9 +961,7 @@ void TuIndexer::emitForwardDeclarations(bool deterministic,
952961
absl::FunctionRef<void(std::string_view &&, DocComment &&)>(
953962
[&](auto &&symbol, auto &&docComment) {
954963
scip::SymbolInformation symbolInfo{};
955-
for (auto &line : docComment.lines) {
956-
*symbolInfo.add_documentation() = std::move(line);
957-
}
964+
docComment.addTo(symbolInfo);
958965
symbolInfo.set_symbol(symbol.data(), symbol.size());
959966
// Add a forward declaration SymbolRole here
960967
// once https://github.com/sourcegraph/scip/issues/131 is fixed.
@@ -975,11 +982,13 @@ TuIndexer::getTokenExpansionRange(
975982

976983
void TuIndexer::saveForwardDeclaration(std::string_view symbol,
977984
clang::SourceLocation loc,
978-
DocComment &&docComments) {
985+
DocComment &&docComment) {
979986
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));
983992
}
984993
return;
985994
}
@@ -1089,22 +1098,18 @@ checkIfCommentBelongsToPreviousEnumCase(const clang::Decl &decl,
10891098

10901099
namespace scip_clang {
10911100

1092-
DocComment TuIndexer::tryGetDocComment(const clang::Decl &decl) const {
1101+
DocComment TuIndexer::getDocComment(const clang::Decl &decl) const {
10931102
auto &astContext = decl.getASTContext();
10941103
// FIXME(def: hovers, issue:
10951104
// https://github.com/sourcegraph/scip-clang/issues/96)
10961105
if (auto *rawComment = astContext.getRawCommentForAnyRedecl(&decl)) {
10971106
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{};
11041108
}
1105-
return out;
1109+
return DocComment{rawComment->getFormattedText(
1110+
sourceManager, astContext.getDiagnostics())};
11061111
}
1107-
return {};
1112+
return DocComment{};
11081113
}
11091114

11101115
} // namespace scip_clang

indexer/Indexer.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,19 @@ struct PartialDocument {
227227
absl::flat_hash_map<std::string_view, scip::SymbolInformation> symbolInfos;
228228
};
229229

230-
struct DocComment {
231-
std::vector<std::string> lines;
230+
class DocComment {
231+
std::string contents;
232+
233+
public:
234+
DocComment() = default;
235+
explicit DocComment(std::string &&contents) : contents(std::move(contents)) {}
236+
DocComment(DocComment &&) = default;
237+
DocComment &operator=(DocComment &&) = default;
238+
DocComment(const DocComment &) = delete;
239+
DocComment &operator=(const DocComment &) = delete;
240+
241+
void replaceIfEmpty(DocComment &&);
242+
void addTo(scip::SymbolInformation &);
232243
};
233244

234245
class TuIndexer final {
@@ -335,7 +346,7 @@ class TuIndexer final {
335346
clang::FileID fileId,
336347
int32_t allRoles = 0);
337348

338-
DocComment tryGetDocComment(const clang::Decl &) const;
349+
DocComment getDocComment(const clang::Decl &) const;
339350
};
340351

341352
} // namespace scip_clang

test/index/types/types.snapshot.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
F2 = E2
9797
// ^^ definition [..] has_anon_enum/F2.
9898
// documentation
99-
// | Everywhere a moo-moo
99+
// | Everywhere a moo-moo
100100
// ^^ reference [..] has_anon_enum/E2.
101101
} f = F1;
102102
// ^ definition [..] has_anon_enum/f.

0 commit comments

Comments
 (0)