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