[clang-tools-extra] [include-cleaner] Turn new/delete usages to ambiguous references (PR #105844)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 23 08:26:33 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: kadir çetinkaya (kadircet)

<details>
<summary>Changes</summary>

In practice most of these expressions just resolve to implicitly
provided `operator new` and standard says it's not necessary to include
`<new>` for that.
Hence this is resulting in a lot of churn in cases where inclusion of
`<new>` 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.


---
Full diff: https://github.com/llvm/llvm-project/pull/105844.diff


2 Files Affected:

- (modified) clang-tools-extra/include-cleaner/lib/WalkAST.cpp (+2-2) 
- (modified) clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp (+2-2) 


``````````diff
diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index a5ac3760a3be2a..598484d09712e5 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<ASTWalker> {
   }
 
   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 b0a4473d4ad2b7..6c8eacbff1cea3 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

``````````

</details>


https://github.com/llvm/llvm-project/pull/105844


More information about the cfe-commits mailing list