• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(ArrayRef<unsigned> IDs, SourceRange range);
30   bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
31 
32   void reportDiagnostics(DiagnosticsEngine &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                           ArrayRef<StoredDiagnostic> diags,
43                           SourceManager &SM, const LangOptions &LangOpts);
44 
45 class TransformActions {
46   DiagnosticsEngine &Diags;
47   CapturedDiagList &CapturedDiags;
48   bool ReportedErrors;
49   void *Impl; // TransformActionsImpl.
50 
51 public:
52   TransformActions(DiagnosticsEngine &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, StringRef text);
61   void insertAfterToken(SourceLocation loc, StringRef text);
62   void remove(SourceRange range);
63   void removeStmt(Stmt *S);
64   void replace(SourceRange range, StringRef text);
65   void replace(SourceRange range, SourceRange replacementRange);
66   void replaceStmt(Stmt *S, StringRef text);
67   void replaceText(SourceLocation loc, StringRef text,
68                    StringRef replacementText);
69   void increaseIndentation(SourceRange range,
70                            SourceLocation parentIndent);
71 
72   bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
clearAllDiagnostics(SourceRange range)73   bool clearAllDiagnostics(SourceRange range) {
74     return clearDiagnostic(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(StringRef error, SourceLocation loc,
96                    SourceRange range = SourceRange());
97   void reportWarning(StringRef warning, SourceLocation loc,
98                    SourceRange range = SourceRange());
99   void reportNote(StringRef note, SourceLocation loc,
100                   SourceRange range = SourceRange());
101 
hasReportedErrors()102   bool hasReportedErrors() const { return ReportedErrors; }
103 
104   class RewriteReceiver {
105   public:
106     virtual ~RewriteReceiver();
107 
108     virtual void insert(SourceLocation loc, StringRef text) = 0;
109     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 
117 class Transaction {
118   TransformActions &TA;
119   bool Aborted;
120 
121 public:
Transaction(TransformActions & TA)122   Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
123     TA.startTransaction();
124   }
125 
~Transaction()126   ~Transaction() {
127     if (!isAborted())
128       TA.commitTransaction();
129   }
130 
abort()131   void abort() {
132     TA.abortTransaction();
133     Aborted = true;
134   }
135 
isAborted()136   bool isAborted() const { return Aborted; }
137 };
138 
139 class MigrationPass {
140 public:
141   ASTContext &Ctx;
142   LangOptions::GCMode OrigGCMode;
143   MigratorOptions MigOptions;
144   Sema &SemaRef;
145   TransformActions &TA;
146   std::vector<SourceLocation> &ARCMTMacroLocs;
147 
MigrationPass(ASTContext & Ctx,LangOptions::GCMode OrigGCMode,Sema & sema,TransformActions & TA,std::vector<SourceLocation> & ARCMTMacroLocs)148   MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
149                 Sema &sema, TransformActions &TA,
150                 std::vector<SourceLocation> &ARCMTMacroLocs)
151     : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
152       SemaRef(sema), TA(TA),
153       ARCMTMacroLocs(ARCMTMacroLocs) { }
154 
isGCMigration()155   bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
noNSAllocReallocError()156   bool noNSAllocReallocError() const { return MigOptions.NoNSAllocReallocError; }
setNSAllocReallocError(bool val)157   void setNSAllocReallocError(bool val) { MigOptions.NoNSAllocReallocError = val; }
noFinalizeRemoval()158   bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
setNoFinalizeRemoval(bool val)159   void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
160 };
161 
getARCMTMacroName()162 static inline StringRef getARCMTMacroName() {
163   return "__IMPL_ARCMT_REMOVED_EXPR__";
164 }
165 
166 } // end namespace arcmt
167 
168 } // end namespace clang
169 
170 #endif
171