Skip to content

Conversation

@zeyi2
Copy link
Contributor

@zeyi2 zeyi2 commented Oct 29, 2025

Moves cert-mem57-cpp check into bugprone module and gives it a clearer name: bugprone-default-operator-new-alignment

This is part of the cleanup described in #157287.
Closes #157289

@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2025

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

Author: mitchell (zeyi2)

Changes

Moves cert-mem57-cpp check into bugprone module and gives it a clearer name: bugprone-default-operator-new-alignment

This is part of the cleanup described in #157287.
Closes #157289


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

13 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp (+3)
  • (modified) clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt (+1)
  • (renamed) clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp (+2-2)
  • (renamed) clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h (+6-6)
  • (modified) clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp (+2-2)
  • (modified) clang-tools-extra/clang-tidy/cert/CMakeLists.txt (-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+6-1)
  • (added) clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst (+19)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst (+3-7)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/list.rst (+2-1)
  • (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp (+12)
  • (renamed) clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp (+3-3)
  • (removed) clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp (-12)
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index e6115f67656bc..0352d6ba02727 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -24,6 +24,7 @@ #include "CopyConstructorInitCheck.h" #include "CrtpConstructorAccessibilityCheck.h" #include "DanglingHandleCheck.h" +#include "DefaultOperatorNewAlignmentCheck.h" #include "DerivedMethodShadowingBaseMethodCheck.h" #include "DynamicStaticInitializersCheck.h" #include "EasilySwappableParametersCheck.h" @@ -139,6 +140,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-copy-constructor-init"); CheckFactories.registerCheck<DanglingHandleCheck>( "bugprone-dangling-handle"); + CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>( + "bugprone-default-operator-new-alignment"); CheckFactories.registerCheck<DerivedMethodShadowingBaseMethodCheck>( "bugprone-derived-method-shadowing-base-method"); CheckFactories.registerCheck<DynamicStaticInitializersCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index c8943e5b22ef8..a62404703ea77 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyBugproneModule STATIC CopyConstructorInitCheck.cpp CrtpConstructorAccessibilityCheck.cpp DanglingHandleCheck.cpp + DefaultOperatorNewAlignmentCheck.cpp DerivedMethodShadowingBaseMethodCheck.cpp DynamicStaticInitializersCheck.cpp EasilySwappableParametersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp similarity index 96% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp index 45c170ec20f4e..17147b0f79d4e 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp @@ -13,7 +13,7 @@ using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( @@ -61,4 +61,4 @@ void DefaultOperatorNewAlignmentCheck::check( << (SpecifiedAlignment / CharWidth); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h similarity index 70% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h index 8f9d0e470a755..d4de0906bb1c2 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H #include "../ClangTidyCheck.h" -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { /// Checks if an object of type with extended alignment is allocated by using /// the default operator new. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/cert/mem57-cpp.html +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/default-operator-new-alignment.html class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck { public: DefaultOperatorNewAlignmentCheck(StringRef Name, ClangTidyContext *Context) @@ -29,6 +29,6 @@ class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck { void check(const ast_matchers::MatchFinder::MatchResult &Result) override; }; -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index c1ca2cec7a1eb..7a2527a12fc02 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModuleRegistry.h" #include "../bugprone/BadSignalToKillThreadCheck.h" #include "../bugprone/CommandProcessorCheck.h" +#include "../bugprone/DefaultOperatorNewAlignmentCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignalHandlerCheck.h" @@ -34,7 +35,6 @@ #include "../performance/MoveConstructorInitCheck.h" #include "../readability/EnumInitialValueCheck.h" #include "../readability/UppercaseLiteralSuffixCheck.h" -#include "DefaultOperatorNewAlignmentCheck.h" #include "DontModifyStdNamespaceCheck.h" #include "FloatLoopCounter.h" #include "LimitedRandomnessCheck.h" @@ -265,7 +265,7 @@ class CERTModule : public ClangTidyModule { CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>( "cert-err61-cpp"); // MEM - CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>( + CheckFactories.registerCheck<bugprone::DefaultOperatorNewAlignmentCheck>( "cert-mem57-cpp"); // MSC CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp"); diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 453d1d30921e9..bbcc81391bbe3 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyCERTModule STATIC CERTTidyModule.cpp - DefaultOperatorNewAlignmentCheck.cpp DontModifyStdNamespaceCheck.cpp FloatLoopCounter.cpp LimitedRandomnessCheck.cpp diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 915b79329dac4..8c8694cc83ce0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -254,6 +254,11 @@ New check aliases <clang-tidy/checks/bugprone/throwing-static-initialization>` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-mem57-cpp <clang-tidy/checks/cert/mem57-cpp>` to + :doc:`bugprone-default-operator-new-alignment + <clang-tidy/checks/bugprone/default-operator-new-alignment>` + keeping initial check as an alias to the new one. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -352,7 +357,7 @@ Changes in existing checks - Improved :doc:`misc-const-correctness <clang-tidy/checks/misc/const-correctness>` check to avoid false - positives when pointers is transferred to non-const references  + positives when pointers is transferred to non-const references and avoid false positives of function pointer and fix false positives on return of non-const pointer. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst new file mode 100644 index 0000000000000..8308a11f28959 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - bugprone-default-operator-new-alignment + +bugprone-default-operator-new-alignment +======================================= + +This check flags uses of default ``operator new`` where the type has extended +alignment (an alignment greater than the fundamental alignment). (The default +``operator new`` is guaranteed to provide the correct alignment if the +requested alignment is less or equal to the fundamental alignment). +Only cases are detected (by design) where the ``operator new`` is not +user-defined and is not a placement new (the reason is that in these cases we +assume that the user provided the correct memory allocation). + +References +---------- + +This check corresponds to the CERT C++ Coding Standard rule +`MEM57-CPP. Avoid using default operator new for over-aligned types +<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MEM57-CPP.+Avoid+using+default+operator+new+for+over-aligned+types>`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst index 135cfb86f3d50..5e5bb9c6af882 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst @@ -3,13 +3,9 @@ cert-mem57-cpp ============== -This check flags uses of default ``operator new`` where the type has extended -alignment (an alignment greater than the fundamental alignment). (The default -``operator new`` is guaranteed to provide the correct alignment if the -requested alignment is less or equal to the fundamental alignment). -Only cases are detected (by design) where the ``operator new`` is not -user-defined and is not a placement new (the reason is that in these cases we -assume that the user provided the correct memory allocation). +The `cert-mem57-cpp` is an aliaes, please see +`bugprone-default-operator-new-alignment <../bugprone/default-operator-new-alignment>`_ +for more information. This check corresponds to the CERT C++ Coding Standard rule `MEM57-CPP. Avoid using default operator new for over-aligned types diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index d3c89e469188d..9abb8131f9314 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ Clang-Tidy Checks :doc:`bugprone-crtp-constructor-accessibility <bugprone/crtp-constructor-accessibility>`, "Yes" :doc:`bugprone-dangling-handle <bugprone/dangling-handle>`, :doc:`bugprone-derived-method-shadowing-base-method <bugprone/derived-method-shadowing-base-method>`, + :doc:`bugprone-default-operator-new-alignment <bugprone/default-operator-new-alignment>`, :doc:`bugprone-dynamic-static-initializers <bugprone/dynamic-static-initializers>`, :doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`, :doc:`bugprone-empty-catch <bugprone/empty-catch>`, @@ -177,7 +178,6 @@ Clang-Tidy Checks :doc:`cert-err33-c <cert/err33-c>`, :doc:`cert-err60-cpp <cert/err60-cpp>`, :doc:`cert-flp30-c <cert/flp30-c>`, - :doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`, :doc:`cert-oop57-cpp <cert/oop57-cpp>`, @@ -452,6 +452,7 @@ Check aliases :doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`, :doc:`cert-flp37-c <cert/flp37-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`, :doc:`cert-int09-c <cert/int09-c>`, :doc:`readability-enum-initial-value <readability/enum-initial-value>`, "Yes" + :doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-alignment <bugprone/default-operator-new-alignment>`, :doc:`cert-msc24-c <cert/msc24-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`, :doc:`cert-msc30-c <cert/msc30-c>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`, :doc:`cert-msc32-c <cert/msc32-c>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`, diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp new file mode 100644 index 0000000000000..a87cf833861e5 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp @@ -0,0 +1,12 @@ +// RUN: %check_clang_tidy %s -std=c++14 bugprone-default-operator-new-alignment %t +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-alignment' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-alignment' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation + +struct alignas(128) Vector { + char Elems[128]; +}; + +void f() { + auto *V1 = new Vector; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] + auto *V1_Arr = new Vector[2]; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp similarity index 77% rename from clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp index e0300e35183dc..89a404ea6b86c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s -std=c++14 cert-mem57-cpp %t +// RUN: %check_clang_tidy %s -std=c++14 bugprone-default-operator-new-alignment %t namespace std { typedef __typeof(sizeof(int)) size_t; @@ -30,10 +30,10 @@ struct alignas(8) Vector4 { void f() { auto *V1 = new Vector1; - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] auto *V2 = new Vector2; auto *V3 = new Vector3; auto *V4 = new Vector4; auto *V1_Arr = new Vector1[2]; - // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] } diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp deleted file mode 100644 index 38ffcbd7e805d..0000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %check_clang_tidy %s -std=c++14 cert-mem57-cpp %t -// RUN: clang-tidy -checks='-*,cert-mem57-cpp' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation -// RUN: clang-tidy -checks='-*,cert-mem57-cpp' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation - -struct alignas(128) Vector { - char Elems[128]; -}; - -void f() { - auto *V1 = new Vector; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] - auto *V1_Arr = new Vector[2]; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] -} 
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2025

@llvm/pr-subscribers-clang-tidy

Author: mitchell (zeyi2)

Changes

Moves cert-mem57-cpp check into bugprone module and gives it a clearer name: bugprone-default-operator-new-alignment

This is part of the cleanup described in #157287.
Closes #157289


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

13 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp (+3)
  • (modified) clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt (+1)
  • (renamed) clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp (+2-2)
  • (renamed) clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h (+6-6)
  • (modified) clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp (+2-2)
  • (modified) clang-tools-extra/clang-tidy/cert/CMakeLists.txt (-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+6-1)
  • (added) clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst (+19)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst (+3-7)
  • (modified) clang-tools-extra/docs/clang-tidy/checks/list.rst (+2-1)
  • (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp (+12)
  • (renamed) clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp (+3-3)
  • (removed) clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp (-12)
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index e6115f67656bc..0352d6ba02727 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -24,6 +24,7 @@ #include "CopyConstructorInitCheck.h" #include "CrtpConstructorAccessibilityCheck.h" #include "DanglingHandleCheck.h" +#include "DefaultOperatorNewAlignmentCheck.h" #include "DerivedMethodShadowingBaseMethodCheck.h" #include "DynamicStaticInitializersCheck.h" #include "EasilySwappableParametersCheck.h" @@ -139,6 +140,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-copy-constructor-init"); CheckFactories.registerCheck<DanglingHandleCheck>( "bugprone-dangling-handle"); + CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>( + "bugprone-default-operator-new-alignment"); CheckFactories.registerCheck<DerivedMethodShadowingBaseMethodCheck>( "bugprone-derived-method-shadowing-base-method"); CheckFactories.registerCheck<DynamicStaticInitializersCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index c8943e5b22ef8..a62404703ea77 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -20,6 +20,7 @@ add_clang_library(clangTidyBugproneModule STATIC CopyConstructorInitCheck.cpp CrtpConstructorAccessibilityCheck.cpp DanglingHandleCheck.cpp + DefaultOperatorNewAlignmentCheck.cpp DerivedMethodShadowingBaseMethodCheck.cpp DynamicStaticInitializersCheck.cpp EasilySwappableParametersCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp similarity index 96% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp index 45c170ec20f4e..17147b0f79d4e 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.cpp @@ -13,7 +13,7 @@ using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher( @@ -61,4 +61,4 @@ void DefaultOperatorNewAlignmentCheck::check( << (SpecifiedAlignment / CharWidth); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h similarity index 70% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h index 8f9d0e470a755..d4de0906bb1c2 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewAlignmentCheck.h @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H #include "../ClangTidyCheck.h" -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { /// Checks if an object of type with extended alignment is allocated by using /// the default operator new. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/cert/mem57-cpp.html +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/default-operator-new-alignment.html class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck { public: DefaultOperatorNewAlignmentCheck(StringRef Name, ClangTidyContext *Context) @@ -29,6 +29,6 @@ class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck { void check(const ast_matchers::MatchFinder::MatchResult &Result) override; }; -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWALIGNMENTCHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index c1ca2cec7a1eb..7a2527a12fc02 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -11,6 +11,7 @@ #include "../ClangTidyModuleRegistry.h" #include "../bugprone/BadSignalToKillThreadCheck.h" #include "../bugprone/CommandProcessorCheck.h" +#include "../bugprone/DefaultOperatorNewAlignmentCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignalHandlerCheck.h" @@ -34,7 +35,6 @@ #include "../performance/MoveConstructorInitCheck.h" #include "../readability/EnumInitialValueCheck.h" #include "../readability/UppercaseLiteralSuffixCheck.h" -#include "DefaultOperatorNewAlignmentCheck.h" #include "DontModifyStdNamespaceCheck.h" #include "FloatLoopCounter.h" #include "LimitedRandomnessCheck.h" @@ -265,7 +265,7 @@ class CERTModule : public ClangTidyModule { CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>( "cert-err61-cpp"); // MEM - CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>( + CheckFactories.registerCheck<bugprone::DefaultOperatorNewAlignmentCheck>( "cert-mem57-cpp"); // MSC CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp"); diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 453d1d30921e9..bbcc81391bbe3 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyCERTModule STATIC CERTTidyModule.cpp - DefaultOperatorNewAlignmentCheck.cpp DontModifyStdNamespaceCheck.cpp FloatLoopCounter.cpp LimitedRandomnessCheck.cpp diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 915b79329dac4..8c8694cc83ce0 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -254,6 +254,11 @@ New check aliases <clang-tidy/checks/bugprone/throwing-static-initialization>` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-mem57-cpp <clang-tidy/checks/cert/mem57-cpp>` to + :doc:`bugprone-default-operator-new-alignment + <clang-tidy/checks/bugprone/default-operator-new-alignment>` + keeping initial check as an alias to the new one. + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -352,7 +357,7 @@ Changes in existing checks - Improved :doc:`misc-const-correctness <clang-tidy/checks/misc/const-correctness>` check to avoid false - positives when pointers is transferred to non-const references  + positives when pointers is transferred to non-const references and avoid false positives of function pointer and fix false positives on return of non-const pointer. diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst new file mode 100644 index 0000000000000..8308a11f28959 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-alignment.rst @@ -0,0 +1,19 @@ +.. title:: clang-tidy - bugprone-default-operator-new-alignment + +bugprone-default-operator-new-alignment +======================================= + +This check flags uses of default ``operator new`` where the type has extended +alignment (an alignment greater than the fundamental alignment). (The default +``operator new`` is guaranteed to provide the correct alignment if the +requested alignment is less or equal to the fundamental alignment). +Only cases are detected (by design) where the ``operator new`` is not +user-defined and is not a placement new (the reason is that in these cases we +assume that the user provided the correct memory allocation). + +References +---------- + +This check corresponds to the CERT C++ Coding Standard rule +`MEM57-CPP. Avoid using default operator new for over-aligned types +<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MEM57-CPP.+Avoid+using+default+operator+new+for+over-aligned+types>`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst index 135cfb86f3d50..5e5bb9c6af882 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst @@ -3,13 +3,9 @@ cert-mem57-cpp ============== -This check flags uses of default ``operator new`` where the type has extended -alignment (an alignment greater than the fundamental alignment). (The default -``operator new`` is guaranteed to provide the correct alignment if the -requested alignment is less or equal to the fundamental alignment). -Only cases are detected (by design) where the ``operator new`` is not -user-defined and is not a placement new (the reason is that in these cases we -assume that the user provided the correct memory allocation). +The `cert-mem57-cpp` is an aliaes, please see +`bugprone-default-operator-new-alignment <../bugprone/default-operator-new-alignment>`_ +for more information. This check corresponds to the CERT C++ Coding Standard rule `MEM57-CPP. Avoid using default operator new for over-aligned types diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index d3c89e469188d..9abb8131f9314 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -93,6 +93,7 @@ Clang-Tidy Checks :doc:`bugprone-crtp-constructor-accessibility <bugprone/crtp-constructor-accessibility>`, "Yes" :doc:`bugprone-dangling-handle <bugprone/dangling-handle>`, :doc:`bugprone-derived-method-shadowing-base-method <bugprone/derived-method-shadowing-base-method>`, + :doc:`bugprone-default-operator-new-alignment <bugprone/default-operator-new-alignment>`, :doc:`bugprone-dynamic-static-initializers <bugprone/dynamic-static-initializers>`, :doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`, :doc:`bugprone-empty-catch <bugprone/empty-catch>`, @@ -177,7 +178,6 @@ Clang-Tidy Checks :doc:`cert-err33-c <cert/err33-c>`, :doc:`cert-err60-cpp <cert/err60-cpp>`, :doc:`cert-flp30-c <cert/flp30-c>`, - :doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`, :doc:`cert-oop57-cpp <cert/oop57-cpp>`, @@ -452,6 +452,7 @@ Check aliases :doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`, :doc:`cert-flp37-c <cert/flp37-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`, :doc:`cert-int09-c <cert/int09-c>`, :doc:`readability-enum-initial-value <readability/enum-initial-value>`, "Yes" + :doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-alignment <bugprone/default-operator-new-alignment>`, :doc:`cert-msc24-c <cert/msc24-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`, :doc:`cert-msc30-c <cert/msc30-c>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`, :doc:`cert-msc32-c <cert/msc32-c>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`, diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp new file mode 100644 index 0000000000000..a87cf833861e5 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment-cpp17.cpp @@ -0,0 +1,12 @@ +// RUN: %check_clang_tidy %s -std=c++14 bugprone-default-operator-new-alignment %t +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-alignment' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-alignment' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation + +struct alignas(128) Vector { + char Elems[128]; +}; + +void f() { + auto *V1 = new Vector; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] + auto *V1_Arr = new Vector[2]; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] +} diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp similarity index 77% rename from clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp index e0300e35183dc..89a404ea6b86c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-alignment.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s -std=c++14 cert-mem57-cpp %t +// RUN: %check_clang_tidy %s -std=c++14 bugprone-default-operator-new-alignment %t namespace std { typedef __typeof(sizeof(int)) size_t; @@ -30,10 +30,10 @@ struct alignas(8) Vector4 { void f() { auto *V1 = new Vector1; - // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] auto *V2 = new Vector2; auto *V3 = new Vector3; auto *V4 = new Vector4; auto *V1_Arr = new Vector1[2]; - // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [bugprone-default-operator-new-alignment] } diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp deleted file mode 100644 index 38ffcbd7e805d..0000000000000 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// RUN: %check_clang_tidy %s -std=c++14 cert-mem57-cpp %t -// RUN: clang-tidy -checks='-*,cert-mem57-cpp' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation -// RUN: clang-tidy -checks='-*,cert-mem57-cpp' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation - -struct alignas(128) Vector { - char Elems[128]; -}; - -void f() { - auto *V1 = new Vector; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] - auto *V1_Arr = new Vector[2]; // CHECK-MESSAGES: warning: allocation function returns a pointer with alignment {{[0-9]+}} but the over-aligned type being allocated requires alignment 128 [cert-mem57-cpp] -} 
@zeyi2 zeyi2 changed the title Rename cert-mem57-cpp to bugprone-default-operator-new-alignment [clang-tidy] Rename cert-mem57-cpp to bugprone-default-operator-new-alignment Oct 29, 2025
@EugeneZelenko EugeneZelenko requested review from HerrCai0907, carlosgalvezp, localspook and vbvictor and removed request for vbvictor October 29, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment