diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp index f46dd4cc6195a..16d4be9802cc6 100644 --- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp @@ -34,6 +34,7 @@ #include "../google/UnnamedNamespaceInHeaderCheck.h" #include "../misc/NewDeleteOverloadsCheck.h" #include "../misc/NonCopyableObjectsCheck.h" +#include "../misc/PredictableRandCheck.h" #include "../misc/StaticAssertCheck.h" #include "../misc/ThrowByValueCatchByReferenceCheck.h" #include "../modernize/AvoidSetjmpLongjmpCheck.h" @@ -41,7 +42,6 @@ #include "../performance/MoveConstructorInitCheck.h" #include "../readability/EnumInitialValueCheck.h" #include "../readability/UppercaseLiteralSuffixCheck.h" -#include "LimitedRandomnessCheck.h" namespace { @@ -271,7 +271,7 @@ class CERTModule : public ClangTidyModule { .registerCheck( "cert-mem57-cpp"); // MSC - CheckFactories.registerCheck("cert-msc50-cpp"); + CheckFactories.registerCheck("cert-msc50-cpp"); CheckFactories.registerCheck( "cert-msc51-cpp"); CheckFactories.registerCheck( @@ -324,7 +324,7 @@ class CERTModule : public ClangTidyModule { // MSC CheckFactories.registerCheck( "cert-msc24-c"); - CheckFactories.registerCheck("cert-msc30-c"); + CheckFactories.registerCheck("cert-msc30-c"); CheckFactories.registerCheck( "cert-msc32-c"); CheckFactories.registerCheck( diff --git a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt index 0ed903c4826a3..313790374788d 100644 --- a/clang-tools-extra/clang-tidy/cert/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/cert/CMakeLists.txt @@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS add_clang_library(clangTidyCERTModule STATIC CERTTidyModule.cpp - LimitedRandomnessCheck.cpp LINK_LIBS clangTidy diff --git a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt index 6214ee92927f2..e8705aada3f22 100644 --- a/clang-tools-extra/clang-tidy/misc/CMakeLists.txt +++ b/clang-tools-extra/clang-tidy/misc/CMakeLists.txt @@ -33,6 +33,7 @@ add_clang_library(clangTidyMiscModule STATIC NonCopyableObjectsCheck.cpp NonPrivateMemberVariablesInClassesCheck.cpp OverrideWithDifferentVisibilityCheck.cpp + PredictableRandCheck.cpp RedundantExpressionCheck.cpp StaticAssertCheck.cpp ThrowByValueCatchByReferenceCheck.cpp diff --git a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp index 347fa2a82e43c..03f25775de0bf 100644 --- a/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp +++ b/clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp @@ -23,6 +23,7 @@ #include "NonCopyableObjectsCheck.h" #include "NonPrivateMemberVariablesInClassesCheck.h" #include "OverrideWithDifferentVisibilityCheck.h" +#include "PredictableRandCheck.h" #include "RedundantExpressionCheck.h" #include "StaticAssertCheck.h" #include "ThrowByValueCatchByReferenceCheck.h" @@ -63,6 +64,9 @@ class MiscModule : public ClangTidyModule { "misc-non-copyable-objects"); CheckFactories.registerCheck( "misc-non-private-member-variables-in-classes"); + CheckFactories.registerCheck( + "misc-override-with-different-visibility"); + CheckFactories.registerCheck("misc-predictable-rand"); CheckFactories.registerCheck( "misc-redundant-expression"); CheckFactories.registerCheck("misc-static-assert"); @@ -82,8 +86,6 @@ class MiscModule : public ClangTidyModule { "misc-use-anonymous-namespace"); CheckFactories.registerCheck( "misc-use-internal-linkage"); - CheckFactories.registerCheck( - "misc-override-with-different-visibility"); } }; diff --git a/clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.cpp b/clang-tools-extra/clang-tidy/misc/PredictableRandCheck.cpp similarity index 80% rename from clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.cpp rename to clang-tools-extra/clang-tidy/misc/PredictableRandCheck.cpp index 4fe9c6c22590b..eed80e07a030d 100644 --- a/clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/PredictableRandCheck.cpp @@ -6,22 +6,22 @@ // //===----------------------------------------------------------------------===// -#include "LimitedRandomnessCheck.h" +#include "PredictableRandCheck.h" #include "clang/AST/ASTContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" using namespace clang::ast_matchers; -namespace clang::tidy::cert { +namespace clang::tidy::misc { -void LimitedRandomnessCheck::registerMatchers(MatchFinder *Finder) { +void PredictableRandCheck::registerMatchers(MatchFinder *Finder) { Finder->addMatcher(callExpr(callee(functionDecl(namedDecl(hasName("::rand")), parameterCountIs(0)))) .bind("randomGenerator"), this); } -void LimitedRandomnessCheck::check(const MatchFinder::MatchResult &Result) { +void PredictableRandCheck::check(const MatchFinder::MatchResult &Result) { std::string Msg; if (getLangOpts().CPlusPlus) Msg = "; use C++11 random library instead"; @@ -30,4 +30,4 @@ void LimitedRandomnessCheck::check(const MatchFinder::MatchResult &Result) { diag(MatchedDecl->getBeginLoc(), "rand() has limited randomness" + Msg); } -} // namespace clang::tidy::cert +} // namespace clang::tidy::misc diff --git a/clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.h b/clang-tools-extra/clang-tidy/misc/PredictableRandCheck.h similarity index 65% rename from clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.h rename to clang-tools-extra/clang-tidy/misc/PredictableRandCheck.h index a806cd344d217..2237e7e752a5e 100644 --- a/clang-tools-extra/clang-tidy/cert/LimitedRandomnessCheck.h +++ b/clang-tools-extra/clang-tidy/misc/PredictableRandCheck.h @@ -6,12 +6,12 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_LIMITEDRANDOMNESSCHECK_H -#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_LIMITEDRANDOMNESSCHECK_H +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PREDICTABLERANDCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PREDICTABLERANDCHECK_H #include "../ClangTidyCheck.h" -namespace clang::tidy::cert { +namespace clang::tidy::misc { /// Pseudorandom number generators are not genuinely random. The result of the /// std::rand() function makes no guarantees as to the quality of the random @@ -19,15 +19,15 @@ namespace clang::tidy::cert { /// This check warns for the usage of std::rand() function. /// /// For the user-facing documentation see: -/// https://clang.llvm.org/extra/clang-tidy/checks/cert/msc50-cpp.html -class LimitedRandomnessCheck : public ClangTidyCheck { +/// https://clang.llvm.org/extra/clang-tidy/checks/misc/predictable-rand.html +class PredictableRandCheck : public ClangTidyCheck { public: - LimitedRandomnessCheck(StringRef Name, ClangTidyContext *Context) + PredictableRandCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context) {} void registerMatchers(ast_matchers::MatchFinder *Finder) override; void check(const ast_matchers::MatchFinder::MatchResult &Result) override; }; -} // namespace clang::tidy::cert +} // namespace clang::tidy::misc -#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_LIMITEDRANDOMNESSCHECK_H +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_PREDICTABLERANDCHECK_H diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 4096624923858..b982216297919 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -283,11 +283,21 @@ New check aliases ` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-msc30-c ` to + :doc:`misc-predictable-rand + ` + keeping initial check as an alias to the new one. + - Renamed :doc:`cert-msc32-c ` to :doc:`bugprone-random-generator-seed ` keeping initial check as an alias to the new one. +- Renamed :doc:`cert-msc50-cpp ` to + :doc:`misc-predictable-rand + ` + keeping initial check as an alias to the new one. + - Renamed :doc:`cert-msc51-cpp ` to :doc:`bugprone-random-generator-seed ` @@ -316,7 +326,7 @@ Changes in existing checks exceptions from captures are now diagnosed, exceptions in the bodies of lambdas that aren't actually invoked are not. Additionally, fixed an issue where the check wouldn't diagnose throws in arguments to functions or - constructors. Added fine-grained configuration via options + constructors. Added fine-grained configuration via options `CheckDestructors`, `CheckMoveMemberFunctions`, `CheckMain`, `CheckedSwapFunctions`, and `CheckNothrowFunctions`. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/msc30-c.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/msc30-c.rst index a2898f1a11acf..27f92a8f45d8b 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/msc30-c.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/msc30-c.rst @@ -1,9 +1,13 @@ .. title:: clang-tidy - cert-msc30-c .. meta:: - :http-equiv=refresh: 5;URL=../cert/msc50-cpp.html + :http-equiv=refresh: 5;URL=../misc/predictable-rand.html cert-msc30-c ============ The `cert-msc30-c` check is an alias, please see -:doc:`cert-msc50-cpp <../cert/msc50-cpp>` for more information. +:doc:`misc-predictable-rand <../misc/predictable-rand>` for more information. + +This check corresponds to the CERT C Coding Standard rule +`MSC30-C. Do not use the rand() function for generating pseudorandom numbers +`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/cert/msc50-cpp.rst b/clang-tools-extra/docs/clang-tidy/checks/cert/msc50-cpp.rst index 3f4b2a8cde680..59738f5bf29ab 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/cert/msc50-cpp.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/cert/msc50-cpp.rst @@ -1,11 +1,13 @@ .. title:: clang-tidy - cert-msc50-cpp +.. meta:: + :http-equiv=refresh: 5;URL=../misc/predictable-rand.html cert-msc50-cpp ============== -Pseudorandom number generators use mathematical algorithms to produce a -sequence of numbers with good statistical properties, but the numbers -produced are not genuinely random. The ``std::rand()`` function takes a -seed (number), runs a mathematical operation on it and returns the result. -By manipulating the seed the result can be predictable. This check warns -for the usage of ``std::rand()``. +The `cert-msc50-cpp` check is an alias, please see +:doc:`misc-predictable-rand <../misc/predictable-rand>` for more information. + +This check corresponds to the CERT C Coding Standard rule +`MSC50-CPP. Do not use std::rand() for generating pseudorandom numbers +`_. diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst index d9102f0327b88..66fe6010a73dc 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst @@ -278,6 +278,7 @@ Clang-Tidy Checks :doc:`misc-non-copyable-objects `, :doc:`misc-non-private-member-variables-in-classes `, :doc:`misc-override-with-different-visibility `, + :doc:`misc-predictable-rand `, :doc:`misc-redundant-expression `, "Yes" :doc:`misc-static-assert `, "Yes" :doc:`misc-throw-by-value-catch-by-reference `, @@ -460,7 +461,8 @@ Check aliases :doc:`cert-int09-c `, :doc:`readability-enum-initial-value `, "Yes" :doc:`cert-mem57-cpp `, :doc:`bugprone-default-operator-new-on-overaligned-type `, :doc:`cert-msc24-c `, :doc:`bugprone-unsafe-functions `, - :doc:`cert-msc30-c `, :doc:`cert-msc50-cpp `, + :doc:`cert-msc30-c `, :doc:`misc-predictable-rand `, + :doc:`cert-msc50-cpp `, :doc:`misc-predictable-rand `, :doc:`cert-msc32-c `, :doc:`bugprone-random-generator-seed `, :doc:`cert-msc33-c `, :doc:`bugprone-unsafe-functions `, :doc:`cert-msc51-cpp `, :doc:`bugprone-random-generator-seed `, diff --git a/clang-tools-extra/docs/clang-tidy/checks/misc/predictable-rand.rst b/clang-tools-extra/docs/clang-tidy/checks/misc/predictable-rand.rst new file mode 100644 index 0000000000000..00156649cf220 --- /dev/null +++ b/clang-tools-extra/docs/clang-tidy/checks/misc/predictable-rand.rst @@ -0,0 +1,20 @@ +.. title:: clang-tidy - misc-predictable-rand + +misc-predictable-rand +===================== + +Warns for the usage of ``std::rand()``. Pseudorandom number generators use +mathematical algorithms to produce a sequence of numbers with good +statistical properties, but the numbers produced are not genuinely random. +The ``std::rand()`` function takes a seed (number), runs a mathematical +operation on it and returns the result. By manipulating the seed the result +can be predictable. + +References +---------- + +This check corresponds to the CERT C Coding Standard rules +`MSC30-C. Do not use the rand() function for generating pseudorandom numbers +`_. +`MSC50-CPP. Do not use std::rand() for generating pseudorandom numbers +`_. diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.c b/clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.c similarity index 66% rename from clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.c rename to clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.c index 32e4a07b40919..d8233092b54ec 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.c +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.c @@ -1,11 +1,11 @@ -// RUN: %check_clang_tidy %s cert-msc30-c %t +// RUN: %check_clang_tidy %s misc-predictable-rand %t extern int rand(void); int nonrand(void); int cTest(void) { int i = rand(); - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness [cert-msc30-c] + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness [misc-predictable-rand] int k = nonrand(); diff --git a/clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.cpp similarity index 61% rename from clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.cpp rename to clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.cpp index 845b7350de9f5..8f001868703d3 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/cert/limited-randomness.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/predictable-rand.cpp @@ -1,4 +1,4 @@ -// RUN: %check_clang_tidy %s cert-msc50-cpp %t +// RUN: %check_clang_tidy %s misc-predictable-rand %t int rand(); int rand(int); @@ -13,16 +13,16 @@ namespace nonstd { void testFunction1() { int i = std::rand(); - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [cert-msc50-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [misc-predictable-rand] int j = ::rand(); - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [cert-msc50-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [misc-predictable-rand] int k = rand(i); int l = nonstd::rand(); int m = rand(); - // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [cert-msc50-cpp] + // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: rand() has limited randomness; use C++11 random library instead [misc-predictable-rand] }