clang-tools  3.8.0
LoopConvertCheck.cpp
Go to the documentation of this file.
1 //===--- 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 
14 using namespace clang;
15 using namespace clang::ast_matchers;
16 using namespace llvm;
17 
18 namespace clang {
19 namespace tidy {
20 namespace modernize {
21 
22 static const char LoopNameArray[] = "forLoopArray";
23 static const char LoopNameIterator[] = "forLoopIterator";
24 static const char LoopNamePseudoArray[] = "forLoopPseudoArray";
25 static const char ConditionBoundName[] = "conditionBound";
26 static const char ConditionVarName[] = "conditionVar";
27 static const char IncrementVarName[] = "incrementVar";
28 static const char InitVarName[] = "initVar";
29 static const char BeginCallName[] = "beginCall";
30 static const char EndCallName[] = "endCall";
31 static const char ConditionEndVarName[] = "conditionEndVar";
32 static const char EndVarName[] = "endVar";
33 static const char DerefByValueResultName[] = "derefByValueResult";
34 static const char DerefByRefResultName[] = "derefByRefResult";
35 
36 // shared matchers
37 static const TypeMatcher AnyType = anything();
38 
39 static const StatementMatcher IntegerComparisonMatcher =
40  expr(ignoringParenImpCasts(
41  declRefExpr(to(varDecl(hasType(isInteger())).bind(ConditionVarName)))));
42 
43 static const DeclarationMatcher InitToZeroMatcher =
44  varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral(equals(0)))))
45  .bind(InitVarName);
46 
47 static 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.
68 StatementMatcher makeArrayLoopMatcher() {
69  StatementMatcher ArrayBoundMatcher =
70  expr(hasType(isInteger())).bind(ConditionBoundName);
71 
72  return forStmt(
73  unless(isInTemplateInstantiation()),
74  hasLoopInit(declStmt(hasSingleDecl(InitToZeroMatcher))),
75  hasCondition(anyOf(
76  binaryOperator(hasOperatorName("<"),
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'.
113 StatementMatcher makeIteratorLoopMatcher() {
114  StatementMatcher BeginCallMatcher =
115  cxxMemberCallExpr(
116  argumentCountIs(0),
117  callee(cxxMethodDecl(anyOf(hasName("begin"), hasName("cbegin")))))
118  .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 
130  StatementMatcher EndCallMatcher = cxxMemberCallExpr(
131  argumentCountIs(0),
132  callee(cxxMethodDecl(anyOf(hasName("end"), hasName("cend")))));
133 
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 =
145  cxxOperatorCallExpr(hasOverloadedOperatorName("!="), argumentCountIs(2),
146  hasArgument(0, IteratorComparisonMatcher),
147  hasArgument(1, IteratorBoundMatcher));
148 
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 =
153  hasType(cxxRecordDecl(hasMethod(allOf(
154  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(
166  unless(isInTemplateInstantiation()),
167  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))))),
184  cxxOperatorCallExpr(
185  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.
220 StatementMatcher 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(anyOf(
234  qualType(isConstQualified(),
235  hasDeclaration(cxxRecordDecl(
236  hasMethod(cxxMethodDecl(hasName("begin"), isConst())),
237  hasMethod(cxxMethodDecl(hasName("end"),
238  isConst())))) // hasDeclaration
239  ), // qualType
240  qualType(
241  unless(isConstQualified()),
242  hasDeclaration(cxxRecordDecl(hasMethod(hasName("begin")),
243  hasMethod(hasName("end"))))) // qualType
244  ));
245 
246  StatementMatcher SizeCallMatcher = cxxMemberCallExpr(
247  argumentCountIs(0),
248  callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))),
249  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(
266  unless(isInTemplateInstantiation()),
267  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 ->.
288 static 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";
301  StringRef ConstTargetName = IsBegin ? "cbegin" : "cend";
302  if (Name != TargetName && Name != ConstTargetName)
303  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.
318 static 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.
343 static StringRef getStringFromRange(SourceManager &SourceMgr,
344  const LangOptions &LangOpts,
345  SourceRange Range) {
346  if (SourceMgr.getFileID(Range.getBegin()) !=
347  SourceMgr.getFileID(Range.getEnd())) {
348  return StringRef(); // Empty string.
349  }
350 
351  return Lexer::getSourceText(CharSourceRange(Range, true), SourceMgr,
352  LangOpts);
353 }
354 
355 /// \brief If the given expression is actually a DeclRefExpr or a MemberExpr,
356 /// find and return the underlying ValueDecl; otherwise, return NULL.
357 static const ValueDecl *getReferencedVariable(const Expr *E) {
358  if (const DeclRefExpr *DRE = getDeclRef(E))
359  return dyn_cast<VarDecl>(DRE->getDecl());
360  if (const auto *Mem = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts()))
361  return dyn_cast<FieldDecl>(Mem->getMemberDecl());
362  return nullptr;
363 }
364 
365 /// \brief Returns true when the given expression is a member expression
366 /// whose base is `this` (implicitly or not).
367 static bool isDirectMemberExpr(const Expr *E) {
368  if (const auto *Member = dyn_cast<MemberExpr>(E->IgnoreParenImpCasts()))
369  return isa<CXXThisExpr>(Member->getBase()->IgnoreParenImpCasts());
370  return false;
371 }
372 
373 /// \brief Given an expression that represents an usage of an element from the
374 /// containter that we are iterating over, returns false when it can be
375 /// guaranteed this element cannot be modified as a result of this usage.
376 static bool canBeModified(ASTContext *Context, const Expr *E) {
377  if (E->getType().isConstQualified())
378  return false;
379  auto Parents = Context->getParents(*E);
380  if (Parents.size() != 1)
381  return true;
382  if (const auto *Cast = Parents[0].get<ImplicitCastExpr>()) {
383  if ((Cast->getCastKind() == CK_NoOp &&
384  Cast->getType() == E->getType().withConst()) ||
385  (Cast->getCastKind() == CK_LValueToRValue &&
386  !Cast->getType().isNull() && Cast->getType()->isFundamentalType()))
387  return false;
388  }
389  // FIXME: Make this function more generic.
390  return true;
391 }
392 
393 /// \brief Returns true when it can be guaranteed that the elements of the
394 /// container are not being modified.
395 static bool usagesAreConst(ASTContext *Context, const UsageResult &Usages) {
396  for (const Usage &U : Usages) {
397  // Lambda captures are just redeclarations (VarDecl) of the same variable,
398  // not expressions. If we want to know if a variable that is captured by
399  // reference can be modified in an usage inside the lambda's body, we need
400  // to find the expression corresponding to that particular usage, later in
401  // this loop.
402  if (U.Kind != Usage::UK_CaptureByCopy && U.Kind != Usage::UK_CaptureByRef &&
403  canBeModified(Context, U.Expression))
404  return false;
405  }
406  return true;
407 }
408 
409 /// \brief Returns true if the elements of the container are never accessed
410 /// by reference.
411 static bool usagesReturnRValues(const UsageResult &Usages) {
412  for (const auto &U : Usages) {
413  if (U.Expression && !U.Expression->isRValue())
414  return false;
415  }
416  return true;
417 }
418 
419 /// \brief Returns true if the container is const-qualified.
420 static bool containerIsConst(const Expr *ContainerExpr, bool Dereference) {
421  if (const auto *VDec = getReferencedVariable(ContainerExpr)) {
422  QualType CType = VDec->getType();
423  if (Dereference) {
424  if (!CType->isPointerType())
425  return false;
426  CType = CType->getPointeeType();
427  }
428  // If VDec is a reference to a container, Dereference is false,
429  // but we still need to check the const-ness of the underlying container
430  // type.
431  CType = CType.getNonReferenceType();
432  return CType.isConstQualified();
433  }
434  return false;
435 }
436 
437 LoopConvertCheck::RangeDescriptor::RangeDescriptor()
438  : ContainerNeedsDereference(false), DerefByConstRef(false),
439  DerefByValue(false) {}
440 
441 LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
442  : ClangTidyCheck(Name, Context), TUInfo(new TUTrackingInfo),
443  MaxCopySize(std::stoull(Options.get("MaxCopySize", "16"))),
444  MinConfidence(StringSwitch<Confidence::Level>(
445  Options.get("MinConfidence", "reasonable"))
446  .Case("safe", Confidence::CL_Safe)
447  .Case("risky", Confidence::CL_Risky)
448  .Default(Confidence::CL_Reasonable)),
449  NamingStyle(StringSwitch<VariableNamer::NamingStyle>(
450  Options.get("NamingStyle", "CamelCase"))
451  .Case("camelBack", VariableNamer::NS_CamelBack)
452  .Case("lower_case", VariableNamer::NS_LowerCase)
453  .Case("UPPER_CASE", VariableNamer::NS_UpperCase)
454  .Default(VariableNamer::NS_CamelCase)) {}
455 
457  Options.store(Opts, "MaxCopySize", std::to_string(MaxCopySize));
458  SmallVector<std::string, 3> Confs{"risky", "reasonable", "safe"};
459  Options.store(Opts, "MinConfidence", Confs[static_cast<int>(MinConfidence)]);
460 
461  SmallVector<std::string, 4> Styles{"camelBack", "CamelCase", "lower_case",
462  "UPPER_CASE"};
463  Options.store(Opts, "NamingStyle", Styles[static_cast<int>(NamingStyle)]);
464 }
465 
467  // Only register the matchers for C++. Because this checker is used for
468  // modernization, it is reasonable to run it on any C++ standard with the
469  // assumption the user is trying to modernize their codebase.
470  if (!getLangOpts().CPlusPlus)
471  return;
472 
473  Finder->addMatcher(makeArrayLoopMatcher(), this);
474  Finder->addMatcher(makeIteratorLoopMatcher(), this);
475  Finder->addMatcher(makePseudoArrayLoopMatcher(), this);
476 }
477 
478 /// \brief Given the range of a single declaration, such as:
479 /// \code
480 /// unsigned &ThisIsADeclarationThatCanSpanSeveralLinesOfCode =
481 /// InitializationValues[I];
482 /// next_instruction;
483 /// \endcode
484 /// Finds the range that has to be erased to remove this declaration without
485 /// leaving empty lines, by extending the range until the beginning of the
486 /// next instruction.
487 ///
488 /// We need to delete a potential newline after the deleted alias, as
489 /// clang-format will leave empty lines untouched. For all other formatting we
490 /// rely on clang-format to fix it.
491 void LoopConvertCheck::getAliasRange(SourceManager &SM, SourceRange &Range) {
492  bool Invalid = false;
493  const char *TextAfter =
494  SM.getCharacterData(Range.getEnd().getLocWithOffset(1), &Invalid);
495  if (Invalid)
496  return;
497  unsigned Offset = std::strspn(TextAfter, " \t\r\n");
498  Range =
499  SourceRange(Range.getBegin(), Range.getEnd().getLocWithOffset(Offset));
500 }
501 
502 /// \brief Computes the changes needed to convert a given for loop, and
503 /// applies them.
504 void LoopConvertCheck::doConversion(
505  ASTContext *Context, const VarDecl *IndexVar,
506  const ValueDecl *MaybeContainer, const UsageResult &Usages,
507  const DeclStmt *AliasDecl, bool AliasUseRequired, bool AliasFromForInit,
508  const ForStmt *Loop, RangeDescriptor Descriptor) {
509  auto Diag = diag(Loop->getForLoc(), "use range-based for loop instead");
510 
511  std::string VarName;
512  bool VarNameFromAlias = (Usages.size() == 1) && AliasDecl;
513  bool AliasVarIsRef = false;
514  bool CanCopy = true;
515 
516  if (VarNameFromAlias) {
517  const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
518  VarName = AliasVar->getName().str();
519  AliasVarIsRef = AliasVar->getType()->isReferenceType();
520 
521  // We keep along the entire DeclStmt to keep the correct range here.
522  SourceRange ReplaceRange = AliasDecl->getSourceRange();
523 
524  std::string ReplacementText;
525  if (AliasUseRequired) {
526  ReplacementText = VarName;
527  } else if (AliasFromForInit) {
528  // FIXME: Clang includes the location of the ';' but only for DeclStmt's
529  // in a for loop's init clause. Need to put this ';' back while removing
530  // the declaration of the alias variable. This is probably a bug.
531  ReplacementText = ";";
532  } else {
533  // Avoid leaving empty lines or trailing whitespaces.
534  getAliasRange(Context->getSourceManager(), ReplaceRange);
535  }
536 
537  Diag << FixItHint::CreateReplacement(
538  CharSourceRange::getTokenRange(ReplaceRange), ReplacementText);
539  // No further replacements are made to the loop, since the iterator or index
540  // was used exactly once - in the initialization of AliasVar.
541  } else {
542  VariableNamer Namer(&TUInfo->getGeneratedDecls(),
543  &TUInfo->getParentFinder().getStmtToParentStmtMap(),
544  Loop, IndexVar, MaybeContainer, Context, NamingStyle);
545  VarName = Namer.createIndexName();
546  // First, replace all usages of the array subscript expression with our new
547  // variable.
548  for (const auto &Usage : Usages) {
549  std::string ReplaceText;
550  SourceRange Range = Usage.Range;
551  if (Usage.Expression) {
552  // If this is an access to a member through the arrow operator, after
553  // the replacement it must be accessed through the '.' operator.
554  ReplaceText = Usage.Kind == Usage::UK_MemberThroughArrow ? VarName + "."
555  : VarName;
556  auto Parents = Context->getParents(*Usage.Expression);
557  if (Parents.size() == 1) {
558  if (const auto *Paren = Parents[0].get<ParenExpr>()) {
559  // Usage.Expression will be replaced with the new index variable,
560  // and parenthesis around a simple DeclRefExpr can always be
561  // removed.
562  Range = Paren->getSourceRange();
563  } else if (const auto *UOP = Parents[0].get<UnaryOperator>()) {
564  // If we are taking the address of the loop variable, then we must
565  // not use a copy, as it would mean taking the address of the loop's
566  // local index instead.
567  // FIXME: This won't catch cases where the address is taken outside
568  // of the loop's body (for instance, in a function that got the
569  // loop's index as a const reference parameter), or where we take
570  // the address of a member (like "&Arr[i].A.B.C").
571  if (UOP->getOpcode() == UO_AddrOf)
572  CanCopy = false;
573  }
574  }
575  } else {
576  // The Usage expression is only null in case of lambda captures (which
577  // are VarDecl). If the index is captured by value, add '&' to capture
578  // by reference instead.
579  ReplaceText =
580  Usage.Kind == Usage::UK_CaptureByCopy ? "&" + VarName : VarName;
581  }
582  TUInfo->getReplacedVars().insert(std::make_pair(Loop, IndexVar));
583  Diag << FixItHint::CreateReplacement(
584  CharSourceRange::getTokenRange(Range), ReplaceText);
585  }
586  }
587 
588  // Now, we need to construct the new range expression.
589  SourceRange ParenRange(Loop->getLParenLoc(), Loop->getRParenLoc());
590 
591  QualType Type = Context->getAutoDeductType();
592  if (!Descriptor.ElemType.isNull() && Descriptor.ElemType->isFundamentalType())
593  Type = Descriptor.ElemType.getUnqualifiedType();
594 
595  // If the new variable name is from the aliased variable, then the reference
596  // type for the new variable should only be used if the aliased variable was
597  // declared as a reference.
598  bool IsCheapToCopy =
599  !Descriptor.ElemType.isNull() &&
600  Descriptor.ElemType.isTriviallyCopyableType(*Context) &&
601  // TypeInfo::Width is in bits.
602  Context->getTypeInfo(Descriptor.ElemType).Width <= 8 * MaxCopySize;
603  bool UseCopy = CanCopy && ((VarNameFromAlias && !AliasVarIsRef) ||
604  (Descriptor.DerefByConstRef && IsCheapToCopy));
605 
606  if (!UseCopy) {
607  if (Descriptor.DerefByConstRef) {
608  Type = Context->getLValueReferenceType(Context->getConstType(Type));
609  } else if (Descriptor.DerefByValue) {
610  if (!IsCheapToCopy)
611  Type = Context->getRValueReferenceType(Type);
612  } else {
613  Type = Context->getLValueReferenceType(Type);
614  }
615  }
616 
617  StringRef MaybeDereference = Descriptor.ContainerNeedsDereference ? "*" : "";
618  std::string TypeString = Type.getAsString(getLangOpts());
619  std::string Range = ("(" + TypeString + " " + VarName + " : " +
620  MaybeDereference + Descriptor.ContainerString + ")")
621  .str();
622  Diag << FixItHint::CreateReplacement(
623  CharSourceRange::getTokenRange(ParenRange), Range);
624  TUInfo->getGeneratedDecls().insert(make_pair(Loop, VarName));
625 }
626 
627 /// \brief Returns a string which refers to the container iterated over.
628 StringRef LoopConvertCheck::getContainerString(ASTContext *Context,
629  const ForStmt *Loop,
630  const Expr *ContainerExpr) {
631  StringRef ContainerString;
632  if (isa<CXXThisExpr>(ContainerExpr->IgnoreParenImpCasts())) {
633  ContainerString = "this";
634  } else {
635  ContainerString =
636  getStringFromRange(Context->getSourceManager(), Context->getLangOpts(),
637  ContainerExpr->getSourceRange());
638  }
639 
640  return ContainerString;
641 }
642 
643 /// \brief Determines what kind of 'auto' must be used after converting a for
644 /// loop that iterates over an array or pseudoarray.
645 void LoopConvertCheck::getArrayLoopQualifiers(ASTContext *Context,
646  const BoundNodes &Nodes,
647  const Expr *ContainerExpr,
648  const UsageResult &Usages,
649  RangeDescriptor &Descriptor) {
650  // On arrays and pseudoarrays, we must figure out the qualifiers from the
651  // usages.
652  if (usagesAreConst(Context, Usages) ||
653  containerIsConst(ContainerExpr, Descriptor.ContainerNeedsDereference)) {
654  Descriptor.DerefByConstRef = true;
655  }
656  if (usagesReturnRValues(Usages)) {
657  // If the index usages (dereference, subscript, at, ...) return rvalues,
658  // then we should not use a reference, because we need to keep the code
659  // correct if it mutates the returned objects.
660  Descriptor.DerefByValue = true;
661  }
662  // Try to find the type of the elements on the container, to check if
663  // they are trivially copyable.
664  for (const Usage &U : Usages) {
665  if (!U.Expression || U.Expression->getType().isNull())
666  continue;
667  QualType Type = U.Expression->getType().getCanonicalType();
668  if (U.Kind == Usage::UK_MemberThroughArrow) {
669  if (!Type->isPointerType()) {
670  continue;
671  }
672  Type = Type->getPointeeType();
673  }
674  Descriptor.ElemType = Type;
675  }
676 }
677 
678 /// \brief Determines what kind of 'auto' must be used after converting an
679 /// iterator based for loop.
680 void LoopConvertCheck::getIteratorLoopQualifiers(ASTContext *Context,
681  const BoundNodes &Nodes,
682  RangeDescriptor &Descriptor) {
683  // The matchers for iterator loops provide bound nodes to obtain this
684  // information.
685  const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
686  QualType CanonicalInitVarType = InitVar->getType().getCanonicalType();
687  const auto *DerefByValueType =
688  Nodes.getNodeAs<QualType>(DerefByValueResultName);
689  Descriptor.DerefByValue = DerefByValueType;
690 
691  if (Descriptor.DerefByValue) {
692  // If the dereference operator returns by value then test for the
693  // canonical const qualification of the init variable type.
694  Descriptor.DerefByConstRef = CanonicalInitVarType.isConstQualified();
695  Descriptor.ElemType = *DerefByValueType;
696  } else {
697  if (const auto *DerefType =
698  Nodes.getNodeAs<QualType>(DerefByRefResultName)) {
699  // A node will only be bound with DerefByRefResultName if we're dealing
700  // with a user-defined iterator type. Test the const qualification of
701  // the reference type.
702  auto ValueType = DerefType->getNonReferenceType();
703 
704  Descriptor.DerefByConstRef = ValueType.isConstQualified();
705  Descriptor.ElemType = ValueType;
706  } else {
707  // By nature of the matcher this case is triggered only for built-in
708  // iterator types (i.e. pointers).
709  assert(isa<PointerType>(CanonicalInitVarType) &&
710  "Non-class iterator type is not a pointer type");
711 
712  // We test for const qualification of the pointed-at type.
713  Descriptor.DerefByConstRef =
714  CanonicalInitVarType->getPointeeType().isConstQualified();
715  Descriptor.ElemType = CanonicalInitVarType->getPointeeType();
716  }
717  }
718 }
719 
720 /// \brief Determines the parameters needed to build the range replacement.
721 void LoopConvertCheck::determineRangeDescriptor(
722  ASTContext *Context, const BoundNodes &Nodes, const ForStmt *Loop,
723  LoopFixerKind FixerKind, const Expr *ContainerExpr,
724  const UsageResult &Usages, RangeDescriptor &Descriptor) {
725  Descriptor.ContainerString = getContainerString(Context, Loop, ContainerExpr);
726 
727  if (FixerKind == LFK_Iterator)
728  getIteratorLoopQualifiers(Context, Nodes, Descriptor);
729  else
730  getArrayLoopQualifiers(Context, Nodes, ContainerExpr, Usages, Descriptor);
731 }
732 
733 /// \brief Check some of the conditions that must be met for the loop to be
734 /// convertible.
735 bool LoopConvertCheck::isConvertible(ASTContext *Context,
736  const ast_matchers::BoundNodes &Nodes,
737  const ForStmt *Loop,
738  LoopFixerKind FixerKind) {
739  // If we already modified the range of this for loop, don't do any further
740  // updates on this iteration.
741  if (TUInfo->getReplacedVars().count(Loop))
742  return false;
743 
744  // Check that we have exactly one index variable and at most one end variable.
745  const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
746  const auto *CondVar = Nodes.getDeclAs<VarDecl>(ConditionVarName);
747  const auto *InitVar = Nodes.getDeclAs<VarDecl>(InitVarName);
748  if (!areSameVariable(LoopVar, CondVar) || !areSameVariable(LoopVar, InitVar))
749  return false;
750  const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
751  const auto *ConditionEndVar = Nodes.getDeclAs<VarDecl>(ConditionEndVarName);
752  if (EndVar && !areSameVariable(EndVar, ConditionEndVar))
753  return false;
754 
755  // FIXME: Try to put most of this logic inside a matcher.
756  if (FixerKind == LFK_Iterator) {
757  QualType InitVarType = InitVar->getType();
758  QualType CanonicalInitVarType = InitVarType.getCanonicalType();
759 
760  const auto *BeginCall = Nodes.getNodeAs<CXXMemberCallExpr>(BeginCallName);
761  assert(BeginCall && "Bad Callback. No begin call expression");
762  QualType CanonicalBeginType =
763  BeginCall->getMethodDecl()->getReturnType().getCanonicalType();
764  if (CanonicalBeginType->isPointerType() &&
765  CanonicalInitVarType->isPointerType()) {
766  // If the initializer and the variable are both pointers check if the
767  // un-qualified pointee types match, otherwise we don't use auto.
768  if (!Context->hasSameUnqualifiedType(
769  CanonicalBeginType->getPointeeType(),
770  CanonicalInitVarType->getPointeeType()))
771  return false;
772  } else if (!Context->hasSameType(CanonicalInitVarType,
773  CanonicalBeginType)) {
774  // Check for qualified types to avoid conversions from non-const to const
775  // iterator types.
776  return false;
777  }
778  } else if (FixerKind == LFK_PseudoArray) {
779  // This call is required to obtain the container.
780  const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
781  if (!EndCall || !dyn_cast<MemberExpr>(EndCall->getCallee()))
782  return false;
783  }
784  return true;
785 }
786 
787 void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) {
788  const BoundNodes &Nodes = Result.Nodes;
789  Confidence ConfidenceLevel(Confidence::CL_Safe);
790  ASTContext *Context = Result.Context;
791 
792  const ForStmt *Loop;
793  LoopFixerKind FixerKind;
794  RangeDescriptor Descriptor;
795 
796  if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameArray))) {
797  FixerKind = LFK_Array;
798  } else if ((Loop = Nodes.getStmtAs<ForStmt>(LoopNameIterator))) {
799  FixerKind = LFK_Iterator;
800  } else {
801  Loop = Nodes.getStmtAs<ForStmt>(LoopNamePseudoArray);
802  assert(Loop && "Bad Callback. No for statement");
803  FixerKind = LFK_PseudoArray;
804  }
805 
806  if (!isConvertible(Context, Nodes, Loop, FixerKind))
807  return;
808 
809  const auto *LoopVar = Nodes.getDeclAs<VarDecl>(IncrementVarName);
810  const auto *EndVar = Nodes.getDeclAs<VarDecl>(EndVarName);
811 
812  // If the loop calls end()/size() after each iteration, lower our confidence
813  // level.
814  if (FixerKind != LFK_Array && !EndVar)
815  ConfidenceLevel.lowerTo(Confidence::CL_Reasonable);
816 
817  // If the end comparison isn't a variable, we can try to work with the
818  // expression the loop variable is being tested against instead.
819  const auto *EndCall = Nodes.getStmtAs<CXXMemberCallExpr>(EndCallName);
820  const auto *BoundExpr = Nodes.getStmtAs<Expr>(ConditionBoundName);
821 
822  // Find container expression of iterators and pseudoarrays, and determine if
823  // this expression needs to be dereferenced to obtain the container.
824  // With array loops, the container is often discovered during the
825  // ForLoopIndexUseVisitor traversal.
826  const Expr *ContainerExpr = nullptr;
827  if (FixerKind == LFK_Iterator) {
828  ContainerExpr = findContainer(Context, LoopVar->getInit(),
829  EndVar ? EndVar->getInit() : EndCall,
830  &Descriptor.ContainerNeedsDereference);
831  } else if (FixerKind == LFK_PseudoArray) {
832  ContainerExpr = EndCall->getImplicitObjectArgument();
833  Descriptor.ContainerNeedsDereference =
834  dyn_cast<MemberExpr>(EndCall->getCallee())->isArrow();
835  }
836 
837  // We must know the container or an array length bound.
838  if (!ContainerExpr && !BoundExpr)
839  return;
840 
841  ForLoopIndexUseVisitor Finder(Context, LoopVar, EndVar, ContainerExpr,
842  BoundExpr,
843  Descriptor.ContainerNeedsDereference);
844 
845  // Find expressions and variables on which the container depends.
846  if (ContainerExpr) {
847  ComponentFinderASTVisitor ComponentFinder;
848  ComponentFinder.findExprComponents(ContainerExpr->IgnoreParenImpCasts());
849  Finder.addComponents(ComponentFinder.getComponents());
850  }
851 
852  // Find usages of the loop index. If they are not used in a convertible way,
853  // stop here.
854  if (!Finder.findAndVerifyUsages(Loop->getBody()))
855  return;
856  ConfidenceLevel.lowerTo(Finder.getConfidenceLevel());
857 
858  // Obtain the container expression, if we don't have it yet.
859  if (FixerKind == LFK_Array) {
860  ContainerExpr = Finder.getContainerIndexed()->IgnoreParenImpCasts();
861 
862  // Very few loops are over expressions that generate arrays rather than
863  // array variables. Consider loops over arrays that aren't just represented
864  // by a variable to be risky conversions.
865  if (!getReferencedVariable(ContainerExpr) &&
866  !isDirectMemberExpr(ContainerExpr))
867  ConfidenceLevel.lowerTo(Confidence::CL_Risky);
868  }
869 
870  // Find out which qualifiers we have to use in the loop range.
871  const UsageResult &Usages = Finder.getUsages();
872  determineRangeDescriptor(Context, Nodes, Loop, FixerKind, ContainerExpr,
873  Usages, Descriptor);
874 
875  // Ensure that we do not try to move an expression dependent on a local
876  // variable declared inside the loop outside of it.
877  // FIXME: Determine when the external dependency isn't an expression converted
878  // by another loop.
879  TUInfo->getParentFinder().gatherAncestors(Context->getTranslationUnitDecl());
880  DependencyFinderASTVisitor DependencyFinder(
881  &TUInfo->getParentFinder().getStmtToParentStmtMap(),
882  &TUInfo->getParentFinder().getDeclToParentStmtMap(),
883  &TUInfo->getReplacedVars(), Loop);
884 
885  if (DependencyFinder.dependsOnInsideVariable(ContainerExpr) ||
886  Descriptor.ContainerString.empty() || Usages.empty() ||
887  ConfidenceLevel.getLevel() < MinConfidence)
888  return;
889 
890  doConversion(Context, LoopVar, getReferencedVariable(ContainerExpr), Usages,
891  Finder.getAliasDecl(), Finder.aliasUseRequired(),
892  Finder.aliasFromForInit(), Loop, Descriptor);
893 }
894 
895 } // namespace modernize
896 } // namespace tidy
897 } // namespace clang
static const char DerefByRefResultName[]
Discover usages of expressions consisting of index or iterator access.
LangOptions LangOpts
Definition: ClangTidy.cpp:168
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:162
StatementMatcher makeIteratorLoopMatcher()
The matcher used for iterator-based for loops.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static const char ConditionVarName[]
static const Expr * getContainerFromBeginEndCall(const Expr *Init, bool IsBegin, bool *IsArrow)
Determine whether Init appears to be an initializing an iterator.
StatementMatcher makeArrayLoopMatcher()
The matcher for loops over arrays.
llvm::SmallVector< Usage, 8 > UsageResult
StringHandle Name
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
bool aliasFromForInit() const
Indicates if the alias declaration came from the init clause of a nested for loop.
static const char EndCallName[]
A class to encapsulate lowering of the tool's confidence level.
static const StatementMatcher IntegerComparisonMatcher
static const DeclarationMatcher InitToZeroMatcher
Class used to determine if an expression is dependent on a variable declared inside of the loop where...
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
const Expr * digThroughConstructors(const Expr *E)
Look through conversion/copy constructors to find the explicit initialization expression, returning it is found.
static const Expr * findContainer(ASTContext *Context, const Expr *BeginExpr, const Expr *EndExpr, bool *ContainerNeedsDereference)
Determines the container whose begin() and end() functions are called for an iterator-based loop...
const Expr * getContainerIndexed() const
Get the container indexed by IndexVar, if any.
static const char InitVarName[]
Level getLevel() const
Return the internal confidence level.
static const ValueDecl * getReferencedVariable(const Expr *E)
If the given expression is actually a DeclRefExpr or a MemberExpr, find and return the underlying Val...
SourceManager SourceMgr
Definition: ClangTidy.cpp:172
static const char EndVarName[]
Confidence::Level getConfidenceLevel() const
Accessor for ConfidenceLevel.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
const DeclRefExpr * getDeclRef(const Expr *E)
Returns the DeclRefExpr represented by E, or NULL if there isn't one.
void findExprComponents(const clang::Expr *SourceExpr)
Find the components of an expression and place them in a ComponentVector.
const ComponentVector & getComponents()
Accessor for Components.
SourceManager & SM
const DeclStmt * getAliasDecl() const
Returns the statement declaring the variable created as an alias for the loop element, if any.
bool areSameVariable(const ValueDecl *First, const ValueDecl *Second)
Returns true when two ValueDecls are the same variable.
static bool usagesReturnRValues(const UsageResult &Usages)
Returns true if the elements of the container are never accessed by reference.
static const char ConditionBoundName[]
static const StatementMatcher IncrementVarMatcher
Create names for generated variables within a particular statement.
static bool usagesAreConst(ASTContext *Context, const UsageResult &Usages)
Returns true when it can be guaranteed that the elements of the container are not being modified...
static const char IncrementVarName[]
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:344
bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second)
Returns true when two Exprs are equivalent.
static bool canBeModified(ASTContext *Context, const Expr *E)
Given an expression that represents an usage of an element from the containter that we are iterating ...
std::map< std::string, std::string > OptionMap
static const char ConditionEndVarName[]
static const char LoopNameArray[]
static StringRef getStringFromRange(SourceManager &SourceMgr, const LangOptions &LangOpts, SourceRange Range)
Obtain the original source code text from a SourceRange.
static const char DerefByValueResultName[]
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static bool isDirectMemberExpr(const Expr *E)
Returns true when the given expression is a member expression whose base is this (implicitly or not)...
const UsageResult & getUsages() const
Accessor for Usages.
static const char LoopNamePseudoArray[]
bool aliasUseRequired() const
Indicates if the alias declaration was in a place where it cannot simply be removed but rather replac...
bool findAndVerifyUsages(const Stmt *Body)
Finds all uses of IndexVar in Body, placing all usages in Usages, and returns true if IndexVar was on...
The information needed to describe a valid convertible usage of an array index or iterator...
CharSourceRange Range
SourceRange for the file name.
void addComponents(const ComponentVector &Components)
Add a set of components that we should consider relevant to the container.
void lowerTo(Confidence::Level Level)
Lower the internal confidence level to Level, but do not raise it.
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
static const char BeginCallName[]
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
static const TypeMatcher AnyType
Class used to find the variables and member expressions on which an arbitrary expression depends...
static const char LoopNameIterator[]
static bool containerIsConst(const Expr *ContainerExpr, bool Dereference)
Returns true if the container is const-qualified.
StatementMatcher makePseudoArrayLoopMatcher()
The matcher used for array-like containers (pseudoarrays).
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:323
const NamedDecl * Result
Definition: USRFinder.cpp:121