clang compile from/to memory buffer

I’m looking at the examples/clang-interpreter example. I’d like to expand the interpreter a little.

It looks like it currently only reads from a file. Is it possible to have clang compile from a memory buffer? And to a memory buffer?

Thank you!

Jason

Take a look at llvm/tools/clang/examples/Tooling/ClangCheck.cpp

Nico

Take a look at llvm/tools/clang/examples/Tooling/ClangCheck.cpp

Thank you Nico!

could you elaborate… would I put the C++ source to be compiled in the JsonDatabase MemoryBuffer?

Jason

Examining the code from examples/Tooling/ClangCheck.cpp…

int main(int argc, char **argv) {

llvm::OwningPtrllvm::MemoryBuffer JsonDatabase; # line

llvm::SmallString<1024> JsonDatabasePath(argv[1]);

llvm::sys::path::append(JsonDatabasePath, “compile_commands.json”);

llvm::error_code Result =
llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase);

clang::tooling::CompileCommand LookupResult =
clang::tooling::FindCompileArgsInJsonDatabase(
File.str(), JsonDatabase->getBuffer(), ErrorMessage);

if (!LookupResult.CommandLine.empty()) {
if (!clang::tooling::RunToolWithFlags(
new clang::SyntaxOnlyAction,
LookupResult.CommandLine.size(),
clang::tooling::CommandLineToArgv(
&LookupResult.CommandLine).data())) {

llvm::outs() << "Error while processing " << File << “.\n”;
}
} else {
llvm::outs() << "Skipping " << File << “. Command line not found.\n”;
}
}
return 0;
}

Take a look at llvm/tools/clang/examples/Tooling/ClangCheck.cpp

Thank you Nico!
could you elaborate... would I put the C++ source to be compiled in
the JsonDatabase MemoryBuffer?
Jason
Examining the code from examples/Tooling/ClangCheck.cpp...
int main(int argc, char **argv) {
...
llvm::OwningPtr<llvm::MemoryBuffer> JsonDatabase; # line
llvm::SmallString<1024> JsonDatabasePath(argv[1]);
llvm::sys::path::append(JsonDatabasePath, "compile_commands.json");
llvm::error_code Result =
llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase);
...
clang::tooling::CompileCommand LookupResult =
clang::tooling::FindCompileArgsInJsonDatabase(
File.str(), JsonDatabase->getBuffer(), ErrorMessage);
...
if (!LookupResult.CommandLine.empty()) {
if (!clang::tooling::RunToolWithFlags(

This function is defined in lib/Tooling/Tooling.cpp. If I'm reading
this file right, the function RunSyntaxOnlyToolOnCode() in the same
file does what you want.