1//===-- ARCMT.h - ARC Migration Rewriter ------------------------*- 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
9#ifndef LLVM_CLANG_ARCMIGRATE_ARCMT_H
10#define LLVM_CLANG_ARCMIGRATE_ARCMT_H
11
12#include "clang/ARCMigrate/FileRemapper.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Frontend/CompilerInvocation.h"
15
16namespace clang {
17  class ASTContext;
18  class DiagnosticConsumer;
19  class PCHContainerOperations;
20
21namespace arcmt {
22  class MigrationPass;
23
24/// Creates an AST with the provided CompilerInvocation but with these
25/// changes:
26///   -if a PCH/PTH is set, the original header is used instead
27///   -Automatic Reference Counting mode is enabled
28///
29/// It then checks the AST and produces errors/warning for ARC migration issues
30/// that the user needs to handle manually.
31///
32/// \param emitPremigrationARCErrors if true all ARC errors will get emitted
33/// even if the migrator can fix them, but the function will still return false
34/// if all ARC errors can be fixed.
35///
36/// \param plistOut if non-empty, it is the file path to store the plist with
37/// the pre-migration ARC diagnostics.
38///
39/// \returns false if no error is produced, true otherwise.
40bool
41checkForManualIssues(CompilerInvocation &CI, const FrontendInputFile &Input,
42                     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
43                     DiagnosticConsumer *DiagClient,
44                     bool emitPremigrationARCErrors = false,
45                     StringRef plistOut = StringRef());
46
47/// Works similar to checkForManualIssues but instead of checking, it
48/// applies automatic modifications to source files to conform to ARC.
49///
50/// \returns false if no error is produced, true otherwise.
51bool
52applyTransformations(CompilerInvocation &origCI,
53                     const FrontendInputFile &Input,
54                     std::shared_ptr<PCHContainerOperations> PCHContainerOps,
55                     DiagnosticConsumer *DiagClient);
56
57/// Applies automatic modifications and produces temporary files
58/// and metadata into the \p outputDir path.
59///
60/// \param emitPremigrationARCErrors if true all ARC errors will get emitted
61/// even if the migrator can fix them, but the function will still return false
62/// if all ARC errors can be fixed.
63///
64/// \param plistOut if non-empty, it is the file path to store the plist with
65/// the pre-migration ARC diagnostics.
66///
67/// \returns false if no error is produced, true otherwise.
68bool migrateWithTemporaryFiles(
69    CompilerInvocation &origCI, const FrontendInputFile &Input,
70    std::shared_ptr<PCHContainerOperations> PCHContainerOps,
71    DiagnosticConsumer *DiagClient, StringRef outputDir,
72    bool emitPremigrationARCErrors, StringRef plistOut);
73
74/// Get the set of file remappings from the \p outputDir path that
75/// migrateWithTemporaryFiles produced.
76///
77/// \returns false if no error is produced, true otherwise.
78bool getFileRemappings(std::vector<std::pair<std::string,std::string> > &remap,
79                       StringRef outputDir,
80                       DiagnosticConsumer *DiagClient);
81
82/// Get the set of file remappings from a list of files with remapping
83/// info.
84///
85/// \returns false if no error is produced, true otherwise.
86bool getFileRemappingsFromFileList(
87                        std::vector<std::pair<std::string,std::string> > &remap,
88                        ArrayRef<StringRef> remapFiles,
89                        DiagnosticConsumer *DiagClient);
90
91typedef void (*TransformFn)(MigrationPass &pass);
92
93std::vector<TransformFn> getAllTransformations(LangOptions::GCMode OrigGCMode,
94                                               bool NoFinalizeRemoval);
95
96class MigrationProcess {
97  CompilerInvocation OrigCI;
98  std::shared_ptr<PCHContainerOperations> PCHContainerOps;
99  DiagnosticConsumer *DiagClient;
100  FileRemapper Remapper;
101
102public:
103  bool HadARCErrors;
104
105  MigrationProcess(const CompilerInvocation &CI,
106                   std::shared_ptr<PCHContainerOperations> PCHContainerOps,
107                   DiagnosticConsumer *diagClient,
108                   StringRef outputDir = StringRef());
109
110  class RewriteListener {
111  public:
112    virtual ~RewriteListener();
113
114    virtual void start(ASTContext &Ctx) { }
115    virtual void finish() { }
116
117    virtual void insert(SourceLocation loc, StringRef text) { }
118    virtual void remove(CharSourceRange range) { }
119  };
120
121  bool applyTransform(TransformFn trans, RewriteListener *listener = nullptr);
122
123  FileRemapper &getRemapper() { return Remapper; }
124};
125
126} // end namespace arcmt
127
128}  // end namespace clang
129
130#endif
131