[PATCH] D158515: [include-cleaner] Make handling of enum constants similar to members
Kadir Cetinkaya via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 22 07:18:14 PDT 2023
kadircet created this revision.
kadircet added a reviewer: sammccall.
Herald added a project: All.
kadircet requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
We were treating enum constants more like regular decls, which results
in ignoring type aliases/exports.
This patch brings the handling to be closer to member-like decls, with
one caveat. When we encounter reference to an enum constant we still
report an explicit reference to the particular enum constant, as
otherwise we might not see any references to the enum itself.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D158515
Files:
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Index: clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -309,6 +309,14 @@
namespace ns { using ::$explicit^Foo; }
template<> struct ns::Foo<int> {};)cpp",
"ns::^Foo<int> x;");
+ testWalk(R"cpp(
+ namespace ns { enum class foo { bar }; }
+ using ns::$implicit^foo;)cpp",
+ "auto x = foo::^bar;");
+ testWalk(R"cpp(
+ namespace ns { enum foo { bar }; }
+ using ns::foo::$explicit^bar;)cpp",
+ "auto x = ^bar;");
}
TEST(WalkAST, Using) {
Index: clang-tools-extra/include-cleaner/lib/WalkAST.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -126,13 +126,27 @@
}
bool VisitDeclRefExpr(DeclRefExpr *DRE) {
- // Static class members are handled here, as they don't produce MemberExprs.
- if (DRE->getFoundDecl()->isCXXClassMember()) {
- if (auto *Qual = DRE->getQualifier())
- report(DRE->getLocation(), Qual->getAsRecordDecl(), RefType::Implicit);
- } else {
- report(DRE->getLocation(), DRE->getFoundDecl());
+ auto *FD = DRE->getFoundDecl();
+ // For refs to non-meber-like decls, use the found decl.
+ if (!FD->isCXXClassMember() && !llvm::isa<EnumConstantDecl>(FD)) {
+ report(DRE->getLocation(), FD);
+ return true;
+ }
+ // For refs to member-like decls, report an implicit ref to the container.
+ if (auto *Qual = DRE->getQualifier()) {
+ if (auto *Ty = Qual->getAsType()) {
+ report(DRE->getLocation(), getMemberProvider(QualType(Ty, 0)),
+ RefType::Implicit);
+ }
+ return true;
}
+ // 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))
+ report(DRE->getLocation(), FD);
return true;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158515.552350.patch
Type: text/x-patch
Size: 2330 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230822/6879aabd/attachment.bin>
More information about the cfe-commits
mailing list