Skip to content

Commit 0659fd9

Browse files
authored
[clangd] Collect comments from function definitions into the index (#67802)
This is useful with projects that put their (doxygen) comments at the implementation site, rather than the header.
1 parent 0cab475 commit 0659fd9

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

clang-tools-extra/clangd/index/Symbol.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,11 @@ struct Symbol {
145145
ImplementationDetail = 1 << 2,
146146
/// Symbol is visible to other files (not e.g. a static helper function).
147147
VisibleOutsideFile = 1 << 3,
148+
/// Symbol has an attached documentation comment.
149+
HasDocComment = 1 << 4
148150
};
149-
150151
SymbolFlag Flags = SymbolFlag::None;
152+
151153
/// FIXME: also add deprecation message and fixit?
152154
};
153155

clang-tools-extra/clangd/index/SymbolCollector.cpp

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -635,17 +635,21 @@ bool SymbolCollector::handleDeclOccurrence(
635635
return true;
636636

637637
const Symbol *BasicSymbol = Symbols.find(ID);
638-
if (isPreferredDeclaration(*OriginalDecl, Roles))
638+
bool SkipDocCheckInDef = false;
639+
if (isPreferredDeclaration(*OriginalDecl, Roles)) {
639640
// If OriginalDecl is preferred, replace/create the existing canonical
640641
// declaration (e.g. a class forward declaration). There should be at most
641642
// one duplicate as we expect to see only one preferred declaration per
642643
// TU, because in practice they are definitions.
643644
BasicSymbol = addDeclaration(*OriginalDecl, std::move(ID), IsMainFileOnly);
644-
else if (!BasicSymbol || DeclIsCanonical)
645+
SkipDocCheckInDef = true;
646+
} else if (!BasicSymbol || DeclIsCanonical) {
645647
BasicSymbol = addDeclaration(*ND, std::move(ID), IsMainFileOnly);
648+
SkipDocCheckInDef = true;
649+
}
646650

647651
if (Roles & static_cast<unsigned>(index::SymbolRole::Definition))
648-
addDefinition(*OriginalDecl, *BasicSymbol);
652+
addDefinition(*OriginalDecl, *BasicSymbol, SkipDocCheckInDef);
649653

650654
return true;
651655
}
@@ -1025,16 +1029,28 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
10251029
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
10261030
*CompletionTUInfo,
10271031
/*IncludeBriefComments*/ false);
1028-
std::string Documentation =
1029-
formatDocumentation(*CCS, getDocComment(Ctx, SymbolCompletion,
1030-
/*CommentsFromHeaders=*/true));
1032+
std::string DocComment;
1033+
std::string Documentation;
1034+
bool AlreadyHasDoc = S.Flags & Symbol::HasDocComment;
1035+
if (!AlreadyHasDoc) {
1036+
DocComment = getDocComment(Ctx, SymbolCompletion,
1037+
/*CommentsFromHeaders=*/true);
1038+
Documentation = formatDocumentation(*CCS, DocComment);
1039+
}
1040+
const auto UpdateDoc = [&] {
1041+
if (!AlreadyHasDoc) {
1042+
if (!DocComment.empty())
1043+
S.Flags |= Symbol::HasDocComment;
1044+
S.Documentation = Documentation;
1045+
}
1046+
};
10311047
if (!(S.Flags & Symbol::IndexedForCodeCompletion)) {
10321048
if (Opts.StoreAllDocumentation)
1033-
S.Documentation = Documentation;
1049+
UpdateDoc();
10341050
Symbols.insert(S);
10351051
return Symbols.find(S.ID);
10361052
}
1037-
S.Documentation = Documentation;
1053+
UpdateDoc();
10381054
std::string Signature;
10391055
std::string SnippetSuffix;
10401056
getSignature(*CCS, &Signature, &SnippetSuffix, SymbolCompletion.Kind,
@@ -1058,8 +1074,8 @@ const Symbol *SymbolCollector::addDeclaration(const NamedDecl &ND, SymbolID ID,
10581074
return Symbols.find(S.ID);
10591075
}
10601076

1061-
void SymbolCollector::addDefinition(const NamedDecl &ND,
1062-
const Symbol &DeclSym) {
1077+
void SymbolCollector::addDefinition(const NamedDecl &ND, const Symbol &DeclSym,
1078+
bool SkipDocCheck) {
10631079
if (DeclSym.Definition)
10641080
return;
10651081
const auto &SM = ND.getASTContext().getSourceManager();
@@ -1074,6 +1090,27 @@ void SymbolCollector::addDefinition(const NamedDecl &ND,
10741090
Symbol S = DeclSym;
10751091
// FIXME: use the result to filter out symbols.
10761092
S.Definition = *DefLoc;
1093+
1094+
std::string DocComment;
1095+
std::string Documentation;
1096+
if (!SkipDocCheck && !(S.Flags & Symbol::HasDocComment) &&
1097+
(llvm::isa<FunctionDecl>(ND) || llvm::isa<CXXMethodDecl>(ND))) {
1098+
CodeCompletionResult SymbolCompletion(&getTemplateOrThis(ND), 0);
1099+
const auto *CCS = SymbolCompletion.CreateCodeCompletionString(
1100+
*ASTCtx, *PP, CodeCompletionContext::CCC_Symbol, *CompletionAllocator,
1101+
*CompletionTUInfo,
1102+
/*IncludeBriefComments*/ false);
1103+
DocComment = getDocComment(ND.getASTContext(), SymbolCompletion,
1104+
/*CommentsFromHeaders=*/true);
1105+
if (!S.Documentation.empty())
1106+
Documentation = S.Documentation.str() + '\n' + DocComment;
1107+
else
1108+
Documentation = formatDocumentation(*CCS, DocComment);
1109+
if (!DocComment.empty())
1110+
S.Flags |= Symbol::HasDocComment;
1111+
S.Documentation = Documentation;
1112+
}
1113+
10771114
Symbols.insert(S);
10781115
}
10791116

clang-tools-extra/clangd/index/SymbolCollector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ class SymbolCollector : public index::IndexDataConsumer {
161161
private:
162162
const Symbol *addDeclaration(const NamedDecl &, SymbolID,
163163
bool IsMainFileSymbol);
164-
void addDefinition(const NamedDecl &, const Symbol &DeclSymbol);
164+
void addDefinition(const NamedDecl &, const Symbol &DeclSymbol,
165+
bool SkipDocCheck);
165166
void processRelations(const NamedDecl &ND, const SymbolID &ID,
166167
ArrayRef<index::SymbolRelation> Relations);
167168

clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,58 @@ TEST_F(SymbolCollectorTest, Documentation) {
14771477
forCodeCompletion(false))));
14781478
}
14791479

1480+
TEST_F(SymbolCollectorTest, DocumentationInMain) {
1481+
const std::string Header = R"(
1482+
// doc Foo
1483+
class Foo {
1484+
void f();
1485+
};
1486+
)";
1487+
const std::string Main = R"(
1488+
// doc f
1489+
void Foo::f() {}
1490+
)";
1491+
CollectorOpts.StoreAllDocumentation = true;
1492+
runSymbolCollector(Header, Main);
1493+
EXPECT_THAT(Symbols,
1494+
UnorderedElementsAre(
1495+
AllOf(qName("Foo"), doc("doc Foo"), forCodeCompletion(true)),
1496+
AllOf(qName("Foo::f"), doc("doc f"), returnType(""),
1497+
forCodeCompletion(false))));
1498+
}
1499+
1500+
TEST_F(SymbolCollectorTest, DocumentationAtDeclThenDef) {
1501+
const std::string Header = R"(
1502+
class Foo {
1503+
// doc f decl
1504+
void f();
1505+
};
1506+
)";
1507+
const std::string Main = R"(
1508+
// doc f def
1509+
void Foo::f() {}
1510+
)";
1511+
CollectorOpts.StoreAllDocumentation = true;
1512+
runSymbolCollector(Header, Main);
1513+
EXPECT_THAT(Symbols,
1514+
UnorderedElementsAre(AllOf(qName("Foo")),
1515+
AllOf(qName("Foo::f"), doc("doc f decl"))));
1516+
}
1517+
1518+
TEST_F(SymbolCollectorTest, DocumentationAtDefThenDecl) {
1519+
const std::string Header = R"(
1520+
// doc f def
1521+
void f() {}
1522+
1523+
// doc f decl
1524+
void f();
1525+
)";
1526+
CollectorOpts.StoreAllDocumentation = true;
1527+
runSymbolCollector(Header, "" /*Main*/);
1528+
EXPECT_THAT(Symbols,
1529+
UnorderedElementsAre(AllOf(qName("f"), doc("doc f def"))));
1530+
}
1531+
14801532
TEST_F(SymbolCollectorTest, ClassMembers) {
14811533
const std::string Header = R"(
14821534
class Foo {

0 commit comments

Comments
 (0)