blob: 06d9f8259fa2940325d86cae455ee25a384db383 [file] [log] [blame]
John McCall8f0e8d22011-06-15 23:25:17 +00001//===-- 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
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000029 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
30 bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
John McCall8f0e8d22011-06-15 23:25:17 +000031
David Blaikied6471f72011-09-25 23:23:43 +000032 void reportDiagnostics(DiagnosticsEngine &diags) const;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +000033
34 bool hasErrors() const;
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000035
36 typedef ListTy::const_iterator iterator;
37 iterator begin() const { return List.begin(); }
38 iterator end() const { return List.end(); }
John McCall8f0e8d22011-06-15 23:25:17 +000039};
40
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000041void writeARCDiagsToPlist(const std::string &outPath,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000042 ArrayRef<StoredDiagnostic> diags,
Argyrios Kyrtzidis7ee20492011-07-19 17:20:03 +000043 SourceManager &SM, const LangOptions &LangOpts);
44
John McCall8f0e8d22011-06-15 23:25:17 +000045class TransformActions {
David Blaikied6471f72011-09-25 23:23:43 +000046 DiagnosticsEngine &Diags;
John McCall8f0e8d22011-06-15 23:25:17 +000047 CapturedDiagList &CapturedDiags;
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +000048 bool ReportedErrors;
John McCall8f0e8d22011-06-15 23:25:17 +000049 void *Impl; // TransformActionsImpl.
50
51public:
David Blaikied6471f72011-09-25 23:23:43 +000052 TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
John McCall8f0e8d22011-06-15 23:25:17 +000053 ASTContext &ctx, Preprocessor &PP);
54 ~TransformActions();
55
56 void startTransaction();
57 bool commitTransaction();
58 void abortTransaction();
59
Chris Lattner686775d2011-07-20 06:58:45 +000060 void insert(SourceLocation loc, StringRef text);
61 void insertAfterToken(SourceLocation loc, StringRef text);
John McCall8f0e8d22011-06-15 23:25:17 +000062 void remove(SourceRange range);
63 void removeStmt(Stmt *S);
Chris Lattner686775d2011-07-20 06:58:45 +000064 void replace(SourceRange range, StringRef text);
John McCall8f0e8d22011-06-15 23:25:17 +000065 void replace(SourceRange range, SourceRange replacementRange);
Chris Lattner686775d2011-07-20 06:58:45 +000066 void replaceStmt(Stmt *S, StringRef text);
67 void replaceText(SourceLocation loc, StringRef text,
68 StringRef replacementText);
John McCall8f0e8d22011-06-15 23:25:17 +000069 void increaseIndentation(SourceRange range,
70 SourceLocation parentIndent);
71
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000072 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
John McCall8f0e8d22011-06-15 23:25:17 +000073 bool clearAllDiagnostics(SourceRange range) {
Chris Lattner2d3ba4f2011-07-23 17:14:25 +000074 return clearDiagnostic(ArrayRef<unsigned>(), range);
John McCall8f0e8d22011-06-15 23:25:17 +000075 }
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
Chris Lattner686775d2011-07-20 06:58:45 +000095 void reportError(StringRef error, SourceLocation loc,
John McCall8f0e8d22011-06-15 23:25:17 +000096 SourceRange range = SourceRange());
Chris Lattner686775d2011-07-20 06:58:45 +000097 void reportNote(StringRef note, SourceLocation loc,
John McCall8f0e8d22011-06-15 23:25:17 +000098 SourceRange range = SourceRange());
99
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000100 bool hasReportedErrors() const { return ReportedErrors; }
101
John McCall8f0e8d22011-06-15 23:25:17 +0000102 class RewriteReceiver {
103 public:
104 virtual ~RewriteReceiver();
105
Chris Lattner686775d2011-07-20 06:58:45 +0000106 virtual void insert(SourceLocation loc, StringRef text) = 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000107 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;
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000140 LangOptions::GCMode OrigGCMode;
John McCall8f0e8d22011-06-15 23:25:17 +0000141 Sema &SemaRef;
142 TransformActions &TA;
143 std::vector<SourceLocation> &ARCMTMacroLocs;
144
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000145 MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
146 Sema &sema, TransformActions &TA,
John McCall8f0e8d22011-06-15 23:25:17 +0000147 std::vector<SourceLocation> &ARCMTMacroLocs)
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000148 : Ctx(Ctx), OrigGCMode(OrigGCMode), SemaRef(sema), TA(TA),
149 ARCMTMacroLocs(ARCMTMacroLocs) { }
150
151 bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
John McCall8f0e8d22011-06-15 23:25:17 +0000152};
153
Chris Lattner686775d2011-07-20 06:58:45 +0000154static inline StringRef getARCMTMacroName() {
John McCall8f0e8d22011-06-15 23:25:17 +0000155 return "__IMPL_ARCMT_REMOVED_EXPR__";
156}
157
158} // end namespace arcmt
159
160} // end namespace clang
161
162#endif