[PATCH] D134379: [clangd] IncludeCleaner: handle using namespace

Aleksandr Platonov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 21 11:48:37 PDT 2022


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134379.461973.patch
Type: text/x-patch
Size: 1086 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220921/8633aef2/attachment.bin>


More information about the cfe-commits mailing list