1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
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.
29 int DiffFilesWithTolerance(StringRef FileA
,
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).
40 SmallString
<128> Filename
;
43 FileRemover() : DeleteIt(false) {}
45 explicit FileRemover(const Twine
& filename
, bool deleteIt
= true)
46 : DeleteIt(deleteIt
) {
47 filename
.toVector(Filename
);
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) {
62 // Ignore problems deleting the file.
63 sys::fs::remove(Filename
);
67 filename
.toVector(Filename
);
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