Skip to content

Instantly share code, notes, and snippets.

@Asher-
Created January 12, 2018 16:48
Show Gist options
  • Save Asher-/43b06e8cdbf025588fa305acbec8e83b to your computer and use it in GitHub Desktop.
Save Asher-/43b06e8cdbf025588fa305acbec8e83b to your computer and use it in GitHub Desktop.
const llvm::Module&
compileString(
std::string source_string
) {
std::vector<std::string> args = {
"/usr/local/Toolchains/LLVM5.0.0.xctoolchain/usr/bin/clang++",
"-std=c++1z",
"-isystem", "/usr/local/Toolchains/LLVM5.0.0.xctoolchain/usr/include/c++/v1",
"-isystem", "/usr/local/Toolchains/LLVM5.0.0.xctoolchain/usr/include",
"-isystem", "/usr/local/Toolchains/LLVM5.0.0.xctoolchain/usr/lib/clang/5.0.0/include",
"-isystem", "/usr/include",
"-isystem", "/usr/local/include",
"-isystem", "/Users/asher/Projects/strongai",
"-isystem", "/Users/asher/Projects/strongai/test",
"-isystem", "/virtual",
"/virtual/source_file.cpp"
};
llvm::IntrusiveRefCntPtr<clang::vfs::OverlayFileSystem> OverlayFileSystem( new clang::vfs::OverlayFileSystem(clang::vfs::getRealFileSystem()) );
llvm::IntrusiveRefCntPtr<clang::vfs::InMemoryFileSystem> InMemoryFileSystem( new clang::vfs::InMemoryFileSystem );
OverlayFileSystem->pushOverlay( InMemoryFileSystem );
llvm::IntrusiveRefCntPtr<clang::FileManager> Files( new clang::FileManager( clang::FileSystemOptions(), OverlayFileSystem ) );
std::unique_ptr<clang::CodeGenAction> code_generator( new clang::EmitLLVMOnlyAction( & TheContext ) );
clang::tooling::ToolInvocation Invocation( args, code_generator.get(), Files.get() );
InMemoryFileSystem->addFile( "/virtual/source_file.cpp", 0, llvm::MemoryBuffer::getMemBuffer( source_string.c_str() ) );
if ( ! Invocation.run() ) throw "Compilation failed!";
llvm::Module& compilation_unit = *code_generator->takeModule();
return compilation_unit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment