How clang works, whant to learn compiler internals

I want to learn clang internals and how it works. But llvm::sys::ExecuteAndWait executes in the debugger and that’s all, finish, console output with my AST (I passes to clang “-ast-dump”). It didn’t jump in the Lexer.cpp, Sema.cpp, etc. What should I do in the debugger, in order to see how Lexer, Sema work?

My steps in the debugger:

  1. compiled debug version of llvm and clang

  2. created a source file “test_deb.cpp” with the following code

enum class Enum : int
{
kOne,
kTwo
};

int f(int x) {
int result = (x / 42);
return result;
}

int main()
{
return f(4);
}

  1. launch clang-4.0 in in the debugger with the following parameters:
    -std=c++1z -Xclang -ast-dump -fsyntax-only test_deb.cpp

  2. and begin to learn how all work:
    the “int main” in driver.cpp

in driver.cpp is called “ExecuteCompilation” of “Driver”
TheDriver.ExecuteCompilation(*C, FailingCommands);

where called “ExecuteJobs” of “Compilation” (the file Compilation.cpp):
C.ExecuteJobs(C.getJobs(), FailingCommands);

in the func ExecuteJobs called ExecuteCommand
if (int Res = ExecuteCommand(Job, FailingCommand)) {

that calls “Execute” of “Command” (the file Jobs.cpp):

where called a func “ExecuteAnddWait” of “llvm::sys”

return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /env/ nullptr,
Redirects, /secondsToWait/ 0,
/memoryLimit/ 0, ErrMsg,
ExecutionFailed);

but the “Executable” is “…path/clang-4.0” - the file, that I launched in the debugger o__O

  1. so llvm::sys::ExecuteAndWait(Executable,…) is called and that’s all, finish, console output with my AST. It didn’t jump in the Lexer.cpp, Sema.cpp, etc.

What should I do in the debugger, in order to see how Lexer, Sema work?

You need to pass argument “-cc1” to the compiler in debug session. In your case parameters must be:
-cc1 -std=c++1z -Xclang -ast-dump -fsyntax-only test_deb.cpp

Without “-cc1” clang works as driver executing other tools to make job, with “-cc1” it works as compiler.

thanks a lot!
with “-cc1” the compiler doesn’t understand “-Xclang” parameter.

-cc1 -std=c++1z -Xclang -ast-dump -fsyntax-only test_deb.cpp

So deleted “-Xclang”, but in that case AST doesn’t output to console.

-cc1 -std=c++1z -ast-dump -fsyntax-only test_deb.cpp

Does someone know what parameters should pass in that case, in order to output AST to console?

thanks a lot!
with "-cc1" the compiler doesn't understand "-Xclang" parameter.

-cc1 -std=c++1z -Xclang -ast-dump -fsyntax-only test_deb.cpp

So deleted "-Xclang", but in that case AST doesn't output to console.

-cc1 -std=c++1z -ast-dump -fsyntax-only test_deb.cpp

Does someone know what parameters should pass in that case, in order to
output AST to console?

You don't need 'fsyntax-only' when dumping the AST, so this should work:

  -cc1 -std=c++1z -ast-dump test_deb.cpp

Ivan,

-Xclang option is for passing arguments to clang directly (like for '-cc1'-style call). With "-cc1" specified you don't need -Xclang, just -cc1 -std=c++1z -ast-dump -fsyntax-only test_deb.cpp.

18.10.2016 11:58, Ivan Kush via cfe-dev пишет:

thanks, know prints to console

Try using the -### option.

$ clang --help
<…>
-### Print (but do not run) the commands to run for this compilation<…>

Awesome, thank you too!

Best regards
Cesar