LLVM  8.0.1
FileUtilities.h
Go to the documentation of this file.
1 //===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a family of utility functions which are useful for doing
11 // various things with files.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
16 #define LLVM_SUPPORT_FILEUTILITIES_H
17 
19 #include "llvm/Support/Path.h"
20 
21 namespace llvm {
22 
23  /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if
24  /// the files match, 1 if they are different, and 2 if there is a file error.
25  /// This function allows you to specify an absolute and relative FP error that
26  /// is allowed to exist. If you specify a string to fill in for the error
27  /// option, it will set the string to an error message if an error occurs, or
28  /// if the files are different.
29  ///
30  int DiffFilesWithTolerance(StringRef FileA,
31  StringRef FileB,
32  double AbsTol, double RelTol,
33  std::string *Error = nullptr);
34 
35 
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 
46  explicit FileRemover(const Twine& filename, bool deleteIt = true)
47  : DeleteIt(deleteIt) {
48  filename.toVector(Filename);
49  }
50 
52  if (DeleteIt) {
53  // Ignore problems deleting the file.
54  sys::fs::remove(Filename);
55  }
56  }
57 
58  /// setFile - Give ownership of the file to the FileRemover so it will
59  /// be removed when the object is destroyed. If the FileRemover already
60  /// had ownership of a file, remove it first.
61  void setFile(const Twine& filename, bool deleteIt = true) {
62  if (DeleteIt) {
63  // Ignore problems deleting the file.
64  sys::fs::remove(Filename);
65  }
66 
67  Filename.clear();
68  filename.toVector(Filename);
69  DeleteIt = deleteIt;
70  }
71 
72  /// releaseFile - Take ownership of the file away from the FileRemover so it
73  /// will not be removed when the object is destroyed.
74  void releaseFile() { DeleteIt = false; }
75  };
76 } // End llvm namespace
77 
78 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:24
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
FileRemover(const Twine &filename, bool deleteIt=true)
Definition: FileUtilities.h:46
void releaseFile()
releaseFile - Take ownership of the file away from the FileRemover so it will not be removed when the...
Definition: FileUtilities.h:74
FileRemover - This class is a simple object meant to be stack allocated.
Definition: FileUtilities.h:40
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
int DiffFilesWithTolerance(StringRef FileA, StringRef FileB, double AbsTol, double RelTol, std::string *Error=nullptr)
DiffFilesWithTolerance - Compare the two files specified, returning 0 if the files match...
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Definition: Twine.cpp:33
void setFile(const Twine &filename, bool deleteIt=true)
setFile - Give ownership of the file to the FileRemover so it will be removed when the object is dest...
Definition: FileUtilities.h:61
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:590