[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)
This commit is contained in:
mitchell 2025-11-03 21:15:26 +08:00 committed by GitHub
parent 6b8ca33e93
commit de2797c888
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 68 additions and 40 deletions

View File

@ -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 @@ public:
"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>(

View File

@ -20,6 +20,7 @@ add_clang_library(clangTidyBugproneModule STATIC
CopyConstructorInitCheck.cpp
CrtpConstructorAccessibilityCheck.cpp
DanglingHandleCheck.cpp
DefaultOperatorNewOnOveralignedTypeCheck.cpp
DerivedMethodShadowingBaseMethodCheck.cpp
DynamicStaticInitializersCheck.cpp
EasilySwappableParametersCheck.cpp

View File

@ -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

View File

@ -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 @@ public:
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

View File

@ -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 @@ public:
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>(

View File

@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangTidyCERTModule STATIC
CERTTidyModule.cpp
DefaultOperatorNewAlignmentCheck.cpp
DontModifyStdNamespaceCheck.cpp
FloatLoopCounter.cpp
LimitedRandomnessCheck.cpp

View File

@ -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>`

View File

@ -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>`_.

View File

@ -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

View File

@ -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>`,

View File

@ -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]
}

View File

@ -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]
}

View File

@ -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]
}