Skip to content

Commit be073c4

Browse files
fix: Prefer doc comment on definition (attempt 2) (#390)
1 parent 277a60e commit be073c4

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

indexer/Indexer.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "clang/AST/ASTContext.h"
1515
#include "clang/AST/CXXInheritance.h"
16+
#include "clang/AST/Comment.h"
1617
#include "clang/AST/Decl.h"
1718
#include "clang/AST/DeclCXX.h"
1819
#include "clang/AST/DeclTemplate.h"
@@ -1165,14 +1166,22 @@ namespace scip_clang {
11651166

11661167
DocComment TuIndexer::getDocComment(const clang::Decl &decl) const {
11671168
auto &astContext = decl.getASTContext();
1168-
// FIXME(def: hovers, issue:
1169-
// https://github.com/sourcegraph/scip-clang/issues/96)
1170-
if (auto *rawComment = astContext.getRawCommentForAnyRedecl(&decl)) {
1169+
auto fromRawComment = [&](const clang::RawComment *rawComment) -> DocComment {
11711170
if (::checkIfCommentBelongsToPreviousEnumCase(decl, *rawComment)) {
11721171
return DocComment{};
11731172
}
11741173
return DocComment{rawComment->getFormattedText(
11751174
sourceManager, astContext.getDiagnostics())};
1175+
};
1176+
// If a forward declaration precedes the definition,
1177+
// then getRawCommentForAnyRedecl will return the doc comment
1178+
// on the forward definition, which is not desirable. So check
1179+
// the decl first.
1180+
if (auto *rawComment = astContext.getRawCommentForDeclNoCache(&decl)) {
1181+
return fromRawComment(rawComment);
1182+
}
1183+
if (auto *rawComment = astContext.getRawCommentForAnyRedecl(&decl)) {
1184+
return fromRawComment(rawComment);
11761185
}
11771186
return DocComment{};
11781187
}

test/index/docs/docs.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ enum class Apink {
1616
//! Maknae
1717
Hayoung,
1818
};
19+
20+
/// Ominous sounds
21+
struct Ghost;
22+
23+
/// Boo!
24+
typedef struct Ghost {} Ghost;

test/index/docs/docs.snapshot.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@
4343
// documentation
4444
// | Maknae
4545
};
46+
47+
/// Ominous sounds
48+
struct Ghost;
49+
// ^^^^^ reference [..] Ghost#
50+
51+
/// Boo!
52+
typedef struct Ghost {} Ghost;
53+
// ^^^^^ definition [..] Ghost#
54+
// documentation
55+
// | Boo!
56+
// ^^^^^ definition [..] Ghost#
57+
// documentation
58+
// | Boo!

0 commit comments

Comments
 (0)