[clang] [clang-tools-extra] [clang-tidy][use-internal-linkage]fix false positives for global overloaded operator new and operator delete (PR #117945)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 27 16:16:11 PST 2024


https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/117945

None

>From 5654b7c96bf5f2e9ef739279f559b1d800afc5ec Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 28 Nov 2024 08:15:34 +0800
Subject: [PATCH] [clang-tidy][use-internal-linkage]fix false positives for
 global overloaded operator new and operator delete

---
 .../misc/UseInternalLinkageCheck.cpp          | 24 ++++++++++++++++++-
 clang-tools-extra/docs/ReleaseNotes.rst       |  3 ++-
 .../misc/use-internal-linkage-func.cpp        | 10 ++++++++
 clang/test/Sema/function-redecl.c             |  2 +-
 4 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 71eb2d94cd4f26..0f05d16870ffff 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -15,7 +15,10 @@
 #include "clang/Basic/SourceLocation.h"
 #include "clang/Basic/Specifiers.h"
 #include "clang/Lex/Token.h"
+#include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/SmallVector.h"
 
 using namespace clang::ast_matchers;
 
@@ -78,6 +81,22 @@ AST_POLYMORPHIC_MATCHER(isExternStorageClass,
   return Node.getStorageClass() == SC_Extern;
 }
 
+AST_MATCHER(FunctionDecl, isAllocationOrDeallocationOverloadedFunction) {
+  // [basic.stc.dynamic.allocation]
+  // An allocation function that is not a class member function shall belong to
+  // the global scope and not have a name with internal linkage.
+  // [basic.stc.dynamic.deallocation]
+  // A deallocation function that is not a class member function shall belong to
+  // the global scope and not have a name with internal linkage.
+  static const llvm::DenseSet<OverloadedOperatorKind> OverloadedOperators{
+      OverloadedOperatorKind::OO_New,
+      OverloadedOperatorKind::OO_Array_New,
+      OverloadedOperatorKind::OO_Delete,
+      OverloadedOperatorKind::OO_Array_Delete,
+  };
+  return OverloadedOperators.contains(Node.getOverloadedOperator());
+}
+
 } // namespace
 
 UseInternalLinkageCheck::UseInternalLinkageCheck(StringRef Name,
@@ -103,7 +122,10 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
                 // 4. friend
                 hasAncestor(friendDecl()))));
   Finder->addMatcher(
-      functionDecl(Common, hasBody(), unless(cxxMethodDecl()), unless(isMain()))
+      functionDecl(Common, hasBody(),
+                   unless(anyOf(cxxMethodDecl(),
+                                isAllocationOrDeallocationOverloadedFunction(),
+                                isMain())))
           .bind("fn"),
       this);
   Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index f050391110385e..9641d3a0711259 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -237,7 +237,8 @@ Changes in existing checks
 - Improved :doc:`misc-use-internal-linkage
   <clang-tidy/checks/misc/use-internal-linkage>` check to insert ``static``
   keyword before type qualifiers such as ``const`` and ``volatile`` and fix
-  false positives for function declaration without body.
+  false positives for function declaration without body and fix false positives
+  for global scoped overloaded ``operator new`` and ``operator delete``.
 
 - Improved :doc:`modernize-avoid-c-arrays
   <clang-tidy/checks/modernize/avoid-c-arrays>` check to suggest using 
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
index bf0d2c2513e562..f441254f0b1af2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
@@ -85,3 +85,13 @@ void func_with_body() {}
 void func_without_body();
 void func_without_body();
 }
+
+// gh117489 start
+namespace std {
+using size_t = unsigned long;
+}
+void * operator new(std::size_t);
+void * operator new[](std::size_t);
+void operator delete(void*);
+void operator delete[](void*);
+// gh117489 end
diff --git a/clang/test/Sema/function-redecl.c b/clang/test/Sema/function-redecl.c
index 3aeef00733d1f4..a1b7b4dc8a8599 100644
--- a/clang/test/Sema/function-redecl.c
+++ b/clang/test/Sema/function-redecl.c
@@ -43,7 +43,7 @@ void test(void) {
 }
 
 extern void g3(int); // expected-note{{previous declaration is here}}
-static void g3(int x) { } // expected-error{{static declaration of 'g3' follows non-static declaration}}
+static void g3(int x) { } // expected-error{{static declaration of 'g3' }}
 
 void test2(void) {
   extern int f2; // expected-note 2 {{previous definition is here}}



More information about the cfe-commits mailing list