1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00

Modernize the .ll parsing interface.

* Use StringRef instead of std::string&
* Return a std::unique_ptr<Module> instead of taking an optional module to write
  to (was not really used).
* Use current comment style.
* Use current naming convention.

llvm-svn: 215989
This commit is contained in:
Rafael Espindola 2014-08-19 16:58:54 +00:00
parent 9e2de65efb
commit 2500d426ff
15 changed files with 71 additions and 82 deletions

View File

@ -14,8 +14,8 @@
#ifndef LLVM_ASMPARSER_PARSER_H
#define LLVM_ASMPARSER_PARSER_H
#include "llvm/ADT/StringRef.h"
#include <memory>
#include <string>
namespace llvm {
@ -30,11 +30,12 @@ class LLVMContext;
/// 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
@ -42,21 +43,21 @@ Module *ParseAssemblyFile(
/// 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

View File

@ -21,25 +21,23 @@
#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()) {
@ -48,13 +46,14 @@ Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
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);
}

View File

@ -29,8 +29,9 @@ namespace llvm {
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;
@ -42,10 +43,10 @@ static Module *getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer,
}
// 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,
@ -58,7 +59,7 @@ 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,
@ -78,9 +79,9 @@ 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,

View File

@ -94,7 +94,7 @@ int main(int argc, char **argv) {
// 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;

View File

@ -168,7 +168,7 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
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;

View File

@ -30,20 +30,16 @@ namespace {
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)

View File

@ -22,18 +22,16 @@ using namespace llvm;
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;

View File

@ -26,18 +26,16 @@ using namespace llvm;
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;

View File

@ -166,15 +166,14 @@ public:
}
};
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 {
@ -200,7 +199,7 @@ class JITTest : public testing::Test {
}
void LoadAssembly(const char *assembly) {
LoadAssemblyInto(M, assembly);
M = loadAssembly(Context, assembly).release();
}
LLVMContext Context;
@ -615,14 +614,13 @@ TEST_F(JITTest, EscapedLazyStubStillCallable) {
// 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;
}

View File

@ -27,8 +27,7 @@ namespace {
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);

View File

@ -186,8 +186,7 @@ namespace llvm {
};
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" \
@ -213,12 +212,12 @@ namespace llvm {
"}\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);

View File

@ -168,7 +168,7 @@ struct TestInvalidationFunctionPass {
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 {

View File

@ -38,7 +38,7 @@ TEST(UseTest, sort) {
"}\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());
@ -83,7 +83,7 @@ TEST(UseTest, reverse) {
"}\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());

View File

@ -65,7 +65,7 @@ TEST(UserTest, ValueOpIteration) {
" 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();

View File

@ -34,7 +34,7 @@ TEST(ValueTest, UsedInBasicBlock) {
" 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");