1cde4d5a6SJacques Pienaar //===- FileUtilities.cpp - utilities for working with files ---------------===//
26e1a050fSAlex Zinenko //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66e1a050fSAlex Zinenko //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
86e1a050fSAlex Zinenko //
96e1a050fSAlex Zinenko // Definitions of common utilities for working with files.
106e1a050fSAlex Zinenko //
116e1a050fSAlex Zinenko //===----------------------------------------------------------------------===//
126e1a050fSAlex Zinenko
135204259fSMehdi Amini #include "mlir/Support/FileUtilities.h"
145204259fSMehdi Amini #include "mlir/Support/LLVM.h"
15c9845735SJohannes Reifferscheid #include "llvm/Support/Alignment.h"
166e1a050fSAlex Zinenko #include "llvm/Support/FileUtilities.h"
17ac5a50e1SLei Zhang #include "llvm/Support/MemoryBuffer.h"
186e1a050fSAlex Zinenko #include "llvm/Support/ToolOutputFile.h"
196e1a050fSAlex Zinenko
206e1a050fSAlex Zinenko using namespace mlir;
216e1a050fSAlex Zinenko
22858a6ec3SRiver Riddle static std::unique_ptr<llvm::MemoryBuffer>
openInputFileImpl(StringRef inputFilename,std::string * errorMessage,std::optional<llvm::Align> alignment)23858a6ec3SRiver Riddle openInputFileImpl(StringRef inputFilename, std::string *errorMessage,
24*b1df3a2cSFangrui Song std::optional<llvm::Align> alignment) {
25858a6ec3SRiver Riddle auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(
26858a6ec3SRiver Riddle inputFilename, /*IsText=*/false, /*RequiresNullTerminator=*/true,
27858a6ec3SRiver Riddle alignment);
28ac5a50e1SLei Zhang if (std::error_code error = fileOrErr.getError()) {
29ac5a50e1SLei Zhang if (errorMessage)
30ac5a50e1SLei Zhang *errorMessage = "cannot open input file '" + inputFilename.str() +
31ac5a50e1SLei Zhang "': " + error.message();
32ac5a50e1SLei Zhang return nullptr;
33ac5a50e1SLei Zhang }
34ac5a50e1SLei Zhang
35ac5a50e1SLei Zhang return std::move(*fileOrErr);
36ac5a50e1SLei Zhang }
37858a6ec3SRiver Riddle std::unique_ptr<llvm::MemoryBuffer>
openInputFile(StringRef inputFilename,std::string * errorMessage)38858a6ec3SRiver Riddle mlir::openInputFile(StringRef inputFilename, std::string *errorMessage) {
39858a6ec3SRiver Riddle return openInputFileImpl(inputFilename, errorMessage,
401a36588eSKazu Hirata /*alignment=*/std::nullopt);
41858a6ec3SRiver Riddle }
42858a6ec3SRiver Riddle std::unique_ptr<llvm::MemoryBuffer>
openInputFile(llvm::StringRef inputFilename,llvm::Align alignment,std::string * errorMessage)43858a6ec3SRiver Riddle mlir::openInputFile(llvm::StringRef inputFilename, llvm::Align alignment,
44858a6ec3SRiver Riddle std::string *errorMessage) {
45858a6ec3SRiver Riddle return openInputFileImpl(inputFilename, errorMessage, alignment);
46858a6ec3SRiver Riddle }
47ac5a50e1SLei Zhang
486e1a050fSAlex Zinenko std::unique_ptr<llvm::ToolOutputFile>
openOutputFile(StringRef outputFilename,std::string * errorMessage)49ac5a50e1SLei Zhang mlir::openOutputFile(StringRef outputFilename, std::string *errorMessage) {
506e1a050fSAlex Zinenko std::error_code error;
5179f53b0cSJacques Pienaar auto result = std::make_unique<llvm::ToolOutputFile>(outputFilename, error,
52518d955fSDuncan P. N. Exon Smith llvm::sys::fs::OF_None);
536e1a050fSAlex Zinenko if (error) {
54ac5a50e1SLei Zhang if (errorMessage)
55ac5a50e1SLei Zhang *errorMessage = "cannot open output file '" + outputFilename.str() +
56ac5a50e1SLei Zhang "': " + error.message();
576e1a050fSAlex Zinenko return nullptr;
586e1a050fSAlex Zinenko }
596e1a050fSAlex Zinenko
606e1a050fSAlex Zinenko return result;
616e1a050fSAlex Zinenko }
62