summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/LoopConvert/LoopActions.h
blob: f70f5736f0cf3bdf1b6a99e12afde6ce21bf9b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//===-- LoopConvert/LoopActions.h - C++11 For loop migration ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file declares matchers and callbacks for use in migrating C++
/// for loops.
///
//===----------------------------------------------------------------------===//

#ifndef CLANG_MODERNIZE_LOOP_ACTIONS_H
#define CLANG_MODERNIZE_LOOP_ACTIONS_H

#include "StmtAncestor.h"
#include "Core/Transform.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Refactoring.h"

struct Usage;
class Confidence;
// The main computational result of ForLoopIndexUseVisitor.
typedef llvm::SmallVector<Usage, 8> UsageResult;

enum LoopFixerKind {
  LFK_Array,
  LFK_Iterator,
  LFK_PseudoArray
};

/// \brief The callback to be used for loop migration matchers.
///
/// The callback does extra checking not possible in matchers, and attempts to
/// convert the for loop, if possible.
class LoopFixer : public clang::ast_matchers::MatchFinder::MatchCallback {
 public:
   LoopFixer(StmtAncestorASTVisitor *ParentFinder,
             StmtGeneratedVarNameMap *GeneratedDecls,
             ReplacedVarsMap *ReplacedVarRanges, unsigned *AcceptedChanges,
             unsigned *DeferredChanges, unsigned *RejectedChanges,
             RiskLevel MaxRisk, LoopFixerKind FixerKind, Transform &Owner)
       : ParentFinder(ParentFinder),
         GeneratedDecls(GeneratedDecls), ReplacedVarRanges(ReplacedVarRanges),
         AcceptedChanges(AcceptedChanges), DeferredChanges(DeferredChanges),
         RejectedChanges(RejectedChanges), MaxRisk(MaxRisk),
         FixerKind(FixerKind), Owner(Owner) {}

   virtual void
       run(const clang::ast_matchers::MatchFinder::MatchResult &Result);

 private:
  StmtAncestorASTVisitor *ParentFinder;
  StmtGeneratedVarNameMap *GeneratedDecls;
  ReplacedVarsMap *ReplacedVarRanges;
  unsigned *AcceptedChanges;
  unsigned *DeferredChanges;
  unsigned *RejectedChanges;
  RiskLevel MaxRisk;
  LoopFixerKind FixerKind;
  Transform &Owner;

  /// \brief Computes the changes needed to convert a given for loop, and
  /// applies it.
  void doConversion(clang::ASTContext *Context,
                    const clang::VarDecl *IndexVar,
                    const clang::VarDecl *MaybeContainer,
                    llvm::StringRef ContainerString,
                    const UsageResult &Usages,
                    const clang::DeclStmt *AliasDecl,
                    bool AliasUseRequired,
                    bool AliasFromForInit,
                    const clang::ForStmt *TheLoop,
                    bool ContainerNeedsDereference,
                    bool DerefByValue,
                    bool DerefByConstRef);

  /// \brief Given a loop header that would be convertible, discover all usages
  /// of the index variable and convert the loop if possible.
  void findAndVerifyUsages(clang::ASTContext *Context,
                           const clang::VarDecl *LoopVar,
                           const clang::VarDecl *EndVar,
                           const clang::Expr *ContainerExpr,
                           const clang::Expr *BoundExpr,
                           bool ContainerNeedsDereference,
                           bool DerefByValue,
                           bool DerefByConstRef,
                           const clang::ForStmt *TheLoop,
                           Confidence ConfidenceLevel);

  /// \brief Determine if the change should be deferred or rejected, returning
  /// text which refers to the container iterated over if the change should
  /// proceed.
  llvm::StringRef checkDeferralsAndRejections(clang::ASTContext *Context,
                                              const clang::Expr *ContainerExpr,
                                              Confidence ConfidenceLevel,
                                              const clang::ForStmt *TheLoop);
};

#endif // CLANG_MODERNIZE_LOOP_ACTIONS_H
OpenPOWER on IntegriCloud