diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp --- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp +++ b/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(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(FD)) + 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 --- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp @@ -309,6 +309,14 @@ namespace ns { using ::$explicit^Foo; } template<> struct ns::Foo {};)cpp", "ns::^Foo 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) {