kadircet created this revision. kadircet added a reviewer: hokein. Herald added a project: All. kadircet requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
Treating constructor calls when the type name isn't explicitly spelled can cause spurious results, so turn them into implicit references. This doesn't change the behaviour for constructor calls that explicitly spell the type name, as we should see a reference through the typeloc. Fixes https://github.com/llvm/llvm-project/issues/60812 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D144582 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 @@ -111,6 +111,9 @@ testWalk("struct $explicit^S {};", "^S *y;"); testWalk("enum $explicit^E {};", "^E *y;"); testWalk("struct $explicit^S { static int x; };", "int y = ^S::x;"); + // One explicit call from the TypeLoc in constructor spelling, another + // implicit reference through the constructor call. + testWalk("struct $explicit^$implicit^S { static int x; };", "auto y = ^S();"); } TEST(WalkAST, Alias) { @@ -241,7 +244,7 @@ TEST(WalkAST, ConstructExprs) { testWalk("struct $implicit^S {};", "S ^t;"); testWalk("struct $implicit^S { S(); };", "S ^t;"); - testWalk("struct $explicit^S { S(int); };", "S ^t(42);"); + testWalk("struct $implicit^S { S(int); };", "S ^t(42);"); testWalk("struct $implicit^S { S(int); };", "S t = ^42;"); testWalk("namespace ns { struct S{}; } using ns::$implicit^S;", "S ^t;"); } 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 @@ -102,9 +102,12 @@ } bool VisitCXXConstructExpr(CXXConstructExpr *E) { + // Always treat consturctor calls as implicit. We'll have an explicit + // reference for the constructor calls that mention the type-name (through + // TypeLocs). This reference only matters for cases where there's no + // explicit syntax at all or there're only braces. report(E->getLocation(), getMemberProvider(E->getType()), - E->getParenOrBraceRange().isValid() ? RefType::Explicit - : RefType::Implicit); + RefType::Implicit); 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 @@ -111,6 +111,9 @@ testWalk("struct $explicit^S {};", "^S *y;"); testWalk("enum $explicit^E {};", "^E *y;"); testWalk("struct $explicit^S { static int x; };", "int y = ^S::x;"); + // One explicit call from the TypeLoc in constructor spelling, another + // implicit reference through the constructor call. + testWalk("struct $explicit^$implicit^S { static int x; };", "auto y = ^S();"); } TEST(WalkAST, Alias) { @@ -241,7 +244,7 @@ TEST(WalkAST, ConstructExprs) { testWalk("struct $implicit^S {};", "S ^t;"); testWalk("struct $implicit^S { S(); };", "S ^t;"); - testWalk("struct $explicit^S { S(int); };", "S ^t(42);"); + testWalk("struct $implicit^S { S(int); };", "S ^t(42);"); testWalk("struct $implicit^S { S(int); };", "S t = ^42;"); testWalk("namespace ns { struct S{}; } using ns::$implicit^S;", "S ^t;"); } 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 @@ -102,9 +102,12 @@ } bool VisitCXXConstructExpr(CXXConstructExpr *E) { + // Always treat consturctor calls as implicit. We'll have an explicit + // reference for the constructor calls that mention the type-name (through + // TypeLocs). This reference only matters for cases where there's no + // explicit syntax at all or there're only braces. report(E->getLocation(), getMemberProvider(E->getType()), - E->getParenOrBraceRange().isValid() ? RefType::Explicit - : RefType::Implicit); + RefType::Implicit); return true; }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits