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