LLVM  9.0.0svn
FileUtilities.h
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_SUPPORT_FILEUTILITIES_H
15 #define LLVM_SUPPORT_FILEUTILITIES_H
16 
18 #include "llvm/Support/Path.h"
19 
20 namespace llvm {
21 
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);
33 
34 
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) {}
44 
45  explicit FileRemover(const Twine& filename, bool deleteIt = true)
46  : DeleteIt(deleteIt) {
47  filename.toVector(Filename);
48  }
49 
51  if (DeleteIt) {
52  // Ignore problems deleting the file.
53  sys::fs::remove(Filename);
54  }
55  }
56 
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);
64  }
65 
66  Filename.clear();
67  filename.toVector(Filename);
68  DeleteIt = deleteIt;
69  }
70 
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; }
74  };
75 } // End llvm namespace
76 
77 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
FileRemover(const Twine &filename, bool deleteIt=true)
Definition: FileUtilities.h:45
void releaseFile()
releaseFile - Take ownership of the file away from the FileRemover so it will not be removed when the...
Definition: FileUtilities.h:73
FileRemover - This class is a simple object meant to be stack allocated.
Definition: FileUtilities.h:39
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
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:32
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:60
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:589