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