Генерация IR из Clang с использованием классов - PullRequest
0 голосов
/ 20 февраля 2020

Я пытаюсь сгенерировать LLVM IR, используя классы Clang. Я довольно новичок в этом, и до сих пор я использовал этот код в качестве ссылки, который, в свою очередь, скопирован из Метод для создания LLVM IR

Приведенная выше ссылка довольно старая, и я изменил свой код здесь и там в соответствии с новыми сигнатурами

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Basic/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>



using namespace std;
using namespace clang;

clang::CodeGenAction * getAction(void)
{

    llvm::LLVMContext *llvmcx;
    static llvm::LLVMContext MyGlobalContext;
    llvmcx = &MyGlobalContext;
    // Path to the C file
    string inputPath = "test.c";


    // Arguments to pass to the clang frontend
    vector<const char *> args;
    args.push_back(inputPath.c_str());

    // The compiler invocation needs a DiagnosticsEngine so it can report problems
    llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> opt(new clang::DiagnosticOptions());
    clang::DiagnosticConsumer *client(new DiagnosticConsumer());
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());
    clang::DiagnosticsEngine Diags(DiagID, opt, client);

    // Create the compiler invocation
    shared_ptr<clang::CompilerInvocation> CI(new clang::CompilerInvocation());
    clang::CompilerInvocation::CreateFromArgs(*CI, &args[0], &args[0] + args.size(),Diags);

    // Create the compiler instance
    clang::CompilerInstance Clang;
    Clang.setInvocation(CI);

    // Get ready to report problems
    Clang.createDiagnostics(&Diags.getDiagnosticOptions());
    if (!Clang.hasDiagnostics())
          return NULL;
    // Create an action and make the compiler instance carry it out
    clang::CodeGenAction *Act = new clang::EmitLLVMOnlyAction(llvmcx);
    if (!Clang.ExecuteAction(*Act))
        return NULL;

    return Act;
}

int main(void)
{
    clang::CodeGenAction* Act = getAction();

    Grab the module built by the EmitLLVMOnlyAction
    auto module = Act->takeModule();

    // Print all functions in the module
    for (llvm::Module::FunctionListType::iterator i = module->getFunctionList().begin(); i != module->getFunctionList().end(); ++i)
        printf("%s\n", i->getName().str().c_str());

    return 0;
}

Но getAction() возвращает NULL. Любая идея о том, где я иду не так?

...