Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ckandeler
Copy link
Member

Otherwise, that definition would not show up in the document outline.

@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Christian Kandeler (ckandeler)

Changes

Otherwise, that definition would not show up in the document outline.


Full diff: https://github.com/llvm/llvm-project/pull/150629.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/FindSymbols.cpp (+11)
  • (modified) clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp (+4-1)
diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp
index 84bcbc1f2ddd3..26a15d7a54d88 100644
--- a/clang-tools-extra/clangd/FindSymbols.cpp
+++ b/clang-tools-extra/clangd/FindSymbols.cpp
@@ -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,16 @@ class DocumentOutline {
         D = TD;
     }
 
+    // Friend declarations should be traversed if and only if
+    // they are also defined here.
+    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;
diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
index 282859c51a66f..5b1630eb00cb1 100644
--- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -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),

@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-clangd

Author: Christian Kandeler (ckandeler)

Changes

Otherwise, that definition would not show up in the document outline.


Full diff: https://github.com/llvm/llvm-project/pull/150629.diff

2 Files Affected:

  • (modified) clang-tools-extra/clangd/FindSymbols.cpp (+11)
  • (modified) clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp (+4-1)
diff --git a/clang-tools-extra/clangd/FindSymbols.cpp b/clang-tools-extra/clangd/FindSymbols.cpp
index 84bcbc1f2ddd3..26a15d7a54d88 100644
--- a/clang-tools-extra/clangd/FindSymbols.cpp
+++ b/clang-tools-extra/clangd/FindSymbols.cpp
@@ -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,16 @@ class DocumentOutline {
         D = TD;
     }
 
+    // Friend declarations should be traversed if and only if
+    // they are also defined here.
+    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;
diff --git a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
index 282859c51a66f..5b1630eb00cb1 100644
--- a/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindSymbolsTests.cpp
@@ -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),

@ckandeler ckandeler requested a review from HighCommander4 July 25, 2025 15:22
Copy link
Member

@kadircet kadircet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Otherwise, that definition would not show up in the document outline.
@ckandeler ckandeler force-pushed the QTCREATORBUG-27788-friend-def-in-outline branch from 83e3f0c to c5cadc6 Compare July 28, 2025 13:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants