ArcsinX created this revision.
ArcsinX added reviewers: sammccall, kbobyrev.
Herald added subscribers: kadircet, arphaman.
Herald added a project: All.
ArcsinX requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay, ilya-biryukov.
Herald added a project: clang-tools-extra.

IncludeCleaner suggests to remove `#include "test.h"` in the following example:

test.h

  namespace ns {}

test.cpp

  include "test.h"
  using namespace ns;

This patch fixes this behavior by handling using-directive declarations


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134379

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


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -77,7 +77,7 @@
           "using ns::foo;",
       },
       {
-          "namespace ns { void foo(); void foo() {}; }",
+          "namespace ^ns { void foo(); void foo() {}; }",
           "using namespace ns;",
       },
       {
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -133,6 +133,11 @@
     return true;
   }
 
+  bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
+    add(D->getNominatedNamespace());
+    return true;
+  }
+
   // Enums may be usefully forward-declared as *complete* types by specifying
   // an underlying type. In this case, the definition should see the 
declaration
   // so they can be checked for compatibility.


Index: clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -77,7 +77,7 @@
           "using ns::foo;",
       },
       {
-          "namespace ns { void foo(); void foo() {}; }",
+          "namespace ^ns { void foo(); void foo() {}; }",
           "using namespace ns;",
       },
       {
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -133,6 +133,11 @@
     return true;
   }
 
+  bool VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
+    add(D->getNominatedNamespace());
+    return true;
+  }
+
   // Enums may be usefully forward-declared as *complete* types by specifying
   // an underlying type. In this case, the definition should see the declaration
   // so they can be checked for compatibility.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to