2016-11-02 22:16:36 +08:00
|
|
|
//===--- LimitedRandomnessCheck.cpp - clang-tidy---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-11-02 22:16:36 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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) {
|
2021-01-29 07:49:53 +08:00
|
|
|
std::string Msg = "";
|
2016-11-02 22:16:36 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2021-01-29 07:49:53 +08:00
|
|
|
Msg = "; use C++11 random library instead";
|
2016-11-02 22:16:36 +08:00
|
|
|
|
|
|
|
const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("randomGenerator");
|
2021-01-29 07:49:53 +08:00
|
|
|
diag(MatchedDecl->getBeginLoc(), "rand() has limited randomness" + Msg);
|
2016-11-02 22:16:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cert
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|