1 | //===-- FileRemapper.h - File Remapping Helper ------------------*- 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_FILEREMAPPER_H |
10 | #define LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H |
11 | |
12 | #include "clang/Basic/FileEntry.h" |
13 | #include "clang/Basic/LLVM.h" |
14 | #include "llvm/ADT/DenseMap.h" |
15 | #include "llvm/ADT/STLExtras.h" |
16 | #include "llvm/ADT/StringRef.h" |
17 | #include <memory> |
18 | #include <variant> |
19 | |
20 | namespace llvm { |
21 | class MemoryBuffer; |
22 | class MemoryBufferRef; |
23 | } |
24 | |
25 | namespace clang { |
26 | class FileManager; |
27 | class DiagnosticsEngine; |
28 | class PreprocessorOptions; |
29 | |
30 | namespace arcmt { |
31 | |
32 | class FileRemapper { |
33 | // FIXME: Reuse the same FileManager for multiple ASTContexts. |
34 | std::unique_ptr<FileManager> FileMgr; |
35 | |
36 | using Target = std::variant<FileEntryRef, llvm::MemoryBuffer *>; |
37 | using MappingsTy = llvm::DenseMap<FileEntryRef, Target>; |
38 | MappingsTy FromToMappings; |
39 | |
40 | llvm::DenseMap<const FileEntry *, FileEntryRef> ToFromMappings; |
41 | |
42 | public: |
43 | FileRemapper(); |
44 | ~FileRemapper(); |
45 | |
46 | bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, |
47 | bool ignoreIfFilesChanged); |
48 | bool initFromFile(StringRef filePath, DiagnosticsEngine &Diag, |
49 | bool ignoreIfFilesChanged); |
50 | bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag); |
51 | bool flushToFile(StringRef outputPath, DiagnosticsEngine &Diag); |
52 | |
53 | bool overwriteOriginal(DiagnosticsEngine &Diag, |
54 | StringRef outputDir = StringRef()); |
55 | |
56 | void remap(StringRef filePath, std::unique_ptr<llvm::MemoryBuffer> memBuf); |
57 | |
58 | void applyMappings(PreprocessorOptions &PPOpts) const; |
59 | |
60 | /// Iterate through all the mappings. |
61 | void forEachMapping( |
62 | llvm::function_ref<void(StringRef, StringRef)> CaptureFile, |
63 | llvm::function_ref<void(StringRef, const llvm::MemoryBufferRef &)> |
64 | CaptureBuffer) const; |
65 | |
66 | void clear(StringRef outputDir = StringRef()); |
67 | |
68 | private: |
69 | void remap(FileEntryRef file, std::unique_ptr<llvm::MemoryBuffer> memBuf); |
70 | void remap(FileEntryRef file, FileEntryRef newfile); |
71 | |
72 | OptionalFileEntryRef getOriginalFile(StringRef filePath); |
73 | void resetTarget(Target &targ); |
74 | |
75 | bool report(const Twine &err, DiagnosticsEngine &Diag); |
76 | |
77 | std::string getRemapInfoFile(StringRef outputDir); |
78 | }; |
79 | |
80 | } // end namespace arcmt |
81 | |
82 | } // end namespace clang |
83 | |
84 | #endif |
85 | |