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

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 30 06:14:40 PST 2024


Author: Congcong Cai
Date: 2024-11-30T22:14:36+08:00
New Revision: f89fa238faa6a63168997a8a1d03c15b71da8080

URL: https://github.com/llvm/llvm-project/commit/f89fa238faa6a63168997a8a1d03c15b71da8080
DIFF: https://github.com/llvm/llvm-project/commit/f89fa238faa6a63168997a8a1d03c15b71da8080.diff

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 71eb2d94cd4f26..0c417f6374bc50 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -15,7 +15,9 @@
 #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/SmallVector.h"
 
 using namespace clang::ast_matchers;
 
@@ -78,6 +80,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 +121,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 62577b9248fef3..ec666aeb2ad8ab 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -247,7 +247,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..68951fcf0aaac9 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 = decltype(sizeof(int));
+}
+void * operator new(std::size_t) { return nullptr; }
+void * operator new[](std::size_t) { return nullptr; }
+void operator delete(void*) noexcept {}
+void operator delete[](void*) noexcept {}
+// gh117489 end


        


More information about the cfe-commits mailing list