[bug?] How to use EmitLLVMOnlyAction in clang tool?

When trying to run an EmitLLVMOnlyAction via ClangTool::run(act_factory) I get the following error:

Clang: Not enough positional command line arguments specified!

Stepping through the code I found that there seems to be a problem in setCommandLineOpts in clang/lib/CodeGen/BackendUtils.cpp, the BackendArgs.push_back with literals doesn’t seem to work and a corrupt SmallVector is passed to ParseCommandLineOptions.

Why is setCommandLineOpts even called, isn’t EmitLLVMOnlyAction supposed to create llvm::Module in memory only without without outputting it to a file? Did I misinterpret the Emit_Nothing flag? What does EmitLLVMOnlyAction do?

What command line args does EmitLLVMOnlyAction expect?

Cmd line: mytool test.cpp -p=compile_commands.json

Compile_commands:

{

“directory”:“path/to/build”,

“command”: “/usr/local/bin/clang++ -Ipath/to/include - std=c++11 -Wall -o path/to/test.cpp.o -c /path/to/test.cpp”,

“file”: “/path/to/test.cpp”

}

Thanks,

Viktor

I’ve realized now that the vector is not a Small String equivalent but is a vector of separate char pointers (argv).

Still, for some reason executing an EmitLLVMOnlyAction from a clang tool seems to fail for some reason.

Lldb always exits in CommandLineParser::ParseCommandLineOptions (lib/Support/CommandLine.cpp)

Line 1088 (clang 6.0.1):

‘Program Name = sys::path::filename(StringRef(argv[0]));’

The error…

‘Clang: Not enough positional command line arguments specified!’

…makes no sense at this particular line.

The only usage of EmitLLVMOnlyAction in the llvm/clang repos seems to be in the clang-interpreter example and for some reason it works there.

Is there some setup missing or present in the ClangTool that screws something up?

gist.github.com/MaliusArth/876db831fca43288bfa6fb78ab6921df

Cheers,

Viktor

Could maybe someone try to reproduce this using the gist:

gist.github.com/MaliusArth/876db831fca43288bfa6fb78ab6921df

I’m using version 6.0.1.

Thanks,

Viktor