b1bc5581dbb37a4c5de18ff623674526bed83349
[lldb.git] / clang-tools-extra / loop-convert / LoopActions.h
1 //===-- loop-convert/LoopActions.h - C++11 For loop migration ---*- 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 // This file declares matchers and callbacks for use in migrating C++ for loops.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_
14 #define _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_
15
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"
21
22 namespace clang {
23 namespace loop_migrate {
24
25 struct Usage;
26 class Confidence;
27 // The main computational result of ForLoopIndexUseVisitor.
28 typedef llvm::SmallVector<Usage, 8> UsageResult;
29
30 /// \brief The level of safety to require of transformations.
31 enum TranslationConfidenceKind {
32   TCK_Risky,
33   TCK_Reasonable,
34   TCK_Safe
35 };
36
37 enum LoopFixerKind {
38   LFK_Array,
39   LFK_Iterator,
40   LFK_PseudoArray
41 };
42
43 /// \brief The callback to be used for loop migration matchers.
44 ///
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 {
48  public:
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);
63
64  private:
65   StmtAncestorASTVisitor *ParentFinder;
66   tooling::Replacements *Replace;
67   StmtGeneratedVarNameMap *GeneratedDecls;
68   ReplacedVarsMap *ReplacedVarRanges;
69   unsigned *AcceptedChanges;
70   unsigned *DeferredChanges;
71   unsigned *RejectedChanges;
72   bool CountOnly;
73   TranslationConfidenceKind RequiredConfidenceLevel;
74   LoopFixerKind FixerKind;
75
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);
83
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);
94 };
95
96 } // namespace loop_migrate
97 } // namespace clang
98 #endif  // _LLVM_TOOLS_CLANG_TOOLS_LOOP_CONVERT_LOOPACTIONS_H_