[clang-tools-extra] [include-cleaner] Add handling for new/delete expressions (PR #104033)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 07:51:10 PDT 2024


https://github.com/kadircet created https://github.com/llvm/llvm-project/pull/104033

None

>From 180279545dd2e6f2b6581376a21fd862d4786197 Mon Sep 17 00:00:00 2001
From: Kadir Cetinkaya <kadircet at google.com>
Date: Wed, 14 Aug 2024 16:50:22 +0200
Subject: [PATCH] [include-cleaner] Add handling for new/delete expressions

---
 clang-tools-extra/include-cleaner/lib/WalkAST.cpp      | 10 +++++++++-
 .../include-cleaner/unittests/WalkASTTest.cpp          |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index f7cc9d19123635..5a6bd85a30e469 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -23,7 +23,6 @@
 #include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
-#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/STLFunctionalExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
@@ -352,6 +351,15 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
            RefType::Implicit);
     return true;
   }
+
+  bool VisitCXXNewExpr(CXXNewExpr *E) {
+    report(E->getExprLoc(), E->getOperatorNew());
+    return true;
+  }
+  bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
+    report(E->getExprLoc(), E->getOperatorDelete());
+    return true;
+  }
 };
 
 } // namespace
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 282abce3246ca3..79371f5978fc33 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -555,5 +555,12 @@ TEST(WalkAST, FriendDecl) {
   testWalk("void $explicit^foo();", "struct Bar { friend void ^foo(); };");
   testWalk("struct $explicit^Foo {};", "struct Bar { friend struct ^Foo; };");
 }
+
+TEST(WalkAST, OperatorNewDelete) {
+  testWalk("void* $explicit^operator new(unsigned long, void*);",
+           "struct Bar { void foo() { Bar b; ^new (&b) Bar; } };");
+  testWalk("struct A { static void $explicit^operator delete(void*); };",
+           "void foo() { A a; ^delete &a; }");
+}
 } // namespace
 } // namespace clang::include_cleaner



More information about the cfe-commits mailing list