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 @@ -11,6 +11,7 @@ #include "clang/AST/DeclCXX.h" #include "clang/AST/Expr.h" #include "clang/AST/ExprCXX.h" +#include "clang/AST/NestedNameSpecifier.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/AST/TemplateName.h" #include "clang/AST/Type.h" @@ -63,6 +64,27 @@ return true; } + bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E) { + QualType QT = E->getBaseType().getCanonicalType(); + if (QT->isPointerType()) { + QT = QT->getPointeeType(); + } + + const Type *UnqualifiedType = QT.getTypePtr(); + if (isa(QT)) { + const DependentNameType *DNT = cast(QT); + UnqualifiedType = DNT->getQualifier()->getAsType(); + } + + if (isa(UnqualifiedType)) { + const TemplateSpecializationType *TST = + cast(UnqualifiedType); + TemplateDecl *TD = TST->getTemplateName().getAsTemplateDecl(); + report(E->getMemberLoc(), TD); + } + return true; + } + bool VisitCXXConstructExpr(CXXConstructExpr *E) { report(E->getLocation(), E->getConstructor(), E->getParenOrBraceRange().isValid() ? RefType::Explicit 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 @@ -88,12 +88,10 @@ auto RTStr = llvm::to_string(RT); for (auto Expected : Target.points(RTStr)) if (!llvm::is_contained(ReferencedOffsets[RT], Expected)) - DiagnosePoint("location not marked used with type " + RTStr, - Expected); + DiagnosePoint("location not marked used with type " + RTStr, Expected); for (auto Actual : ReferencedOffsets[RT]) if (!llvm::is_contained(Target.points(RTStr), Actual)) - DiagnosePoint("location unexpectedly used with type " + RTStr, - Actual); + DiagnosePoint("location unexpectedly used with type " + RTStr, Actual); } // If there were any differences, we print the entire referencing code once. @@ -178,6 +176,42 @@ "void foo() { X{}.^foo(); }"); } +TEST(WalkAST, CXXDependentScopeMemberExprs) { + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base t) { t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base& t) { t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k(Base* t) { t->^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k() { Base t; t.^method(); }"); + testWalk("template struct $explicit^Base { void method(); };", + "template void k() { Base* t; t->^method(); }"); + testWalk("template struct Base { void method(); }; " + "template struct $explicit^Derived: Base {};", + "template void k(Derived t) { t.^method(); }"); + testWalk("template struct $explicit^Base { struct Nested { void " + "method(); }; };", + "template void k(typename Base::Nested d) { " + "d.^method(); }"); + testWalk("template struct $explicit^Base { struct Nested { void " + "method(); }; };", + "template void k(typename Base::Nested& d) { " + "d.^method(); }"); + testWalk("template struct $explicit^Base { struct Nested { void " + "method(); }; };", + "template void k(typename Base::Nested* d) { " + "d->^method(); }"); + testWalk("template struct $explicit^Base { struct Nested { void " + "method(); }; };", + "template void k() { " + "typename Base::Nested d; d.^method(); }"); + testWalk("template struct $explicit^Base { struct Nested { void " + "method(); }; };", + "template void k() { " + "typename Base::Nested* d; d->^method(); }"); +} + TEST(WalkAST, ConstructExprs) { testWalk("struct $implicit^S {};", "S ^t;"); testWalk("struct S { $implicit^S(); };", "S ^t;");