Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
#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"
#include "../modernize/AvoidVariadicFunctionsCheck.h"
#include "../performance/MoveConstructorInitCheck.h"
#include "../readability/EnumInitialValueCheck.h"
#include "../readability/UppercaseLiteralSuffixCheck.h"
#include "LimitedRandomnessCheck.h"

namespace {

Expand Down Expand Up @@ -271,7 +271,7 @@ class CERTModule : public ClangTidyModule {
.registerCheck<bugprone::DefaultOperatorNewOnOveralignedTypeCheck>(
"cert-mem57-cpp");
// MSC
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
CheckFactories.registerCheck<misc::PredictableRandCheck>("cert-msc50-cpp");
CheckFactories.registerCheck<bugprone::RandomGeneratorSeedCheck>(
"cert-msc51-cpp");
CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(
Expand Down Expand Up @@ -324,7 +324,7 @@ class CERTModule : public ClangTidyModule {
// MSC
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
"cert-msc24-c");
CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
CheckFactories.registerCheck<misc::PredictableRandCheck>("cert-msc30-c");
CheckFactories.registerCheck<bugprone::RandomGeneratorSeedCheck>(
"cert-msc32-c");
CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
Expand Down
1 change: 0 additions & 1 deletion clang-tools-extra/clang-tidy/cert/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS

add_clang_library(clangTidyCERTModule STATIC
CERTTidyModule.cpp
LimitedRandomnessCheck.cpp

LINK_LIBS
clangTidy
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clang-tidy/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_clang_library(clangTidyMiscModule STATIC
NonCopyableObjectsCheck.cpp
NonPrivateMemberVariablesInClassesCheck.cpp
OverrideWithDifferentVisibilityCheck.cpp
PredictableRandCheck.cpp
RedundantExpressionCheck.cpp
StaticAssertCheck.cpp
ThrowByValueCatchByReferenceCheck.cpp
Expand Down
6 changes: 4 additions & 2 deletions clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,6 +64,9 @@ class MiscModule : public ClangTidyModule {
"misc-non-copyable-objects");
CheckFactories.registerCheck<NonPrivateMemberVariablesInClassesCheck>(
"misc-non-private-member-variables-in-classes");
CheckFactories.registerCheck<OverrideWithDifferentVisibilityCheck>(
"misc-override-with-different-visibility");
CheckFactories.registerCheck<PredictableRandCheck>("misc-predictable-rand");
CheckFactories.registerCheck<RedundantExpressionCheck>(
"misc-redundant-expression");
CheckFactories.registerCheck<StaticAssertCheck>("misc-static-assert");
Expand All @@ -82,8 +86,6 @@ class MiscModule : public ClangTidyModule {
"misc-use-anonymous-namespace");
CheckFactories.registerCheck<UseInternalLinkageCheck>(
"misc-use-internal-linkage");
CheckFactories.registerCheck<OverrideWithDifferentVisibilityCheck>(
"misc-override-with-different-visibility");
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
//
//===----------------------------------------------------------------------===//

#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
/// sequence produced.
/// 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
12 changes: 11 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,21 @@ New check aliases
<clang-tidy/checks/bugprone/default-operator-new-on-overaligned-type>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-msc30-c <clang-tidy/checks/cert/msc30-c>` to
:doc:`misc-predictable-rand
<clang-tidy/checks/misc/predictable-rand>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-msc32-c <clang-tidy/checks/cert/msc32-c>` to
:doc:`bugprone-random-generator-seed
<clang-tidy/checks/bugprone/random-generator-seed>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-msc50-cpp <clang-tidy/checks/cert/msc50-cpp>` to
:doc:`misc-predictable-rand
<clang-tidy/checks/misc/predictable-rand>`
keeping initial check as an alias to the new one.

- Renamed :doc:`cert-msc51-cpp <clang-tidy/checks/cert/msc51-cpp>` to
:doc:`bugprone-random-generator-seed
<clang-tidy/checks/bugprone/random-generator-seed>`
Expand Down Expand Up @@ -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`.

Expand Down
8 changes: 6 additions & 2 deletions clang-tools-extra/docs/clang-tidy/checks/cert/msc30-c.rst
Original file line number Diff line number Diff line change
@@ -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
<https://wiki.sei.cmu.edu/confluence/display/c/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers>`_.
14 changes: 8 additions & 6 deletions clang-tools-extra/docs/clang-tidy/checks/cert/msc50-cpp.rst
Original file line number Diff line number Diff line change
@@ -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
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC50-CPP.+Do+not+use+std%3A%3Arand%28%29+for+generating+pseudorandom+numbers>`_.
4 changes: 3 additions & 1 deletion clang-tools-extra/docs/clang-tidy/checks/list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Clang-Tidy Checks
:doc:`misc-non-copyable-objects <misc/non-copyable-objects>`,
:doc:`misc-non-private-member-variables-in-classes <misc/non-private-member-variables-in-classes>`,
:doc:`misc-override-with-different-visibility <misc/override-with-different-visibility>`,
:doc:`misc-predictable-rand <misc/predictable-rand>`,
:doc:`misc-redundant-expression <misc/redundant-expression>`, "Yes"
:doc:`misc-static-assert <misc/static-assert>`, "Yes"
:doc:`misc-throw-by-value-catch-by-reference <misc/throw-by-value-catch-by-reference>`,
Expand Down Expand Up @@ -460,7 +461,8 @@ Check aliases
:doc:`cert-int09-c <cert/int09-c>`, :doc:`readability-enum-initial-value <readability/enum-initial-value>`, "Yes"
:doc:`cert-mem57-cpp <cert/mem57-cpp>`, :doc:`bugprone-default-operator-new-on-overaligned-type <bugprone/default-operator-new-on-overaligned-type>`,
:doc:`cert-msc24-c <cert/msc24-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`,
:doc:`cert-msc30-c <cert/msc30-c>`, :doc:`cert-msc50-cpp <cert/msc50-cpp>`,
:doc:`cert-msc30-c <cert/msc30-c>`, :doc:`misc-predictable-rand <misc/predictable-rand>`,
:doc:`cert-msc50-cpp <cert/msc50-cpp>`, :doc:`misc-predictable-rand <misc/predictable-rand>`,
:doc:`cert-msc32-c <cert/msc32-c>`, :doc:`bugprone-random-generator-seed <bugprone/random-generator-seed>`,
:doc:`cert-msc33-c <cert/msc33-c>`, :doc:`bugprone-unsafe-functions <bugprone/unsafe-functions>`,
:doc:`cert-msc51-cpp <cert/msc51-cpp>`, :doc:`bugprone-random-generator-seed <bugprone/random-generator-seed>`,
Expand Down
20 changes: 20 additions & 0 deletions clang-tools-extra/docs/clang-tidy/checks/misc/predictable-rand.rst
Original file line number Diff line number Diff line change
@@ -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
<https://wiki.sei.cmu.edu/confluence/display/c/MSC30-C.+Do+not+use+the+rand%28%29+function+for+generating+pseudorandom+numbers>`_.
`MSC50-CPP. Do not use std::rand() for generating pseudorandom numbers
<https://wiki.sei.cmu.edu/confluence/display/cplusplus/MSC50-CPP.+Do+not+use+std%3A%3Arand%28%29+for+generating+pseudorandom+numbers>`_.
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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]
}

Loading