1 //===-- ARCMT.h - ARC Migration Rewriter ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_ARCMIGRATE_ARCMT_H
10 #define LLVM_CLANG_ARCMIGRATE_ARCMT_H
12 #include "clang/ARCMigrate/FileRemapper.h"
13 #include "clang/Basic/SourceLocation.h"
14 #include "clang/Frontend/CompilerInvocation.h"
18 class DiagnosticConsumer;
19 class PCHContainerOperations;
24 /// Creates an AST with the provided CompilerInvocation but with these
26 /// -if a PCH/PTH is set, the original header is used instead
27 /// -Automatic Reference Counting mode is enabled
29 /// It then checks the AST and produces errors/warning for ARC migration issues
30 /// that the user needs to handle manually.
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.
36 /// \param plistOut if non-empty, it is the file path to store the plist with
37 /// the pre-migration ARC diagnostics.
39 /// \returns false if no error is produced, true otherwise.
41 checkForManualIssues(CompilerInvocation &CI, const FrontendInputFile &Input,
42 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
43 DiagnosticConsumer *DiagClient,
44 bool emitPremigrationARCErrors = false,
45 StringRef plistOut = StringRef());
47 /// Works similar to checkForManualIssues but instead of checking, it
48 /// applies automatic modifications to source files to conform to ARC.
50 /// \returns false if no error is produced, true otherwise.
52 applyTransformations(CompilerInvocation &origCI,
53 const FrontendInputFile &Input,
54 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
55 DiagnosticConsumer *DiagClient);
57 /// Applies automatic modifications and produces temporary files
58 /// and metadata into the \p outputDir path.
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.
64 /// \param plistOut if non-empty, it is the file path to store the plist with
65 /// the pre-migration ARC diagnostics.
67 /// \returns false if no error is produced, true otherwise.
68 bool migrateWithTemporaryFiles(
69 CompilerInvocation &origCI, const FrontendInputFile &Input,
70 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
71 DiagnosticConsumer *DiagClient, StringRef outputDir,
72 bool emitPremigrationARCErrors, StringRef plistOut);
74 /// Get the set of file remappings from the \p outputDir path that
75 /// migrateWithTemporaryFiles produced.
77 /// \returns false if no error is produced, true otherwise.
78 bool getFileRemappings(std::vector<std::pair<std::string,std::string> > &remap,
80 DiagnosticConsumer *DiagClient);
82 /// Get the set of file remappings from a list of files with remapping
85 /// \returns false if no error is produced, true otherwise.
86 bool getFileRemappingsFromFileList(
87 std::vector<std::pair<std::string,std::string> > &remap,
88 ArrayRef<StringRef> remapFiles,
89 DiagnosticConsumer *DiagClient);
91 typedef void (*TransformFn)(MigrationPass &pass);
93 std::vector<TransformFn> getAllTransformations(LangOptions::GCMode OrigGCMode,
94 bool NoFinalizeRemoval);
96 class MigrationProcess {
97 CompilerInvocation OrigCI;
98 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
99 DiagnosticConsumer *DiagClient;
100 FileRemapper Remapper;
105 MigrationProcess(const CompilerInvocation &CI,
106 std::shared_ptr<PCHContainerOperations> PCHContainerOps,
107 DiagnosticConsumer *diagClient,
108 StringRef outputDir = StringRef());
110 class RewriteListener {
112 virtual ~RewriteListener();
114 virtual void start(ASTContext &Ctx) { }
115 virtual void finish() { }
117 virtual void insert(SourceLocation loc, StringRef text) { }
118 virtual void remove(CharSourceRange range) { }
121 bool applyTransform(TransformFn trans, RewriteListener *listener = nullptr);
123 FileRemapper &getRemapper() { return Remapper; }
126 } // end namespace arcmt
128 } // end namespace clang