Skip to content

[include-cleaner] Fix handling of enums in presence of qualifiers #65952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clang-tools-extra/include-cleaner/lib/WalkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 12 additions & 1 deletion clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down