1 //===-- loop-convert/LoopActions.h - C++11 For loop migration ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares matchers and callbacks for use in migrating C++ for loops.
12 //===----------------------------------------------------------------------===//
13 #ifndef _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_
14 #define _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_
16 #include "StmtAncestor.h"
17 #include "clang/Tooling/Refactoring.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/RecursiveASTVisitor.h"
20 #include "clang/ASTMatchers/ASTMatchFinder.h"
23 namespace loop_migrate {
27 // The main computational result of ForLoopIndexUseVisitor.
28 typedef llvm::SmallVector<Usage, 8> UsageResult;
30 /// \brief The level of safety to require of transformations.
31 enum TranslationConfidenceKind {
43 /// \brief The callback to be used for loop migration matchers.
45 /// The callback does extra checking not possible in matchers, and attempts to
46 /// convert the for loop, if possible.
47 class LoopFixer : public ast_matchers::MatchFinder::MatchCallback {
49 LoopFixer(StmtAncestorASTVisitor *ParentFinder,
50 tooling::Replacements *Replace,
51 StmtGeneratedVarNameMap *GeneratedDecls,
52 ReplacedVarsMap *ReplacedVarRanges,
53 unsigned *AcceptedChanges, unsigned *DeferredChanges,
54 unsigned *RejectedChanges, bool CountOnly,
55 TranslationConfidenceKind RequiredConfidenceLevel,
56 LoopFixerKind FixerKind) :
57 ParentFinder(ParentFinder), Replace(Replace),
58 GeneratedDecls(GeneratedDecls), ReplacedVarRanges(ReplacedVarRanges),
59 AcceptedChanges(AcceptedChanges), DeferredChanges(DeferredChanges),
60 RejectedChanges(RejectedChanges), CountOnly(CountOnly),
61 RequiredConfidenceLevel(RequiredConfidenceLevel), FixerKind(FixerKind) { }
62 virtual void run(const ast_matchers::MatchFinder::MatchResult &Result);
65 StmtAncestorASTVisitor *ParentFinder;
66 tooling::Replacements *Replace;
67 StmtGeneratedVarNameMap *GeneratedDecls;
68 ReplacedVarsMap *ReplacedVarRanges;
69 unsigned *AcceptedChanges;
70 unsigned *DeferredChanges;
71 unsigned *RejectedChanges;
73 TranslationConfidenceKind RequiredConfidenceLevel;
74 LoopFixerKind FixerKind;
76 /// \brief Computes the changes needed to convert a given for loop, and
77 /// applies it if this->CountOnly is false.
78 void doConversion(ASTContext *Context,
79 const VarDecl *IndexVar,
80 const Expr *ContainerExpr, const UsageResult &Usages,
81 const DeclStmt *AliasDecl, const ForStmt *TheLoop,
82 bool ContainerNeedsDereference);
84 /// \brief Given a loop header that would be convertible, discover all usages
85 /// of the index variable and convert the loop if possible.
86 void FindAndVerifyUsages(ASTContext *Context,
87 const VarDecl *LoopVar,
88 const VarDecl *EndVar,
89 const Expr *ContainerExpr,
90 const Expr *BoundExpr,
91 bool ContainerNeedsDereference,
92 const ForStmt *TheLoop,
93 Confidence ConfidenceLevel);
96 } // namespace loop_migrate
98 #endif // _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_