blob: d87d1eca1de364bf8d7f672864808ce86b3a661e [file] [log] [blame]
John McCalld70fb982011-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"
Douglas Gregor79591782012-10-23 23:11:23 +000014#include "clang/Basic/Diagnostic.h"
John McCalld70fb982011-06-15 23:25:17 +000015#include "llvm/ADT/ArrayRef.h"
Argyrios Kyrtzidis273c7c42012-06-01 00:10:47 +000016#include "llvm/ADT/Optional.h"
Douglas Gregor79591782012-10-23 23:11:23 +000017#include <list>
John McCalld70fb982011-06-15 23:25:17 +000018
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
Chris Lattner54b16772011-07-23 17:14:25 +000032 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
33 bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
John McCalld70fb982011-06-15 23:25:17 +000034
David Blaikie9c902b52011-09-25 23:23:43 +000035 void reportDiagnostics(DiagnosticsEngine &diags) const;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +000036
37 bool hasErrors() const;
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000038
39 typedef ListTy::const_iterator iterator;
40 iterator begin() const { return List.begin(); }
41 iterator end() const { return List.end(); }
John McCalld70fb982011-06-15 23:25:17 +000042};
43
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000044void writeARCDiagsToPlist(const std::string &outPath,
Chris Lattner54b16772011-07-23 17:14:25 +000045 ArrayRef<StoredDiagnostic> diags,
Argyrios Kyrtzidisd5713632011-07-19 17:20:03 +000046 SourceManager &SM, const LangOptions &LangOpts);
47
John McCalld70fb982011-06-15 23:25:17 +000048class TransformActions {
David Blaikie9c902b52011-09-25 23:23:43 +000049 DiagnosticsEngine &Diags;
John McCalld70fb982011-06-15 23:25:17 +000050 CapturedDiagList &CapturedDiags;
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +000051 bool ReportedErrors;
John McCalld70fb982011-06-15 23:25:17 +000052 void *Impl; // TransformActionsImpl.
53
54public:
David Blaikie9c902b52011-09-25 23:23:43 +000055 TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
John McCalld70fb982011-06-15 23:25:17 +000056 ASTContext &ctx, Preprocessor &PP);
57 ~TransformActions();
58
59 void startTransaction();
60 bool commitTransaction();
61 void abortTransaction();
62
Chris Lattner01cf8db2011-07-20 06:58:45 +000063 void insert(SourceLocation loc, StringRef text);
64 void insertAfterToken(SourceLocation loc, StringRef text);
John McCalld70fb982011-06-15 23:25:17 +000065 void remove(SourceRange range);
66 void removeStmt(Stmt *S);
Chris Lattner01cf8db2011-07-20 06:58:45 +000067 void replace(SourceRange range, StringRef text);
John McCalld70fb982011-06-15 23:25:17 +000068 void replace(SourceRange range, SourceRange replacementRange);
Chris Lattner01cf8db2011-07-20 06:58:45 +000069 void replaceStmt(Stmt *S, StringRef text);
70 void replaceText(SourceLocation loc, StringRef text,
71 StringRef replacementText);
John McCalld70fb982011-06-15 23:25:17 +000072 void increaseIndentation(SourceRange range,
73 SourceLocation parentIndent);
74
Chris Lattner54b16772011-07-23 17:14:25 +000075 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
John McCalld70fb982011-06-15 23:25:17 +000076 bool clearAllDiagnostics(SourceRange range) {
Chris Lattner54b16772011-07-23 17:14:25 +000077 return clearDiagnostic(ArrayRef<unsigned>(), range);
John McCalld70fb982011-06-15 23:25:17 +000078 }
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
Alp Toker42aa2122014-01-26 05:07:32 +000098 DiagnosticBuilder report(SourceLocation loc, unsigned diagId,
99 SourceRange range = SourceRange());
Chris Lattner01cf8db2011-07-20 06:58:45 +0000100 void reportError(StringRef error, SourceLocation loc,
John McCalld70fb982011-06-15 23:25:17 +0000101 SourceRange range = SourceRange());
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000102 void reportWarning(StringRef warning, SourceLocation loc,
103 SourceRange range = SourceRange());
Chris Lattner01cf8db2011-07-20 06:58:45 +0000104 void reportNote(StringRef note, SourceLocation loc,
John McCalld70fb982011-06-15 23:25:17 +0000105 SourceRange range = SourceRange());
106
Argyrios Kyrtzidis73a0d322011-07-18 07:44:45 +0000107 bool hasReportedErrors() const { return ReportedErrors; }
108
John McCalld70fb982011-06-15 23:25:17 +0000109 class RewriteReceiver {
110 public:
111 virtual ~RewriteReceiver();
112
Chris Lattner01cf8db2011-07-20 06:58:45 +0000113 virtual void insert(SourceLocation loc, StringRef text) = 0;
John McCalld70fb982011-06-15 23:25:17 +0000114 virtual void remove(CharSourceRange range) = 0;
115 virtual void increaseIndentation(CharSourceRange range,
116 SourceLocation parentIndent) = 0;
117 };
118
119 void applyRewrites(RewriteReceiver &receiver);
120};
121
122class Transaction {
123 TransformActions &TA;
124 bool Aborted;
125
126public:
127 Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
128 TA.startTransaction();
129 }
130
131 ~Transaction() {
132 if (!isAborted())
133 TA.commitTransaction();
134 }
135
136 void abort() {
137 TA.abortTransaction();
138 Aborted = true;
139 }
140
141 bool isAborted() const { return Aborted; }
142};
143
144class MigrationPass {
145public:
146 ASTContext &Ctx;
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000147 LangOptions::GCMode OrigGCMode;
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000148 MigratorOptions MigOptions;
John McCalld70fb982011-06-15 23:25:17 +0000149 Sema &SemaRef;
150 TransformActions &TA;
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000151 const CapturedDiagList &CapturedDiags;
John McCalld70fb982011-06-15 23:25:17 +0000152 std::vector<SourceLocation> &ARCMTMacroLocs;
David Blaikie05785d12013-02-20 22:23:23 +0000153 Optional<bool> EnableCFBridgeFns;
John McCalld70fb982011-06-15 23:25:17 +0000154
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000155 MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
156 Sema &sema, TransformActions &TA,
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000157 const CapturedDiagList &capturedDiags,
John McCalld70fb982011-06-15 23:25:17 +0000158 std::vector<SourceLocation> &ARCMTMacroLocs)
Fariborz Jahanianaa7b9aa2012-01-25 00:20:29 +0000159 : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000160 SemaRef(sema), TA(TA), CapturedDiags(capturedDiags),
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000161 ARCMTMacroLocs(ARCMTMacroLocs) { }
162
Argyrios Kyrtzidis03fbe3e2013-01-04 18:30:08 +0000163 const CapturedDiagList &getDiags() const { return CapturedDiags; }
164
Argyrios Kyrtzidisd208ef92011-11-04 15:58:08 +0000165 bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
Fariborz Jahanian0c859d62012-01-26 00:08:04 +0000166 bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
167 void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
Argyrios Kyrtzidis273c7c42012-06-01 00:10:47 +0000168
169 bool CFBridgingFunctionsDefined();
John McCalld70fb982011-06-15 23:25:17 +0000170};
171
Chris Lattner01cf8db2011-07-20 06:58:45 +0000172static inline StringRef getARCMTMacroName() {
John McCalld70fb982011-06-15 23:25:17 +0000173 return "__IMPL_ARCMT_REMOVED_EXPR__";
174}
175
176} // end namespace arcmt
177
178} // end namespace clang
179
180#endif