Skip to content

Commit 33e978f

Browse files
authored
[clangd] Make inline friend functions appear in document symbols (#150629)
Otherwise, that definition would not show up in the document outline.
1 parent 4ec8503 commit 33e978f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang-tools-extra/clangd/FindSymbols.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "SourceCode.h"
1515
#include "index/Index.h"
1616
#include "support/Logger.h"
17+
#include "clang/AST/DeclFriend.h"
1718
#include "clang/AST/DeclTemplate.h"
1819
#include "clang/Index/IndexSymbol.h"
1920
#include "llvm/ADT/ArrayRef.h"
@@ -391,6 +392,17 @@ class DocumentOutline {
391392
D = TD;
392393
}
393394

395+
// FriendDecls don't act as DeclContexts, but they might wrap a function
396+
// definition that won't be visible through other means in the AST. Hence
397+
// unwrap it here instead.
398+
if (auto *Friend = llvm::dyn_cast<FriendDecl>(D)) {
399+
if (auto *Func =
400+
llvm::dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl())) {
401+
if (Func->isThisDeclarationADefinition())
402+
D = Func;
403+
}
404+
}
405+
394406
VisitKind Visit = shouldVisit(D);
395407
if (Visit == VisitKind::No)
396408
return;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ TEST(DocumentSymbols, BasicSymbols) {
335335
Foo(int a) {}
336336
void $decl[[f]]();
337337
friend void f1();
338+
friend void f2() {}
338339
friend class Friend;
339340
Foo& operator=(const Foo&);
340341
~Foo();
@@ -346,7 +347,7 @@ TEST(DocumentSymbols, BasicSymbols) {
346347
};
347348
348349
void f1();
349-
inline void f2() {}
350+
void f2();
350351
static const int KInt = 2;
351352
const char* kStr = "123";
352353
@@ -386,6 +387,8 @@ TEST(DocumentSymbols, BasicSymbols) {
386387
withDetail("(int)"), children()),
387388
AllOf(withName("f"), withKind(SymbolKind::Method),
388389
withDetail("void ()"), children()),
390+
AllOf(withName("f2"), withKind(SymbolKind::Function),
391+
withDetail("void ()"), children()),
389392
AllOf(withName("operator="), withKind(SymbolKind::Method),
390393
withDetail("Foo &(const Foo &)"), children()),
391394
AllOf(withName("~Foo"), withKind(SymbolKind::Constructor),

0 commit comments

Comments
 (0)