#ifndef LLVM_ASMPARSER_PARSER_H
#define LLVM_ASMPARSER_PARSER_H
+#include "llvm/ADT/StringRef.h"
#include <memory>
-#include <string>
namespace llvm {
/// that this does not verify that the generated Module is valid, so you should
/// run the verifier after parsing the file to check that it is okay.
/// @brief Parse LLVM Assembly from a file
-Module *ParseAssemblyFile(
- const std::string &Filename, ///< The name of the file to parse
- SMDiagnostic &Error, ///< Error result info.
- LLVMContext &Context ///< Context in which to allocate globals info.
-);
+/// @param Filename The name of the file to parse
+/// @param Error Error result info.
+/// @param Context Context in which to allocate globals info.
+std::unique_ptr<Module> parseAssemblyFile(StringRef Filename,
+ SMDiagnostic &Error,
+ LLVMContext &Context);
/// The function is a secondary interface to the LLVM Assembly Parser. It parses
/// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
/// that this does not verify that the generated Module is valid, so you should
/// run the verifier after parsing the file to check that it is okay.
/// @brief Parse LLVM Assembly from a string
-Module *ParseAssemblyString(
- const char *AsmString, ///< The string containing assembly
- Module *M, ///< A module to add the assembly too.
- SMDiagnostic &Error, ///< Error result info.
- LLVMContext &Context
-);
+/// @param AsmString The string containing assembly
+/// @param Error Error result info.
+/// @param Context Context in which to allocate globals info.
+std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
+ SMDiagnostic &Error,
+ LLVMContext &Context);
/// This function is the low-level interface to the LLVM Assembly Parser.
/// ParseAssemblyFile and ParseAssemblyString are wrappers around this function.
/// @brief Parse LLVM Assembly from a MemoryBuffer.
-Module *ParseAssembly(
- std::unique_ptr<MemoryBuffer> F, ///< The MemoryBuffer containing assembly
- Module *M, ///< A module to add the assembly too.
- SMDiagnostic &Err, ///< Error result info.
- LLVMContext &Context);
+/// @param F The MemoryBuffer containing assembly
+/// @param Err Error result info.
+/// @param Context Context in which to allocate globals info.
+std::unique_ptr<Module> parseAssembly(std::unique_ptr<MemoryBuffer> F,
+ SMDiagnostic &Err, LLVMContext &Context);
} // End llvm namespace
#include <system_error>
using namespace llvm;
-Module *llvm::ParseAssembly(std::unique_ptr<MemoryBuffer> F, Module *M,
- SMDiagnostic &Err, LLVMContext &Context) {
+std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
+ SMDiagnostic &Err,
+ LLVMContext &Context) {
SourceMgr SM;
MemoryBuffer *Buf = F.get();
SM.AddNewSourceBuffer(F.release(), SMLoc());
- // If we are parsing into an existing module, do it.
- if (M)
- return LLParser(Buf->getBuffer(), SM, Err, M).Run() ? nullptr : M;
-
- // Otherwise create a new module.
- std::unique_ptr<Module> M2(new Module(Buf->getBufferIdentifier(), Context));
- if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run())
+ std::unique_ptr<Module> M =
+ make_unique<Module>(Buf->getBufferIdentifier(), Context);
+ if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run())
return nullptr;
- return M2.release();
+ return std::move(M);
}
-Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
- LLVMContext &Context) {
+std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
+ SMDiagnostic &Err,
+ LLVMContext &Context) {
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Filename);
if (std::error_code EC = FileOrErr.getError()) {
return nullptr;
}
- return ParseAssembly(std::move(FileOrErr.get()), nullptr, Err, Context);
+ return parseAssembly(std::move(FileOrErr.get()), Err, Context);
}
-Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
- SMDiagnostic &Err, LLVMContext &Context) {
- MemoryBuffer *F =
- MemoryBuffer::getMemBuffer(StringRef(AsmString), "<string>");
+std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
+ SMDiagnostic &Err,
+ LLVMContext &Context) {
+ std::unique_ptr<MemoryBuffer> F(
+ MemoryBuffer::getMemBuffer(AsmString, "<string>"));
- return ParseAssembly(std::unique_ptr<MemoryBuffer>(F), M, Err, Context);
+ return parseAssembly(std::move(F), Err, Context);
}
static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
static const char *const TimeIRParsingName = "Parse IR";
-static Module *getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer,
- SMDiagnostic &Err, LLVMContext &Context) {
+static std::unique_ptr<Module>
+getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
+ LLVMContext &Context) {
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) {
std::string ErrMsg;
}
// getLazyBitcodeModule takes ownership of the Buffer when successful.
Buffer.release();
- return ModuleOrErr.get();
+ return std::unique_ptr<Module>(ModuleOrErr.get());
}
- return ParseAssembly(std::move(Buffer), nullptr, Err, Context);
+ return parseAssembly(std::move(Buffer), Err, Context);
}
Module *llvm::getLazyIRFileModule(const std::string &Filename,
return nullptr;
}
- return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
+ return getLazyIRModule(std::move(FileOrErr.get()), Err, Context).release();
}
Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
return M;
}
- return ParseAssembly(std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(
+ return parseAssembly(std::unique_ptr<MemoryBuffer>(MemoryBuffer::getMemBuffer(
Buffer->getBuffer(), Buffer->getBufferIdentifier())),
- nullptr, Err, Context);
+ Err, Context).release();
}
Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
// Parse the file now...
SMDiagnostic Err;
- std::unique_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
+ std::unique_ptr<Module> M = parseAssemblyFile(InputFilename, Err, Context);
if (!M.get()) {
Err.print(argv[0], errs());
return 1;
std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {
DEBUG(dbgs() << " - read assembly\n");
SMDiagnostic Err;
- std::unique_ptr<Module> M(ParseAssemblyFile(Filename, Err, Context));
+ std::unique_ptr<Module> M = parseAssemblyFile(Filename, Err, Context);
if (!M.get())
DEBUG(dbgs() << "error: "; Err.print("verify-use-list-order", dbgs()));
return M;
class IsPotentiallyReachableTest : public testing::Test {
protected:
void ParseAssembly(const char *Assembly) {
- M.reset(new Module("Module", getGlobalContext()));
-
SMDiagnostic Error;
- bool Parsed = ParseAssemblyString(Assembly, M.get(),
- Error, M->getContext()) == M.get();
+ M = parseAssemblyString(Assembly, Error, getGlobalContext());
std::string errMsg;
raw_string_ostream os(errMsg);
Error.print("", os);
- if (!Parsed) {
- // A failure here means that the test itself is buggy.
+ // A failure here means that the test itself is buggy.
+ if (!M)
report_fatal_error(os.str().c_str());
- }
Function *F = M->getFunction("test");
if (F == nullptr)
namespace {
std::unique_ptr<Module> parseAssembly(const char *Assembly) {
- auto M = make_unique<Module>("Module", getGlobalContext());
-
SMDiagnostic Error;
- bool Parsed =
- ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
+ std::unique_ptr<Module> M =
+ parseAssemblyString(Assembly, Error, getGlobalContext());
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
// A failure here means that the test itself is buggy.
- if (!Parsed)
+ if (!M)
report_fatal_error(OS.str().c_str());
return M;
namespace {
std::unique_ptr<Module> parseAssembly(const char *Assembly) {
- auto M = make_unique<Module>("Module", getGlobalContext());
-
SMDiagnostic Error;
- bool Parsed =
- ParseAssemblyString(Assembly, M.get(), Error, M->getContext()) == M.get();
+ std::unique_ptr<Module> M =
+ parseAssemblyString(Assembly, Error, getGlobalContext());
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
// A failure here means that the test itself is buggy.
- if (!Parsed)
+ if (!M)
report_fatal_error(OS.str().c_str());
return M;
}
};
-bool LoadAssemblyInto(Module *M, const char *assembly) {
+std::unique_ptr<Module> loadAssembly(LLVMContext &C, const char *Assembly) {
SMDiagnostic Error;
- bool success =
- nullptr != ParseAssemblyString(assembly, M, Error, M->getContext());
+ std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, C);
std::string errMsg;
raw_string_ostream os(errMsg);
Error.print("", os);
- EXPECT_TRUE(success) << os.str();
- return success;
+ EXPECT_TRUE((bool)M) << os.str();
+ return M;
}
class JITTest : public testing::Test {
}
void LoadAssembly(const char *assembly) {
- LoadAssemblyInto(M, assembly);
+ M = loadAssembly(Context, assembly).release();
}
LLVMContext Context;
// Converts the LLVM assembly to bitcode and returns it in a std::string. An
// empty string indicates an error.
std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) {
- Module TempModule("TempModule", Context);
- if (!LoadAssemblyInto(&TempModule, Assembly)) {
+ std::unique_ptr<Module> TempModule = loadAssembly(Context, Assembly);
+ if (!TempModule)
return "";
- }
std::string Result;
raw_string_ostream OS(Result);
- WriteBitcodeToFile(&TempModule, OS);
+ WriteBitcodeToFile(TempModule.get(), OS);
OS.flush();
return Result;
}
std::unique_ptr<Module> loadAssembly(LLVMContext &Context,
const char *Assembly) {
SMDiagnostic Error;
- std::unique_ptr<Module> Ret(
- ParseAssemblyString(Assembly, nullptr, Error, Context));
+ std::unique_ptr<Module> Ret = parseAssemblyString(Assembly, Error, Context);
std::string errMsg;
raw_string_ostream os(errMsg);
Error.print("", os);
};
char DPass::ID = 0;
-
- Module* makeLLVMModule(DPass *P) {
+ std::unique_ptr<Module> makeLLVMModule(DPass *P) {
const char *ModuleStrig =
"declare i32 @g()\n" \
"define void @f(i32 %x) {\n" \
"}\n";
LLVMContext &C = getGlobalContext();
SMDiagnostic Err;
- return ParseAssemblyString(ModuleStrig, nullptr, Err, C);
+ return parseAssemblyString(ModuleStrig, Err, C);
}
TEST(DominatorTree, Unreachable) {
DPass *P = new DPass();
- std::unique_ptr<Module> M(makeLLVMModule(P));
+ std::unique_ptr<Module> M = makeLLVMModule(P);
PassManager Passes;
Passes.add(P);
Passes.run(*M);
Module *parseIR(const char *IR) {
LLVMContext &C = getGlobalContext();
SMDiagnostic Err;
- return ParseAssemblyString(IR, nullptr, Err, C);
+ return parseAssemblyString(IR, Err, C).release();
}
class PassManagerTest : public ::testing::Test {
"}\n";
SMDiagnostic Err;
char vnbuf[8];
- Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
+ std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Function *F = M->getFunction("f");
ASSERT_TRUE(F);
ASSERT_TRUE(F->arg_begin() != F->arg_end());
"}\n";
SMDiagnostic Err;
char vnbuf[8];
- Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
+ std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Function *F = M->getFunction("f");
ASSERT_TRUE(F);
ASSERT_TRUE(F->arg_begin() != F->arg_end());
" ret void\n"
"}\n";
SMDiagnostic Err;
- Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
+ std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Function *F = M->getFunction("f");
BasicBlock &ExitBB = F->back();
" ret void\n"
"}\n";
SMDiagnostic Err;
- Module *M = ParseAssemblyString(ModuleString, nullptr, Err, C);
+ std::unique_ptr<Module> M = parseAssemblyString(ModuleString, Err, C);
Function *F = M->getFunction("f");