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"
24 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
25 utils::IncludeSorter::IS_LLVM),
26 areDiagsSelfContained()) {}
29 const auto Begin = hasArgument(0, expr());
30 const auto End = hasArgument(1, expr());
31 const auto RandomFunc = hasArgument(2, expr().bind(
"randomFunc"));
36 anyOf(allOf(Begin, End, argumentCountIs(2)),
37 allOf(Begin, End, RandomFunc, argumentCountIs(3))),
38 hasDeclaration(functionDecl(hasName(
"::std::random_shuffle"))),
39 has(implicitCastExpr(has(declRefExpr().bind(
"name")))))
45 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
55 const auto *MatchedDecl = Result.Nodes.getNodeAs<DeclRefExpr>(
"name");
56 const auto *MatchedArgumentThree = Result.Nodes.getNodeAs<Expr>(
"randomFunc");
57 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(
"match");
59 if (MatchedCallExpr->getBeginLoc().isMacroID())
63 if (MatchedCallExpr->getNumArgs() == 3) {
65 diag(MatchedCallExpr->getBeginLoc(),
66 "'std::random_shuffle' has been removed in C++17; use "
67 "'std::shuffle' and an alternative random mechanism instead");
68 DiagL << FixItHint::CreateReplacement(
69 MatchedArgumentThree->getSourceRange(),
70 "std::mt19937(std::random_device()())");
73 auto DiagL =
diag(MatchedCallExpr->getBeginLoc(),
74 "'std::random_shuffle' has been removed in C++17; use "
75 "'std::shuffle' instead");
76 DiagL << FixItHint::CreateInsertion(
77 MatchedCallExpr->getRParenLoc(),
78 ", std::mt19937(std::random_device()())");
82 std::string NewName =
"shuffle";
83 StringRef ContainerText = Lexer::getSourceText(
84 CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
86 if (ContainerText.starts_with(
"std::"))
87 NewName =
"std::" + NewName;
89 Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
90 Diag << FixItHint::CreateInsertion(MatchedDecl->getBeginLoc(), NewName);
92 Result.Context->getSourceManager().getFileID(
93 MatchedCallExpr->getBeginLoc()),
llvm::SmallString< 256U > Name
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
ReplaceRandomShuffleCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
llvm::StringMap< ClangTidyValue > OptionMap