blob: 59177c483ea0c1b73de6c63fe97ebd0a443d6387 [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());
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +000097 void reportWarning(StringRef warning, SourceLocation loc,
98 SourceRange range = SourceRange());
Chris Lattner686775d2011-07-20 06:58:45 +000099 void reportNote(StringRef note, SourceLocation loc,
John McCall8f0e8d22011-06-15 23:25:17 +0000100 SourceRange range = SourceRange());
101
Argyrios Kyrtzidisfd103982011-07-18 07:44:45 +0000102 bool hasReportedErrors() const { return ReportedErrors; }
103
John McCall8f0e8d22011-06-15 23:25:17 +0000104 class RewriteReceiver {
105 public:
106 virtual ~RewriteReceiver();
107
Chris Lattner686775d2011-07-20 06:58:45 +0000108 virtual void insert(SourceLocation loc, StringRef text) = 0;
John McCall8f0e8d22011-06-15 23:25:17 +0000109 virtual void remove(CharSourceRange range) = 0;
110 virtual void increaseIndentation(CharSourceRange range,
111 SourceLocation parentIndent) = 0;
112 };
113
114 void applyRewrites(RewriteReceiver &receiver);
115};
116
117class Transaction {
118 TransformActions &TA;
119 bool Aborted;
120
121public:
122 Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
123 TA.startTransaction();
124 }
125
126 ~Transaction() {
127 if (!isAborted())
128 TA.commitTransaction();
129 }
130
131 void abort() {
132 TA.abortTransaction();
133 Aborted = true;
134 }
135
136 bool isAborted() const { return Aborted; }
137};
138
139class MigrationPass {
140public:
141 ASTContext &Ctx;
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000142 LangOptions::GCMode OrigGCMode;
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000143 MigratorOptions MigOptions;
John McCall8f0e8d22011-06-15 23:25:17 +0000144 Sema &SemaRef;
145 TransformActions &TA;
146 std::vector<SourceLocation> &ARCMTMacroLocs;
147
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000148 MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
149 Sema &sema, TransformActions &TA,
John McCall8f0e8d22011-06-15 23:25:17 +0000150 std::vector<SourceLocation> &ARCMTMacroLocs)
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000151 : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
152 SemaRef(sema), TA(TA),
Argyrios Kyrtzidise0ac7452011-11-04 15:58:08 +0000153 ARCMTMacroLocs(ARCMTMacroLocs) { }
154
155 bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
Fariborz Jahanianb5c6bab2012-01-25 00:20:29 +0000156 bool noNSAllocReallocError() const { return MigOptions.NoNSAllocReallocError; }
157 void setNSAllocReallocError(bool val) { MigOptions.NoNSAllocReallocError = val; }
Fariborz Jahanian26f0e4e2012-01-26 00:08:04 +0000158 bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
159 void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
John McCall8f0e8d22011-06-15 23:25:17 +0000160};
161
Chris Lattner686775d2011-07-20 06:58:45 +0000162static inline StringRef getARCMTMacroName() {
John McCall8f0e8d22011-06-15 23:25:17 +0000163 return "__IMPL_ARCMT_REMOVED_EXPR__";
164}
165
166} // end namespace arcmt
167
168} // end namespace clang
169
170#endif