1 //===-- Internals.h - Implementation Details---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
10 #define LLVM_CLANG_LIB_ARCMIGRATE_INTERNALS_H
12 #include "clang/ARCMigrate/ARCMT.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/Optional.h"
24 class CapturedDiagList {
25 typedef std::list<StoredDiagnostic> ListTy;
29 void push_back(const StoredDiagnostic &diag) { List.push_back(diag); }
31 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
32 bool hasDiagnostic(ArrayRef<unsigned> IDs, SourceRange range) const;
34 void reportDiagnostics(DiagnosticsEngine &diags) const;
36 bool hasErrors() const;
38 typedef ListTy::const_iterator iterator;
39 iterator begin() const { return List.begin(); }
40 iterator end() const { return List.end(); }
43 void writeARCDiagsToPlist(const std::string &outPath,
44 ArrayRef<StoredDiagnostic> diags,
45 SourceManager &SM, const LangOptions &LangOpts);
47 class TransformActions {
48 DiagnosticsEngine &Diags;
49 CapturedDiagList &CapturedDiags;
50 void *Impl; // TransformActionsImpl.
53 TransformActions(DiagnosticsEngine &diag, CapturedDiagList &capturedDiags,
54 ASTContext &ctx, Preprocessor &PP);
57 void startTransaction();
58 bool commitTransaction();
59 void abortTransaction();
61 void insert(SourceLocation loc, StringRef text);
62 void insertAfterToken(SourceLocation loc, StringRef text);
63 void remove(SourceRange range);
64 void removeStmt(Stmt *S);
65 void replace(SourceRange range, StringRef text);
66 void replace(SourceRange range, SourceRange replacementRange);
67 void replaceStmt(Stmt *S, StringRef text);
68 void replaceText(SourceLocation loc, StringRef text,
69 StringRef replacementText);
70 void increaseIndentation(SourceRange range,
71 SourceLocation parentIndent);
73 bool clearDiagnostic(ArrayRef<unsigned> IDs, SourceRange range);
74 bool clearAllDiagnostics(SourceRange range) {
75 return clearDiagnostic(None, range);
77 bool clearDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
78 unsigned IDs[] = { ID1, ID2 };
79 return clearDiagnostic(IDs, range);
81 bool clearDiagnostic(unsigned ID1, unsigned ID2, unsigned ID3,
83 unsigned IDs[] = { ID1, ID2, ID3 };
84 return clearDiagnostic(IDs, range);
87 bool hasDiagnostic(unsigned ID, SourceRange range) {
88 return CapturedDiags.hasDiagnostic(ID, range);
91 bool hasDiagnostic(unsigned ID1, unsigned ID2, SourceRange range) {
92 unsigned IDs[] = { ID1, ID2 };
93 return CapturedDiags.hasDiagnostic(IDs, range);
96 DiagnosticBuilder report(SourceLocation loc, unsigned diagId,
97 SourceRange range = SourceRange());
98 void reportError(StringRef error, SourceLocation loc,
99 SourceRange range = SourceRange());
100 void reportWarning(StringRef warning, SourceLocation loc,
101 SourceRange range = SourceRange());
102 void reportNote(StringRef note, SourceLocation loc,
103 SourceRange range = SourceRange());
105 bool hasReportedErrors() const {
106 return Diags.hasUnrecoverableErrorOccurred();
109 class RewriteReceiver {
111 virtual ~RewriteReceiver();
113 virtual void insert(SourceLocation loc, StringRef text) = 0;
114 virtual void remove(CharSourceRange range) = 0;
115 virtual void increaseIndentation(CharSourceRange range,
116 SourceLocation parentIndent) = 0;
119 void applyRewrites(RewriteReceiver &receiver);
123 TransformActions &TA;
127 Transaction(TransformActions &TA) : TA(TA), Aborted(false) {
128 TA.startTransaction();
133 TA.commitTransaction();
137 TA.abortTransaction();
141 bool isAborted() const { return Aborted; }
144 class MigrationPass {
147 LangOptions::GCMode OrigGCMode;
148 MigratorOptions MigOptions;
150 TransformActions &TA;
151 const CapturedDiagList &CapturedDiags;
152 std::vector<SourceLocation> &ARCMTMacroLocs;
153 Optional<bool> EnableCFBridgeFns;
155 MigrationPass(ASTContext &Ctx, LangOptions::GCMode OrigGCMode,
156 Sema &sema, TransformActions &TA,
157 const CapturedDiagList &capturedDiags,
158 std::vector<SourceLocation> &ARCMTMacroLocs)
159 : Ctx(Ctx), OrigGCMode(OrigGCMode), MigOptions(),
160 SemaRef(sema), TA(TA), CapturedDiags(capturedDiags),
161 ARCMTMacroLocs(ARCMTMacroLocs) { }
163 const CapturedDiagList &getDiags() const { return CapturedDiags; }
165 bool isGCMigration() const { return OrigGCMode != LangOptions::NonGC; }
166 bool noFinalizeRemoval() const { return MigOptions.NoFinalizeRemoval; }
167 void setNoFinalizeRemoval(bool val) {MigOptions.NoFinalizeRemoval = val; }
169 bool CFBridgingFunctionsDefined();
172 static inline StringRef getARCMTMacroName() {
173 return "__IMPL_ARCMT_REMOVED_EXPR__";
176 } // end namespace arcmt
178 } // end namespace clang