From f02c2f052c0ad7a3a51ead978c883f7ee35e3c3f Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Fri, 23 Aug 2024 17:21:19 +0200 Subject: [PATCH] [include-cleaner] Turn new/delete usages to ambiguous references In practice most of these expressions just resolve to implicitly provided `operator new` and standard says it's not necessary to include `` for that. Hence this is resulting in a lot of churn in cases where inclusion of `` doesn't matter, and might even be undesired by the developer. By switching to an ambiguous reference we try to find a middle ground here, ensuring that we don't drop providers of `operator new` when the developer explicitly listed them in the includes, and chose to believe it's the implicitly provided `operator new` and don't insert an include in other cases. --- clang-tools-extra/include-cleaner/lib/WalkAST.cpp | 4 ++-- clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp index a5ac3760a3be2..598484d09712e 100644 --- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp +++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp @@ -351,11 +351,11 @@ class ASTWalker : public RecursiveASTVisitor { } bool VisitCXXNewExpr(CXXNewExpr *E) { - report(E->getExprLoc(), E->getOperatorNew()); + report(E->getExprLoc(), E->getOperatorNew(), RefType::Ambiguous); return true; } bool VisitCXXDeleteExpr(CXXDeleteExpr *E) { - report(E->getExprLoc(), E->getOperatorDelete()); + report(E->getExprLoc(), E->getOperatorDelete(), RefType::Ambiguous); return true; } }; diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp index b0a4473d4ad2b..6c8eacbff1cea 100644 --- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp @@ -557,9 +557,9 @@ TEST(WalkAST, FriendDecl) { } TEST(WalkAST, OperatorNewDelete) { - testWalk("void* $explicit^operator new(decltype(sizeof(int)), void*);", + testWalk("void* $ambiguous^operator new(decltype(sizeof(int)), void*);", "struct Bar { void foo() { Bar b; ^new (&b) Bar; } };"); - testWalk("struct A { static void $explicit^operator delete(void*); };", + testWalk("struct A { static void $ambiguous^operator delete(void*); };", "void foo() { A a; ^delete &a; }"); } } // namespace