Add a parserSourceFile function that takes a file path directly

This avoids adding boilerplate around the SourceMgr on the client.

PiperOrigin-RevId: 239918122
This commit is contained in:
Mehdi Amini 2019-03-22 22:17:10 -07:00 committed by jpienaar
parent 94764ee036
commit 0f5602ae3d
2 changed files with 22 additions and 0 deletions

View File

@ -37,6 +37,11 @@ class MLIRContext;
/// the error handler registered in the context, and a null pointer is returned.
Module *parseSourceFile(const llvm::SourceMgr &sourceMgr, MLIRContext *context);
/// This parses the file specified by the indicated filename and returns an
/// MLIR module if it was valid. If not, the error message is emitted through
/// the error handler registered in the context, and a null pointer is returned.
Module *parseSourceFile(llvm::StringRef filename, MLIRContext *context);
/// This parses the module string to a MLIR module if it was valid. If not, the
/// error message is emitted through / the error handler registered in the
/// context, and a null pointer is returned.

View File

@ -3743,6 +3743,23 @@ Module *mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
return module.release();
}
/// This parses the file specified by the indicated filename and returns an
/// MLIR module if it was valid. If not, the error message is emitted through
/// the error handler registered in the context, and a null pointer is returned.
Module *parseSourceFile(StringRef filename, MLIRContext *context) {
auto file_or_err = llvm::MemoryBuffer::getFile(filename);
if (std::error_code error = file_or_err.getError()) {
context->emitError(mlir::UnknownLoc::get(context),
"Could not open input file " + filename);
return nullptr;
}
// Load the MLIR module.
llvm::SourceMgr source_mgr;
source_mgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
return parseSourceFile(source_mgr, context);
}
/// This parses the program string to a MLIR module if it was valid. If not,
/// it emits diagnostics and returns null.
Module *mlir::parseSourceString(StringRef moduleStr, MLIRContext *context) {