blob: 33a2d6da96a25ea76604f5bd427c5826dc7cf7f4 [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
22const char LoopNameArray[] = "forLoopArray";
23const char LoopNameIterator[] = "forLoopIterator";
24const char LoopNamePseudoArray[] = "forLoopPseudoArray";
25const char ConditionBoundName[] = "conditionBound";
26const char ConditionVarName[] = "conditionVar";
27const char IncrementVarName[] = "incrementVar";
28const char InitVarName[] = "initVar";
29const char BeginCallName[] = "beginCall";
30const char EndCallName[] = "endCall";
31const char ConditionEndVarName[] = "conditionEndVar";
32const char EndVarName[] = "endVar";
33const char DerefByValueResultName[] = "derefByValueResult";
34const char DerefByRefResultName[] = "derefByRefResult";
35
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;
480
481 if (VarNameFromAlias) {
482 const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
483 VarName = AliasVar->getName().str();
484 AliasVarIsRef = AliasVar->getType()->isReferenceType();
485
486 // We keep along the entire DeclStmt to keep the correct range here.
Angel Garcia Gomez90bf8952015-10-01 13:08:21 +0000487 SourceRange ReplaceRange = AliasDecl->getSourceRange();
Alexander Kornienko04970842015-08-19 09:11:46 +0000488
489 std::string ReplacementText;
490 if (AliasUseRequired) {
491 ReplacementText = VarName;
492 } else if (AliasFromForInit) {
493 // FIXME: Clang includes the location of the ';' but only for DeclStmt's
494 // in a for loop's init clause. Need to put this ';' back while removing
495 // the declaration of the alias variable. This is probably a bug.
496 ReplacementText = ";";
Angel Garcia Gomez90bf8952015-10-01 13:08:21 +0000497 } else {
498 // Avoid leaving empty lines or trailing whitespaces.
499 getAliasRange(Context->getSourceManager(), ReplaceRange);
Alexander Kornienko04970842015-08-19 09:11:46 +0000500 }
501
502 Diag << FixItHint::CreateReplacement(
503 CharSourceRange::getTokenRange(ReplaceRange), ReplacementText);
504 // No further replacements are made to the loop, since the iterator or index
505 // was used exactly once - in the initialization of AliasVar.
506 } else {
507 VariableNamer Namer(&TUInfo->getGeneratedDecls(),
508 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000509 Loop, IndexVar, MaybeContainer, Context, NamingStyle);
Alexander Kornienko04970842015-08-19 09:11:46 +0000510 VarName = Namer.createIndexName();
511 // First, replace all usages of the array subscript expression with our new
512 // variable.
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000513 for (const auto &Usage : Usages) {
514 std::string ReplaceText;
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000515 SourceRange Range = Usage.Range;
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000516 if (Usage.Expression) {
517 // If this is an access to a member through the arrow operator, after
518 // the replacement it must be accessed through the '.' operator.
519 ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow ? VarName + "."
520 : VarName;
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000521 auto Parents = Context->getParents(*Usage.Expression);
522 if (Parents.size() == 1) {
523 if (const auto *Paren = Parents[0].get<ParenExpr>()) {
524 // Usage.Expression will be replaced with the new index variable,
525 // and parenthesis around a simple DeclRefExpr can always be
526 // removed.
527 Range = Paren->getSourceRange();
528 }
529 }
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000530 } else {
531 // The Usage expression is only null in case of lambda captures (which
532 // are VarDecl). If the index is captured by value, add '&' to capture
533 // by reference instead.
534 ReplaceText =
535 Usage.Kind == Usage::UK_CaptureByCopy ? "&" + VarName : VarName;
536 }
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000537 TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar));
Alexander Kornienko04970842015-08-19 09:11:46 +0000538 Diag << FixItHint::CreateReplacement(
Angel Garcia Gomezbd432b22015-09-24 15:29:46 +0000539 CharSourceRange::getTokenRange(Range), ReplaceText);
Alexander Kornienko04970842015-08-19 09:11:46 +0000540 }
541 }
542
543 // Now, we need to construct the new range expression.
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000544 SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc());
Alexander Kornienko04970842015-08-19 09:11:46 +0000545
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000546 QualType AutoType = Context->getAutoDeductType();
Alexander Kornienko04970842015-08-19 09:11:46 +0000547
548 // If the new variable name is from the aliased variable, then the reference
549 // type for the new variable should only be used if the aliased variable was
550 // declared as a reference.
Manuel Klimekb457b682015-09-23 22:28:14 +0000551 bool UseCopy = (VarNameFromAlias && !AliasVarIsRef) ||
552 (Descriptor.DerefByConstRef && Descriptor.IsTriviallyCopyable);
553
554 if (!UseCopy) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000555 if (Descriptor.DerefByConstRef) {
556 AutoType =
557 Context->getLValueReferenceType(Context->getConstType(AutoType));
558 } else if (Descriptor.DerefByValue) {
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000559 if (!Descriptor.IsTriviallyCopyable)
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000560 AutoType = Context->getRValueReferenceType(AutoType);
Alexander Kornienko04970842015-08-19 09:11:46 +0000561 } else {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000562 AutoType = Context->getLValueReferenceType(AutoType);
Alexander Kornienko04970842015-08-19 09:11:46 +0000563 }
564 }
565
Angel Garcia Gomezd930ef72015-09-08 09:01:21 +0000566 StringRef MaybeDereference = Descriptor.ContainerNeedsDereference ? "*" : "";
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000567 std::string TypeString = AutoType.getAsString();
Alexander Kornienkoe1292f82015-08-19 16:54:51 +0000568 std::string Range = ("(" + TypeString + " " + VarName + " : " +
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000569 MaybeDereference + Descriptor.ContainerString + ")")
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000570 .str();
Alexander Kornienko04970842015-08-19 09:11:46 +0000571 Diag << FixItHint::CreateReplacement(
572 CharSourceRange::getTokenRange(ParenRange), Range);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000573 TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName));
Alexander Kornienko04970842015-08-19 09:11:46 +0000574}
575
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000576/// \brief Returns a string which refers to the container iterated over.
577StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
578 const ForStmt *Loop,
579 const Expr *ContainerExpr) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000580 StringRef ContainerString;
581 if (isa<CXXThisExpr>(ContainerExpr->IgnoreParenImpCasts())) {
582 ContainerString = "this";
583 } else {
584 ContainerString =
585 getStringFromRange(Context->getSourceManager(), Context->getLangOpts(),
586 ContainerExpr->getSourceRange());
587 }
588
589 return ContainerString;
590}
591
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000592/// \brief Determines what kind of 'auto' must be used after converting a for
593/// loop that iterates over an array or pseudoarray.
594void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
595 const BoundNodes &Nodes,
596 const Expr *ContainerExpr,
597 const UsageResult &Usages,
598 RangeDescriptor &Descriptor) {
599 // On arrays and pseudoarrays, we must figure out the qualifiers from the
600 // usages.
Manuel Klimekb457b682015-09-23 22:28:14 +0000601 if (usagesAreConst(Usages) ||
602 containerIsConst(ContainerExpr, Descriptor.ContainerNeedsDereference)) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000603 Descriptor.DerefByConstRef = true;
Manuel Klimekb457b682015-09-23 22:28:14 +0000604 }
605 if (usagesReturnRValues(Usages)) {
606 // If the index usages (dereference, subscript, at, ...) return rvalues,
607 // then we should not use a reference, because we need to keep the code
608 // correct if it mutates the returned objects.
609 Descriptor.DerefByValue = true;
610 }
611 // Try to find the type of the elements on the container, to check if
612 // they are trivially copyable.
613 for (const Usage &U : Usages) {
614 if (!U.Expression || U.Expression->getType().isNull())
615 continue;
616 QualType Type = U.Expression->getType().getCanonicalType();
617 if (U.Kind == Usage::UK_MemberThroughArrow) {
618 if (!Type->isPointerType()) {
619 continue;
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000620 }
Manuel Klimekb457b682015-09-23 22:28:14 +0000621 Type = Type->getPointeeType();
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000622 }
Manuel Klimekb457b682015-09-23 22:28:14 +0000623 Descriptor.IsTriviallyCopyable = Type.isTriviallyCopyableType(*Context);
Alexander Kornienko04970842015-08-19 09:11:46 +0000624 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000625}
626
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000627/// \brief Determines what kind of 'auto' must be used after converting an
628/// iterator based for loop.
629void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
630 const BoundNodes &Nodes,
631 RangeDescriptor &Descriptor) {
632 // The matchers for iterator loops provide bound nodes to obtain this
633 // information.
634 const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
635 QualType CanonicalInitVarType = InitVar->getType().getCanonicalType();
636 const auto *DerefByValueType =
637 Nodes.getNodeAs<QualType>(DerefByValueResultName);
638 Descriptor.DerefByValue = DerefByValueType;
Alexander Kornienko04970842015-08-19 09:11:46 +0000639
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000640 if (Descriptor.DerefByValue) {
641 // If the dereference operator returns by value then test for the
642 // canonical const qualification of the init variable type.
643 Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified();
644 Descriptor.IsTriviallyCopyable =
645 DerefByValueType->isTriviallyCopyableType(*Context);
Alexander Kornienko04970842015-08-19 09:11:46 +0000646 } else {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000647 if (const auto *DerefType =
648 Nodes.getNodeAs<QualType>(DerefByRefResultName)) {
649 // A node will only be bound with DerefByRefResultName if we're dealing
650 // with a user-defined iterator type. Test the const qualification of
651 // the reference type.
Manuel Klimeka88ce502015-09-24 00:16:38 +0000652 auto ValueType = DerefType->getNonReferenceType();
Manuel Klimekb457b682015-09-23 22:28:14 +0000653
654 Descriptor.DerefByConstRef = ValueType.isConstQualified();
655 Descriptor.IsTriviallyCopyable =
656 ValueType.isTriviallyCopyableType(*Context);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000657 } else {
658 // By nature of the matcher this case is triggered only for built-in
659 // iterator types (i.e. pointers).
660 assert(isa<PointerType>(CanonicalInitVarType) &&
661 "Non-class iterator type is not a pointer type");
662
663 // We test for const qualification of the pointed-at type.
664 Descriptor.DerefByConstRef =
665 CanonicalInitVarType->getPointeeType().isConstQualified();
Manuel Klimekb457b682015-09-23 22:28:14 +0000666 Descriptor.IsTriviallyCopyable =
667 CanonicalInitVarType->getPointeeType().isTriviallyCopyableType(
668 *Context);
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000669 }
Alexander Kornienko04970842015-08-19 09:11:46 +0000670 }
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000671}
672
673/// \brief Determines the parameters needed to build the range replacement.
674void LoopConvertCheck::determineRangeDescriptor(
675 ASTContext *Context, const BoundNodes &Nodes, const ForStmt *Loop,
676 LoopFixerKind FixerKind, const Expr *ContainerExpr,
677 const UsageResult &Usages, RangeDescriptor &Descriptor) {
678 Descriptor.ContainerString = getContainerString(Context, Loop, ContainerExpr);
679
680 if (FixerKind == LFK_Iterator)
681 getIteratorLoopQualifiers(Context, Nodes, Descriptor);
682 else
683 getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor);
684}
685
686/// \brief Check some of the conditions that must be met for the loop to be
687/// convertible.
688bool LoopConvertCheck::isConvertible(ASTContext *Context,
689 const ast_matchers::BoundNodes &Nodes,
690 const ForStmt *Loop,
691 LoopFixerKind FixerKind) {
692 // If we already modified the range of this for loop, don't do any further
693 // updates on this iteration.
694 if (TUInfo->getReplacedVars().count(Loop))
695 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000696
697 // Check that we have exactly one index variable and at most one end variable.
698 const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
699 const auto *CondVar = Nodes.getDeclAs<VarDecl>(ConditionVarName);
700 const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
701 if (!areSameVariable(LoopVar, CondVar) || !areSameVariable(LoopVar, InitVar))
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000702 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000703 const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
704 const auto *ConditionEndVar = Nodes.getDeclAs<VarDecl>(ConditionEndVarName);
705 if (EndVar && !areSameVariable(EndVar, ConditionEndVar))
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000706 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000707
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000708 // FIXME: Try to put most of this logic inside a matcher.
Alexander Kornienko04970842015-08-19 09:11:46 +0000709 if (FixerKind == LFK_Iterator) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000710 QualType InitVarType = InitVar->getType();
711 QualType CanonicalInitVarType = InitVarType.getCanonicalType();
712
713 const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(BeginCallName);
714 assert(BeginCall && "Bad Callback. No begin call expression");
715 QualType CanonicalBeginType =
716 BeginCall->getMethodDecl()->getReturnType().getCanonicalType();
717 if (CanonicalBeginType->isPointerType() &&
718 CanonicalInitVarType->isPointerType()) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000719 // If the initializer and the variable are both pointers check if the
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000720 // un-qualified pointee types match, otherwise we don't use auto.
721 if (!Context->hasSameUnqualifiedType(
722 CanonicalBeginType->getPointeeType(),
723 CanonicalInitVarType->getPointeeType()))
724 return false;
725 } else if (!Context->hasSameType(CanonicalInitVarType,
726 CanonicalBeginType)) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000727 // Check for qualified types to avoid conversions from non-const to const
728 // iterator types.
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000729 return false;
Alexander Kornienko04970842015-08-19 09:11:46 +0000730 }
731 } else if (FixerKind == LFK_PseudoArray) {
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000732 // This call is required to obtain the container.
733 const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
734 if (!EndCall || !dyn_cast<MemberExpr>(EndCall->getCallee()))
735 return false;
736 }
737 return true;
738}
739
740void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) {
741 const BoundNodes &Nodes = Result.Nodes;
742 Confidence ConfidenceLevel(Confidence::CL_Safe);
743 ASTContext *Context = Result.Context;
744
745 const ForStmt *Loop;
746 LoopFixerKind FixerKind;
747 RangeDescriptor Descriptor;
748
749 if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameArray))) {
750 FixerKind = LFK_Array;
751 } else if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameIterator))) {
752 FixerKind = LFK_Iterator;
753 } else {
754 Loop = Nodes.getStmtAs<ForStmt>(LoopNamePseudoArray);
755 assert(Loop && "Bad Callback. No for statement");
756 FixerKind = LFK_PseudoArray;
757 }
758
759 if (!isConvertible(Context, Nodes, Loop, FixerKind))
760 return;
761
762 const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
763 const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
764
765 // If the loop calls end()/size() after each iteration, lower our confidence
766 // level.
767 if (FixerKind != LFK_Array && !EndVar)
768 ConfidenceLevel.lowerTo(Confidence::CL_Reasonable);
769
770 // If the end comparison isn't a variable, we can try to work with the
771 // expression the loop variable is being tested against instead.
772 const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
773 const auto *BoundExpr = Nodes.getStmtAs<Expr>(ConditionBoundName);
774
775 // Find container expression of iterators and pseudoarrays, and determine if
776 // this expression needs to be dereferenced to obtain the container.
777 // With array loops, the container is often discovered during the
778 // ForLoopIndexUseVisitor traversal.
779 const Expr *ContainerExpr = nullptr;
780 if (FixerKind == LFK_Iterator) {
781 ContainerExpr = findContainer(Context, LoopVar->getInit(),
782 EndVar ? EndVar->getInit() : EndCall,
783 &Descriptor.ContainerNeedsDereference);
784 } else if (FixerKind == LFK_PseudoArray) {
Alexander Kornienko04970842015-08-19 09:11:46 +0000785 ContainerExpr = EndCall->getImplicitObjectArgument();
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000786 Descriptor.ContainerNeedsDereference =
787 dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow();
Alexander Kornienko04970842015-08-19 09:11:46 +0000788 }
789
790 // We must know the container or an array length bound.
791 if (!ContainerExpr && !BoundExpr)
792 return;
793
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000794 ForLoopIndexUseVisitor Finder(Context, LoopVar, EndVar, ContainerExpr,
795 BoundExpr,
796 Descriptor.ContainerNeedsDereference);
797
798 // Find expressions and variables on which the container depends.
799 if (ContainerExpr) {
800 ComponentFinderASTVisitor ComponentFinder;
801 ComponentFinder.findExprComponents(ContainerExpr->IgnoreParenImpCasts());
802 Finder.addComponents(ComponentFinder.getComponents());
803 }
804
805 // Find usages of the loop index. If they are not used in a convertible way,
806 // stop here.
807 if (!Finder.findAndVerifyUsages(Loop->getBody()))
808 return;
809 ConfidenceLevel.lowerTo(Finder.getConfidenceLevel());
810
811 // Obtain the container expression, if we don't have it yet.
812 if (FixerKind == LFK_Array) {
813 ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts();
814
815 // Very few loops are over expressions that generate arrays rather than
816 // array variables. Consider loops over arrays that aren't just represented
817 // by a variable to be risky conversions.
818 if (!getReferencedVariable(ContainerExpr) &&
819 !isDirectMemberExpr(ContainerExpr))
820 ConfidenceLevel.lowerTo(Confidence::CL_Risky);
821 }
822
823 // Find out which qualifiers we have to use in the loop range.
824 const UsageResult &Usages = Finder.getUsages();
825 determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr,
826 Usages, Descriptor);
827
828 // Ensure that we do not try to move an expression dependent on a local
829 // variable declared inside the loop outside of it.
830 // FIXME: Determine when the external dependency isn't an expression converted
831 // by another loop.
832 TUInfo->getParentFinder().gatherAncestors(Context->getTranslationUnitDecl());
833 DependencyFinderASTVisitor DependencyFinder(
834 &TUInfo->getParentFinder().getStmtToParentStmtMap(),
835 &TUInfo->getParentFinder().getDeclToParentStmtMap(),
836 &TUInfo->getReplacedVars(), Loop);
837
838 if (DependencyFinder.dependsOnInsideVariable(ContainerExpr) ||
839 Descriptor.ContainerString.empty() || Usages.empty() ||
840 ConfidenceLevel.getLevel() < MinConfidence)
Alexander Kornienko04970842015-08-19 09:11:46 +0000841 return;
842
Angel Garcia Gomezf41a6312015-09-21 09:32:59 +0000843 doConversion(Context, LoopVar, getReferencedVariable(ContainerExpr), Usages,
844 Finder.getAliasDecl(), Finder.aliasUseRequired(),
845 Finder.aliasFromForInit(), Loop, Descriptor);
Alexander Kornienko04970842015-08-19 09:11:46 +0000846}
847
848} // namespace modernize
849} // namespace tidy
850} // namespace clang