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