blob: fc5427e3b02b114098598f868cdb01efea2e9ad3 [file] [log] [blame]
Alexander Kornienko04970842015-08-19 09:11:46 +00001//===--- LoopConvertUtils.h - clang-tidy ------------------------*- C++ -*-===//
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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H
12
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/RecursiveASTVisitor.h"
15#include "clang/ASTMatchers/ASTMatchFinder.h"
16#include "clang/Lex/Lexer.h"
17#include "clang/Tooling/Refactoring.h"
18
19namespace clang {
20namespace tidy {
21namespace modernize {
22
23enum LoopFixerKind { LFK_Array, LFK_Iterator, LFK_PseudoArray };
24
25/// A map used to walk the AST in reverse: maps child Stmt to parent Stmt.
26typedef llvm::DenseMap<const clang::Stmt *, const clang::Stmt *> StmtParentMap;
27
28/// A map used to walk the AST in reverse:
29/// maps VarDecl to the to parent DeclStmt.
30typedef llvm::DenseMap<const clang::VarDecl *, const clang::DeclStmt *>
31 DeclParentMap;
32
33/// A map used to track which variables have been removed by a refactoring pass.
34/// It maps the parent ForStmt to the removed index variable's VarDecl.
35typedef llvm::DenseMap<const clang::ForStmt *, const clang::VarDecl *>
36 ReplacedVarsMap;
37
38/// A map used to remember the variable names generated in a Stmt
39typedef llvm::DenseMap<const clang::Stmt *, std::string>
40 StmtGeneratedVarNameMap;
41
42/// A vector used to store the AST subtrees of an Expr.
43typedef llvm::SmallVector<const clang::Expr *, 16> ComponentVector;
44
45/// \brief Class used build the reverse AST properties needed to detect
46/// name conflicts and free variables.
47class StmtAncestorASTVisitor
48 : public clang::RecursiveASTVisitor<StmtAncestorASTVisitor> {
49public:
50 StmtAncestorASTVisitor() { StmtStack.push_back(nullptr); }
51
52 /// \brief Run the analysis on the TranslationUnitDecl.
53 ///
54 /// In case we're running this analysis multiple times, don't repeat the work.
55 void gatherAncestors(const clang::TranslationUnitDecl *T) {
56 if (StmtAncestors.empty())
57 TraverseDecl(const_cast<clang::TranslationUnitDecl *>(T));
58 }
59
60 /// Accessor for StmtAncestors.
61 const StmtParentMap &getStmtToParentStmtMap() { return StmtAncestors; }
62
63 /// Accessor for DeclParents.
64 const DeclParentMap &getDeclToParentStmtMap() { return DeclParents; }
65
66 friend class clang::RecursiveASTVisitor<StmtAncestorASTVisitor>;
67
68private:
69 StmtParentMap StmtAncestors;
70 DeclParentMap DeclParents;
71 llvm::SmallVector<const clang::Stmt *, 16> StmtStack;
72
73 bool TraverseStmt(clang::Stmt *Statement);
74 bool VisitDeclStmt(clang::DeclStmt *Statement);
75};
76
77/// Class used to find the variables and member expressions on which an
78/// arbitrary expression depends.
79class ComponentFinderASTVisitor
80 : public clang::RecursiveASTVisitor<ComponentFinderASTVisitor> {
81public:
82 ComponentFinderASTVisitor() {}
83
84 /// Find the components of an expression and place them in a ComponentVector.
85 void findExprComponents(const clang::Expr *SourceExpr) {
86 TraverseStmt(const_cast<clang::Expr *>(SourceExpr));
87 }
88
89 /// Accessor for Components.
90 const ComponentVector &getComponents() { return Components; }
91
92 friend class clang::RecursiveASTVisitor<ComponentFinderASTVisitor>;
93
94private:
95 ComponentVector Components;
96
97 bool VisitDeclRefExpr(clang::DeclRefExpr *E);
98 bool VisitMemberExpr(clang::MemberExpr *Member);
99};
100
101/// Class used to determine if an expression is dependent on a variable declared
102/// inside of the loop where it would be used.
103class DependencyFinderASTVisitor
104 : public clang::RecursiveASTVisitor<DependencyFinderASTVisitor> {
105public:
106 DependencyFinderASTVisitor(const StmtParentMap *StmtParents,
107 const DeclParentMap *DeclParents,
108 const ReplacedVarsMap *ReplacedVars,
109 const clang::Stmt *ContainingStmt)
110 : StmtParents(StmtParents), DeclParents(DeclParents),
111 ContainingStmt(ContainingStmt), ReplacedVars(ReplacedVars) {}
112
113 /// \brief Run the analysis on Body, and return true iff the expression
114 /// depends on some variable declared within ContainingStmt.
115 ///
116 /// This is intended to protect against hoisting the container expression
117 /// outside of an inner context if part of that expression is declared in that
118 /// inner context.
119 ///
120 /// For example,
121 /// \code
122 /// const int N = 10, M = 20;
123 /// int arr[N][M];
124 /// int getRow();
125 ///
126 /// for (int i = 0; i < M; ++i) {
127 /// int k = getRow();
128 /// printf("%d:", arr[k][i]);
129 /// }
130 /// \endcode
131 /// At first glance, this loop looks like it could be changed to
132 /// \code
133 /// for (int elem : arr[k]) {
134 /// int k = getIndex();
135 /// printf("%d:", elem);
136 /// }
137 /// \endcode
138 /// But this is malformed, since `k` is used before it is defined!
139 ///
140 /// In order to avoid this, this class looks at the container expression
141 /// `arr[k]` and decides whether or not it contains a sub-expression declared
142 /// within the the loop body.
143 bool dependsOnInsideVariable(const clang::Stmt *Body) {
144 DependsOnInsideVariable = false;
145 TraverseStmt(const_cast<clang::Stmt *>(Body));
146 return DependsOnInsideVariable;
147 }
148
149 friend class clang::RecursiveASTVisitor<DependencyFinderASTVisitor>;
150
151private:
152 const StmtParentMap *StmtParents;
153 const DeclParentMap *DeclParents;
154 const clang::Stmt *ContainingStmt;
155 const ReplacedVarsMap *ReplacedVars;
156 bool DependsOnInsideVariable;
157
158 bool VisitVarDecl(clang::VarDecl *V);
159 bool VisitDeclRefExpr(clang::DeclRefExpr *D);
160};
161
162/// Class used to determine if any declarations used in a Stmt would conflict
163/// with a particular identifier. This search includes the names that don't
164/// actually appear in the AST (i.e. created by a refactoring tool) by including
165/// a map from Stmts to generated names associated with those stmts.
166class DeclFinderASTVisitor
167 : public clang::RecursiveASTVisitor<DeclFinderASTVisitor> {
168public:
169 DeclFinderASTVisitor(const std::string &Name,
170 const StmtGeneratedVarNameMap *GeneratedDecls)
171 : Name(Name), GeneratedDecls(GeneratedDecls), Found(false) {}
172
173 /// Attempts to find any usages of variables name Name in Body, returning
174 /// true when it is used in Body. This includes the generated loop variables
175 /// of ForStmts which have already been transformed.
176 bool findUsages(const clang::Stmt *Body) {
177 Found = false;
178 TraverseStmt(const_cast<clang::Stmt *>(Body));
179 return Found;
180 }
181
182 friend class clang::RecursiveASTVisitor<DeclFinderASTVisitor>;
183
184private:
185 std::string Name;
186 /// GeneratedDecls keeps track of ForStmts which have been transformed,
187 /// mapping each modified ForStmt to the variable generated in the loop.
188 const StmtGeneratedVarNameMap *GeneratedDecls;
189 bool Found;
190
191 bool VisitForStmt(clang::ForStmt *F);
192 bool VisitNamedDecl(clang::NamedDecl *D);
193 bool VisitDeclRefExpr(clang::DeclRefExpr *D);
194 bool VisitTypeLoc(clang::TypeLoc TL);
195};
196
197/// \brief The information needed to describe a valid convertible usage
198/// of an array index or iterator.
199struct Usage {
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000200 enum UsageKind {
201 // Regular usages of the loop index (the ones not specified below). Some
202 // examples:
203 // \code
204 // int X = 8 * Arr[i];
205 // ^~~~~~
206 // f(param1, param2, *It);
207 // ^~~
208 // if (Vec[i].SomeBool) {}
209 // ^~~~~~
210 // \endcode
211 UK_Default,
212 // Indicates whether this is an access to a member through the arrow
213 // operator on pointers or iterators.
214 UK_MemberThroughArrow,
215 // If the variable is being captured by a lambda, indicates whether the
216 // capture was done by value or by reference.
217 UK_CaptureByCopy,
218 UK_CaptureByRef
219 };
220 // The expression that is going to be converted. Null in case of lambda
221 // captures.
Angel Garcia Gomez692cbb52015-09-01 15:05:15 +0000222 const Expr *Expression;
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000223
224 UsageKind Kind;
225
226 // Range that covers this usage.
Alexander Kornienko04970842015-08-19 09:11:46 +0000227 SourceRange Range;
228
229 explicit Usage(const Expr *E)
Angel Garcia Gomezbb9ca542015-09-11 10:02:07 +0000230 : Expression(E), Kind(UK_Default), Range(Expression->getSourceRange()) {}
231 Usage(const Expr *E, UsageKind Kind, SourceRange Range)
232 : Expression(E), Kind(Kind), Range(std::move(Range)) {}
Alexander Kornienko04970842015-08-19 09:11:46 +0000233};
234
235/// \brief A class to encapsulate lowering of the tool's confidence level.
236class Confidence {
237public:
238 enum Level {
239 // Transformations that are likely to change semantics.
240 CL_Risky,
241
242 // Transformations that might change semantics.
243 CL_Reasonable,
244
245 // Transformations that will not change semantics.
246 CL_Safe
247 };
248 /// \brief Initialize confidence level.
249 explicit Confidence(Confidence::Level Level) : CurrentLevel(Level) {}
250
251 /// \brief Lower the internal confidence level to Level, but do not raise it.
252 void lowerTo(Confidence::Level Level) {
253 CurrentLevel = std::min(Level, CurrentLevel);
254 }
255
256 /// \brief Return the internal confidence level.
257 Level getLevel() const { return CurrentLevel; }
258
259private:
260 Level CurrentLevel;
261};
262
263// The main computational result of ForLoopIndexVisitor.
264typedef llvm::SmallVector<Usage, 8> UsageResult;
265
266// General functions used by ForLoopIndexUseVisitor and LoopConvertCheck.
267const Expr *digThroughConstructors(const Expr *E);
268bool areSameExpr(ASTContext *Context, const Expr *First, const Expr *Second);
269const DeclRefExpr *getDeclRef(const Expr *E);
270bool areSameVariable(const ValueDecl *First, const ValueDecl *Second);
271
272/// \brief Discover usages of expressions consisting of index or iterator
273/// access.
274///
275/// Given an index variable, recursively crawls a for loop to discover if the
276/// index variable is used in a way consistent with range-based for loop access.
277class ForLoopIndexUseVisitor
278 : public RecursiveASTVisitor<ForLoopIndexUseVisitor> {
279public:
280 ForLoopIndexUseVisitor(ASTContext *Context, const VarDecl *IndexVar,
281 const VarDecl *EndVar, const Expr *ContainerExpr,
282 const Expr *ArrayBoundExpr,
283 bool ContainerNeedsDereference);
284
285 /// \brief Finds all uses of IndexVar in Body, placing all usages in Usages,
286 /// and returns true if IndexVar was only used in a way consistent with a
287 /// range-based for loop.
288 ///
289 /// The general strategy is to reject any DeclRefExprs referencing IndexVar,
290 /// with the exception of certain acceptable patterns.
291 /// For arrays, the DeclRefExpr for IndexVar must appear as the index of an
292 /// ArraySubscriptExpression. Iterator-based loops may dereference
293 /// IndexVar or call methods through operator-> (builtin or overloaded).
294 /// Array-like containers may use IndexVar as a parameter to the at() member
295 /// function and in overloaded operator[].
296 bool findAndVerifyUsages(const Stmt *Body);
297
298 /// \brief Add a set of components that we should consider relevant to the
299 /// container.
300 void addComponents(const ComponentVector &Components);
301
302 /// \brief Accessor for Usages.
303 const UsageResult &getUsages() const { return Usages; }
304
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000305 /// \brief Adds the Usage if it was not added before.
306 void addUsage(const Usage &U);
307
Alexander Kornienko04970842015-08-19 09:11:46 +0000308 /// \brief Get the container indexed by IndexVar, if any.
309 const Expr *getContainerIndexed() const { return ContainerExpr; }
310
311 /// \brief Returns the statement declaring the variable created as an alias
312 /// for the loop element, if any.
313 const DeclStmt *getAliasDecl() const { return AliasDecl; }
314
315 /// \brief Accessor for ConfidenceLevel.
316 Confidence::Level getConfidenceLevel() const {
317 return ConfidenceLevel.getLevel();
318 }
319
320 /// \brief Indicates if the alias declaration was in a place where it cannot
321 /// simply be removed but rather replaced with a use of the alias variable.
322 /// For example, variables declared in the condition of an if, switch, or for
323 /// stmt.
324 bool aliasUseRequired() const { return ReplaceWithAliasUse; }
325
326 /// \brief Indicates if the alias declaration came from the init clause of a
327 /// nested for loop. SourceRanges provided by Clang for DeclStmts in this
328 /// case need to be adjusted.
329 bool aliasFromForInit() const { return AliasFromForInit; }
330
331private:
332 /// Typedef used in CRTP functions.
333 typedef RecursiveASTVisitor<ForLoopIndexUseVisitor> VisitorBase;
334 friend class RecursiveASTVisitor<ForLoopIndexUseVisitor>;
335
336 /// Overriden methods for RecursiveASTVisitor's traversal.
337 bool TraverseArraySubscriptExpr(ArraySubscriptExpr *E);
338 bool TraverseCXXMemberCallExpr(CXXMemberCallExpr *MemberCall);
339 bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *OpCall);
Martin Bohmee9a265a2016-08-17 15:00:22 +0000340 bool TraverseLambdaCapture(LambdaExpr *LE, const LambdaCapture *C,
341 Expr *Init);
Alexander Kornienko04970842015-08-19 09:11:46 +0000342 bool TraverseMemberExpr(MemberExpr *Member);
343 bool TraverseUnaryDeref(UnaryOperator *Uop);
344 bool VisitDeclRefExpr(DeclRefExpr *E);
345 bool VisitDeclStmt(DeclStmt *S);
346 bool TraverseStmt(Stmt *S);
347
348 /// \brief Add an expression to the list of expressions on which the container
349 /// expression depends.
350 void addComponent(const Expr *E);
351
352 // Input member variables:
353 ASTContext *Context;
354 /// The index variable's VarDecl.
355 const VarDecl *IndexVar;
356 /// The loop's 'end' variable, which cannot be mentioned at all.
357 const VarDecl *EndVar;
358 /// The Expr which refers to the container.
359 const Expr *ContainerExpr;
360 /// The Expr which refers to the terminating condition for array-based loops.
361 const Expr *ArrayBoundExpr;
362 bool ContainerNeedsDereference;
363
364 // Output member variables:
365 /// A container which holds all usages of IndexVar as the index of
366 /// ArraySubscriptExpressions.
367 UsageResult Usages;
Angel Garcia Gomezbd0ec692015-09-04 21:37:05 +0000368 llvm::SmallSet<SourceLocation, 8> UsageLocations;
Alexander Kornienko04970842015-08-19 09:11:46 +0000369 bool OnlyUsedAsIndex;
370 /// The DeclStmt for an alias to the container element.
371 const DeclStmt *AliasDecl;
372 Confidence ConfidenceLevel;
373 /// \brief A list of expressions on which ContainerExpr depends.
374 ///
375 /// If any of these expressions are encountered outside of an acceptable usage
376 /// of the loop element, lower our confidence level.
377 llvm::SmallVector<std::pair<const Expr *, llvm::FoldingSetNodeID>, 16>
378 DependentExprs;
379
380 /// The parent-in-waiting. Will become the real parent once we traverse down
381 /// one level in the AST.
382 const Stmt *NextStmtParent;
383 /// The actual parent of a node when Visit*() calls are made. Only the
384 /// parentage of DeclStmt's to possible iteration/selection statements is of
385 /// importance.
386 const Stmt *CurrStmtParent;
387
388 /// \see aliasUseRequired().
389 bool ReplaceWithAliasUse;
390 /// \see aliasFromForInit().
391 bool AliasFromForInit;
392};
393
394struct TUTrackingInfo {
395 /// \brief Reset and initialize per-TU tracking information.
396 ///
397 /// Must be called before using container accessors.
398 TUTrackingInfo() : ParentFinder(new StmtAncestorASTVisitor) {}
399
400 StmtAncestorASTVisitor &getParentFinder() { return *ParentFinder; }
401 StmtGeneratedVarNameMap &getGeneratedDecls() { return GeneratedDecls; }
402 ReplacedVarsMap &getReplacedVars() { return ReplacedVars; }
403
404private:
405 std::unique_ptr<StmtAncestorASTVisitor> ParentFinder;
406 StmtGeneratedVarNameMap GeneratedDecls;
407 ReplacedVarsMap ReplacedVars;
408};
409
410/// \brief Create names for generated variables within a particular statement.
411///
412/// VariableNamer uses a DeclContext as a reference point, checking for any
413/// conflicting declarations higher up in the context or within SourceStmt.
414/// It creates a variable name using hints from a source container and the old
415/// index, if they exist.
416class VariableNamer {
417public:
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000418 // Supported naming styles.
419 enum NamingStyle {
420 NS_CamelBack,
421 NS_CamelCase,
422 NS_LowerCase,
423 NS_UpperCase,
424 };
425
Alexander Kornienko04970842015-08-19 09:11:46 +0000426 VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls,
427 const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt,
428 const clang::VarDecl *OldIndex,
Angel Garcia Gomez432ff5e2015-11-03 16:38:31 +0000429 const clang::ValueDecl *TheContainer,
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000430 const clang::ASTContext *Context, NamingStyle Style)
Alexander Kornienko04970842015-08-19 09:11:46 +0000431 : GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
432 SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer),
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000433 Context(Context), Style(Style) {}
Alexander Kornienko04970842015-08-19 09:11:46 +0000434
435 /// \brief Generate a new index name.
436 ///
437 /// Generates the name to be used for an inserted iterator. It relies on
438 /// declarationExists() to determine that there are no naming conflicts, and
439 /// tries to use some hints from the container name and the old index name.
440 std::string createIndexName();
441
442private:
443 StmtGeneratedVarNameMap *GeneratedDecls;
444 const StmtParentMap *ReverseAST;
445 const clang::Stmt *SourceStmt;
446 const clang::VarDecl *OldIndex;
Angel Garcia Gomez432ff5e2015-11-03 16:38:31 +0000447 const clang::ValueDecl *TheContainer;
Alexander Kornienko04970842015-08-19 09:11:46 +0000448 const clang::ASTContext *Context;
Angel Garcia Gomez8535c6c2015-09-24 17:02:19 +0000449 const NamingStyle Style;
Alexander Kornienko04970842015-08-19 09:11:46 +0000450
451 // Determine whether or not a declaration that would conflict with Symbol
452 // exists in an outer context or in any statement contained in SourceStmt.
453 bool declarationExists(llvm::StringRef Symbol);
Alexander Kornienko04970842015-08-19 09:11:46 +0000454};
455
456} // namespace modernize
457} // namespace tidy
458} // namespace clang
459
460#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_LOOP_CONVERT_UTILS_H