clang-tools 19.0.0git
ReplaceRandomShuffleCheck.cpp
Go to the documentation of this file.
1//===--- ReplaceRandomShuffleCheck.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
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"
16
17using namespace clang::ast_matchers;
18
19namespace clang::tidy::modernize {
20
22 ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
25 utils::IncludeSorter::IS_LLVM),
26 areDiagsSelfContained()) {}
27
29 const auto Begin = hasArgument(0, expr());
30 const auto End = hasArgument(1, expr());
31 const auto RandomFunc = hasArgument(2, expr().bind("randomFunc"));
32 Finder->addMatcher(
33 traverse(
34 TK_AsIs,
35 callExpr(
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")))))
40 .bind("match")),
41 this);
42}
43
45 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
46 IncludeInserter.registerPreprocessor(PP);
47}
48
51 Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
52}
53
54void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {
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");
58
59 if (MatchedCallExpr->getBeginLoc().isMacroID())
60 return;
61
62 auto Diag = [&] {
63 if (MatchedCallExpr->getNumArgs() == 3) {
64 auto DiagL =
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()())");
71 return DiagL;
72 }
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()())");
79 return DiagL;
80 }();
81
82 std::string NewName = "shuffle";
83 StringRef ContainerText = Lexer::getSourceText(
84 CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
85 *Result.SourceManager, getLangOpts());
86 if (ContainerText.starts_with("std::"))
87 NewName = "std::" + NewName;
88
89 Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
90 Diag << FixItHint::CreateInsertion(MatchedDecl->getBeginLoc(), NewName);
91 Diag << IncludeInserter.createIncludeInsertion(
92 Result.Context->getSourceManager().getFileID(
93 MatchedCallExpr->getBeginLoc()),
94 "<random>");
95}
96
97} // namespace clang::tidy::modernize
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