Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:27

0001 //===-- FileRemapper.h - File Remapping Helper ------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
0010 #define LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
0011 
0012 #include "clang/Basic/FileEntry.h"
0013 #include "clang/Basic/LLVM.h"
0014 #include "llvm/ADT/DenseMap.h"
0015 #include "llvm/ADT/STLExtras.h"
0016 #include "llvm/ADT/StringRef.h"
0017 #include <memory>
0018 #include <variant>
0019 
0020 namespace llvm {
0021   class MemoryBuffer;
0022   class MemoryBufferRef;
0023 }
0024 
0025 namespace clang {
0026   class FileManager;
0027   class DiagnosticsEngine;
0028   class PreprocessorOptions;
0029 
0030 namespace arcmt {
0031 
0032 class FileRemapper {
0033   // FIXME: Reuse the same FileManager for multiple ASTContexts.
0034   std::unique_ptr<FileManager> FileMgr;
0035 
0036   using Target = std::variant<FileEntryRef, llvm::MemoryBuffer *>;
0037   using MappingsTy = llvm::DenseMap<FileEntryRef, Target>;
0038   MappingsTy FromToMappings;
0039 
0040   llvm::DenseMap<const FileEntry *, FileEntryRef> ToFromMappings;
0041 
0042 public:
0043   FileRemapper();
0044   ~FileRemapper();
0045 
0046   bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
0047                     bool ignoreIfFilesChanged);
0048   bool initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
0049                     bool ignoreIfFilesChanged);
0050   bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag);
0051   bool flushToFile(StringRef outputPath, DiagnosticsEngine &Diag);
0052 
0053   bool overwriteOriginal(DiagnosticsEngine &Diag,
0054                          StringRef outputDir = StringRef());
0055 
0056   void remap(StringRef filePath, std::unique_ptr<llvm::MemoryBuffer> memBuf);
0057 
0058   void applyMappings(PreprocessorOptions &PPOpts) const;
0059 
0060   /// Iterate through all the mappings.
0061   void forEachMapping(
0062       llvm::function_ref<void(StringRef, StringRef)> CaptureFile,
0063       llvm::function_ref<void(StringRef, const llvm::MemoryBufferRef &)>
0064           CaptureBuffer) const;
0065 
0066   void clear(StringRef outputDir = StringRef());
0067 
0068 private:
0069   void remap(FileEntryRef file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
0070   void remap(FileEntryRef file, FileEntryRef newfile);
0071 
0072   OptionalFileEntryRef getOriginalFile(StringRef filePath);
0073   void resetTarget(Target &targ);
0074 
0075   bool report(const Twine &err, DiagnosticsEngine &Diag);
0076 
0077   std::string getRemapInfoFile(StringRef outputDir);
0078 };
0079 
0080 } // end namespace arcmt
0081 
0082 }  // end namespace clang
0083 
0084 #endif