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