[clang-tools-extra] 279b498 - [include-cleaner] Always treat constructor calls as implicit
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 23 01:01:33 PST 2023
Author: Kadir Cetinkaya
Date: 2023-02-23T10:00:50+01:00
New Revision: 279b4985ed4f62eb8e9a71e79379c2adfa8d8b99
URL: https://github.com/llvm/llvm-project/commit/279b4985ed4f62eb8e9a71e79379c2adfa8d8b99
DIFF: https://github.com/llvm/llvm-project/commit/279b4985ed4f62eb8e9a71e79379c2adfa8d8b99.diff
LOG: [include-cleaner] Always treat constructor calls as implicit
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
Differential Revision: https://reviews.llvm.org/D144582
Added:
Modified:
clang-tools-extra/include-cleaner/lib/WalkAST.cpp
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index f317b0dc2d07..0ca84145721a 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -102,9 +102,12 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
}
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;
}
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 3b97cc8cdfd5..7dcbf475e986 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -111,6 +111,9 @@ TEST(WalkAST, TagType) {
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, MemberExprs) {
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;");
}
More information about the cfe-commits
mailing list