[clangd] Make inline friend functions appear in document symbols (#150629)

Otherwise, that definition would not show up in the document outline.
This commit is contained in:
Christian Kandeler
2025-07-30 10:43:32 +02:00
committed by GitHub
parent 4ec8503e4c
commit 33e978fbfa
2 changed files with 16 additions and 1 deletions

View File

@@ -14,6 +14,7 @@
#include "SourceCode.h"
#include "index/Index.h"
#include "support/Logger.h"
#include "clang/AST/DeclFriend.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Index/IndexSymbol.h"
#include "llvm/ADT/ArrayRef.h"
@@ -391,6 +392,17 @@ private:
D = TD;
}
// FriendDecls don't act as DeclContexts, but they might wrap a function
// definition that won't be visible through other means in the AST. Hence
// unwrap it here instead.
if (auto *Friend = llvm::dyn_cast<FriendDecl>(D)) {
if (auto *Func =
llvm::dyn_cast_or_null<FunctionDecl>(Friend->getFriendDecl())) {
if (Func->isThisDeclarationADefinition())
D = Func;
}
}
VisitKind Visit = shouldVisit(D);
if (Visit == VisitKind::No)
return;

View File

@@ -335,6 +335,7 @@ TEST(DocumentSymbols, BasicSymbols) {
Foo(int a) {}
void $decl[[f]]();
friend void f1();
friend void f2() {}
friend class Friend;
Foo& operator=(const Foo&);
~Foo();
@@ -346,7 +347,7 @@ TEST(DocumentSymbols, BasicSymbols) {
};
void f1();
inline void f2() {}
void f2();
static const int KInt = 2;
const char* kStr = "123";
@@ -386,6 +387,8 @@ TEST(DocumentSymbols, BasicSymbols) {
withDetail("(int)"), children()),
AllOf(withName("f"), withKind(SymbolKind::Method),
withDetail("void ()"), children()),
AllOf(withName("f2"), withKind(SymbolKind::Function),
withDetail("void ()"), children()),
AllOf(withName("operator="), withKind(SymbolKind::Method),
withDetail("Foo &(const Foo &)"), children()),
AllOf(withName("~Foo"), withKind(SymbolKind::Constructor),