FileRemapper.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- FileRemapper.h - File Remapping Helper ------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
  14. #define LLVM_CLANG_ARCMIGRATE_FILEREMAPPER_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/PointerUnion.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/StringRef.h"
  20. #include <memory>
  21. namespace llvm {
  22. class MemoryBuffer;
  23. class MemoryBufferRef;
  24. }
  25. namespace clang {
  26. class FileManager;
  27. class FileEntry;
  28. class DiagnosticsEngine;
  29. class PreprocessorOptions;
  30. namespace arcmt {
  31. class FileRemapper {
  32. // FIXME: Reuse the same FileManager for multiple ASTContexts.
  33. std::unique_ptr<FileManager> FileMgr;
  34. typedef llvm::PointerUnion<const FileEntry *, llvm::MemoryBuffer *> Target;
  35. typedef llvm::DenseMap<const FileEntry *, Target> MappingsTy;
  36. MappingsTy FromToMappings;
  37. llvm::DenseMap<const FileEntry *, const FileEntry *> ToFromMappings;
  38. public:
  39. FileRemapper();
  40. ~FileRemapper();
  41. bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
  42. bool ignoreIfFilesChanged);
  43. bool initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
  44. bool ignoreIfFilesChanged);
  45. bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag);
  46. bool flushToFile(StringRef outputPath, DiagnosticsEngine &Diag);
  47. bool overwriteOriginal(DiagnosticsEngine &Diag,
  48. StringRef outputDir = StringRef());
  49. void remap(StringRef filePath, std::unique_ptr<llvm::MemoryBuffer> memBuf);
  50. void applyMappings(PreprocessorOptions &PPOpts) const;
  51. /// Iterate through all the mappings.
  52. void forEachMapping(
  53. llvm::function_ref<void(StringRef, StringRef)> CaptureFile,
  54. llvm::function_ref<void(StringRef, const llvm::MemoryBufferRef &)>
  55. CaptureBuffer) const;
  56. void clear(StringRef outputDir = StringRef());
  57. private:
  58. void remap(const FileEntry *file, std::unique_ptr<llvm::MemoryBuffer> memBuf);
  59. void remap(const FileEntry *file, const FileEntry *newfile);
  60. const FileEntry *getOriginalFile(StringRef filePath);
  61. void resetTarget(Target &targ);
  62. bool report(const Twine &err, DiagnosticsEngine &Diag);
  63. std::string getRemapInfoFile(StringRef outputDir);
  64. };
  65. } // end namespace arcmt
  66. } // end namespace clang
  67. #endif
  68. #ifdef __GNUC__
  69. #pragma GCC diagnostic pop
  70. #endif