Skip to content

Commit de2797c

Browse files
authored
[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 #157287. Closes [#157289](#157289)
1 parent 6b8ca33 commit de2797c

13 files changed

+68
-40
lines changed

clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "CopyConstructorInitCheck.h"
2525
#include "CrtpConstructorAccessibilityCheck.h"
2626
#include "DanglingHandleCheck.h"
27+
#include "DefaultOperatorNewOnOveralignedTypeCheck.h"
2728
#include "DerivedMethodShadowingBaseMethodCheck.h"
2829
#include "DynamicStaticInitializersCheck.h"
2930
#include "EasilySwappableParametersCheck.h"
@@ -140,6 +141,8 @@ class BugproneModule : public ClangTidyModule {
140141
"bugprone-copy-constructor-init");
141142
CheckFactories.registerCheck<DanglingHandleCheck>(
142143
"bugprone-dangling-handle");
144+
CheckFactories.registerCheck<DefaultOperatorNewOnOveralignedTypeCheck>(
145+
"bugprone-default-operator-new-on-overaligned-type");
143146
CheckFactories.registerCheck<DerivedMethodShadowingBaseMethodCheck>(
144147
"bugprone-derived-method-shadowing-base-method");
145148
CheckFactories.registerCheck<DynamicStaticInitializersCheck>(

clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_clang_library(clangTidyBugproneModule STATIC
2020
CopyConstructorInitCheck.cpp
2121
CrtpConstructorAccessibilityCheck.cpp
2222
DanglingHandleCheck.cpp
23+
DefaultOperatorNewOnOveralignedTypeCheck.cpp
2324
DerivedMethodShadowingBaseMethodCheck.cpp
2425
DynamicStaticInitializersCheck.cpp
2526
EasilySwappableParametersCheck.cpp

clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "DefaultOperatorNewAlignmentCheck.h"
9+
#include "DefaultOperatorNewOnOveralignedTypeCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "clang/Basic/TargetInfo.h"
1313

1414
using namespace clang::ast_matchers;
1515

16-
namespace clang::tidy::cert {
16+
namespace clang::tidy::bugprone {
1717

18-
void DefaultOperatorNewAlignmentCheck::registerMatchers(MatchFinder *Finder) {
18+
void DefaultOperatorNewOnOveralignedTypeCheck::registerMatchers(
19+
MatchFinder *Finder) {
1920
Finder->addMatcher(
2021
cxxNewExpr(unless(hasAnyPlacementArg(anything()))).bind("new"), this);
2122
}
2223

23-
void DefaultOperatorNewAlignmentCheck::check(
24+
void DefaultOperatorNewOnOveralignedTypeCheck::check(
2425
const MatchFinder::MatchResult &Result) {
2526
// Get the found 'new' expression.
2627
const auto *NewExpr = Result.Nodes.getNodeAs<CXXNewExpr>("new");
@@ -61,4 +62,4 @@ void DefaultOperatorNewAlignmentCheck::check(
6162
<< (SpecifiedAlignment / CharWidth);
6263
}
6364

64-
} // namespace clang::tidy::cert
65+
} // namespace clang::tidy::bugprone

clang-tools-extra/clang-tidy/cert/DefaultOperatorNewAlignmentCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::cert {
14+
namespace clang::tidy::bugprone {
1515

1616
/// Checks if an object of type with extended alignment is allocated by using
1717
/// the default operator new.
1818
///
1919
/// For the user-facing documentation see:
20-
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/mem57-cpp.html
21-
class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck {
20+
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/bugprone-default-operator-new-on-overaligned-type.html
21+
class DefaultOperatorNewOnOveralignedTypeCheck : public ClangTidyCheck {
2222
public:
23-
DefaultOperatorNewAlignmentCheck(StringRef Name, ClangTidyContext *Context)
23+
DefaultOperatorNewOnOveralignedTypeCheck(StringRef Name,
24+
ClangTidyContext *Context)
2425
: ClangTidyCheck(Name, Context) {}
2526
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2627
return !LangOpts.CPlusPlus17;
@@ -29,6 +30,6 @@ class DefaultOperatorNewAlignmentCheck : public ClangTidyCheck {
2930
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3031
};
3132

32-
} // namespace clang::tidy::cert
33+
} // namespace clang::tidy::bugprone
3334

34-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DEFAULTOPERATORNEWALIGNMENTCHECK_H
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DEFAULTOPERATORNEWONOVERALIGNEDTYPECHECK_H

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModuleRegistry.h"
1212
#include "../bugprone/BadSignalToKillThreadCheck.h"
1313
#include "../bugprone/CommandProcessorCheck.h"
14+
#include "../bugprone/DefaultOperatorNewOnOveralignedTypeCheck.h"
1415
#include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h"
1516
#include "../bugprone/RawMemoryCallOnNonTrivialTypeCheck.h"
1617
#include "../bugprone/ReservedIdentifierCheck.h"
@@ -35,7 +36,6 @@
3536
#include "../performance/MoveConstructorInitCheck.h"
3637
#include "../readability/EnumInitialValueCheck.h"
3738
#include "../readability/UppercaseLiteralSuffixCheck.h"
38-
#include "DefaultOperatorNewAlignmentCheck.h"
3939
#include "DontModifyStdNamespaceCheck.h"
4040
#include "FloatLoopCounter.h"
4141
#include "LimitedRandomnessCheck.h"
@@ -265,8 +265,9 @@ class CERTModule : public ClangTidyModule {
265265
CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
266266
"cert-err61-cpp");
267267
// MEM
268-
CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>(
269-
"cert-mem57-cpp");
268+
CheckFactories
269+
.registerCheck<bugprone::DefaultOperatorNewOnOveralignedTypeCheck>(
270+
"cert-mem57-cpp");
270271
// MSC
271272
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
272273
CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(

clang-tools-extra/clang-tidy/cert/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS
55

66
add_clang_library(clangTidyCERTModule STATIC
77
CERTTidyModule.cpp
8-
DefaultOperatorNewAlignmentCheck.cpp
98
DontModifyStdNamespaceCheck.cpp
109
FloatLoopCounter.cpp
1110
LimitedRandomnessCheck.cpp

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ New check aliases
264264
<clang-tidy/checks/bugprone/throwing-static-initialization>`
265265
keeping initial check as an alias to the new one.
266266

267+
- Renamed :doc:`cert-mem57-cpp <clang-tidy/checks/cert/mem57-cpp>` to
268+
:doc:`bugprone-default-operator-new-on-overaligned-type
269+
<clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type>`
270+
keeping initial check as an alias to the new one.
271+
267272
- Renamed :doc:`cert-oop57-cpp <clang-tidy/checks/cert/oop57-cpp>` to
268273
:doc:`bugprone-raw-memory-call-on-non-trivial-type
269274
<clang-tidy/checks/bugprone/raw-memory-call-on-non-trivial-type>`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.. title:: clang-tidy - bugprone-default-operator-new-on-overaligned-type
2+
3+
bugprone-default-operator-new-on-overaligned-type
4+
=================================================
5+
6+
Flags uses of default ``operator new`` where the type has extended
7+
alignment (an alignment greater than the fundamental alignment).
8+
9+
The default ``operator new`` is guaranteed to provide the correct alignment
10+
if the requested alignment is less or equal to the fundamental alignment.
11+
Only cases are detected (by design) where the ``operator new`` is not
12+
user-defined and is not a placement new (the reason is that in these cases we
13+
assume that the user provided the correct memory allocation).
14+
15+
References
16+
----------
17+
18+
This check corresponds to the CERT C++ Coding Standard rule
19+
`MEM57-CPP. Avoid using default operator new for over-aligned types
20+
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MEM57-CPP.+Avoid+using+default+operator+new+for+over-aligned+types>`_.

clang-tools-extra/docs/clang-tidy/checks/cert/mem57-cpp.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
cert-mem57-cpp
44
==============
55

6-
This check flags uses of default ``operator new`` where the type has extended
7-
alignment (an alignment greater than the fundamental alignment). (The default
8-
``operator new`` is guaranteed to provide the correct alignment if the
9-
requested alignment is less or equal to the fundamental alignment).
10-
Only cases are detected (by design) where the ``operator new`` is not
11-
user-defined and is not a placement new (the reason is that in these cases we
12-
assume that the user provided the correct memory allocation).
6+
The `cert-mem57-cpp` is an aliaes, please see
7+
`bugprone-default-operator-new-on-overaligned-type <../bugprone/default-operator-new-on-overaligned-type>`_
8+
for more information.
139

1410
This check corresponds to the CERT C++ Coding Standard rule
1511
`MEM57-CPP. Avoid using default operator new for over-aligned types

clang-tools-extra/docs/clang-tidy/checks/list.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Clang-Tidy Checks
9292
:doc:`bugprone-copy-constructor-init <bugprone/copy-constructor-init>`, "Yes"
9393
:doc:`bugprone-crtp-constructor-accessibility <bugprone/crtp-constructor-accessibility>`, "Yes"
9494
:doc:`bugprone-dangling-handle <bugprone/dangling-handle>`,
95+
:doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`,
9596
:doc:`bugprone-derived-method-shadowing-base-method <bugprone/derived-method-shadowing-base-method>`,
9697
:doc:`bugprone-dynamic-static-initializers <bugprone/dynamic-static-initializers>`,
9798
:doc:`bugprone-easily-swappable-parameters <bugprone/easily-swappable-parameters>`,
@@ -178,7 +179,6 @@ Clang-Tidy Checks
178179
:doc:`cert-err33-c <cert/err33-c>`,
179180
:doc:`cert-err60-cpp <cert/err60-cpp>`,
180181
:doc:`cert-flp30-c <cert/flp30-c>`,
181-
:doc:`cert-mem57-cpp <cert/mem57-cpp>`,
182182
:doc:`cert-msc50-cpp <cert/msc50-cpp>`,
183183
:doc:`cert-msc51-cpp <cert/msc51-cpp>`,
184184
:doc:`cert-oop58-cpp <cert/oop58-cpp>`,
@@ -452,6 +452,7 @@ Check aliases
452452
:doc:`cert-fio38-c <cert/fio38-c>`, :doc:`misc-non-copyable-objects <misc/non-copyable-objects>`,
453453
:doc:`cert-flp37-c <cert/flp37-c>`, :doc:`bugprone-suspicious-memory-comparison <bugprone/suspicious-memory-comparison>`,
454454
:doc:`cert-int09-c <cert/int09-c>`, :doc:`readability-enum-initial-value <readability/enum-initial-value>`, "Yes"
455+
:doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`,
455456
:doc:`cert-msc24-c <cert/msc24-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`,
456457
:doc:`cert-msc30-c <cert/msc30-c>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`,
457458
:doc:`cert-msc32-c <cert/msc32-c>`, :doc:`cert-msc51-cpp <cert/msc51-cpp>`,

0 commit comments

Comments
 (0)