|
| 1 | +//===--- MinMaxUseInitializerListCheck.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 "MinMaxUseInitializerListCheck.h" |
| 10 | +#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 11 | +#include "clang/Frontend/CompilerInstance.h" |
| 12 | +#include "clang/Lex/Lexer.h" |
| 13 | + |
| 14 | +using namespace clang::ast_matchers; |
| 15 | + |
| 16 | +namespace clang::tidy::modernize { |
| 17 | + |
| 18 | +MinMaxUseInitializerListCheck::MinMaxUseInitializerListCheck( |
| 19 | + StringRef Name, ClangTidyContext *Context) |
| 20 | + : ClangTidyCheck(Name, Context), |
| 21 | + Inserter(Options.getLocalOrGlobal("IncludeStyle", |
| 22 | + utils::IncludeSorter::IS_LLVM), |
| 23 | + areDiagsSelfContained()) {} |
| 24 | + |
| 25 | +void MinMaxUseInitializerListCheck::storeOptions( |
| 26 | + ClangTidyOptions::OptionMap &Opts) { |
| 27 | + Options.store(Opts, "IncludeStyle", Inserter.getStyle()); |
| 28 | +} |
| 29 | + |
| 30 | +void MinMaxUseInitializerListCheck::registerMatchers(MatchFinder *Finder) { |
| 31 | + Finder->addMatcher( |
| 32 | + callExpr( |
| 33 | + callee(functionDecl(hasName("::std::max"))), |
| 34 | + hasAnyArgument(callExpr(callee(functionDecl(hasName("::std::max"))))), |
| 35 | + unless( |
| 36 | + hasParent(callExpr(callee(functionDecl(hasName("::std::max"))))))) |
| 37 | + .bind("maxCall"), |
| 38 | + this); |
| 39 | + |
| 40 | + Finder->addMatcher( |
| 41 | + callExpr( |
| 42 | + callee(functionDecl(hasName("::std::min"))), |
| 43 | + hasAnyArgument(callExpr(callee(functionDecl(hasName("::std::min"))))), |
| 44 | + unless( |
| 45 | + hasParent(callExpr(callee(functionDecl(hasName("::std::min"))))))) |
| 46 | + .bind("minCall"), |
| 47 | + this); |
| 48 | +} |
| 49 | + |
| 50 | +void MinMaxUseInitializerListCheck::registerPPCallbacks( |
| 51 | + const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { |
| 52 | + Inserter.registerPreprocessor(PP); |
| 53 | +} |
| 54 | + |
| 55 | +void MinMaxUseInitializerListCheck::check( |
| 56 | + const MatchFinder::MatchResult &Result) { |
| 57 | + const auto *MaxCall = Result.Nodes.getNodeAs<CallExpr>("maxCall"); |
| 58 | + const auto *MinCall = Result.Nodes.getNodeAs<CallExpr>("minCall"); |
| 59 | + |
| 60 | + const CallExpr *TopCall = MaxCall ? MaxCall : MinCall; |
| 61 | + if (!TopCall) { |
| 62 | + return; |
| 63 | + } |
| 64 | + const QualType ResultType = |
| 65 | + TopCall->getDirectCallee()->getReturnType().getNonReferenceType(); |
| 66 | + |
| 67 | + const Expr *FirstArg = nullptr; |
| 68 | + const Expr *LastArg = nullptr; |
| 69 | + std::vector<const Expr *> Args; |
| 70 | + findArgs(TopCall, &FirstArg, &LastArg, Args); |
| 71 | + |
| 72 | + if (!FirstArg || !LastArg || Args.size() <= 2) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + std::string ReplacementText = "{"; |
| 77 | + for (const Expr *Arg : Args) { |
| 78 | + QualType ArgType = Arg->getType(); |
| 79 | + bool CastNeeded = |
| 80 | + ArgType.getCanonicalType() != ResultType.getCanonicalType(); |
| 81 | + |
| 82 | + if (CastNeeded) |
| 83 | + ReplacementText += "static_cast<" + ResultType.getAsString() + ">("; |
| 84 | + |
| 85 | + ReplacementText += Lexer::getSourceText( |
| 86 | + CharSourceRange::getTokenRange(Arg->getSourceRange()), |
| 87 | + *Result.SourceManager, Result.Context->getLangOpts()); |
| 88 | + |
| 89 | + if (CastNeeded) |
| 90 | + ReplacementText += ")"; |
| 91 | + ReplacementText += ", "; |
| 92 | + } |
| 93 | + ReplacementText = ReplacementText.substr(0, ReplacementText.size() - 2) + "}"; |
| 94 | + |
| 95 | + diag(TopCall->getBeginLoc(), |
| 96 | + "do not use nested std::%0 calls, use %1 instead") |
| 97 | + << TopCall->getDirectCallee()->getName() << ReplacementText |
| 98 | + << FixItHint::CreateReplacement( |
| 99 | + CharSourceRange::getTokenRange( |
| 100 | + FirstArg->getBeginLoc(), |
| 101 | + Lexer::getLocForEndOfToken(TopCall->getEndLoc(), 0, |
| 102 | + Result.Context->getSourceManager(), |
| 103 | + Result.Context->getLangOpts()) |
| 104 | + .getLocWithOffset(-2)), |
| 105 | + ReplacementText) |
| 106 | + << Inserter.createMainFileIncludeInsertion("<algorithm>"); |
| 107 | +} |
| 108 | + |
| 109 | +void MinMaxUseInitializerListCheck::findArgs(const CallExpr *Call, |
| 110 | + const Expr **First, |
| 111 | + const Expr **Last, |
| 112 | + std::vector<const Expr *> &Args) { |
| 113 | + if (!Call) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const FunctionDecl *Callee = Call->getDirectCallee(); |
| 118 | + if (!Callee) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + for (const Expr *Arg : Call->arguments()) { |
| 123 | + if (!*First) |
| 124 | + *First = Arg; |
| 125 | + |
| 126 | + const CallExpr *InnerCall = dyn_cast<CallExpr>(Arg); |
| 127 | + if (InnerCall && InnerCall->getDirectCallee() && |
| 128 | + InnerCall->getDirectCallee()->getNameAsString() == |
| 129 | + Call->getDirectCallee()->getNameAsString()) { |
| 130 | + findArgs(InnerCall, First, Last, Args); |
| 131 | + } else |
| 132 | + Args.push_back(Arg); |
| 133 | + |
| 134 | + *Last = Arg; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +} // namespace clang::tidy::modernize |
0 commit comments