diff options
Diffstat (limited to 'llvm/examples/OrcV2Examples/ExampleModules.h')
-rw-r--r-- | llvm/examples/OrcV2Examples/ExampleModules.h | 42 |
1 files changed, 27 insertions, 15 deletions
diff --git a/llvm/examples/OrcV2Examples/ExampleModules.h b/llvm/examples/OrcV2Examples/ExampleModules.h index 72b0cc767f11..53da756e15f7 100644 --- a/llvm/examples/OrcV2Examples/ExampleModules.h +++ b/llvm/examples/OrcV2Examples/ExampleModules.h @@ -10,8 +10,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H -#define LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H +#ifndef LLVM_EXAMPLES_ORCV2EXAMPLES_EXAMPLEMODULES_H +#define LLVM_EXAMPLES_ORCV2EXAMPLES_EXAMPLEMODULES_H #include "llvm/ADT/StringRef.h" #include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h" @@ -31,25 +31,37 @@ const llvm::StringRef Add1Example = } )"; +inline llvm::Error createSMDiagnosticError(llvm::SMDiagnostic &Diag) { + using namespace llvm; + std::string Msg; + { + raw_string_ostream OS(Msg); + Diag.print("", OS); + } + return make_error<StringError>(std::move(Msg), inconvertibleErrorCode()); +} + inline llvm::Expected<llvm::orc::ThreadSafeModule> parseExampleModule(llvm::StringRef Source, llvm::StringRef Name) { using namespace llvm; - using namespace llvm::orc; + auto Ctx = std::make_unique<LLVMContext>(); + SMDiagnostic Err; + if (auto M = parseIR(MemoryBufferRef(Source, Name), Err, *Ctx)) + return orc::ThreadSafeModule(std::move(M), std::move(Ctx)); + return createSMDiagnosticError(Err); +} + +inline llvm::Expected<llvm::orc::ThreadSafeModule> +parseExampleModuleFromFile(llvm::StringRef FileName) { + using namespace llvm; auto Ctx = std::make_unique<LLVMContext>(); SMDiagnostic Err; - auto M = parseIR(MemoryBufferRef(Source, Name), Err, *Ctx); - - if (!M) { - std::string ErrMsg; - { - raw_string_ostream ErrStream(ErrMsg); - Err.print("", ErrStream); - } - return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode()); - } - return ThreadSafeModule(std::move(M), std::move(Ctx)); + if (auto M = parseIRFile(FileName, Err, *Ctx)) + return orc::ThreadSafeModule(std::move(M), std::move(Ctx)); + + return createSMDiagnosticError(Err); } -#endif // LLVM_EXAMPLES_HOWTOUSELLJIT_EXAMPLEMODULES_H +#endif // LLVM_EXAMPLES_ORCV2EXAMPLES_EXAMPLEMODULES_H |