blob: 2f37aa2804c768f29564d240125f85a4f961b4d5 [file] [log] [blame]
Alexander Kornienko04970842015-08-19 09:11:46 +00001//===--- LoopConvertCheck.cpp - clang-tidy---------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "LoopConvertCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang;
15using namespace clang::ast_matchers;
16using namespace llvm;
17
18namespace clang {
19namespace tidy {
20namespace modernize {
21
Angel Garcia Gomez146b96d2015-10-22 09:48:23 +000022static const char LoopNameArray[] = "forLoopArray";
23static const char LoopNameIterator[] = "forLoopIterator";
24static const char LoopNamePseudoArray[] = "forLoopPseudoArray";
25static const char ConditionBoundName[] = "conditionBound";
26static const char ConditionVarName[] = "conditionVar";
27static const char IncrementVarName[] = "incrementVar";
28static const char InitVarName[] = "initVar";
29static const char BeginCallName[] = "beginCall";
30static const char EndCallName[] = "endCall";
31static const char ConditionEndVarName[] = "conditionEndVar";
32static const char EndVarName[] = "endVar";
33static const char DerefByValueResultName[] = "derefByValueResult";
34static const char DerefByRefResultName[] = "derefByRefResult";
Alexander Kornienko04970842015-08-19 09:11:46 +000035
36// shared matchers
37static const TypeMatcher AnyType = anything();
38
39static const StatementMatcher IntegerComparisonMatcher =
40 expr(ignoringParenImpCasts(
41 declRefExpr(to(varDecl(hasType(isInteger())).bind(ConditionVarName)))));
42
43static const DeclarationMatcher InitToZeroMatcher =
44 varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
45 .bind(InitVarName);
46
47static const StatementMatcher IncrementVarMatcher =
48 declRefExpr(to(varDecl(hasType(isInteger())).bind(IncrementVarName)));
49
50/// \brief The matcher for loops over arrays.
51///
52/// In this general example, assuming 'j' and 'k' are of integral type:
53/// \code
54/// for (int i = 0; j < 3 + 2; ++k) { ... }
55/// \endcode
56/// The following string identifiers are bound to these parts of the AST:
57/// ConditionVarName: 'j' (as a VarDecl)
58/// ConditionBoundName: '3 + 2' (as an Expr)
59/// InitVarName: 'i' (as a VarDecl)
60/// IncrementVarName: 'k' (as a VarDecl)
61/// LoopName: The entire for loop (as a ForStmt)
62///
63/// Client code will need to make sure that:
64/// - The three index variables identified by the matcher are the same
65/// VarDecl.
66/// - The index variable is only used as an array index.
67/// - All arrays indexed by the loop are the same.
68StatementMatcher makeArrayLoopMatcher() {
69 StatementMatcher ArrayBoundMatcher =
70 expr(hasType(isInteger())).bind(ConditionBoundName);
71
72 return forStmt(
Angel Garcia Gomez06d010c2015-08-25 15:44:00 +000073 unless(isInTemplateInstantiation()),
Alexander Kornienko04970842015-08-19 09:11:46 +000074 hasLoopInit(declStmt(hasSingleDecl(InitToZeroMatcher))),
75 hasCondition(anyOf(
76 binaryOperator(hasOperatorName("<"),
77 hasLHS(IntegerComparisonMatcher),
78 hasRHS(ArrayBoundMatcher)),
79 binaryOperator(hasOperatorName(">"), hasLHS(ArrayBoundMatcher),
80 hasRHS(IntegerComparisonMatcher)))),
81 hasIncrement(unaryOperator(hasOperatorName("++"),
82 hasUnaryOperand(IncrementVarMatcher))))
83 .bind(LoopNameArray);
84}
85
86/// \brief The matcher used for iterator-based for loops.
87///
88/// This matcher is more flexible than array-based loops. It will match
89/// catch loops of the following textual forms (regardless of whether the
90/// iterator type is actually a pointer type or a class type):
91///
92/// Assuming f, g, and h are of type containerType::iterator,
93/// \code
94/// for (containerType::iterator it = container.begin(),
95/// e = createIterator(); f != g; ++h) { ... }
96/// for (containerType::iterator it = container.begin();
97/// f != anotherContainer.end(); ++h) { ... }
98/// \endcode
99/// The following string identifiers are bound to the parts of the AST:
100/// InitVarName: 'it' (as a VarDecl)
101/// ConditionVarName: 'f' (as a VarDecl)
102/// LoopName: The entire for loop (as a ForStmt)
103/// In the first example only:
104/// EndVarName: 'e' (as a VarDecl)
105/// ConditionEndVarName: 'g' (as a VarDecl)
106/// In the second example only:
107/// EndCallName: 'container.end()' (as a CXXMemberCallExpr)
108///
109/// Client code will need to make sure that:
110/// - The iterator variables 'it', 'f', and 'h' are the same.
111/// - The two containers on which 'begin' and 'end' are called are the same.
112/// - If the end iterator variable 'g' is defined, it is the same as 'f'.
113StatementMatcher makeIteratorLoopMatcher() {
114 StatementMatcher BeginCallMatcher =
Angel Garcia Gomez2bfb7cb2015-10-01 08:57:11 +0000115 cxxMemberCallExpr(
116 argumentCountIs(0),
117 callee(cxxMethodDecl(anyOf(hasName("begin"), hasName("cbegin")))))
Alexander Kornienko04970842015-08-19 09:11:46 +0000118 .bind(BeginCallName);
119
120 DeclarationMatcher InitDeclMatcher =
121 varDecl(hasInitializer(anyOf(ignoringParenImpCasts(BeginCallMatcher),
122 materializeTemporaryExpr(
123 ignoringParenImpCasts(BeginCallMatcher)),
124 hasDescendant(BeginCallMatcher))))
125 .bind(InitVarName);
126
127 DeclarationMatcher EndDeclMatcher =
128 varDecl(hasInitializer(anything())).bind(EndVarName);
129
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000130 StatementMatcher EndCallMatcher = cxxMemberCallExpr(
Angel Garcia Gomez2bfb7cb2015-10-01 08:57:11 +0000131 argumentCountIs(0),
132 callee(cxxMethodDecl(anyOf(hasName("end"), hasName("cend")))));
Alexander Kornienko04970842015-08-19 09:11:46 +0000133
134 StatementMatcher IteratorBoundMatcher =
135 expr(anyOf(ignoringParenImpCasts(
136 declRefExpr(to(varDecl().bind(ConditionEndVarName)))),
137 ignoringParenImpCasts(expr(EndCallMatcher).bind(EndCallName)),
138 materializeTemporaryExpr(ignoringParenImpCasts(
139 expr(EndCallMatcher).bind(EndCallName)))));
140
141 StatementMatcher IteratorComparisonMatcher = expr(
142 ignoringParenImpCasts(declRefExpr(to(varDecl().bind(ConditionVarName)))));
143
144 StatementMatcher OverloadedNEQMatcher =
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000145 cxxOperatorCallExpr(hasOverloadedOperatorName("!="), argumentCountIs(2),
146 hasArgument(0, IteratorComparisonMatcher),
147 hasArgument(1, IteratorBoundMatcher));
Alexander Kornienko04970842015-08-19 09:11:46 +0000148
149 // This matcher tests that a declaration is a CXXRecordDecl that has an
150 // overloaded operator*(). If the operator*() returns by value instead of by
151 // reference then the return type is tagged with DerefByValueResultName.
152 internal::Matcher<VarDecl> TestDerefReturnsByValue =
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000153 hasType(cxxRecordDecl(hasMethod(allOf(
Alexander Kornienko04970842015-08-19 09:11:46 +0000154 hasOverloadedOperatorName("*"),
155 anyOf(
156 // Tag the return type if it's by value.
157 returns(qualType(unless(hasCanonicalType(referenceType())))
158 .bind(DerefByValueResultName)),
159 returns(
160 // Skip loops where the iterator's operator* returns an
161 // rvalue reference. This is just weird.
162 qualType(unless(hasCanonicalType(rValueReferenceType())))
163 .bind(DerefByRefResultName)))))));
164
165 return forStmt(
Angel Garcia Gomez06d010c2015-08-25 15:44:00 +0000166 unless(isInTemplateInstantiation()),
Alexander Kornienko04970842015-08-19 09:11:46 +0000167 hasLoopInit(anyOf(declStmt(declCountIs(2),
168 containsDeclaration(0, InitDeclMatcher),
169 containsDeclaration(1, EndDeclMatcher)),
170 declStmt(hasSingleDecl(InitDeclMatcher)))),
171 hasCondition(
172 anyOf(binaryOperator(hasOperatorName("!="),
173 hasLHS(IteratorComparisonMatcher),
174 hasRHS(IteratorBoundMatcher)),
175 binaryOperator(hasOperatorName("!="),
176 hasLHS(IteratorBoundMatcher),
177 hasRHS(IteratorComparisonMatcher)),
178 OverloadedNEQMatcher)),
179 hasIncrement(anyOf(
180 unaryOperator(hasOperatorName("++"),
181 hasUnaryOperand(declRefExpr(
182 to(varDecl(hasType(pointsTo(AnyType)))
183 .bind(IncrementVarName))))),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000184 cxxOperatorCallExpr(
Alexander Kornienko04970842015-08-19 09:11:46 +0000185 hasOverloadedOperatorName("++"),
186 hasArgument(
187 0, declRefExpr(to(varDecl(TestDerefReturnsByValue)
188 .bind(IncrementVarName))))))))
189 .bind(LoopNameIterator);
190}
191
192/// \brief The matcher used for array-like containers (pseudoarrays).
193///
194/// This matcher is more flexible than array-based loops. It will match
195/// loops of the following textual forms (regardless of whether the
196/// iterator type is actually a pointer type or a class type):
197///
198/// Assuming f, g, and h are of type containerType::iterator,
199/// \code
200/// for (int i = 0, j = container.size(); f < g; ++h) { ... }
201/// for (int i = 0; f < container.size(); ++h) { ... }
202/// \endcode
203/// The following string identifiers are bound to the parts of the AST:
204/// InitVarName: 'i' (as a VarDecl)
205/// ConditionVarName: 'f' (as a VarDecl)
206/// LoopName: The entire for loop (as a ForStmt)
207/// In the first example only:
208/// EndVarName: 'j' (as a VarDecl)
209/// ConditionEndVarName: 'g' (as a VarDecl)
210/// In the second example only:
211/// EndCallName: 'container.size()' (as a CXXMemberCallExpr)
212///
213/// Client code will need to make sure that:
214/// - The index variables 'i', 'f', and 'h' are the same.
215/// - The containers on which 'size()' is called is the container indexed.
216/// - The index variable is only used in overloaded operator[] or
217/// container.at().
218/// - If the end iterator variable 'g' is defined, it is the same as 'j'.
219/// - The container's iterators would not be invalidated during the loop.
220StatementMatcher makePseudoArrayLoopMatcher() {
221 // Test that the incoming type has a record declaration that has methods
222 // called 'begin' and 'end'. If the incoming type is const, then make sure
223 // these methods are also marked const.
224 //
225 // FIXME: To be completely thorough this matcher should also ensure the
226 // return type of begin/end is an iterator that dereferences to the same as
227 // what operator[] or at() returns. Such a test isn't likely to fail except
228 // for pathological cases.
229 //
230 // FIXME: Also, a record doesn't necessarily need begin() and end(). Free
231 // functions called begin() and end() taking the container as an argument
232 // are also allowed.
233 TypeMatcher RecordWithBeginEnd = qualType(
234 anyOf(qualType(isConstQualified(),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000235 hasDeclaration(cxxRecordDecl(
236 hasMethod(cxxMethodDecl(hasName("begin"), isConst())),
237 hasMethod(cxxMethodDecl(hasName("end"),
238 isConst())))) // hasDeclaration
239 ), // qualType
Alexander Kornienko04970842015-08-19 09:11:46 +0000240 qualType(unless(isConstQualified()),
241 hasDeclaration(
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000242 cxxRecordDecl(hasMethod(hasName("begin")),
243 hasMethod(hasName("end"))))) // qualType
Alexander Kornienko04970842015-08-19 09:11:46 +0000244 ));
245
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000246 StatementMatcher SizeCallMatcher = cxxMemberCallExpr(
Alexander Kornienko04970842015-08-19 09:11:46 +0000247 argumentCountIs(0),
Aaron Ballmanb9ea09c2015-09-17 13:31:25 +0000248 callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))),
Alexander Kornienko04970842015-08-19 09:11:46 +0000249 on(anyOf(hasType(pointsTo(RecordWithBeginEnd)),
250 hasType(RecordWithBeginEnd))));
251
252 StatementMatcher EndInitMatcher =
253 expr(anyOf(ignoringParenImpCasts(expr(SizeCallMatcher).bind(EndCallName)),
254 explicitCastExpr(hasSourceExpression(ignoringParenImpCasts(
255 expr(SizeCallMatcher).bind(EndCallName))))));
256
257 DeclarationMatcher EndDeclMatcher =
258 varDecl(hasInitializer(EndInitMatcher)).bind(EndVarName);
259
260 StatementMatcher IndexBoundMatcher =
261 expr(anyOf(ignoringParenImpCasts(declRefExpr(to(
262 varDecl(hasType(isInteger())).bind(ConditionEndVarName)))),
263 EndInitMatcher));
264
265 return forStmt(
Angel Garcia Gomez06d010c2015-08-25 15:44:00 +0000266 unless(isInTemplateInstantiation()),
Alexander Kornienko04970842015-08-19 09:11:46 +0000267 hasLoopInit(
268 anyOf(declStmt(declCountIs(2),
269 containsDeclaration(0, InitToZeroMatcher),
270 containsDeclaration(1, EndDeclMatcher)),
271 declStmt(hasSingleDecl(InitToZeroMatcher)))),
272 hasCondition(anyOf(
273 binaryOperator(hasOperatorName("<"),
274 hasLHS(IntegerComparisonMatcher),
275 hasRHS(IndexBoundMatcher)),
276 binaryOperator(hasOperatorName(">"), hasLHS(IndexBoundMatcher),
277 hasRHS(IntegerComparisonMatcher)))),
278 hasIncrement(unaryOperator(hasOperatorName("++"),
279 hasUnaryOperand(IncrementVarMatcher))))
280 .bind(LoopNamePseudoArray);
281}
282
283/// \brief Determine whether Init appears to be an initializing an iterator.
284///
285/// If it is, returns the object whose begin() or end() method is called, and
286/// the output parameter isArrow is set to indicate whether the initialization
287/// is called via . or ->.
288static const Expr *getContainerFromBeginEndCall(const Expr *Init, bool IsBegin,
289 bool *IsArrow) {
290 // FIXME: Maybe allow declaration/initialization outside of the for loop.
291 const auto *TheCall =
292 dyn_cast_or_null<CXXMemberCallExpr>(digThroughConstructors(Init));
293 if (!TheCall || TheCall->getNumArgs() != 0)
294 return nullptr;
295
296 const auto *Member = dyn_cast<MemberExpr>(TheCall->getCallee());
297 if (!Member)
298 return nullptr;
299 StringRef Name = Member->getMemberDecl()->getName();
300 StringRef TargetName = IsBegin ? "begin" : "end";
Angel Garcia Gomez2bfb7cb2015-10-01 08:57:11 +0000301 StringRef ConstTargetName = IsBegin ? "cbegin" : "cend";
302 if (Name != TargetName && Name != ConstTargetName)
Alexander Kornienko04970842015-08-19 09:11:46 +0000303 return nullptr;
304
305 const Expr *SourceExpr = Member->getBase();
306 if (!SourceExpr)
307 return nullptr;
308
309 *IsArrow = Member->isArrow();
310 return SourceExpr;
311}
312
313/// \brief Determines the container whose begin() and end() functions are called
314/// for an iterator-based loop.
315///
316/// BeginExpr must be a member call to a function named "begin()", and EndExpr
317/// must be a member.
318static const Expr *findContainer(ASTContext *Context, const Expr *BeginExpr,
319 const Expr *EndExpr,
320 bool *ContainerNeedsDereference) {
321 // Now that we know the loop variable and test expression, make sure they are
322 // valid.
323 bool BeginIsArrow = false;
324 bool EndIsArrow = false;
325 const Expr *BeginContainerExpr =
326 getContainerFromBeginEndCall(BeginExpr, /*IsBegin=*/true, &BeginIsArrow);
327 if (!BeginContainerExpr)
328 return nullptr;
329
330 const Expr *EndContainerExpr =
331 getContainerFromBeginEndCall(EndExpr, /*IsBegin=*/false, &EndIsArrow);
332 // Disallow loops that try evil things like this (note the dot and arrow):
333 // for (IteratorType It = Obj.begin(), E = Obj->end(); It != E; ++It) { }
334 if (!EndContainerExpr || BeginIsArrow != EndIsArrow ||
335 !areSameExpr(Context, EndContainerExpr, BeginContainerExpr))
336 return nullptr;
337
338 *ContainerNeedsDereference = BeginIsArrow;
339 return BeginContainerExpr;
340}
341
342/// \brief Obtain the original source code text from a SourceRange.
343static StringRef getStringFromRange(SourceManager &SourceMgr,
344 const LangOptions &LangOpts,
345 SourceRange Range) {
346 if (SourceMgr.getFileID(Range.getBegin()) !=
Alexander Kornienko74a44d92015-08-20 13:18:23 +0000347 SourceMgr.getFileID(Range.getEnd())) {
348 return StringRef(); // Empty string.
349 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000350
351 return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
352 LangOpts);
353}
354
355/// \brief If the given expression is actually a DeclRefExpr, find and return
356/// the underlying VarDecl; otherwise, return NULL.
357static const VarDecl *getReferencedVariable(const Expr *E) {
358 if (const DeclRefExpr *DRE = getDeclRef(E))
359 return dyn_cast<VarDecl>(DRE->getDecl());
360 return nullptr;
361}
362
363/// \brief Returns true when the given expression is a member expression
364/// whose base is `this` (implicitly or not).
365static bool isDirectMemberExpr(const Expr *E) {
366 if (const auto *Member = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts()))
367 return isa<CXXThisExpr>(Member->getBase()->IgnoreParenImpCasts());
368 return false;
369}
370
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000371/// \brief Returns true when it can be guaranteed that the elements of the
372/// container are not being modified.
373static bool usagesAreConst(const UsageResult &Usages) {
374 // FIXME: Make this function more generic.
375 return Usages.empty();
376}
377
378/// \brief Returns true if the elements of the container are never accessed
379/// by reference.
380static bool usagesReturnRValues(const UsageResult &Usages) {
381 for (const auto &U : Usages) {
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000382 if (U.Expression && !U.Expression->isRValue())
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000383 return false;
384 }
385 return true;
386}
387
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000388/// \brief Returns true if the container is const-qualified.
389static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) {
390 if (const auto *VDec = getReferencedVariable(ContainerExpr)) {
391 QualType CType = VDec->getType();
392 if (Dereference) {
393 if (!CType->isPointerType())
394 return false;
395 CType = CType->getPointeeType();
396 }
Manuel Klimek143b6442015-09-23 18:40:47 +0000397 // If VDec is a reference to a container, Dereference is false,
398 // but we still need to check the const-ness of the underlying container
399 // type.
Angel Garcia Gomezaed6dde2015-09-24 13:26:28 +0000400 CType = CType.getNonReferenceType();
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000401 return CType.isConstQualified();
402 }
403 return false;
404}
405
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000406LoopConvertCheck::RangeDescriptor::RangeDescriptor()
407 : ContainerNeedsDereference(false), DerefByConstRef(false),
408 DerefByValue(false), IsTriviallyCopyable(false) {}
409
Alexander Kornienko04970842015-08-19 09:11:46 +0000410LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
411 : ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
412 MinConfidence(StringSwitch<Confidence::Level>(
413 Options.get("MinConfidence", "reasonable"))
414 .Case("safe", Confidence::CL_Safe)
415 .Case("risky", Confidence::CL_Risky)
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000416 .Default(Confidence::CL_Reasonable)),
417 NamingStyle(StringSwitch<VariableNamer::NamingStyle>(
418 Options.get("NamingStyle", "CamelCase"))
419 .Case("camelBack", VariableNamer::NS_CamelBack)
420 .Case("lower_case", VariableNamer::NS_LowerCase)
421 .Case("UPPER_CASE", VariableNamer::NS_UpperCase)
422 .Default(VariableNamer::NS_CamelCase)) {}
Alexander Kornienko04970842015-08-19 09:11:46 +0000423
424void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
425 SmallVector<std::string, 3> Confs{"risky", "reasonable", "safe"};
426 Options.store(Opts, "MinConfidence", Confs[static_cast<int>(MinConfidence)]);
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000427
428 SmallVector<std::string, 4> Styles{"camelBack", "CamelCase", "lower_case",
Angel Garcia Gomez2bfb7cb2015-10-01 08:57:11 +0000429 "UPPER_CASE"};
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000430 Options.store(Opts, "NamingStyle", Styles[static_cast<int>(NamingStyle)]);
Alexander Kornienko04970842015-08-19 09:11:46 +0000431}
432
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000433void LoopConvertCheck::registerMatchers(MatchFinder *Finder) {
434 // Only register the matchers for C++. Because this checker is used for
435 // modernization, it is reasonable to run it on any C++ standard with the
436 // assumption the user is trying to modernize their codebase.
437 if (!getLangOpts().CPlusPlus)
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000438 return;
439
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000440 Finder->addMatcher(makeArrayLoopMatcher(), this);
441 Finder->addMatcher(makeIteratorLoopMatcher(), this);
442 Finder->addMatcher(makePseudoArrayLoopMatcher(), this);
443}
444
Angel Garcia Gomez90bf8952015-10-01 13:08:21 +0000445/// \brief Given the range of a single declaration, such as:
446/// \code
447/// unsigned &ThisIsADeclarationThatCanSpanSeveralLinesOfCode =
448/// InitializationValues[I];
449/// next_instruction;
450/// \endcode
451/// Finds the range that has to be erased to remove this declaration without
452/// leaving empty lines, by extending the range until the beginning of the
453/// next instruction.
454///
455/// We need to delete a potential newline after the deleted alias, as
456/// clang-format will leave empty lines untouched. For all other formatting we
457/// rely on clang-format to fix it.
458void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) {
459 bool Invalid = false;
460 const char *TextAfter =
461 SM.getCharacterData(Range.getEnd().getLocWithOffset(1), &Invalid);
462 if (Invalid)
463 return;
464 unsigned Offset = std::strspn(TextAfter, " \t\r\n");
465 Range =
466 SourceRange(Range.getBegin(), Range.getEnd().getLocWithOffset(Offset));
467}
468
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000469/// \brief Computes the changes needed to convert a given for loop, and
470/// applies them.
471void LoopConvertCheck::doConversion(
472 ASTContext *Context, const VarDecl *IndexVar, const VarDecl *MaybeContainer,
473 const UsageResult &Usages, const DeclStmt *AliasDecl, bool AliasUseRequired,
474 bool AliasFromForInit, const ForStmt *Loop, RangeDescriptor Descriptor) {
475 auto Diag = diag(Loop->getForLoc(), "use range-based for loop instead");
Alexander Kornienko04970842015-08-19 09:11:46 +0000476
477 std::string VarName;
478 bool VarNameFromAlias = (Usages.size() == 1) && AliasDecl;
479 bool AliasVarIsRef = false;
Angel Garcia Gomez199e5232015-10-05 11:15:39 +0000480 bool CanCopy = true;
Alexander Kornienko04970842015-08-19 09:11:46 +0000481
482 if (VarNameFromAlias) {
483 const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
484 VarName = AliasVar->getName().str();
485 AliasVarIsRef = AliasVar->getType()->isReferenceType();
486
487 // We keep along the entire DeclStmt to keep the correct range here.
Angel Garcia Gomez90bf8952015-10-01 13:08:21 +0000488 SourceRange ReplaceRange = AliasDecl->getSourceRange();
Alexander Kornienko04970842015-08-19 09:11:46 +0000489
490 std::string ReplacementText;
491 if (AliasUseRequired) {
492 ReplacementText = VarName;
493 } else if (AliasFromForInit) {
494 // FIXME: Clang includes the location of the ';' but only for DeclStmt's
495 // in a for loop's init clause. Need to put this ';' back while removing
496 // the declaration of the alias variable. This is probably a bug.
497 ReplacementText = ";";
Angel Garcia Gomez90bf8952015-10-01 13:08:21 +0000498 } else {
499 // Avoid leaving empty lines or trailing whitespaces.
500 getAliasRange(Context->getSourceManager(), ReplaceRange);
Alexander Kornienko04970842015-08-19 09:11:46 +0000501 }
502
503 Diag << FixItHint::CreateReplacement(
504 CharSourceRange::getTokenRange(ReplaceRange), ReplacementText);
505 // No further replacements are made to the loop, since the iterator or index
506 // was used exactly once - in the initialization of AliasVar.
507 } else {
508 VariableNamer Namer(&TUInfo->getGeneratedDecls(),
509 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000510 Loop, IndexVar, MaybeContainer, Context, NamingStyle);
Alexander Kornienko04970842015-08-19 09:11:46 +0000511 VarName = Namer.createIndexName();
512 // First, replace all usages of the array subscript expression with our new
513 // variable.
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000514 for (const auto &Usage : Usages) {
515 std::string ReplaceText;
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000516 SourceRange Range = Usage.Range;
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000517 if (Usage.Expression) {
518 // If this is an access to a member through the arrow operator, after
519 // the replacement it must be accessed through the '.' operator.
520 ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow ? VarName + "."
521 : VarName;
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000522 auto Parents = Context->getParents(*Usage.Expression);
523 if (Parents.size() == 1) {
524 if (const auto *Paren = Parents[0].get<ParenExpr>()) {
525 // Usage.Expression will be replaced with the new index variable,
526 // and parenthesis around a simple DeclRefExpr can always be
527 // removed.
528 Range = Paren->getSourceRange();
Angel Garcia Gomez199e5232015-10-05 11:15:39 +0000529 } else if (const auto *UOP = Parents[0].get<UnaryOperator>()) {
530 // If we are taking the address of the loop variable, then we must
531 // not use a copy, as it would mean taking the address of the loop's
532 // local index instead.
533 // FIXME: This won't catch cases where the address is taken outside
534 // of the loop's body (for instance, in a function that got the
535 // loop's index as a const reference parameter), or where we take
536 // the address of a member (like "&Arr[i].A.B.C").
537 if (UOP->getOpcode() == UO_AddrOf)
538 CanCopy = false;
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000539 }
540 }
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000541 } else {
542 // The Usage expression is only null in case of lambda captures (which
543 // are VarDecl). If the index is captured by value, add '&' to capture
544 // by reference instead.
545 ReplaceText =
546 Usage.Kind == Usage::UK_CaptureByCopy ? "&" + VarName : VarName;
547 }
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000548 TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar));
Alexander Kornienko04970842015-08-19 09:11:46 +0000549 Diag << FixItHint::CreateReplacement(
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000550 CharSourceRange::getTokenRange(Range), ReplaceText);
Alexander Kornienko04970842015-08-19 09:11:46 +0000551 }
552 }
553
554 // Now, we need to construct the new range expression.
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000555 SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc());
Alexander Kornienko04970842015-08-19 09:11:46 +0000556
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000557 QualType AutoType = Context->getAutoDeductType();
Alexander Kornienko04970842015-08-19 09:11:46 +0000558
559 // If the new variable name is from the aliased variable, then the reference
560 // type for the new variable should only be used if the aliased variable was
561 // declared as a reference.
Angel Garcia Gomez199e5232015-10-05 11:15:39 +0000562 bool UseCopy =
563 CanCopy &&
564 ((VarNameFromAlias && !AliasVarIsRef) ||
565 (Descriptor.DerefByConstRef && Descriptor.IsTriviallyCopyable));
Manuel Klimekb457b682015-09-23 22:28:14 +0000566
567 if (!UseCopy) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000568 if (Descriptor.DerefByConstRef) {
569 AutoType =
570 Context->getLValueReferenceType(Context->getConstType(AutoType));
571 } else if (Descriptor.DerefByValue) {
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000572 if (!Descriptor.IsTriviallyCopyable)
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000573 AutoType = Context->getRValueReferenceType(AutoType);
Alexander Kornienko04970842015-08-19 09:11:46 +0000574 } else {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000575 AutoType = Context->getLValueReferenceType(AutoType);
Alexander Kornienko04970842015-08-19 09:11:46 +0000576 }
577 }
578
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000579 StringRef MaybeDereference = Descriptor.ContainerNeedsDereference ? "*" : "";
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000580 std::string TypeString = AutoType.getAsString();
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000581 std::string Range = ("(" + TypeString + " " + VarName + " : " +
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000582 MaybeDereference + Descriptor.ContainerString + ")")
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000583 .str();
Alexander Kornienko04970842015-08-19 09:11:46 +0000584 Diag << FixItHint::CreateReplacement(
585 CharSourceRange::getTokenRange(ParenRange), Range);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000586 TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName));
Alexander Kornienko04970842015-08-19 09:11:46 +0000587}
588
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000589/// \brief Returns a string which refers to the container iterated over.
590StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
591 const ForStmt *Loop,
592 const Expr *ContainerExpr) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000593 StringRef ContainerString;
594 if (isa<CXXThisExpr>(ContainerExpr->IgnoreParenImpCasts())) {
595 ContainerString = "this";
596 } else {
597 ContainerString =
598 getStringFromRange(Context->getSourceManager(), Context->getLangOpts(),
599 ContainerExpr->getSourceRange());
600 }
601
602 return ContainerString;
603}
604
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000605/// \brief Determines what kind of 'auto' must be used after converting a for
606/// loop that iterates over an array or pseudoarray.
607void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
608 const BoundNodes &Nodes,
609 const Expr *ContainerExpr,
610 const UsageResult &Usages,
611 RangeDescriptor &Descriptor) {
612 // On arrays and pseudoarrays, we must figure out the qualifiers from the
613 // usages.
Manuel Klimekb457b682015-09-23 22:28:14 +0000614 if (usagesAreConst(Usages) ||
615 containerIsConst(ContainerExpr, Descriptor.ContainerNeedsDereference)) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000616 Descriptor.DerefByConstRef = true;
Manuel Klimekb457b682015-09-23 22:28:14 +0000617 }
618 if (usagesReturnRValues(Usages)) {
619 // If the index usages (dereference, subscript, at, ...) return rvalues,
620 // then we should not use a reference, because we need to keep the code
621 // correct if it mutates the returned objects.
622 Descriptor.DerefByValue = true;
623 }
624 // Try to find the type of the elements on the container, to check if
625 // they are trivially copyable.
626 for (const Usage &U : Usages) {
627 if (!U.Expression || U.Expression->getType().isNull())
628 continue;
629 QualType Type = U.Expression->getType().getCanonicalType();
630 if (U.Kind == Usage::UK_MemberThroughArrow) {
631 if (!Type->isPointerType()) {
632 continue;
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000633 }
Manuel Klimekb457b682015-09-23 22:28:14 +0000634 Type = Type->getPointeeType();
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000635 }
Manuel Klimekb457b682015-09-23 22:28:14 +0000636 Descriptor.IsTriviallyCopyable = Type.isTriviallyCopyableType(*Context);
Alexander Kornienko04970842015-08-19 09:11:46 +0000637 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000638}
639
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000640/// \brief Determines what kind of 'auto' must be used after converting an
641/// iterator based for loop.
642void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
643 const BoundNodes &Nodes,
644 RangeDescriptor &Descriptor) {
645 // The matchers for iterator loops provide bound nodes to obtain this
646 // information.
647 const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
648 QualType CanonicalInitVarType = InitVar->getType().getCanonicalType();
649 const auto *DerefByValueType =
650 Nodes.getNodeAs<QualType>(DerefByValueResultName);
651 Descriptor.DerefByValue = DerefByValueType;
Alexander Kornienko04970842015-08-19 09:11:46 +0000652
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000653 if (Descriptor.DerefByValue) {
654 // If the dereference operator returns by value then test for the
655 // canonical const qualification of the init variable type.
656 Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified();
657 Descriptor.IsTriviallyCopyable =
658 DerefByValueType->isTriviallyCopyableType(*Context);
Alexander Kornienko04970842015-08-19 09:11:46 +0000659 } else {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000660 if (const auto *DerefType =
661 Nodes.getNodeAs<QualType>(DerefByRefResultName)) {
662 // A node will only be bound with DerefByRefResultName if we're dealing
663 // with a user-defined iterator type. Test the const qualification of
664 // the reference type.
Manuel Klimeka88ce502015-09-24 00:16:38 +0000665 auto ValueType = DerefType->getNonReferenceType();
Manuel Klimekb457b682015-09-23 22:28:14 +0000666
667 Descriptor.DerefByConstRef = ValueType.isConstQualified();
668 Descriptor.IsTriviallyCopyable =
669 ValueType.isTriviallyCopyableType(*Context);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000670 } else {
671 // By nature of the matcher this case is triggered only for built-in
672 // iterator types (i.e. pointers).
673 assert(isa<PointerType>(CanonicalInitVarType) &&
674 "Non-class iterator type is not a pointer type");
675
676 // We test for const qualification of the pointed-at type.
677 Descriptor.DerefByConstRef =
678 CanonicalInitVarType->getPointeeType().isConstQualified();
Manuel Klimekb457b682015-09-23 22:28:14 +0000679 Descriptor.IsTriviallyCopyable =
680 CanonicalInitVarType->getPointeeType().isTriviallyCopyableType(
681 *Context);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000682 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000683 }
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000684}
685
686/// \brief Determines the parameters needed to build the range replacement.
687void LoopConvertCheck::determineRangeDescriptor(
688 ASTContext *Context, const BoundNodes &Nodes, const ForStmt *Loop,
689 LoopFixerKind FixerKind, const Expr *ContainerExpr,
690 const UsageResult &Usages, RangeDescriptor &Descriptor) {
691 Descriptor.ContainerString = getContainerString(Context, Loop, ContainerExpr);
692
693 if (FixerKind == LFK_Iterator)
694 getIteratorLoopQualifiers(Context, Nodes, Descriptor);
695 else
696 getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor);
697}
698
699/// \brief Check some of the conditions that must be met for the loop to be
700/// convertible.
701bool LoopConvertCheck::isConvertible(ASTContext *Context,
702 const ast_matchers::BoundNodes &Nodes,
703 const ForStmt *Loop,
704 LoopFixerKind FixerKind) {
705 // If we already modified the range of this for loop, don't do any further
706 // updates on this iteration.
707 if (TUInfo->getReplacedVars().count(Loop))
708 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000709
710 // Check that we have exactly one index variable and at most one end variable.
711 const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
712 const auto *CondVar = Nodes.getDeclAs<VarDecl>(ConditionVarName);
713 const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
714 if (!areSameVariable(LoopVar, CondVar) || !areSameVariable(LoopVar, InitVar))
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000715 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000716 const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
717 const auto *ConditionEndVar = Nodes.getDeclAs<VarDecl>(ConditionEndVarName);
718 if (EndVar && !areSameVariable(EndVar, ConditionEndVar))
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000719 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000720
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000721 // FIXME: Try to put most of this logic inside a matcher.
Alexander Kornienko04970842015-08-19 09:11:46 +0000722 if (FixerKind == LFK_Iterator) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000723 QualType InitVarType = InitVar->getType();
724 QualType CanonicalInitVarType = InitVarType.getCanonicalType();
725
726 const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(BeginCallName);
727 assert(BeginCall && "Bad Callback. No begin call expression");
728 QualType CanonicalBeginType =
729 BeginCall->getMethodDecl()->getReturnType().getCanonicalType();
730 if (CanonicalBeginType->isPointerType() &&
731 CanonicalInitVarType->isPointerType()) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000732 // If the initializer and the variable are both pointers check if the
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000733 // un-qualified pointee types match, otherwise we don't use auto.
734 if (!Context->hasSameUnqualifiedType(
735 CanonicalBeginType->getPointeeType(),
736 CanonicalInitVarType->getPointeeType()))
737 return false;
738 } else if (!Context->hasSameType(CanonicalInitVarType,
739 CanonicalBeginType)) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000740 // Check for qualified types to avoid conversions from non-const to const
741 // iterator types.
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000742 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000743 }
744 } else if (FixerKind == LFK_PseudoArray) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000745 // This call is required to obtain the container.
746 const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
747 if (!EndCall || !dyn_cast<MemberExpr>(EndCall->getCallee()))
748 return false;
749 }
750 return true;
751}
752
753void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) {
754 const BoundNodes &Nodes = Result.Nodes;
755 Confidence ConfidenceLevel(Confidence::CL_Safe);
756 ASTContext *Context = Result.Context;
757
758 const ForStmt *Loop;
759 LoopFixerKind FixerKind;
760 RangeDescriptor Descriptor;
761
762 if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameArray))) {
763 FixerKind = LFK_Array;
764 } else if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameIterator))) {
765 FixerKind = LFK_Iterator;
766 } else {
767 Loop = Nodes.getStmtAs<ForStmt>(LoopNamePseudoArray);
768 assert(Loop && "Bad Callback. No for statement");
769 FixerKind = LFK_PseudoArray;
770 }
771
772 if (!isConvertible(Context, Nodes, Loop, FixerKind))
773 return;
774
775 const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
776 const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
777
778 // If the loop calls end()/size() after each iteration, lower our confidence
779 // level.
780 if (FixerKind != LFK_Array && !EndVar)
781 ConfidenceLevel.lowerTo(Confidence::CL_Reasonable);
782
783 // If the end comparison isn't a variable, we can try to work with the
784 // expression the loop variable is being tested against instead.
785 const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
786 const auto *BoundExpr = Nodes.getStmtAs<Expr>(ConditionBoundName);
787
788 // Find container expression of iterators and pseudoarrays, and determine if
789 // this expression needs to be dereferenced to obtain the container.
790 // With array loops, the container is often discovered during the
791 // ForLoopIndexUseVisitor traversal.
792 const Expr *ContainerExpr = nullptr;
793 if (FixerKind == LFK_Iterator) {
794 ContainerExpr = findContainer(Context, LoopVar->getInit(),
795 EndVar ? EndVar->getInit() : EndCall,
796 &Descriptor.ContainerNeedsDereference);
797 } else if (FixerKind == LFK_PseudoArray) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000798 ContainerExpr = EndCall->getImplicitObjectArgument();
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000799 Descriptor.ContainerNeedsDereference =
800 dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow();
Alexander Kornienko04970842015-08-19 09:11:46 +0000801 }
802
803 // We must know the container or an array length bound.
804 if (!ContainerExpr && !BoundExpr)
805 return;
806
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000807 ForLoopIndexUseVisitor Finder(Context, LoopVar, EndVar, ContainerExpr,
808 BoundExpr,
809 Descriptor.ContainerNeedsDereference);
810
811 // Find expressions and variables on which the container depends.
812 if (ContainerExpr) {
813 ComponentFinderASTVisitor ComponentFinder;
814 ComponentFinder.findExprComponents(ContainerExpr->IgnoreParenImpCasts());
815 Finder.addComponents(ComponentFinder.getComponents());
816 }
817
818 // Find usages of the loop index. If they are not used in a convertible way,
819 // stop here.
820 if (!Finder.findAndVerifyUsages(Loop->getBody()))
821 return;
822 ConfidenceLevel.lowerTo(Finder.getConfidenceLevel());
823
824 // Obtain the container expression, if we don't have it yet.
825 if (FixerKind == LFK_Array) {
826 ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts();
827
828 // Very few loops are over expressions that generate arrays rather than
829 // array variables. Consider loops over arrays that aren't just represented
830 // by a variable to be risky conversions.
831 if (!getReferencedVariable(ContainerExpr) &&
832 !isDirectMemberExpr(ContainerExpr))
833 ConfidenceLevel.lowerTo(Confidence::CL_Risky);
834 }
835
836 // Find out which qualifiers we have to use in the loop range.
837 const UsageResult &Usages = Finder.getUsages();
838 determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr,
839 Usages, Descriptor);
840
841 // Ensure that we do not try to move an expression dependent on a local
842 // variable declared inside the loop outside of it.
843 // FIXME: Determine when the external dependency isn't an expression converted
844 // by another loop.
845 TUInfo->getParentFinder().gatherAncestors(Context->getTranslationUnitDecl());
846 DependencyFinderASTVisitor DependencyFinder(
847 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
848 &TUInfo->getParentFinder().getDeclToParentStmtMap(),
849 &TUInfo->getReplacedVars(), Loop);
850
851 if (DependencyFinder.dependsOnInsideVariable(ContainerExpr) ||
852 Descriptor.ContainerString.empty() || Usages.empty() ||
853 ConfidenceLevel.getLevel() < MinConfidence)
Alexander Kornienko04970842015-08-19 09:11:46 +0000854 return;
855
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000856 doConversion(Context, LoopVar, getReferencedVariable(ContainerExpr), Usages,
857 Finder.getAliasDecl(), Finder.aliasUseRequired(),
858 Finder.aliasFromForInit(), Loop, Descriptor);
Alexander Kornienko04970842015-08-19 09:11:46 +0000859}
860
861} // namespace modernize
862} // namespace tidy
863} // namespace clang