11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/Expr.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/ASTMatchers/ASTMatchersInternal.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/SourceLocation.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Lex/Lexer.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SmallBitVector.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/ADT/StringRef.h"
27#include "llvm/ADT/Twine.h"
28#include "llvm/Support/raw_ostream.h"
35static constexpr const char BoundCall[] =
"CallExpr";
36static constexpr const char FuncDecl[] =
"FuncDecl";
37static constexpr const char ArgName[] =
"ArgName";
41static std::string
getFullPrefix(ArrayRef<UseRangesCheck::Indexes> Signature) {
43 llvm::raw_string_ostream OS(Output);
45 OS << Item.BeginArg <<
":" << Item.EndArg <<
":"
52AST_MATCHER(Expr, hasSideEffects) {
53 return Node.HasSideEffects(Finder->getASTContext());
59 ArrayRef<StringRef> MethodNames,
60 ArrayRef<StringRef> FreeNames) {
62 anyOf(cxxMemberCallExpr(argumentCountIs(0),
63 callee(cxxMethodDecl(hasAnyName(MethodNames))),
65 callExpr(argumentCountIs(1), hasArgument(0, ArgumentMatcher),
66 hasDeclaration(functionDecl(hasAnyName(FreeNames))))));
69static ast_matchers::internal::Matcher<CallExpr>
71 ArrayRef<StringRef> BeginFreeNames,
72 ArrayRef<StringRef> EndFreeNames,
73 const std::optional<UseRangesCheck::ReverseIteratorDescriptor>
77 ast_matchers::internal::Matcher<CallExpr> ArgumentMatcher = allOf(
80 {
"begin",
"cbegin"}, BeginFreeNames)),
81 hasArgument(Indexes.
EndArg,
83 expr(matchers::isStatementIdenticalToBoundNode(ArgBound)),
84 {
"end",
"cend"}, EndFreeNames)));
85 if (ReverseDescriptor) {
86 ArgBound.push_back(
'R');
87 SmallVector<StringRef> RBegin{
88 llvm::make_first_range(ReverseDescriptor->FreeReverseNames)};
89 SmallVector<StringRef> REnd{
90 llvm::make_second_range(ReverseDescriptor->FreeReverseNames)};
91 ArgumentMatcher = anyOf(
96 {
"rbegin",
"crbegin"}, RBegin)),
100 expr(matchers::isStatementIdenticalToBoundNode(ArgBound)),
101 {
"rend",
"crend"}, REnd))));
103 return callExpr(argumentCountAtLeast(
113 llvm::SmallVector<StringRef, 4> BeginNames{
114 llvm::make_first_range(BeginEndNames)};
115 llvm::SmallVector<StringRef, 4> EndNames{
116 llvm::make_second_range(BeginEndNames)};
118 llvm::DenseSet<Replacer *> SeenRepl;
119 for (
auto I = Replaces.begin(), E = Replaces.end(); I != E; ++I) {
121 if (!SeenRepl.insert(
Replacer.get()).second)
126 [](
auto Index) { return !Index.empty(); }));
127 std::vector<StringRef> Names(1, I->getKey());
128 for (
auto J = std::next(I); J != E; ++J)
130 Names.push_back(J->getKey());
132 std::vector<ast_matchers::internal::DynTypedMatcher> TotalMatchers;
138 llvm::sort(SigVec, [](
auto &L,
auto &R) {
return R.size() < L.size(); });
140 std::vector<ast_matchers::internal::DynTypedMatcher> Matchers;
143 BeginNames, EndNames,
145 TotalMatchers.push_back(
146 ast_matchers::internal::DynTypedMatcher::constructVariadic(
147 ast_matchers::internal::DynTypedMatcher::VO_AllOf,
148 ASTNodeKind::getFromNodeKind<CallExpr>(), std::move(Matchers)));
152 callee(functionDecl(hasAnyName(Names))
153 .bind((
FuncDecl + Twine(Replacers.size() - 1).str()))),
154 ast_matchers::internal::DynTypedMatcher::constructVariadic(
155 ast_matchers::internal::DynTypedMatcher::VO_AnyOf,
156 ASTNodeKind::getFromNodeKind<CallExpr>(),
157 std::move(TotalMatchers))
158 .convertTo<CallExpr>()),
164 ArrayRef<unsigned> Indexes,
165 const ASTContext &Ctx) {
166 llvm::SmallVector<unsigned> Sorted(Indexes);
169 llvm::SmallBitVector Commas(Call.getNumArgs());
172 for (
unsigned Index : Sorted) {
173 const Expr *Arg = Call.getArg(Index);
175 if (Index >= Commas.size()) {
176 Diag << FixItHint::CreateRemoval(Arg->getSourceRange());
179 Commas[Index + 1] =
true;
180 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
182 Lexer::getLocForEndOfToken(
183 Arg->getEndLoc(), 0, Ctx.getSourceManager(), Ctx.getLangOpts())
184 .getLocWithOffset(1)}));
187 Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
188 Arg->getBeginLoc().getLocWithOffset(-1), Arg->getEndLoc()));
189 Commas[Index] =
true;
196 const FunctionDecl *Function =
nullptr;
197 for (
const auto &[Node, Value] : Result.Nodes.getMap()) {
198 StringRef NodeStr(Node);
199 if (!NodeStr.consume_front(
FuncDecl))
201 Function = Value.get<FunctionDecl>();
203 if (NodeStr.getAsInteger(10, Index)) {
204 llvm_unreachable(
"Unable to extract replacer index");
206 assert(Index < Replacers.size());
211 SmallString<64> Buffer;
214 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(Buffer);
220 if (Function->getName() ==
"find") {
221 const unsigned ValueArgIndex = 2;
222 if (Call->getNumArgs() <= ValueArgIndex)
224 const Expr *ValueExpr =
225 Call->getArg(ValueArgIndex)->IgnoreParenImpCasts();
226 if (isa<CXXNullPtrLiteralExpr>(ValueExpr))
232 Diag << FixItHint::CreateReplacement(Call->getCallee()->getSourceRange(),
235 Diag << Inserter.createIncludeInsertion(
236 Result.SourceManager->getFileID(Call->getBeginLoc()), *Include);
237 llvm::SmallVector<unsigned, 3> ToRemove;
238 for (
const auto &[First, Second, Replace] : Sig) {
239 auto ArgNode =
ArgName + std::to_string(First);
240 if (
const auto *ArgExpr = Result.Nodes.getNodeAs<Expr>(ArgNode)) {
241 Diag << FixItHint::CreateReplacement(
244 Lexer::getSourceText(
245 CharSourceRange::getTokenRange(ArgExpr->getSourceRange()),
246 Result.Context->getSourceManager(),
247 Result.Context->getLangOpts()));
249 assert(ReverseDescriptor &&
"Couldn't find forward argument");
250 ArgNode.push_back(
'R');
251 ArgExpr = Result.Nodes.getNodeAs<Expr>(ArgNode);
252 assert(ArgExpr &&
"Couldn't find forward or reverse argument");
253 if (ReverseDescriptor->ReverseHeader)
254 Diag << Inserter.createIncludeInsertion(
255 Result.SourceManager->getFileID(Call->getBeginLoc()),
256 *ReverseDescriptor->ReverseHeader);
257 StringRef ArgText = Lexer::getSourceText(
258 CharSourceRange::getTokenRange(ArgExpr->getSourceRange()),
259 Result.Context->getSourceManager(), Result.Context->getLangOpts());
260 SmallString<128> ReplaceText;
261 if (ReverseDescriptor->IsPipeSyntax)
263 {ArgText,
" | ", ReverseDescriptor->ReverseAdaptorName});
266 {ReverseDescriptor->ReverseAdaptorName,
"(", ArgText,
")"});
267 Diag << FixItHint::CreateReplacement(
277 llvm_unreachable(
"No valid signature found");
281 const LangOptions &LangOpts)
const {
282 return LangOpts.CPlusPlus11;
287 Inserter(Options.getLocalOrGlobal(
"IncludeStyle",
289 areDiagsSelfContained()) {}
292 Preprocessor *PP, Preprocessor *) {
293 Inserter.registerPreprocessor(PP);
297 Options.store(Opts,
"IncludeStyle", Inserter.getStyle());
300std::optional<std::string>
306 return diag(Call.getBeginLoc(),
"use a ranges version of this algorithm");
309std::optional<UseRangesCheck::ReverseIteratorDescriptor>
314ArrayRef<std::pair<StringRef, StringRef>>
320 return TK_IgnoreUnlessSpelledInSource;
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
virtual ArrayRef< Signature > getReplacementSignatures() const =0
Gets an array of all the possible overloads for a function with indexes where begin and end arguments...
virtual std::optional< std::string > getHeaderInclusion(const NamedDecl &OriginalName) const
Gets the header needed to access the replaced function Return std::nullopt if no new header is needed...
virtual std::optional< std::string > getReplaceName(const NamedDecl &OriginalName) const =0
Gets the name to replace a function with, return std::nullopt for a replacement where we just call a ...
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
virtual DiagnosticBuilder createDiag(const CallExpr &Call)
Create a diagnostic for the CallExpr Override this to support custom diagnostic messages.
void registerMatchers(ast_matchers::MatchFinder *Finder) final
std::optional< TraversalKind > getCheckTraversalKind() const override
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) final
virtual ReplacerMap getReplacerMap() const =0
Gets a map of function to replace and methods to create the replacements.
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
UseRangesCheck(StringRef Name, ClangTidyContext *Context)
virtual std::optional< ReverseIteratorDescriptor > getReverseDescriptor() const
virtual ArrayRef< std::pair< StringRef, StringRef > > getFreeBeginEndMethods() const
Gets the fully qualified names of begin and end functions.
SmallVector< Indexes, 2 > Signature
static std::string getFullPrefix(ArrayRef< UseRangesCheck::Indexes > Signature)
static ast_matchers::internal::Matcher< CallExpr > makeMatcherPair(StringRef State, const UseRangesCheck::Indexes &Indexes, ArrayRef< StringRef > BeginFreeNames, ArrayRef< StringRef > EndFreeNames, const std::optional< UseRangesCheck::ReverseIteratorDescriptor > &ReverseDescriptor)
static auto makeExprMatcher(ast_matchers::internal::Matcher< Expr > ArgumentMatcher, ArrayRef< StringRef > MethodNames, ArrayRef< StringRef > FreeNames)
static void removeFunctionArgs(DiagnosticBuilder &Diag, const CallExpr &Call, ArrayRef< unsigned > Indexes, const ASTContext &Ctx)
IncludeSorter(const SourceManager *SourceMgr, const FileID FileID, StringRef FileName, IncludeStyle Style)
Class used by IncludeInserterCallback to record the names of the / inclusions in a given source file ...
llvm::StringMap< ClangTidyValue > OptionMap
static constexpr const char ArgName[]
static constexpr const char BoundCall[]
static constexpr const char FuncDecl[]