Skip to content

Commit a4d1401

Browse files
authored
[include-cleaner] Fix handling of enums in presence of qualifiers (#65952)
1 parent b40a5be commit a4d1401

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,13 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
138138
// If the ref is without a qualifier, and is a member, ignore it. As it is
139139
// available in current context due to some other construct (e.g. base
140140
// specifiers, using decls) that has to spell the name explicitly.
141+
//
141142
// If it's an enum constant, it must be due to prior decl. Report references
142-
// to it instead.
143-
if (llvm::isa<EnumConstantDecl>(FD) && !DRE->hasQualifier())
144-
report(DRE->getLocation(), FD);
143+
// to it when qualifier isn't a type.
144+
if (llvm::isa<EnumConstantDecl>(FD)) {
145+
if (!DRE->getQualifier() || DRE->getQualifier()->getAsNamespace())
146+
report(DRE->getLocation(), FD);
147+
}
145148
return true;
146149
}
147150

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,20 @@ TEST(WalkAST, Functions) {
514514
}
515515

516516
TEST(WalkAST, Enums) {
517-
testWalk("enum E { $explicit^A = 42, B = 43 };", "int e = ^A;");
517+
testWalk("enum E { $explicit^A = 42 };", "int e = ^A;");
518518
testWalk("enum class $explicit^E : int;", "enum class ^E : int {};");
519519
testWalk("enum class E : int {};", "enum class ^E : int ;");
520+
testWalk("namespace ns { enum E { $explicit^A = 42 }; }", "int e = ns::^A;");
521+
testWalk("namespace ns { enum E { A = 42 }; } using ns::E::$explicit^A;",
522+
"int e = ^A;");
523+
testWalk("namespace ns { enum E { A = 42 }; } using enum ns::$explicit^E;",
524+
"int e = ^A;");
525+
testWalk(R"(namespace ns { enum E { A = 42 }; }
526+
struct S { using enum ns::E; };)",
527+
"int e = S::^A;");
528+
testWalk(R"(namespace ns { enum E { A = 42 }; }
529+
struct S { using ns::E::A; };)",
530+
"int e = S::^A;");
520531
}
521532

522533
TEST(WalkAST, InitializerList) {

0 commit comments

Comments
 (0)