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