10 #include "../utils/FixItHintUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Frontend/CompilerInstance.h" 14 #include "clang/Lex/Preprocessor.h" 15 #include "clang/Tooling/FixIt.h" 23 ReplaceRandomShuffleCheck::ReplaceRandomShuffleCheck(StringRef
Name,
26 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
27 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))) {}
33 const auto Begin = hasArgument(0, expr());
34 const auto End = hasArgument(1, expr());
35 const auto RandomFunc = hasArgument(2, expr().bind(
"randomFunc"));
37 callExpr(anyOf(allOf(Begin, End, argumentCountIs(2)),
38 allOf(Begin, End, RandomFunc, argumentCountIs(3))),
39 hasDeclaration(functionDecl(hasName(
"::std::random_shuffle"))),
40 has(implicitCastExpr(has(declRefExpr().bind(
"name")))))
46 const SourceManager &SM, Preprocessor *
PP, Preprocessor *ModuleExpanderPP) {
47 IncludeInserter = std::make_unique<utils::IncludeInserter>(SM,
getLangOpts(),
49 PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
59 const auto *MatchedDecl = Result.Nodes.getNodeAs<DeclRefExpr>(
"name");
60 const auto *MatchedArgumentThree = Result.Nodes.getNodeAs<Expr>(
"randomFunc");
61 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(
"match");
63 if (MatchedCallExpr->getBeginLoc().isMacroID())
67 if (MatchedCallExpr->getNumArgs() == 3) {
69 diag(MatchedCallExpr->getBeginLoc(),
70 "'std::random_shuffle' has been removed in C++17; use " 71 "'std::shuffle' and an alternative random mechanism instead");
72 DiagL << FixItHint::CreateReplacement(
73 MatchedArgumentThree->getSourceRange(),
74 "std::mt19937(std::random_device()())");
77 auto DiagL =
diag(MatchedCallExpr->getBeginLoc(),
78 "'std::random_shuffle' has been removed in C++17; use " 79 "'std::shuffle' instead");
80 DiagL << FixItHint::CreateInsertion(
81 MatchedCallExpr->getRParenLoc(),
82 ", std::mt19937(std::random_device()())");
87 std::string NewName =
"shuffle";
88 StringRef ContainerText = Lexer::getSourceText(
91 if (ContainerText.startswith(
"std::"))
92 NewName =
"std::" + NewName;
94 Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
95 Diag << FixItHint::CreateInsertion(MatchedDecl->getBeginLoc(), NewName);
97 if (Optional<FixItHint> IncludeFixit =
98 IncludeInserter->CreateIncludeInsertion(
99 Result.Context->getSourceManager().getFileID(
100 MatchedCallExpr->getBeginLoc()),
102 Diag << IncludeFixit.getValue();
static StringRef toString(IncludeStyle Style)
Converts IncludeStyle to string representation.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
===– 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.