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

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 10:21:06 PDT 2023


Author: kadir çetinkaya
Date: 2023-09-11T19:21:02+02:00
New Revision: a4d14011e57b91f8040ac08fd3496780aee3c253

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

LOG: [include-cleaner] Fix handling of enums in presence of qualifiers (#65952)

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/lib/WalkAST.cpp
    clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Removed: 
    


################################################################################
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