[clang-tools-extra] [include-cleaner] Fix handling of enums in presence of qualifiers (PR #65952)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 05:06:27 PDT 2023


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/65952:

None

>From a53f4dbec3a6a0944e67d09bce1b9af2d107e418 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Mon, 11 Sep 2023 14:00:58 +0200
Subject: [PATCH] [include-cleaner] Fix handling of enums in presence of
 qualifiers

---
 clang-tools-extra/include-cleaner/lib/WalkAST.cpp   |  9 ++++++---
 .../include-cleaner/unittests/WalkASTTest.cpp       | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index 796a645ec29466c..307e0652f9ccaf8 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -138,10 +138,13 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
     // If the ref is without a qualifier, and is a member, ignore it. As it is
     // available in current context due to some other construct (e.g. base
     // specifiers, using decls) that has to spell the name explicitly.
+    //
     // If it's an enum constant, it must be due to prior decl. Report references
-    // to it instead.
-    if (llvm::isa<EnumConstantDecl>(FD) && !DRE->hasQualifier())
-      report(DRE->getLocation(), FD);
+    // to it when qualifier isn't a type.
+    if (llvm::isa<EnumConstantDecl>(FD)) {
+      if (!DRE->getQualifier() || DRE->getQualifier()->getAsNamespace())
+        report(DRE->getLocation(), FD);
+    }
     return true;
   }
 
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index f53959877dc5133..9b99d5a5c32bad3 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -514,9 +514,20 @@ TEST(WalkAST, Functions) {
 }
 
 TEST(WalkAST, Enums) {
-  testWalk("enum E { $explicit^A = 42, B = 43 };", "int e = ^A;");
+  testWalk("enum E { $explicit^A = 42 };", "int e = ^A;");
   testWalk("enum class $explicit^E : int;", "enum class ^E : int {};");
   testWalk("enum class E : int {};", "enum class ^E : int ;");
+  testWalk("namespace ns { enum E { $explicit^A = 42 }; }", "int e = ns::^A;");
+  testWalk("namespace ns { enum E { A = 42 }; } using ns::E::$explicit^A;",
+           "int e = ^A;");
+  testWalk("namespace ns { enum E { A = 42 }; } using enum ns::$explicit^E;",
+           "int e = ^A;");
+  testWalk(R"(namespace ns { enum E { A = 42 }; }
+              struct S { using enum ns::E; };)",
+           "int e = S::^A;");
+  testWalk(R"(namespace ns { enum E { A = 42 }; }
+              struct S { using ns::E::A; };)",
+           "int e = S::^A;");
 }
 
 TEST(WalkAST, InitializerList) {



More information about the cfe-commits mailing list