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
9#include "ReplaceRandomShuffleCheck.h"
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
21ReplaceRandomShuffleCheck::ReplaceRandomShuffleCheck(StringRef Name,
22 ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 IncludeInserter(Options.getLocalOrGlobal(LocalName: "IncludeStyle",
25 Default: utils::IncludeSorter::IS_LLVM),
26 areDiagsSelfContained()) {}
27
28void ReplaceRandomShuffleCheck::registerMatchers(MatchFinder *Finder) {
29 const auto Begin = hasArgument(N: 0, InnerMatcher: expr());
30 const auto End = hasArgument(N: 1, InnerMatcher: expr());
31 const auto RandomFunc = hasArgument(N: 2, InnerMatcher: expr().bind(ID: "randomFunc"));
32 Finder->addMatcher(
33 NodeMatch: traverse(
34 TK: TK_AsIs,
35 InnerMatcher: callExpr(
36 anyOf(allOf(Begin, End, argumentCountIs(N: 2)),
37 allOf(Begin, End, RandomFunc, argumentCountIs(N: 3))),
38 hasDeclaration(InnerMatcher: functionDecl(hasName(Name: "::std::random_shuffle"))),
39 has(implicitCastExpr(has(declRefExpr().bind(ID: "name")))))
40 .bind(ID: "match")),
41 Action: this);
42}
43
44void ReplaceRandomShuffleCheck::registerPPCallbacks(
45 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
46 IncludeInserter.registerPreprocessor(PP);
47}
48
49void ReplaceRandomShuffleCheck::storeOptions(
50 ClangTidyOptions::OptionMap &Opts) {
51 Options.store(Options&: Opts, LocalName: "IncludeStyle", Value: IncludeInserter.getStyle());
52}
53
54void ReplaceRandomShuffleCheck::check(const MatchFinder::MatchResult &Result) {
55 const auto *MatchedDecl = Result.Nodes.getNodeAs<DeclRefExpr>(ID: "name");
56 const auto *MatchedArgumentThree = Result.Nodes.getNodeAs<Expr>(ID: "randomFunc");
57 const auto *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>(ID: "match");
58
59 if (MatchedCallExpr->getBeginLoc().isMacroID())
60 return;
61
62 auto Diag = [&] {
63 if (MatchedCallExpr->getNumArgs() == 3) {
64 auto DiagL =
65 diag(Loc: MatchedCallExpr->getBeginLoc(),
66 Description: "'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(Loc: MatchedCallExpr->getBeginLoc(),
74 Description: "'std::random_shuffle' has been removed in C++17; use "
75 "'std::shuffle' instead");
76 DiagL << FixItHint::CreateInsertion(
77 InsertionLoc: MatchedCallExpr->getRParenLoc(),
78 Code: ", std::mt19937(std::random_device()())");
79 return DiagL;
80 }();
81
82 std::string NewName = "shuffle";
83 StringRef ContainerText = Lexer::getSourceText(
84 Range: CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
85 SM: *Result.SourceManager, LangOpts: getLangOpts());
86 if (ContainerText.starts_with(Prefix: "std::"))
87 NewName = "std::" + NewName;
88
89 Diag << FixItHint::CreateRemoval(MatchedDecl->getSourceRange());
90 Diag << FixItHint::CreateInsertion(InsertionLoc: MatchedDecl->getBeginLoc(), Code: NewName);
91 Diag << IncludeInserter.createIncludeInsertion(
92 FileID: Result.Context->getSourceManager().getFileID(
93 SpellingLoc: MatchedCallExpr->getBeginLoc()),
94 Header: "<random>");
95}
96
97} // namespace clang::tidy::modernize
98

source code of clang-tools-extra/clang-tidy/modernize/ReplaceRandomShuffleCheck.cpp