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 @@ -49,6 +49,12 @@ Callback(Loc, *cast(ND->getCanonicalDecl()), RT); } + NamedDecl *resolveType(QualType Type) { + if (Type->isPointerType()) + Type = Type->getPointeeType(); + return Type->getAsRecordDecl(); + } + public: ASTWalker(DeclCallback Callback, bool IsHeader) : Callback(Callback), IsHeader(IsHeader) {} @@ -59,7 +65,12 @@ } bool VisitMemberExpr(MemberExpr *E) { - report(E->getMemberLoc(), E->getFoundDecl().getDecl()); + // A member expr implies a usage of the class type + // (e.g., to prevent inserting a header of base class when using base + // members from a derived object). + // FIXME: support dependent types, e.g., "std::vector().size()". + QualType Type = E->getBase()->IgnoreImpCasts()->getType(); + report(E->getMemberLoc(), resolveType(Type)); 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 @@ -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. @@ -172,10 +170,22 @@ } TEST(WalkAST, MemberExprs) { - testWalk("struct S { void $explicit^foo(); };", "void foo() { S{}.^foo(); }"); + testWalk("struct $explicit^S { void foo(); };", "void foo() { S{}.^foo(); }"); testWalk( - "struct S { void foo(); }; struct X : S { using S::$explicit^foo; };", + "struct S { void foo(); }; struct $explicit^X : S { using S::foo; };", "void foo() { X{}.^foo(); }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "void fun(Derived d) { d.^a; }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "void fun(Derived* d) { d->^a; }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "void fun(Derived& d) { d.^a; }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "void fun() { Derived().^a; }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "Derived foo(); void fun() { foo().^a; }"); + testWalk("struct Base { int a; }; struct $explicit^Derived : public Base {};", + "Derived& foo(); void fun() { foo().^a; }"); } TEST(WalkAST, ConstructExprs) {