Author: mitchell Date: 2025-11-03T16:15:26+03:00 New Revision: de2797c888e03d99c554d11e830ea1b3fbdae29f
URL: https://github.com/llvm/llvm-project/commit/de2797c888e03d99c554d11e830ea1b3fbdae29f DIFF: https://github.com/llvm/llvm-project/commit/de2797c888e03d99c554d11e830ea1b3fbdae29f.diff LOG: [clang-tidy] Rename `cert-mem57-cpp` to `bugprone-default-operator-new-on-overaligned-type` (#165542) Moves `cert-mem57-cpp` check into `bugprone` module and gives it a clearer name: `bugprone-default-operator-new-on-overaligned-type` This is part of the cleanup described in https://github.com/llvm/llvm-project/issues/157287. Closes [#157289](https://github.com/llvm/llvm-project/issues/157289) Added: clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.cpp clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-on-overaligned-type-cpp17.cpp clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-on-overaligned-type.cpp Modified: clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp clang-tools-extra/clang-tidy/cert/CMakeLists.txt clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst clang-tools-extra/docs/clang-tidy/checks/list.rst Removed: clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp-cpp17.cpp clang-tools-extra/test/clang-tidy/checkers/cert/mem57-cpp.cpp ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index 7adff8a641fb8..2e21a4c4fd1f9 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 "DefaultOperatorNewOnOveralignedTypeCheck.h" #include "DerivedMethodShadowingBaseMethodCheck.h" #include "DynamicStaticInitializersCheck.h" #include "EasilySwappableParametersCheck.h" @@ -140,6 +141,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-copy-constructor-init"); CheckFactories.registerCheck<DanglingHandleCheck>( "bugprone-dangling-handle"); + CheckFactories.registerCheck<DefaultOperatorNewOnOveralignedTypeCheck>( + "bugprone-default-operator-new-on-overaligned-type"); 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 c0fdb4daaa305..31a0e6906866a 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 + DefaultOperatorNewOnOveralignedTypeCheck.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/DefaultOperatorNewOnOveralignedTypeCheck.cpp similarity index 88% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.cpp index 45c170ec20f4e..0aafdfd202390 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.cpp @@ -6,21 +6,22 @@ // //===----------------------------------------------------------------------===// -#include "DefaultOperatorNewAlignmentCheck.h" +#include "DefaultOperatorNewOnOveralignedTypeCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Basic/TargetInfo.h" using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { -void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) { +void DefaultOperatorNewOnOveralignedTypeCheck::registerMatchers( + MatchFinder *Finder) { Finder->addMatcher( cxxNewExpr(unless(hasAnyPlacementArg(anything()))).bind("new"), this); } -void DefaultOperatorNewAlignmentCheck::check( +void DefaultOperatorNewOnOveralignedTypeCheck::check( const MatchFinder::MatchResult &Result) { // Get the found 'new' expression. const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new"); @@ -61,4 +62,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/DefaultOperatorNewOnOveralignedTypeCheck.h similarity index 56% rename from clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h rename to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h index 8f9d0e470a755..b5b365b441209 100644 --- a/clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h @@ -6,21 +6,22 @@ // //===----------------------------------------------------------------------===// -#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_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_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 -class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/bugprone-default-operator-new-on-overaligned-type.html +class DefaultOperatorNewOnOveralignedTypeCheck : public ClangTidyCheck { public: - DefaultOperatorNewAlignmentCheck(StringRef Name, ClangTidyContext *Context) + DefaultOperatorNewOnOveralignedTypeCheck(StringRef Name, + ClangTidyContext *Context) : ClangTidyCheck(Name, Context) {} bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return !LangOpts.CPlusPlus17; @@ -29,6 +30,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_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index fa1eb4abc1dd8..d5179770008e5 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/DefaultOperatorNewOnOveralignedTypeCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" @@ -35,7 +36,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,8 +265,9 @@ class CERTModule : public ClangTidyModule { CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>( "cert-err61-cpp"); // MEM - CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>( - "cert-mem57-cpp"); + CheckFactories + .registerCheck<bugprone::DefaultOperatorNewOnOveralignedTypeCheck>( + "cert-mem57-cpp"); // MSC CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp"); CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>( diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index ce57faadcf749..db3b2f5a08286 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 f4eeb3e7942a8..35a64395f04e9 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -264,6 +264,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-on-overaligned-type + <clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type>` + keeping initial check as an alias to the new one. + - Renamed :doc:`cert-oop57-cpp <clang-tidy/checks/cert/oop57-cpp>` to :doc:`bugprone-raw-memory-call-on-non-trivial-type <clang-tidy/checks/bugprone/raw-memory-call-on-non-trivial-type>` diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type.rst new file mode 100644 index 0000000000000..c9918120f0770 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type.rst @@ -0,0 +1,20 @@ +.. title:: clang-tidy - bugprone-default-operator-new-on-overaligned-type + +bugprone-default-operator-new-on-overaligned-type +================================================= + +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..cc0c729aaacdc 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-on-overaligned-type <../bugprone/default-operator-new-on-overaligned-type>`_ +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 c7a922a91c6e0..e14ac715cfeeb 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -92,6 +92,7 @@ Clang-Tidy Checks :doc:`bugprone-copy-constructor-init <bugprone/copy-constructor-init>`, "Yes" :doc:`bugprone-crtp-constructor-accessibility <bugprone/crtp-constructor-accessibility>`, "Yes" :doc:`bugprone-dangling-handle <bugprone/dangling-handle>`, + :doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`, :doc:`bugprone-derived-method-shadowing-base-method <bugprone/derived-method-shadowing-base-method>`, :doc:`bugprone-dynamic-static-initializers <bugprone/dynamic-static-initializers>`, :doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`, @@ -178,7 +179,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-oop58-cpp <cert/oop58-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-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`, :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-on-overaligned-type-cpp17.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-on-overaligned-type-cpp17.cpp new file mode 100644 index 0000000000000..b05108c1e9775 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/default-operator-new-on-overaligned-type-cpp17.cpp @@ -0,0 +1,12 @@ +// RUN: %check_clang_tidy %s -std=c++14 bugprone-default-operator-new-on-overaligned-type %t +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-on-overaligned-type' --extra-arg=-Wno-unused-variable --warnings-as-errors='*' %s -- -std=c++17 -faligned-allocation +// RUN: clang-tidy -checks='-*,bugprone-default-operator-new-on-overaligned-type' --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-on-overaligned-type] + 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-on-overaligned-type] +} 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-on-overaligned-type.cpp similarity index 75% 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-on-overaligned-type.cpp index e0300e35183dc..379d8a2ff0f3c 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-on-overaligned-type.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-on-overaligned-type %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-on-overaligned-type] 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-on-overaligned-type] } 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] -} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
