Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Support / FileUtilities.h
blob16b2206924c3d8b9f4c84ad3012bfaf6350ceb52
1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- 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 // This file defines a family of utility functions which are useful for doing
10 // various things with files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
15 #define LLVM_SUPPORT_FILEUTILITIES_H
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
20 namespace llvm {
22 /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
23 /// the files match, 1 if they are different, and 2 if there is a file error.
24 /// This function allows you to specify an absolute and relative FP error that
25 /// is allowed to exist. If you specify a string to fill in for the error
26 /// option, it will set the string to an error message if an error occurs, or
27 /// if the files are different.
28 ///
29 int DiffFilesWithTolerance(StringRef FileA,
30 StringRef FileB,
31 double AbsTol, double RelTol,
32 std::string *Error = nullptr);
35 /// FileRemover - This class is a simple object meant to be stack allocated.
36 /// If an exception is thrown from a region, the object removes the filename
37 /// specified (if deleteIt is true).
38 ///
39 class FileRemover {
40 SmallString<128> Filename;
41 bool DeleteIt;
42 public:
43 FileRemover() : DeleteIt(false) {}
45 explicit FileRemover(const Twine& filename, bool deleteIt = true)
46 : DeleteIt(deleteIt) {
47 filename.toVector(Filename);
50 ~FileRemover() {
51 if (DeleteIt) {
52 // Ignore problems deleting the file.
53 sys::fs::remove(Filename);
57 /// setFile - Give ownership of the file to the FileRemover so it will
58 /// be removed when the object is destroyed. If the FileRemover already
59 /// had ownership of a file, remove it first.
60 void setFile(const Twine& filename, bool deleteIt = true) {
61 if (DeleteIt) {
62 // Ignore problems deleting the file.
63 sys::fs::remove(Filename);
66 Filename.clear();
67 filename.toVector(Filename);
68 DeleteIt = deleteIt;
71 /// releaseFile - Take ownership of the file away from the FileRemover so it
72 /// will not be removed when the object is destroyed.
73 void releaseFile() { DeleteIt = false; }
75 } // End llvm namespace
77 #endif