2016-11-02 22:16:36 +08:00
|
|
|
//===--- LimitedRandomnessCheck.cpp - clang-tidy---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LimitedRandomnessCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace cert {
|
|
|
|
|
|
|
|
void LimitedRandomnessCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
Finder->addMatcher(callExpr(callee(functionDecl(namedDecl(hasName("::rand")),
|
|
|
|
parameterCountIs(0))))
|
|
|
|
.bind("randomGenerator"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LimitedRandomnessCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
std::string msg = "";
|
|
|
|
if (getLangOpts().CPlusPlus)
|
|
|
|
msg = "; use C++11 random library instead";
|
|
|
|
|
|
|
|
const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("randomGenerator");
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(MatchedDecl->getBeginLoc(), "rand() has limited randomness" + msg);
|
2016-11-02 22:16:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cert
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|