11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "llvm/ADT/STLExtras.h" 21 ProperlySeededRandomGeneratorCheck::ProperlySeededRandomGeneratorCheck(
24 RawDisallowedSeedTypes(
25 Options.get(
"DisallowedSeedTypes",
"time_t,std::time_t")) {
26 StringRef(RawDisallowedSeedTypes).split(DisallowedSeedTypes,
',');
31 Options.
store(Opts,
"DisallowedSeedTypes", RawDisallowedSeedTypes);
35 auto RandomGeneratorEngineDecl = cxxRecordDecl(hasAnyName(
36 "::std::linear_congruential_engine",
"::std::mersenne_twister_engine",
37 "::std::subtract_with_carry_engine",
"::std::discard_block_engine",
38 "::std::independent_bits_engine",
"::std::shuffle_order_engine"));
39 auto RandomGeneratorEngineTypeMatcher = hasType(hasUnqualifiedDesugaredType(
40 recordType(hasDeclaration(RandomGeneratorEngineDecl))));
52 has(memberExpr(has(declRefExpr(RandomGeneratorEngineTypeMatcher)),
53 member(hasName(
"seed")),
54 unless(hasDescendant(cxxThisExpr())))))
66 cxxConstructExpr(RandomGeneratorEngineTypeMatcher).bind(
"ctor"),
this);
74 callExpr(callee(functionDecl(hasAnyName(
"::srand",
"::std::srand"))))
80 const MatchFinder::MatchResult &Result) {
81 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructExpr>(
"ctor");
83 checkSeed(Result, Ctor);
85 const auto *Func = Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"seed");
87 checkSeed(Result, Func);
89 const auto *Srand = Result.Nodes.getNodeAs<CallExpr>(
"srand");
91 checkSeed(Result, Srand);
95 void ProperlySeededRandomGeneratorCheck::checkSeed(
96 const MatchFinder::MatchResult &Result,
const T *Func) {
97 if (Func->getNumArgs() == 0 || Func->getArg(0)->isDefaultArgument()) {
98 diag(Func->getExprLoc(),
99 "random number generator seeded with a default argument will generate " 100 "a predictable sequence of values");
105 if (Func->getArg(0)->EvaluateAsInt(Value, *Result.Context)) {
106 diag(Func->getExprLoc(),
107 "random number generator seeded with a constant value will generate a " 108 "predictable sequence of values");
112 const std::string SeedType(
113 Func->getArg(0)->IgnoreCasts()->getType().getAsString());
114 if (llvm::find(DisallowedSeedTypes, SeedType) != DisallowedSeedTypes.end()) {
115 diag(Func->getExprLoc(),
116 "random number generator seeded with a disallowed source of seed " 117 "value will generate a predictable sequence of values");
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.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.