Skip to content

Commit 279b498

Browse files
committed
[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 llvm/llvm-project#60812 Differential Revision: https://reviews.llvm.org/D144582
1 parent 2b89525 commit 279b498

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
102102
}
103103

104104
bool VisitCXXConstructExpr(CXXConstructExpr *E) {
105+
// Always treat consturctor calls as implicit. We'll have an explicit
106+
// reference for the constructor calls that mention the type-name (through
107+
// TypeLocs). This reference only matters for cases where there's no
108+
// explicit syntax at all or there're only braces.
105109
report(E->getLocation(), getMemberProvider(E->getType()),
106-
E->getParenOrBraceRange().isValid() ? RefType::Explicit
107-
: RefType::Implicit);
110+
RefType::Implicit);
108111
return true;
109112
}
110113

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ TEST(WalkAST, TagType) {
111111
testWalk("struct $explicit^S {};", "^S *y;");
112112
testWalk("enum $explicit^E {};", "^E *y;");
113113
testWalk("struct $explicit^S { static int x; };", "int y = ^S::x;");
114+
// One explicit call from the TypeLoc in constructor spelling, another
115+
// implicit reference through the constructor call.
116+
testWalk("struct $explicit^$implicit^S { static int x; };", "auto y = ^S();");
114117
}
115118

116119
TEST(WalkAST, Alias) {
@@ -241,7 +244,7 @@ TEST(WalkAST, MemberExprs) {
241244
TEST(WalkAST, ConstructExprs) {
242245
testWalk("struct $implicit^S {};", "S ^t;");
243246
testWalk("struct $implicit^S { S(); };", "S ^t;");
244-
testWalk("struct $explicit^S { S(int); };", "S ^t(42);");
247+
testWalk("struct $implicit^S { S(int); };", "S ^t(42);");
245248
testWalk("struct $implicit^S { S(int); };", "S t = ^42;");
246249
testWalk("namespace ns { struct S{}; } using ns::$implicit^S;", "S ^t;");
247250
}

0 commit comments

Comments
 (0)