Skip to content

Commit 51d0f6d

Browse files
authored
[clang-tidy] Rename cert-dcl58-cpp to bugprone-std-namespace-modification (#165659)
Closes [#157290](#157290)
1 parent 988c1b1 commit 51d0f6d

File tree

12 files changed

+96
-71
lines changed

12 files changed

+96
-71
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "SizeofExpressionCheck.h"
7474
#include "SpuriouslyWakeUpFunctionsCheck.h"
7575
#include "StandaloneEmptyCheck.h"
76+
#include "StdNamespaceModificationCheck.h"
7677
#include "StringConstructorCheck.h"
7778
#include "StringIntegerAssignmentCheck.h"
7879
#include "StringLiteralWithEmbeddedNulCheck.h"
@@ -237,6 +238,8 @@ class BugproneModule : public ClangTidyModule {
237238
"bugprone-spuriously-wake-up-functions");
238239
CheckFactories.registerCheck<StandaloneEmptyCheck>(
239240
"bugprone-standalone-empty");
241+
CheckFactories.registerCheck<StdNamespaceModificationCheck>(
242+
"bugprone-std-namespace-modification");
240243
CheckFactories.registerCheck<StringConstructorCheck>(
241244
"bugprone-string-constructor");
242245
CheckFactories.registerCheck<StringIntegerAssignmentCheck>(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_clang_library(clangTidyBugproneModule STATIC
7575
SmartPtrArrayMismatchCheck.cpp
7676
SpuriouslyWakeUpFunctionsCheck.cpp
7777
StandaloneEmptyCheck.cpp
78+
StdNamespaceModificationCheck.cpp
7879
StringConstructorCheck.cpp
7980
StringIntegerAssignmentCheck.cpp
8081
StringLiteralWithEmbeddedNulCheck.cpp

clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp

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

9-
#include "DontModifyStdNamespaceCheck.h"
9+
#include "StdNamespaceModificationCheck.h"
1010
#include "clang/ASTMatchers/ASTMatchFinder.h"
1111
#include "clang/ASTMatchers/ASTMatchersInternal.h"
1212

@@ -36,9 +36,9 @@ AST_POLYMORPHIC_MATCHER_P(
3636

3737
} // namespace
3838

39-
namespace clang::tidy::cert {
39+
namespace clang::tidy::bugprone {
4040

41-
void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
41+
void StdNamespaceModificationCheck::registerMatchers(MatchFinder *Finder) {
4242
auto HasStdParent =
4343
hasDeclContext(namespaceDecl(hasAnyName("std", "posix"),
4444
unless(hasParent(namespaceDecl())))
@@ -96,7 +96,7 @@ void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
9696
.bind("decl"),
9797
this);
9898
}
99-
} // namespace clang::tidy::cert
99+
} // namespace clang::tidy::bugprone
100100

101101
static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) {
102102
const NamespaceDecl *LastNS = nullptr;
@@ -108,7 +108,7 @@ static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) {
108108
return LastNS;
109109
}
110110

111-
void clang::tidy::cert::DontModifyStdNamespaceCheck::check(
111+
void clang::tidy::bugprone::StdNamespaceModificationCheck::check(
112112
const MatchFinder::MatchResult &Result) {
113113
const auto *D = Result.Nodes.getNodeAs<Decl>("decl");
114114
const auto *NS = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc");

clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.h

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

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H
1111

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

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

1616
/// Modification of the std or posix namespace can result in undefined behavior.
1717
/// This check warns for such modifications.
1818
///
1919
/// For the user-facing documentation see:
20-
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/dcl58-cpp.html
21-
class DontModifyStdNamespaceCheck : public ClangTidyCheck {
20+
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/std-namespace-modification.html
21+
class StdNamespaceModificationCheck : public ClangTidyCheck {
2222
public:
23-
DontModifyStdNamespaceCheck(StringRef Name, ClangTidyContext *Context)
23+
StdNamespaceModificationCheck(StringRef Name, ClangTidyContext *Context)
2424
: ClangTidyCheck(Name, Context) {}
2525
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2626
return LangOpts.CPlusPlus;
@@ -29,6 +29,6 @@ class DontModifyStdNamespaceCheck : public ClangTidyCheck {
2929
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3030
};
3131

32-
} // namespace clang::tidy::cert
32+
} // namespace clang::tidy::bugprone
3333

34-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../bugprone/SignedCharMisuseCheck.h"
2020
#include "../bugprone/SizeofExpressionCheck.h"
2121
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
22+
#include "../bugprone/StdNamespaceModificationCheck.h"
2223
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
2324
#include "../bugprone/ThrowingStaticInitializationCheck.h"
2425
#include "../bugprone/UncheckedStringToNumberConversionCheck.h"
@@ -36,7 +37,6 @@
3637
#include "../performance/MoveConstructorInitCheck.h"
3738
#include "../readability/EnumInitialValueCheck.h"
3839
#include "../readability/UppercaseLiteralSuffixCheck.h"
39-
#include "DontModifyStdNamespaceCheck.h"
4040
#include "FloatLoopCounter.h"
4141
#include "LimitedRandomnessCheck.h"
4242
#include "MutatingCopyCheck.h"
@@ -251,7 +251,8 @@ class CERTModule : public ClangTidyModule {
251251
"cert-dcl51-cpp");
252252
CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
253253
"cert-dcl54-cpp");
254-
CheckFactories.registerCheck<DontModifyStdNamespaceCheck>("cert-dcl58-cpp");
254+
CheckFactories.registerCheck<bugprone::StdNamespaceModificationCheck>(
255+
"cert-dcl58-cpp");
255256
CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
256257
"cert-dcl59-cpp");
257258
// ERR

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-
DontModifyStdNamespaceCheck.cpp
98
FloatLoopCounter.cpp
109
LimitedRandomnessCheck.cpp
1110
MutatingCopyCheck.cpp

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ New check aliases
244244
<clang-tidy/checks/modernize/avoid-variadic-functions>`
245245
keeping initial check as an alias to the new one.
246246

247+
- Renamed :doc:`cert-dcl58-cpp <clang-tidy/checks/cert/dcl58-cpp>` to
248+
:doc:`bugprone-std-namespace-modification
249+
<clang-tidy/checks/bugprone/std-namespace-modification>`
250+
keeping initial check as an alias to the new one.
251+
247252
- Renamed :doc:`cert-env33-c <clang-tidy/checks/cert/env33-c>` to
248253
:doc:`bugprone-command-processor
249254
<clang-tidy/checks/bugprone/command-processor>`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. title:: clang-tidy - bugprone-std-namespace-modification
2+
3+
bugprone-std-namespace-modification
4+
===================================
5+
6+
Warns on modifications of the ``std`` or ``posix`` namespaces which can
7+
result in undefined behavior.
8+
9+
The ``std`` (or ``posix``) namespace is allowed to be extended with (class or
10+
function) template specializations that depend on an user-defined type (a type
11+
that is not defined in the standard system headers).
12+
13+
The check detects the following (user provided) declarations in namespace ``std`` or ``posix``:
14+
15+
- Anything that is not a template specialization.
16+
- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument.
17+
- Explicit specializations of any member function of a standard library class template.
18+
- Explicit specializations of any member function template of a standard library class or class template.
19+
- Explicit or partial specialization of any member class template of a standard library class or class template.
20+
21+
Examples:
22+
23+
.. code-block:: c++
24+
25+
namespace std {
26+
int x; // warning: modification of 'std' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace]
27+
}
28+
29+
namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior
30+
}
31+
32+
template <>
33+
struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior
34+
unsigned long operator()(const long &K) const {
35+
return K;
36+
}
37+
};
38+
39+
struct MyData { long data; };
40+
41+
template <>
42+
struct ::std::hash<MyData> { // no warning: specialization with user-defined type
43+
unsigned long operator()(const MyData &K) const {
44+
return K.data;
45+
}
46+
};
47+
48+
namespace std {
49+
template <>
50+
void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior
51+
52+
template <>
53+
bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior
54+
return true;
55+
}
56+
}
57+
58+
References
59+
----------
60+
61+
This check corresponds to the CERT C++ Coding Standard rule
62+
`DCL58-CPP. Do not modify the standard namespaces
63+
<https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_.

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

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,9 @@
33
cert-dcl58-cpp
44
==============
55

6-
Modification of the ``std`` or ``posix`` namespace can result in undefined
7-
behavior.
8-
This check warns for such modifications.
9-
The ``std`` (or ``posix``) namespace is allowed to be extended with (class or
10-
function) template specializations that depend on an user-defined type (a type
11-
that is not defined in the standard system headers).
12-
13-
The check detects the following (user provided) declarations in namespace ``std`` or ``posix``:
14-
15-
- Anything that is not a template specialization.
16-
- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument.
17-
- Explicit specializations of any member function of a standard library class template.
18-
- Explicit specializations of any member function template of a standard library class or class template.
19-
- Explicit or partial specialization of any member class template of a standard library class or class template.
20-
21-
Examples:
22-
23-
.. code-block:: c++
24-
25-
namespace std {
26-
int x; // warning: modification of 'std' namespace can result in undefined behavior [cert-dcl58-cpp]
27-
}
28-
29-
namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior
30-
}
31-
32-
template <>
33-
struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior
34-
unsigned long operator()(const long &K) const {
35-
return K;
36-
}
37-
};
38-
39-
struct MyData { long data; };
40-
41-
template <>
42-
struct ::std::hash<MyData> { // no warning: specialization with user-defined type
43-
unsigned long operator()(const MyData &K) const {
44-
return K.data;
45-
}
46-
};
47-
48-
namespace std {
49-
template <>
50-
void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior
51-
52-
template <>
53-
bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior
54-
return true;
55-
}
56-
}
6+
The `cert-dcl58-cpp` is an aliaes, please see
7+
`bugprone-std-namespace-modification <../bugprone/std-namespace-modification.html>`_
8+
for more information.
579

5810
This check corresponds to the CERT C++ Coding Standard rule
5911
`DCL58-CPP. Do not modify the standard namespaces

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Clang-Tidy Checks
141141
:doc:`bugprone-sizeof-expression <bugprone/sizeof-expression>`,
142142
:doc:`bugprone-spuriously-wake-up-functions <bugprone/spuriously-wake-up-functions>`,
143143
:doc:`bugprone-standalone-empty <bugprone/standalone-empty>`, "Yes"
144+
:doc:`bugprone-std-namespace-modification <bugprone/std-namespace-modification>`,
144145
:doc:`bugprone-string-constructor <bugprone/string-constructor>`, "Yes"
145146
:doc:`bugprone-string-integer-assignment <bugprone/string-integer-assignment>`, "Yes"
146147
:doc:`bugprone-string-literal-with-embedded-nul <bugprone/string-literal-with-embedded-nul>`,
@@ -175,7 +176,6 @@ Clang-Tidy Checks
175176
:doc:`bugprone-unused-return-value <bugprone/unused-return-value>`,
176177
:doc:`bugprone-use-after-move <bugprone/use-after-move>`,
177178
:doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes"
178-
:doc:`cert-dcl58-cpp <cert/dcl58-cpp>`,
179179
:doc:`cert-err33-c <cert/err33-c>`,
180180
:doc:`cert-err60-cpp <cert/err60-cpp>`,
181181
:doc:`cert-flp30-c <cert/flp30-c>`,
@@ -441,6 +441,7 @@ Check aliases
441441
:doc:`cert-dcl50-cpp <cert/dcl50-cpp>`, :doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`,
442442
:doc:`cert-dcl51-cpp <cert/dcl51-cpp>`, :doc:`bugprone-reserved-identifier <bugprone/reserved-identifier>`, "Yes"
443443
:doc:`cert-dcl54-cpp <cert/dcl54-cpp>`, :doc:`misc-new-delete-overloads <misc/new-delete-overloads>`,
444+
:doc:`cert-dcl58-cpp <cert/dcl58-cpp>`, :doc:`bugprone-std-namespace-modification <bugprone/std-namespace-modification>`,
444445
:doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`,
445446
:doc:`cert-env33-c <cert/env33-c>`, :doc:`bugprone-command-processor <bugprone/command-processor>`,
446447
:doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,

0 commit comments

Comments
 (0)