diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp index 796a645ec2946..307e0652f9cca 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 { // 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(FD) && !DRE->hasQualifier()) - report(DRE->getLocation(), FD); + // to it when qualifier isn't a type. + if (llvm::isa(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 f53959877dc51..9b99d5a5c32ba 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) {