FileUtilities.h revision 353358
1193323Sed//===- llvm/Support/FileUtilities.h - File System Utilities -----*- C++ -*-===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This file defines a family of utility functions which are useful for doing 10193323Sed// various things with files. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#ifndef LLVM_SUPPORT_FILEUTILITIES_H 15193323Sed#define LLVM_SUPPORT_FILEUTILITIES_H 16193323Sed 17221345Sdim#include "llvm/Support/FileSystem.h" 18218893Sdim#include "llvm/Support/Path.h" 19193323Sed 20193323Sednamespace llvm { 21193323Sed 22193323Sed /// DiffFilesWithTolerance - Compare the two files specified, returning 0 if 23193323Sed /// the files match, 1 if they are different, and 2 if there is a file error. 24221345Sdim /// This function allows you to specify an absolute and relative FP error that 25193323Sed /// is allowed to exist. If you specify a string to fill in for the error 26193323Sed /// option, it will set the string to an error message if an error occurs, or 27193323Sed /// if the files are different. 28193323Sed /// 29261991Sdim int DiffFilesWithTolerance(StringRef FileA, 30261991Sdim StringRef FileB, 31193323Sed double AbsTol, double RelTol, 32276479Sdim std::string *Error = nullptr); 33193323Sed 34193323Sed 35193323Sed /// FileRemover - This class is a simple object meant to be stack allocated. 36193323Sed /// If an exception is thrown from a region, the object removes the filename 37193323Sed /// specified (if deleteIt is true). 38193323Sed /// 39193323Sed class FileRemover { 40221345Sdim SmallString<128> Filename; 41193323Sed bool DeleteIt; 42193323Sed public: 43206083Srdivacky FileRemover() : DeleteIt(false) {} 44206083Srdivacky 45221345Sdim explicit FileRemover(const Twine& filename, bool deleteIt = true) 46221345Sdim : DeleteIt(deleteIt) { 47221345Sdim filename.toVector(Filename); 48221345Sdim } 49193323Sed 50193323Sed ~FileRemover() { 51193323Sed if (DeleteIt) { 52193323Sed // Ignore problems deleting the file. 53288943Sdim sys::fs::remove(Filename); 54193323Sed } 55193323Sed } 56193323Sed 57206083Srdivacky /// setFile - Give ownership of the file to the FileRemover so it will 58206083Srdivacky /// be removed when the object is destroyed. If the FileRemover already 59206083Srdivacky /// had ownership of a file, remove it first. 60221345Sdim void setFile(const Twine& filename, bool deleteIt = true) { 61221345Sdim if (DeleteIt) { 62221345Sdim // Ignore problems deleting the file. 63288943Sdim sys::fs::remove(Filename); 64221345Sdim } 65206083Srdivacky 66221345Sdim Filename.clear(); 67221345Sdim filename.toVector(Filename); 68206083Srdivacky DeleteIt = deleteIt; 69206083Srdivacky } 70206083Srdivacky 71193323Sed /// releaseFile - Take ownership of the file away from the FileRemover so it 72193323Sed /// will not be removed when the object is destroyed. 73193323Sed void releaseFile() { DeleteIt = false; } 74193323Sed }; 75193323Sed} // End llvm namespace 76193323Sed 77193323Sed#endif 78