clang-tools  4.0.0
LimitedRandomnessCheck.cpp
Go to the documentation of this file.
1 //===--- LimitedRandomnessCheck.cpp - clang-tidy---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "LimitedRandomnessCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace cert {
19 
20 void LimitedRandomnessCheck::registerMatchers(MatchFinder *Finder) {
21  Finder->addMatcher(callExpr(callee(functionDecl(namedDecl(hasName("::rand")),
22  parameterCountIs(0))))
23  .bind("randomGenerator"),
24  this);
25 }
26 
27 void LimitedRandomnessCheck::check(const MatchFinder::MatchResult &Result) {
28  std::string msg = "";
29  if (getLangOpts().CPlusPlus)
30  msg = "; use C++11 random library instead";
31 
32  const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("randomGenerator");
33  diag(MatchedDecl->getLocStart(), "rand() has limited randomness" + msg);
34 }
35 
36 } // namespace cert
37 } // namespace tidy
38 } // namespace clang
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
const NamedDecl * Result
Definition: USRFinder.cpp:162