|
| 1 | +//===--- MigrationState.h - Migration State --------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This class is an explicit container for a state during migration, its input |
| 14 | +// and output text, as well as what created this state. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_MIGRATOR_MIGRATIONSTATE_H |
| 19 | +#define SWIFT_MIGRATOR_MIGRATIONSTATE_H |
| 20 | + |
| 21 | +#include "swift/Migrator/Replacement.h" |
| 22 | +#include "swift/Syntax/References.h" |
| 23 | +#include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 24 | +#include "llvm/ADT/StringRef.h" |
| 25 | + |
| 26 | +namespace swift { |
| 27 | + |
| 28 | +class SourceManager; |
| 29 | + |
| 30 | +namespace migrator { |
| 31 | + |
| 32 | +enum class MigrationKind { |
| 33 | + /// The compiler has made several passes over the input file and we |
| 34 | + /// applied the suggested fix-its we deemed appropriate. |
| 35 | + CompilerFixits, |
| 36 | +}; |
| 37 | + |
| 38 | +struct MigrationState : public llvm::ThreadSafeRefCountedBase<MigrationState> { |
| 39 | + const MigrationKind Kind; |
| 40 | + SourceManager &SrcMgr; |
| 41 | + |
| 42 | + MigrationState(const MigrationKind Kind, SourceManager &SrcMgr) |
| 43 | + : Kind(Kind), SrcMgr(SrcMgr) {} |
| 44 | + |
| 45 | + MigrationKind getKind() const { |
| 46 | + return Kind; |
| 47 | + } |
| 48 | + |
| 49 | + std::string getInputText() const; |
| 50 | + std::string getOutputText() const; |
| 51 | + |
| 52 | + /// Write all relevant information about the state to OutDir, such as the |
| 53 | + /// input file, output file, replacements, syntax trees, etc. |
| 54 | + bool print(size_t StateNumber, StringRef OutDir) const; |
| 55 | +}; |
| 56 | + |
| 57 | +struct FixitMigrationState final : public MigrationState { |
| 58 | + const unsigned InputBufferID; |
| 59 | + const unsigned OutputBufferID; |
| 60 | + const std::vector<Replacement> Replacements; |
| 61 | + |
| 62 | + // Other information about the migration? |
| 63 | + |
| 64 | + FixitMigrationState(SourceManager &SrcMgr, |
| 65 | + const unsigned InputBufferID, |
| 66 | + const unsigned OutputBufferID, |
| 67 | + const std::vector<Replacement> &Replacements) |
| 68 | + : MigrationState(MigrationKind::CompilerFixits, SrcMgr), |
| 69 | + InputBufferID(InputBufferID), |
| 70 | + OutputBufferID(OutputBufferID), |
| 71 | + Replacements(Replacements) {} |
| 72 | + |
| 73 | + std::string getInputText() const; |
| 74 | + |
| 75 | + unsigned getInputBufferID() const { |
| 76 | + return InputBufferID; |
| 77 | + } |
| 78 | + |
| 79 | + std::string getOutputText() const; |
| 80 | + |
| 81 | + unsigned getOutputBufferID() const { |
| 82 | + return OutputBufferID; |
| 83 | + } |
| 84 | + |
| 85 | + bool print(size_t StateNumber, StringRef OutDir) const; |
| 86 | + |
| 87 | + bool outputDiffersFromInput() const { |
| 88 | + return InputBufferID != OutputBufferID; |
| 89 | + } |
| 90 | + |
| 91 | + /// Dump the remap between the current input and output text. |
| 92 | + void dumpRemap() const; |
| 93 | + |
| 94 | + static RC<FixitMigrationState> |
| 95 | + make(SourceManager &SrcMgr, const unsigned InputBufferID, |
| 96 | + const unsigned OutputBufferID, |
| 97 | + const std::vector<Replacement> &Replacements) { |
| 98 | + return RC<FixitMigrationState> { |
| 99 | + new FixitMigrationState { |
| 100 | + SrcMgr, |
| 101 | + InputBufferID, |
| 102 | + // The input is the output here, because nothing happened yet. |
| 103 | + OutputBufferID, |
| 104 | + Replacements |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + static RC<FixitMigrationState> |
| 110 | + start(SourceManager &SrcMgr, const unsigned InputBufferID) { |
| 111 | + return make(SrcMgr, InputBufferID, InputBufferID, {}); |
| 112 | + } |
| 113 | + |
| 114 | + static bool classof(const MigrationState *MS) { |
| 115 | + return MS->getKind() == MigrationKind::CompilerFixits; |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +} |
| 120 | +} |
| 121 | +#endif // SWIFT_MIGRATOR_MIGRATIONSTATE_H |
0 commit comments