This revision was automatically updated to reflect the committed changes. Closed by commit rGd03e9f8fb074: [include-cleaner] Handle base class member access from derived class. (authored by VitaNuo).
Changed prior to commit: https://reviews.llvm.org/D139087?vs=480422&id=481217#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D139087/new/ https://reviews.llvm.org/D139087 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 @@ -180,10 +180,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) { 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 @@ -44,6 +44,12 @@ Callback(Loc, *cast<NamedDecl>(ND->getCanonicalDecl()), RT); } + NamedDecl *resolveType(QualType Type) { + if (Type->isPointerType()) + Type = Type->getPointeeType(); + return Type->getAsRecordDecl(); + } + public: ASTWalker(DeclCallback Callback) : Callback(Callback) {} @@ -53,7 +59,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<T>().size()". + QualType Type = E->getBase()->IgnoreImpCasts()->getType(); + report(E->getMemberLoc(), resolveType(Type)); return true; }
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 @@ -180,10 +180,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) { 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 @@ -44,6 +44,12 @@ Callback(Loc, *cast<NamedDecl>(ND->getCanonicalDecl()), RT); } + NamedDecl *resolveType(QualType Type) { + if (Type->isPointerType()) + Type = Type->getPointeeType(); + return Type->getAsRecordDecl(); + } + public: ASTWalker(DeclCallback Callback) : Callback(Callback) {} @@ -53,7 +59,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<T>().size()". + QualType Type = E->getBase()->IgnoreImpCasts()->getType(); + report(E->getMemberLoc(), resolveType(Type)); return true; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits