https://github.com/zeyi2 created https://github.com/llvm/llvm-project/pull/165659
Closes [#157290](https://github.com/llvm/llvm-project/issues/157290) >From 0f42a4416fbc86556d45df83fbbe269253b6eb8b Mon Sep 17 00:00:00 2001 From: mtx <[email protected]> Date: Thu, 30 Oct 2025 13:12:26 +0800 Subject: [PATCH] [clang-tidy] Rename `cert-dcl58-cpp` to `bugprone-dont-modify-std-namespace` --- .../bugprone/BugproneTidyModule.cpp | 3 + .../clang-tidy/bugprone/CMakeLists.txt | 1 + .../DontModifyStdNamespaceCheck.cpp | 6 +- .../DontModifyStdNamespaceCheck.h | 12 ++-- .../clang-tidy/cert/CERTTidyModule.cpp | 5 +- .../clang-tidy/cert/CMakeLists.txt | 1 - clang-tools-extra/docs/ReleaseNotes.rst | 7 ++- .../bugprone/dont-modify-std-namespace.rst | 63 +++++++++++++++++++ .../docs/clang-tidy/checks/cert/dcl58-cpp.rst | 54 +--------------- .../docs/clang-tidy/checks/list.rst | 3 +- .../Inputs/Headers/system-header-simulation.h | 2 +- .../dont-modify-std-namespace.cpp} | 4 +- 12 files changed, 93 insertions(+), 68 deletions(-) rename clang-tools-extra/clang-tidy/{cert => bugprone}/DontModifyStdNamespaceCheck.cpp (97%) rename clang-tools-extra/clang-tidy/{cert => bugprone}/DontModifyStdNamespaceCheck.h (71%) create mode 100644 clang-tools-extra/docs/clang-tidy/checks/bugprone/dont-modify-std-namespace.rst rename clang-tools-extra/test/clang-tidy/checkers/{cert/dcl58-cpp.cpp => bugprone/dont-modify-std-namespace.cpp} (97%) diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index e6115f67656bc..6f30e4e232c81 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -25,6 +25,7 @@ #include "CrtpConstructorAccessibilityCheck.h" #include "DanglingHandleCheck.h" #include "DerivedMethodShadowingBaseMethodCheck.h" +#include "DontModifyStdNamespaceCheck.h" #include "DynamicStaticInitializersCheck.h" #include "EasilySwappableParametersCheck.h" #include "EmptyCatchCheck.h" @@ -141,6 +142,8 @@ class BugproneModule : public ClangTidyModule { "bugprone-dangling-handle"); CheckFactories.registerCheck<DerivedMethodShadowingBaseMethodCheck>( "bugprone-derived-method-shadowing-base-method"); + CheckFactories.registerCheck<DontModifyStdNamespaceCheck>( + "bugprone-dont-modify-std-namespace"); CheckFactories.registerCheck<DynamicStaticInitializersCheck>( "bugprone-dynamic-static-initializers"); CheckFactories.registerCheck<EasilySwappableParametersCheck>( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index c8943e5b22ef8..358a73f36c9e6 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -21,6 +21,7 @@ add_clang_library(clangTidyBugproneModule STATIC CrtpConstructorAccessibilityCheck.cpp DanglingHandleCheck.cpp DerivedMethodShadowingBaseMethodCheck.cpp + DontModifyStdNamespaceCheck.cpp DynamicStaticInitializersCheck.cpp EasilySwappableParametersCheck.cpp EmptyCatchCheck.cpp diff --git a/clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.cpp similarity index 97% rename from clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.cpp index 79fbc66b5f8a3..869120a9a0864 100644 --- a/clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.cpp @@ -36,7 +36,7 @@ AST_POLYMORPHIC_MATCHER_P( } // namespace -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) { auto HasStdParent = @@ -96,7 +96,7 @@ void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) { .bind("decl"), this); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::bugprone static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) { const NamespaceDecl *LastNS = nullptr; @@ -108,7 +108,7 @@ static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) { return LastNS; } -void clang::tidy::cert::DontModifyStdNamespaceCheck::check( +void clang::tidy::bugprone::DontModifyStdNamespaceCheck::check( const MatchFinder::MatchResult &Result) { const auto *D = Result.Nodes.getNodeAs<Decl>("decl"); const auto *NS = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc"); diff --git a/clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.h b/clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.h similarity index 71% rename from clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.h rename to clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.h index cfcd878644ddb..eb8125f4092d2 100644 --- a/clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/DontModifyStdNamespaceCheck.h @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DONT_MODIFY_STD_NAMESPACE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DONT_MODIFY_STD_NAMESPACE_H #include "../ClangTidyCheck.h" -namespace clang::tidy::cert { +namespace clang::tidy::bugprone { /// Modification of the std or posix namespace can result in undefined behavior. /// This check warns for such modifications. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/cert/dcl58-cpp.html +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/dont-modify-std-namespace.html class DontModifyStdNamespaceCheck : public ClangTidyCheck { public: DontModifyStdNamespaceCheck(StringRef Name, ClangTidyContext *Context) @@ -29,6 +29,6 @@ class DontModifyStdNamespaceCheck : 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_DONT_MODIFY_STD_NAMESPACE_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_DONT_MODIFY_STD_NAMESPACE_H diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index c1ca2cec7a1eb..cdd3a8b54f99b 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/DontModifyStdNamespaceCheck.h" #include "../bugprone/PointerArithmeticOnPolymorphicObjectCheck.h" #include "../bugprone/ReservedIdentifierCheck.h" #include "../bugprone/SignalHandlerCheck.h" @@ -35,7 +36,6 @@ #include "../readability/EnumInitialValueCheck.h" #include "../readability/UppercaseLiteralSuffixCheck.h" #include "DefaultOperatorNewAlignmentCheck.h" -#include "DontModifyStdNamespaceCheck.h" #include "FloatLoopCounter.h" #include "LimitedRandomnessCheck.h" #include "MutatingCopyCheck.h" @@ -251,7 +251,8 @@ class CERTModule : public ClangTidyModule { "cert-dcl51-cpp"); CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>( "cert-dcl54-cpp"); - CheckFactories.registerCheck<DontModifyStdNamespaceCheck>("cert-dcl58-cpp"); + CheckFactories.registerCheck<bugprone::DontModifyStdNamespaceCheck>( + "cert-dcl58-cpp"); CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>( "cert-dcl59-cpp"); // ERR diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 453d1d30921e9..33965a3d236c8 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyCERTModule STATIC CERTTidyModule.cpp DefaultOperatorNewAlignmentCheck.cpp - DontModifyStdNamespaceCheck.cpp FloatLoopCounter.cpp LimitedRandomnessCheck.cpp MutatingCopyCheck.cpp diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 6701bf25df166..d1702bb9286de 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -244,6 +244,11 @@ New check aliases <clang-tidy/checks/modernize/avoid-variadic-functions>` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-dcl58-cpp <clang-tidy/checks/cert/dcl58-cpp>` to + :doc:`bugprone-dont-modify-std-namespace + <clang-tidy/checks/bugprone/dont-modify-std-namespace>` + keeping initial check as an alias to the new one. + - Renamed :doc:`cert-env33-c <clang-tidy/checks/cert/env33-c>` to :doc:`bugprone-command-processor <clang-tidy/checks/bugprone/command-processor>` @@ -367,7 +372,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/dont-modify-std-namespace.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/dont-modify-std-namespace.rst new file mode 100644 index 0000000000000..9293fa0a42763 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/dont-modify-std-namespace.rst @@ -0,0 +1,63 @@ +.. title:: clang-tidy - bugprone-dont-modify-std-namespace + +bugprone-dont-modify-std-namespace +================================== + +Modification of the ``std`` or ``posix`` namespace can result in undefined +behavior. +This check warns for such modifications. +The ``std`` (or ``posix``) namespace is allowed to be extended with (class or +function) template specializations that depend on an user-defined type (a type +that is not defined in the standard system headers). + +The check detects the following (user provided) declarations in namespace ``std`` or ``posix``: + +- Anything that is not a template specialization. +- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument. +- Explicit specializations of any member function of a standard library class template. +- Explicit specializations of any member function template of a standard library class or class template. +- Explicit or partial specialization of any member class template of a standard library class or class template. + +Examples: + +.. code-block:: c++ + + namespace std { + int x; // warning: modification of 'std' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace] + } + + namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior + } + + template <> + struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior + unsigned long operator()(const long &K) const { + return K; + } + }; + + struct MyData { long data; }; + + template <> + struct ::std::hash<MyData> { // no warning: specialization with user-defined type + unsigned long operator()(const MyData &K) const { + return K.data; + } + }; + + namespace std { + template <> + void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior + + template <> + bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior + return true; + } + } + +References +---------- + +This check corresponds to the CERT C++ Coding Standard rule +`DCL58-CPP. Do not modify the standard namespaces +<https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst index fbcc6281a8898..ed2e4ec383c2e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/dcl58-cpp.rst @@ -3,57 +3,9 @@ cert-dcl58-cpp ============== -Modification of the ``std`` or ``posix`` namespace can result in undefined -behavior. -This check warns for such modifications. -The ``std`` (or ``posix``) namespace is allowed to be extended with (class or -function) template specializations that depend on an user-defined type (a type -that is not defined in the standard system headers). - -The check detects the following (user provided) declarations in namespace ``std`` or ``posix``: - -- Anything that is not a template specialization. -- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument. -- Explicit specializations of any member function of a standard library class template. -- Explicit specializations of any member function template of a standard library class or class template. -- Explicit or partial specialization of any member class template of a standard library class or class template. - -Examples: - -.. code-block:: c++ - - namespace std { - int x; // warning: modification of 'std' namespace can result in undefined behavior [cert-dcl58-cpp] - } - - namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior - } - - template <> - struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior - unsigned long operator()(const long &K) const { - return K; - } - }; - - struct MyData { long data; }; - - template <> - struct ::std::hash<MyData> { // no warning: specialization with user-defined type - unsigned long operator()(const MyData &K) const { - return K.data; - } - }; - - namespace std { - template <> - void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior - - template <> - bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior - return true; - } - } +The `cert-dcl58-cpp` is an aliaes, please see +`bugprone-dont-modify-std-namespace <../bugprone/dont-modify-std-namespace.html>`_ +for more information. This check corresponds to the CERT C++ Coding Standard rule `DCL58-CPP. Do not modify the standard namespaces diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index d3c89e469188d..27d444351c70f 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-dont-modify-std-namespace <bugprone/dont-modify-std-namespace>`, :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>`, @@ -173,7 +174,6 @@ Clang-Tidy Checks :doc:`bugprone-unused-return-value <bugprone/unused-return-value>`, :doc:`bugprone-use-after-move <bugprone/use-after-move>`, :doc:`bugprone-virtual-near-miss <bugprone/virtual-near-miss>`, "Yes" - :doc:`cert-dcl58-cpp <cert/dcl58-cpp>`, :doc:`cert-err33-c <cert/err33-c>`, :doc:`cert-err60-cpp <cert/err60-cpp>`, :doc:`cert-flp30-c <cert/flp30-c>`, @@ -441,6 +441,7 @@ Check aliases :doc:`cert-dcl50-cpp <cert/dcl50-cpp>`, :doc:`modernize-avoid-variadic-functions <modernize/avoid-variadic-functions>`, :doc:`cert-dcl51-cpp <cert/dcl51-cpp>`, :doc:`bugprone-reserved-identifier <bugprone/reserved-identifier>`, "Yes" :doc:`cert-dcl54-cpp <cert/dcl54-cpp>`, :doc:`misc-new-delete-overloads <misc/new-delete-overloads>`, + :docs:`cert-dcl58-cpp <cert/dcl58-cpp>`, :doc:`bugprone-dont-modify-std-namespace <bugprone/dont-modify-std-namespace>`, :doc:`cert-dcl59-cpp <cert/dcl59-cpp>`, :doc:`google-build-namespaces <google/build-namespaces>`, :doc:`cert-err09-cpp <cert/err09-cpp>`, :doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`, :doc:`cert-env33-c <cert/env33-c>`, :doc:`bugprone-command-processor <bugprone/command-processor>`, diff --git a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/system-header-simulation.h b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/system-header-simulation.h index b6977cd9ce6c6..2c4293dafb3e2 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/system-header-simulation.h +++ b/clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/system-header-simulation.h @@ -59,7 +59,7 @@ struct X {}; } // namespace std // Template specializations that are in a system-header file. -// The purpose is to test cert-dcl58-cpp (no warnings here). +// The purpose is to test bugprone-dont-modify-std-namespace (no warnings here). namespace std { template <> void swap<short>(short &, short &){}; diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/dcl58-cpp.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dont-modify-std-namespace.cpp similarity index 97% rename from clang-tools-extra/test/clang-tidy/checkers/cert/dcl58-cpp.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/dont-modify-std-namespace.cpp index 01964e7dc6c76..a53ba37d5ac95 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/dcl58-cpp.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/dont-modify-std-namespace.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy -std=c++17-or-later %s cert-dcl58-cpp %t -- -- -I %clang_tidy_headers +// RUN: %check_clang_tidy -std=c++17-or-later %s bugprone-dont-modify-std-namespace %t -- -- -I %clang_tidy_headers #include "system-header-simulation.h" @@ -15,7 +15,7 @@ namespace A { } namespace posix { -// CHECK-MESSAGES: :[[@LINE+2]]:11: warning: modification of 'posix' namespace can result in undefined behavior [cert-dcl58-cpp] +// CHECK-MESSAGES: :[[@LINE+2]]:11: warning: modification of 'posix' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace] // CHECK-MESSAGES: :[[@LINE-2]]:11: note: 'posix' namespace opened here namespace foo { int foobar; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
