1//===-- Internals.h - Implementation Details---------------------*- 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_LIB_ARCMIGRATE_INTERNALS_H
11#define LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
12
13#include "clang/ARCMigrate/ARCMT.h"
14#include "clang/Basic/Diagnostic.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/Optional.h"
17#include <list>
18
19namespace clang {
20  class Sema;
21  class Stmt;
22
23namespace arcmt {
24
25class CapturedDiagList {
26  typedef std::list<StoredDiagnostic> ListTy;
27  ListTy List;
28
29public:
30  void push_back(const StoredDiagnostic &diag) { List.push_back(diag); }
31
32  bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
33  bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
34
35  void reportDiagnostics(DiagnosticsEngine &diags) const;
36
37  bool hasErrors() const;
38
39  typedef ListTy::const_iterator iterator;
40  iterator begin() const { return List.begin(); }
41  iterator end()   const { return List.end();   }
42};
43
44void writeARCDiagsToPlist(const std::string &outPath,
45                          ArrayRef<StoredDiagnostic> diags,
46                          SourceManager &SM, const LangOptions &LangOpts);
47
48class TransformActions {
49  DiagnosticsEngine &Diags;
50  CapturedDiagList &CapturedDiags;
51  bool ReportedErrors;
52  void *Impl; // TransformActionsImpl.
53
54public:
55  TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
56                   ASTContext &ctx, Preprocessor &PP);
57  ~TransformActions();
58
59  void startTransaction();
60  bool commitTransaction();
61  void abortTransaction();
62
63  void insert(SourceLocation loc, StringRef text);
64  void insertAfterToken(SourceLocation loc, StringRef text);
65  void remove(SourceRange range);
66  void removeStmt(Stmt *S);
67  void replace(SourceRange range, StringRef text);
68  void replace(SourceRange range, SourceRange replacementRange);
69  void replaceStmt(Stmt *S, StringRef text);
70  void replaceText(SourceLocation loc, StringRef text,
71                   StringRef replacementText);
72  void increaseIndentation(SourceRange range,
73                           SourceLocation parentIndent);
74
75  bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
76  bool clearAllDiagnostics(SourceRange range) {
77    return clearDiagnostic(ArrayRef<unsigned>(), range);
78  }
79  bool clearDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
80    unsigned IDs[] = { ID1, ID2 };
81    return clearDiagnostic(IDs, range);
82  }
83  bool clearDiagnostic(unsigned ID1, unsigned ID2, unsigned ID3,
84                       SourceRange range) {
85    unsigned IDs[] = { ID1, ID2, ID3 };
86    return clearDiagnostic(IDs, range);
87  }
88
89  bool hasDiagnostic(unsigned ID, SourceRange range) {
90    return CapturedDiags.hasDiagnostic(ID, range);
91  }
92
93  bool hasDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
94    unsigned IDs[] = { ID1, ID2 };
95    return CapturedDiags.hasDiagnostic(IDs, range);
96  }
97
98  void reportError(StringRef error, SourceLocation loc,
99                   SourceRange range = SourceRange());
100  void reportWarning(StringRef warning, SourceLocation loc,
101                   SourceRange range = SourceRange());
102  void reportNote(StringRef note, SourceLocation loc,
103                  SourceRange range = SourceRange());
104
105  bool hasReportedErrors() const { return ReportedErrors; }
106
107  class RewriteReceiver {
108  public:
109    virtual ~RewriteReceiver();
110
111    virtual void insert(SourceLocation loc, StringRef text) = 0;
112    virtual void remove(CharSourceRange range) = 0;
113    virtual void increaseIndentation(CharSourceRange range,
114                                     SourceLocation parentIndent) = 0;
115  };
116
117  void applyRewrites(RewriteReceiver &receiver);
118};
119
120class Transaction {
121  TransformActions &TA;
122  bool Aborted;
123
124public:
125  Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
126    TA.startTransaction();
127  }
128
129  ~Transaction() {
130    if (!isAborted())
131      TA.commitTransaction();
132  }
133
134  void abort() {
135    TA.abortTransaction();
136    Aborted = true;
137  }
138
139  bool isAborted() const { return Aborted; }
140};
141
142class MigrationPass {
143public:
144  ASTContext &Ctx;
145  LangOptions::GCMode OrigGCMode;
146  MigratorOptions MigOptions;
147  Sema &SemaRef;
148  TransformActions &TA;
149  const CapturedDiagList &CapturedDiags;
150  std::vector<SourceLocation> &ARCMTMacroLocs;
151  Optional<bool> EnableCFBridgeFns;
152
153  MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
154                Sema &sema, TransformActions &TA,
155                const CapturedDiagList &capturedDiags,
156                std::vector<SourceLocation> &ARCMTMacroLocs)
157    : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
158      SemaRef(sema), TA(TA), CapturedDiags(capturedDiags),
159      ARCMTMacroLocs(ARCMTMacroLocs) { }
160
161  const CapturedDiagList &getDiags() const { return CapturedDiags; }
162
163  bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
164  bool noNSAllocReallocError() const { return MigOptions.NoNSAllocReallocError; }
165  void setNSAllocReallocError(bool val) { MigOptions.NoNSAllocReallocError = val; }
166  bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
167  void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
168
169  bool CFBridgingFunctionsDefined();
170};
171
172static inline StringRef getARCMTMacroName() {
173  return "__IMPL_ARCMT_REMOVED_EXPR__";
174}
175
176} // end namespace arcmt
177
178} // end namespace clang
179
180#endif
181