LimitedRandomnessCheck.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. //===--- LimitedRandomnessCheck.cpp - clang-tidy---------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "LimitedRandomnessCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::cert {
  13. void LimitedRandomnessCheck::registerMatchers(MatchFinder *Finder) {
  14. Finder->addMatcher(callExpr(callee(functionDecl(namedDecl(hasName("::rand")),
  15. parameterCountIs(0))))
  16. .bind("randomGenerator"),
  17. this);
  18. }
  19. void LimitedRandomnessCheck::check(const MatchFinder::MatchResult &Result) {
  20. std::string Msg;
  21. if (getLangOpts().CPlusPlus)
  22. Msg = "; use C++11 random library instead";
  23. const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("randomGenerator");
  24. diag(MatchedDecl->getBeginLoc(), "rand() has limited randomness" + Msg);
  25. }
  26. } // namespace clang::tidy::cert