10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/ADT/STLExtras.h"
21 RawDisallowedSeedTypes(
22 Options.get(
"DisallowedSeedTypes",
"time_t,std::time_t")) {
23 StringRef(RawDisallowedSeedTypes).split(DisallowedSeedTypes,
',');
28 Options.
store(Opts,
"DisallowedSeedTypes", RawDisallowedSeedTypes);
32 auto RandomGeneratorEngineDecl = cxxRecordDecl(hasAnyName(
33 "::std::linear_congruential_engine",
"::std::mersenne_twister_engine",
34 "::std::subtract_with_carry_engine",
"::std::discard_block_engine",
35 "::std::independent_bits_engine",
"::std::shuffle_order_engine"));
36 auto RandomGeneratorEngineTypeMatcher = hasType(hasUnqualifiedDesugaredType(
37 recordType(hasDeclaration(RandomGeneratorEngineDecl))));
49 has(memberExpr(has(declRefExpr(RandomGeneratorEngineTypeMatcher)),
50 member(hasName(
"seed")),
51 unless(hasDescendant(cxxThisExpr())))))
64 cxxConstructExpr(RandomGeneratorEngineTypeMatcher).bind(
"ctor")),
73 callExpr(callee(functionDecl(hasAnyName(
"::srand",
"::std::srand"))))
79 const MatchFinder::MatchResult &Result) {
80 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructExpr>(
"ctor");
82 checkSeed(Result, Ctor);
84 const auto *Func = Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"seed");
86 checkSeed(Result, Func);
88 const auto *Srand = Result.Nodes.getNodeAs<CallExpr>(
"srand");
90 checkSeed(Result, Srand);
94void ProperlySeededRandomGeneratorCheck::checkSeed(
95 const MatchFinder::MatchResult &Result,
const T *Func) {
96 if (Func->getNumArgs() == 0 || Func->getArg(0)->isDefaultArgument()) {
97 diag(Func->getExprLoc(),
98 "random number generator seeded with a default argument will generate "
99 "a predictable sequence of values");
103 Expr::EvalResult EVResult;
104 if (Func->getArg(0)->EvaluateAsInt(EVResult, *Result.Context)) {
105 diag(Func->getExprLoc(),
106 "random number generator seeded with a constant value will generate a "
107 "predictable sequence of values");
111 const std::string SeedType(
112 Func->getArg(0)->IgnoreCasts()->getType().getAsString());
113 if (llvm::is_contained(DisallowedSeedTypes, SeedType)) {
114 diag(Func->getExprLoc(),
115 "random number generator seeded with a disallowed source of seed "
116 "value will generate a predictable sequence of values");
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ProperlySeededRandomGeneratorCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
llvm::StringMap< ClangTidyValue > OptionMap