[cfe-users] Parsing C++ and creating a module

David Kretzmer via cfe-users cfe-users at lists.llvm.org
Mon Sep 7 05:31:26 PDT 2015


Hello,

I'm trying to create a C++ interpreter. Some time ago I wrote some code
which, if I remember correctly, was working correctly.

----------------------------------------
// Setup compiler
auto context = &llvm::getGlobalContext();

clang::CompilerInstance compiler;
compiler.createDiagnostics();

auto target_options = std::make_shared<clang::TargetOptions>();
target_options->Triple = llvm::sys::getDefaultTargetTriple();
compiler.setTarget(clang::TargetInfo::CreateTargetInfo(compiler.getDiag
nostics(), target_options));

compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
compiler.createPreprocessor(clang::TU_Complete);
compiler.createASTContext();
compiler.createSema(clang::TU_Complete, nullptr);

// Set input and generate code
auto src_buffer = llvm::MemoryBuffer::getMemBuffer("int i = 0;");
clang::FrontendInputFile input{src_buffer.get(),
clang::InputKind::IK_CXX};

std::unique_ptr<clang::CompilerInvocation> cinvoke{new
clang::CompilerInvocation()};
cinvoke->getFrontendOpts().Inputs.push_back(input);
cinvoke->getTargetOpts().Triple = llvm::sys::getProcessTriple();

compiler.setInvocation(cinvoke.release());

std::unique_ptr<clang::CodeGenAction> codegen_action(new
clang::EmitLLVMOnlyAction(context));
if(!compiler.ExecuteAction(*codegen_action))
	std::cerr << "Executing codegen action failed" << std::endl;
----------------------------------------

Now, with clang 3.6, I get a segmentation fault and the following backt
race:

#0  0x000000000124e727 in
llvm::StringMapImpl::LookupBucketFor(llvm::StringRef) ()
#1  0x0000000001047c2d in
clang::PragmaNamespace::AddPragma(clang::PragmaHandler*) ()
#2  0x000000000104ae94 in
clang::Preprocessor::AddPragmaHandler(llvm::StringRef,
clang::PragmaHandler*) ()
#3  0x0000000000a5fa35 in clang::Parser::initializePragmaHandlers() ()
#4  0x0000000000a00aa0 in clang::Parser::Parser(clang::Preprocessor&,
clang::Sema&, bool) ()
#5  0x00000000009f5518 in clang::ParseAST(clang::Sema&, bool, bool) ()
#6  0x000000000069c476 in clang::FrontendAction::Execute() ()
#7  0x0000000000675e59 in
clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) ()
#8  0x000000000066d8c5 in main () at
/home/david/Entwicklung/spielwiese/main.cpp:219

Any ideas? Did I forget to initialize something?
Thanks,
David



More information about the cfe-users mailing list