FileUtilities.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Support/FileUtilities.h - File System Utilities -----*- 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. //
  14. // This file defines a family of utility functions which are useful for doing
  15. // various things with files.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_SUPPORT_FILEUTILITIES_H
  19. #define LLVM_SUPPORT_FILEUTILITIES_H
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/Error.h"
  22. #include "llvm/Support/FileSystem.h"
  23. #include <system_error>
  24. namespace llvm {
  25. /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
  26. /// the files match, 1 if they are different, and 2 if there is a file error.
  27. /// This function allows you to specify an absolute and relative FP error that
  28. /// is allowed to exist. If you specify a string to fill in for the error
  29. /// option, it will set the string to an error message if an error occurs, or
  30. /// if the files are different.
  31. ///
  32. int DiffFilesWithTolerance(StringRef FileA,
  33. StringRef FileB,
  34. double AbsTol, double RelTol,
  35. std::string *Error = nullptr);
  36. /// FileRemover - This class is a simple object meant to be stack allocated.
  37. /// If an exception is thrown from a region, the object removes the filename
  38. /// specified (if deleteIt is true).
  39. ///
  40. class FileRemover {
  41. SmallString<128> Filename;
  42. bool DeleteIt;
  43. public:
  44. FileRemover() : DeleteIt(false) {}
  45. explicit FileRemover(const Twine& filename, bool deleteIt = true)
  46. : DeleteIt(deleteIt) {
  47. filename.toVector(Filename);
  48. }
  49. ~FileRemover() {
  50. if (DeleteIt) {
  51. // Ignore problems deleting the file.
  52. sys::fs::remove(Filename);
  53. }
  54. }
  55. /// setFile - Give ownership of the file to the FileRemover so it will
  56. /// be removed when the object is destroyed. If the FileRemover already
  57. /// had ownership of a file, remove it first.
  58. void setFile(const Twine& filename, bool deleteIt = true) {
  59. if (DeleteIt) {
  60. // Ignore problems deleting the file.
  61. sys::fs::remove(Filename);
  62. }
  63. Filename.clear();
  64. filename.toVector(Filename);
  65. DeleteIt = deleteIt;
  66. }
  67. /// releaseFile - Take ownership of the file away from the FileRemover so it
  68. /// will not be removed when the object is destroyed.
  69. void releaseFile() { DeleteIt = false; }
  70. };
  71. enum class atomic_write_error {
  72. failed_to_create_uniq_file = 0,
  73. output_stream_error,
  74. failed_to_rename_temp_file
  75. };
  76. class AtomicFileWriteError : public llvm::ErrorInfo<AtomicFileWriteError> {
  77. public:
  78. AtomicFileWriteError(atomic_write_error Error) : Error(Error) {}
  79. void log(raw_ostream &OS) const override;
  80. const atomic_write_error Error;
  81. static char ID;
  82. private:
  83. // Users are not expected to use error_code.
  84. std::error_code convertToErrorCode() const override {
  85. return llvm::inconvertibleErrorCode();
  86. }
  87. };
  88. // atomic_write_error + whatever the Writer can return
  89. /// Creates a unique file with name according to the given \p TempPathModel,
  90. /// writes content of \p Buffer to the file and renames it to \p FinalPath.
  91. ///
  92. /// \returns \c AtomicFileWriteError in case of error.
  93. llvm::Error writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
  94. StringRef Buffer);
  95. llvm::Error
  96. writeFileAtomically(StringRef TempPathModel, StringRef FinalPath,
  97. std::function<llvm::Error(llvm::raw_ostream &)> Writer);
  98. } // End llvm namespace
  99. #endif
  100. #ifdef __GNUC__
  101. #pragma GCC diagnostic pop
  102. #endif