diff options
| author | Lei Zhang <antiagainst@google.com> | 2019-01-11 07:22:57 -0800 |
|---|---|---|
| committer | jpienaar <jpienaar@google.com> | 2019-03-29 15:09:11 -0700 |
| commit | ac5a50e1e4f7545aa7e7abc18bfb6ccf80a36795 (patch) | |
| tree | b04151b405498acb825e2064e5dc92bb4ac8fd75 /mlir/lib/Support | |
| parent | e8d0e1f72a093a12ccac613bb1811bb6f3e0c606 (diff) | |
| download | bcm5719-llvm-ac5a50e1e4f7545aa7e7abc18bfb6ccf80a36795.tar.gz bcm5719-llvm-ac5a50e1e4f7545aa7e7abc18bfb6ccf80a36795.zip | |
Extract openInputFile() into Support/FileUtilities
Multiple binaries have the needs to open input files. Use this function
to de-duplicate the code.
Also changed openOutputFile() to return errors using std::string since
it is a library call and accessing I/O in library call is not friendly.
PiperOrigin-RevId: 228878221
Diffstat (limited to 'mlir/lib/Support')
| -rw-r--r-- | mlir/lib/Support/FileUtilities.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/mlir/lib/Support/FileUtilities.cpp b/mlir/lib/Support/FileUtilities.cpp index e9717abfc0d..e5eba800179 100644 --- a/mlir/lib/Support/FileUtilities.cpp +++ b/mlir/lib/Support/FileUtilities.cpp @@ -20,18 +20,35 @@ //===----------------------------------------------------------------------===// #include "mlir/Support/FileUtilities.h" +#include "mlir/Support/LLVM.h" #include "llvm/Support/FileUtilities.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ToolOutputFile.h" using namespace mlir; +std::unique_ptr<llvm::MemoryBuffer> +mlir::openInputFile(StringRef inputFilename, std::string *errorMessage) { + auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(inputFilename); + if (std::error_code error = fileOrErr.getError()) { + if (errorMessage) + *errorMessage = "cannot open input file '" + inputFilename.str() + + "': " + error.message(); + return nullptr; + } + + return std::move(*fileOrErr); +} + std::unique_ptr<llvm::ToolOutputFile> -mlir::openOutputFile(StringRef outputFilename) { +mlir::openOutputFile(StringRef outputFilename, std::string *errorMessage) { std::error_code error; auto result = llvm::make_unique<llvm::ToolOutputFile>(outputFilename, error, llvm::sys::fs::F_None); if (error) { - llvm::errs() << error.message(); + if (errorMessage) + *errorMessage = "cannot open output file '" + outputFilename.str() + + "': " + error.message(); return nullptr; } |

