[clang-tools-extra] c521288 - [clangd] IncludeCleaner: Don't consider the definition as usage for function forward declarations

Kirill Bobyrev via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 26 01:37:26 PDT 2021


Author: Kirill Bobyrev
Date: 2021-10-26T10:37:10+02:00
New Revision: c521288ed391bd29a37f1c444d08ae542df5341c

URL: https://github.com/llvm/llvm-project/commit/c521288ed391bd29a37f1c444d08ae542df5341c
DIFF: https://github.com/llvm/llvm-project/commit/c521288ed391bd29a37f1c444d08ae542df5341c.diff

LOG: [clangd] IncludeCleaner: Don't consider the definition as usage for function forward declarations

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D111711

Added: 
    

Modified: 
    clang-tools-extra/clangd/IncludeCleaner.cpp
    clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index aea34a73d3f57..4178b27857250 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -39,6 +39,13 @@ class ReferencedLocationCrawler
     return true;
   }
 
+  bool VisitFunctionDecl(FunctionDecl *FD) {
+    // Function definition will require redeclarations to be included.
+    if (FD == FD->getDefinition())
+      add(FD);
+    return true;
+  }
+
   bool VisitCXXConstructExpr(CXXConstructExpr *CCE) {
     add(CCE->getConstructor());
     return true;

diff  --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index 783ab773b283e..978d3794fa0e0 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -79,6 +79,19 @@ TEST(IncludeCleaner, ReferencedLocations) {
           "struct ^X { ^X(int) {} int ^foo(); };",
           "auto x = X(42); auto y = x.foo();",
       },
+      // Function
+      {
+          "void ^foo();",
+          "void foo() {}",
+      },
+      {
+          "void foo() {}",
+          "void foo();",
+      },
+      {
+          "inline void ^foo() {}",
+          "void bar() { foo(); }",
+      },
       // Static function
       {
           "struct ^X { static bool ^foo(); }; bool X::^foo() {}",


        


More information about the cfe-commits mailing list