diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp index b39aea62a9546..e06b6c530dc86 100644 --- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp @@ -71,6 +71,7 @@ #include "ReturnConstRefFromParameterCheck.h" #include "SharedPtrArrayMismatchCheck.h" #include "SignalHandlerCheck.h" +#include "SignedBitwiseCheck.h" #include "SignedCharMisuseCheck.h" #include "SizeofContainerCheck.h" #include "SizeofExpressionCheck.h" @@ -244,6 +245,7 @@ class BugproneModule : public ClangTidyModule { CheckFactories.registerCheck( "bugprone-shared-ptr-array-mismatch"); CheckFactories.registerCheck("bugprone-signal-handler"); + CheckFactories.registerCheck("bugprone-signed-bitwise"); CheckFactories.registerCheck( "bugprone-signed-char-misuse"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt index f7f185d53b269..248641828b1f6 100644 --- a/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/bugprone/CMakeLists.txt @@ -74,6 +74,7 @@ add_clang_library(clangTidyBugproneModule STATIC ReturnConstRefFromParameterCheck.cpp SharedPtrArrayMismatchCheck.cpp SignalHandlerCheck.cpp + SignedBitwiseCheck.cpp SignedCharMisuseCheck.cpp SizeofContainerCheck.cpp SizeofExpressionCheck.cpp diff --git a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.cpp similarity index 98% rename from clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp rename to clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.cpp index 19c716e941271..5a3a82af24c98 100644 --- a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.cpp @@ -14,7 +14,7 @@ using namespace clang::ast_matchers; using namespace clang::ast_matchers::internal; -namespace clang::tidy::hicpp { +namespace clang::tidy::bugprone { SignedBitwiseCheck::SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context) @@ -99,4 +99,4 @@ void SignedBitwiseCheck::check(const MatchFinder::MatchResult &Result) { << IsUnary << SignedOperand->getSourceRange() << OperatorLoc; } -} // namespace clang::tidy::hicpp +} // namespace clang::tidy::bugprone diff --git a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.h b/clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.h similarity index 72% rename from clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.h rename to clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.h index ef92a4d13f43e..d5293248f5595 100644 --- a/clang-tools-extra/clang-tidy/hicpp/SignedBitwiseCheck.h +++ b/clang-tools-extra/clang-tidy/bugprone/SignedBitwiseCheck.h @@ -6,18 +6,18 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNEDBITWISECHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNEDBITWISECHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNEDBITWISECHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNEDBITWISECHECK_H #include "../ClangTidyCheck.h" -namespace clang::tidy::hicpp { +namespace clang::tidy::bugprone { /// This check implements the rule 5.6.1 of the HICPP Standard, which disallows /// bitwise operations on signed integer types. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/hicpp/signed-bitwise.html +/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/signed-bitwise.html class SignedBitwiseCheck : public ClangTidyCheck { public: SignedBitwiseCheck(StringRef Name, ClangTidyContext *Context); @@ -29,6 +29,6 @@ class SignedBitwiseCheck : public ClangTidyCheck { bool IgnorePositiveIntegerLiterals; }; -} // namespace clang::tidy::hicpp +} // namespace clang::tidy::bugprone -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_HICPP_SIGNEDBITWISECHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_SIGNEDBITWISECHECK_H diff --git a/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt b/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt index b5bcbf389b4ae..b9b7e716d00a4 100644 --- a/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/hicpp/CMakeLists.txt @@ -6,7 +6,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyHICPPModule STATIC HICPPTidyModule.cpp MultiwayPathsCoveredCheck.cpp - SignedBitwiseCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp index 38d9235878e62..501e7fc0e2d9b 100644 --- a/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/hicpp/HICPPTidyModule.cpp @@ -8,6 +8,7 @@ #include "../ClangTidy.h" #include "../ClangTidyModule.h" +#include "../bugprone/SignedBitwiseCheck.h" #include "../bugprone/StdExceptionBaseclassCheck.h" #include "../bugprone/UndelegatedConstructorCheck.h" #include "../bugprone/UnusedReturnValueCheck.h" @@ -38,7 +39,6 @@ #include "../readability/NamedParameterCheck.h" #include "../readability/UppercaseLiteralSuffixCheck.h" #include "MultiwayPathsCoveredCheck.h" -#include "SignedBitwiseCheck.h" namespace clang::tidy { namespace hicpp { @@ -61,7 +61,8 @@ class HICPPModule : public ClangTidyModule { "hicpp-ignored-remove-result"); CheckFactories.registerCheck( "hicpp-multiway-paths-covered"); - CheckFactories.registerCheck("hicpp-signed-bitwise"); + CheckFactories.registerCheck( + "hicpp-signed-bitwise"); CheckFactories.registerCheck( "hicpp-explicit-conversions"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 36e311341f336..eee5741ef62d4 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -195,6 +195,11 @@ New check aliases `. The `hicpp-no-assembler` name is kept as an alias. +- Renamed :doc:`hicpp-signed-bitwise ` + to :doc:`bugprone-signed-bitwise + `. The `hicpp-signed-bitwise` + name is kept as an alias. + - Renamed :doc:`performance-faster-string-find ` to :doc:`performance-prefer-single-char-overloads diff --git a/clang-tools-extra/docs/clang-tidy/checks/bugprone/signed-bitwise.rst b/clang-tools-extra/docs/clang-tidy/checks/bugprone/signed-bitwise.rst new file mode 100644 index 0000000000000..0646a977867f8 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/signed-bitwise.rst @@ -0,0 +1,28 @@ +.. title:: clang-tidy - bugprone-signed-bitwise + +bugprone-signed-bitwise +======================= + +Finds uses of bitwise operations on signed integer types, which may lead to +undefined or implementation defined behavior. + +Performing bitwise operations on signed integers can be confusing and may +lead to undefined or implementation dependent behavior. In particular, right +shift a signed integer is implementation dependent, while left shift a signed +integer may result in undefined behavior. + +.. code-block:: c++ + + int main(){ + int x = -4; + int y = x >> 1; // y can be -2 or 2147483646 + } + +Options +------- + +.. option:: IgnorePositiveIntegerLiterals + + If this option is set to `true`, the check will not warn on bitwise + operations with positive integer literals, e.g. ``~0``, ``2 << 1``, etc. + Default value is `false`. diff --git a/clang-tools-extra/docs/clang-tidy/checks/hicpp/signed-bitwise.rst b/clang-tools-extra/docs/clang-tidy/checks/hicpp/signed-bitwise.rst index 0461f0cd61911..e23c936f08ca5 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/hicpp/signed-bitwise.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/hicpp/signed-bitwise.rst @@ -1,17 +1,11 @@ .. title:: clang-tidy - hicpp-signed-bitwise +.. meta:: + :http-equiv=refresh: 5;URL=../bugprone/signed-bitwise.html hicpp-signed-bitwise ==================== -Finds uses of bitwise operations on signed integer types, which may lead to -undefined or implementation defined behavior. +The `hicpp-signed-bitwise` check is an alias, please see +`bugprone-signed-bitwise <../bugprone/signed-bitwise.html>`_ for more +information. -The according rule is defined in the `High Integrity C++ Standard, Section 5.6.1 `_. - -Options -------- - -.. option:: IgnorePositiveIntegerLiterals - - If this option is set to `true`, the check will not warn on bitwise operations with positive integer literals, e.g. `~0`, `2 << 1`, etc. - Default value is `false`. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index 2b5be931271ec..f0838cbfb9b1e 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -141,6 +141,7 @@ Clang-Tidy Checks :doc:`bugprone-return-const-ref-from-parameter `, :doc:`bugprone-shared-ptr-array-mismatch `, "Yes" :doc:`bugprone-signal-handler `, + :doc:`bugprone-signed-bitwise `, :doc:`bugprone-signed-char-misuse `, :doc:`bugprone-sizeof-container `, :doc:`bugprone-sizeof-expression `, @@ -243,7 +244,6 @@ Clang-Tidy Checks :doc:`google-upgrade-googletest-case `, "Yes" :doc:`hicpp-exception-baseclass `, :doc:`hicpp-multiway-paths-covered `, - :doc:`hicpp-signed-bitwise `, :doc:`linuxkernel-must-check-errs `, :doc:`llvm-header-guard `, :doc:`llvm-include-order `, "Yes" @@ -616,6 +616,7 @@ Check aliases :doc:`hicpp-no-assembler `, :doc:`portability-no-assembler `, :doc:`hicpp-no-malloc `, :doc:`cppcoreguidelines-no-malloc `, :doc:`hicpp-noexcept-move `, :doc:`performance-noexcept-move-constructor `, "Yes" + :doc:`hicpp-signed-bitwise `, :doc:`bugprone-signed-bitwise `, :doc:`hicpp-special-member-functions `, :doc:`cppcoreguidelines-special-member-functions `, :doc:`hicpp-static-assert `, :doc:`misc-static-assert `, "Yes" :doc:`hicpp-undelegated-constructor `, :doc:`bugprone-undelegated-constructor `, diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-bug34747.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-bug34747.cpp similarity index 91% rename from clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-bug34747.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-bug34747.cpp index 1502b809e6188..cbd8787409453 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-bug34747.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-bug34747.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s hicpp-signed-bitwise %t -- +// RUN: %check_clang_tidy %s bugprone-signed-bitwise %t -- // Note: this test expects no diagnostics, but FileCheck cannot handle that, // hence the use of | count 0. diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-integer-literals.cpp similarity index 79% rename from clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-integer-literals.cpp index 509988875c3d5..009d8bdd812f1 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-integer-literals.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-integer-literals.cpp @@ -1,5 +1,5 @@ -// RUN: %check_clang_tidy -std=c++11-or-later %s hicpp-signed-bitwise %t -- \ -// RUN: -config="{CheckOptions: {hicpp-signed-bitwise.IgnorePositiveIntegerLiterals: true}}" +// RUN: %check_clang_tidy -std=c++11-or-later %s bugprone-signed-bitwise %t -- \ +// RUN: -config="{CheckOptions: {bugprone-signed-bitwise.IgnorePositiveIntegerLiterals: true}}" void examples() { unsigned UValue = 40u; @@ -22,7 +22,7 @@ void examples() { // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator IResult = ~0; //Ok IResult = -1 & 1; - // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise] + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use of a signed integer operand with a binary bitwise operator [bugprone-signed-bitwise] } enum EnumConstruction { diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-standard-types.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-standard-types.cpp similarity index 99% rename from clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-standard-types.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-standard-types.cpp index a2fe5835e0149..875aad545e76d 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-standard-types.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-standard-types.cpp @@ -1,4 +1,4 @@ -// RUN: clang-tidy %s -checks='-*,hicpp-signed-bitwise' -- -std=c++11 +// RUN: clang-tidy %s -checks='-*,bugprone-signed-bitwise' -- -std=c++11 // FIXME: Make the test work in all language modes. #include "signed-bitwise-standard-types.h" diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-standard-types.h b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-standard-types.h similarity index 100% rename from clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise-standard-types.h rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise-standard-types.h diff --git a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise.cpp similarity index 99% rename from clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp rename to clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise.cpp index bea9366ea5a30..d267129e032ec 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/hicpp/signed-bitwise.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/signed-bitwise.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s hicpp-signed-bitwise %t -- -- --target=x86_64-linux +// RUN: %check_clang_tidy %s bugprone-signed-bitwise %t -- -- --target=x86_64-linux // These could cause false positives and should not be considered. struct StreamClass {