clang  14.0.0git
FileRemapper.h
Go to the documentation of this file.
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/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
17 #include <memory>
18 
19 namespace llvm {
20  class MemoryBuffer;
21  class MemoryBufferRef;
22 }
23 
24 namespace clang {
25  class FileManager;
26  class FileEntry;
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  typedef llvm::PointerUnion<const FileEntry *, llvm::MemoryBuffer *> Target;
37  typedef llvm::DenseMap<const FileEntry *, Target> MappingsTy;
38  MappingsTy FromToMappings;
39 
40  llvm::DenseMap<const FileEntry *, const FileEntry *> 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 
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(const FileEntry *file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
70  void remap(const FileEntry *file, const FileEntry *newfile);
71 
72  const FileEntry *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
llvm
Definition: Dominators.h:30
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::PreprocessorOptions
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Definition: PreprocessorOptions.h:64
clang::arcmt::FileRemapper::clear
void clear(StringRef outputDir=StringRef())
Definition: FileRemapper.cpp:30
Diag
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Definition: LiteralSupport.cpp:78
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::arcmt::FileRemapper::flushToDisk
bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag)
Definition: FileRemapper.cpp:109
clang::arcmt::FileRemapper::initFromFile
bool initFromFile(StringRef filePath, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged)
Definition: FileRemapper.cpp:55
clang::arcmt::FileRemapper::forEachMapping
void forEachMapping(llvm::function_ref< void(StringRef, StringRef)> CaptureFile, llvm::function_ref< void(StringRef, const llvm::MemoryBufferRef &)> CaptureBuffer) const
Iterate through all the mappings.
Definition: FileRemapper.cpp:194
clang::arcmt::FileRemapper::initFromDisk
bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged)
Definition: FileRemapper.cpp:49
clang::arcmt::FileRemapper
Definition: FileRemapper.h:32
clang::arcmt::FileRemapper::FileRemapper
FileRemapper()
Definition: FileRemapper.cpp:22
clang::arcmt::FileRemapper::overwriteOriginal
bool overwriteOriginal(DiagnosticsEngine &Diag, StringRef outputDir=StringRef())
Definition: FileRemapper.cpp:168
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::arcmt::FileRemapper::applyMappings
void applyMappings(PreprocessorOptions &PPOpts) const
Definition: FileRemapper.cpp:209
LLVM.h
clang
Definition: CalledOnceCheck.h:17
clang::arcmt::FileRemapper::~FileRemapper
~FileRemapper()
Definition: FileRemapper.cpp:26
clang::arcmt::FileRemapper::flushToFile
bool flushToFile(StringRef outputPath, DiagnosticsEngine &Diag)
Definition: FileRemapper.cpp:119
clang::arcmt::FileRemapper::remap
void remap(StringRef filePath, std::unique_ptr< llvm::MemoryBuffer > memBuf)
Definition: FileRemapper.cpp:223