Internals.h 5.4 KB

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