[cfe-dev] How to check the flow of the clang compiler with an example

Charles Davis cdavis at mymail.mines.edu
Wed Feb 24 14:31:46 PST 2010


kalyan ponnala wrote:
> Hi,
> I would like to go through the clang compiler (the lexer and the parser)
> by compiling a small program like helloworld.c . I am using visual
> studio 2008 solution file for clang/llvm. Can an expert tell me how to
> proceed and the steps that I need to know to do this.
> I am not able to figure out what is the starting program that does the
> lexer and parser stages of the compiler, so that I could put some
> breakpoints that will be hit when we try to compile a program like
> helloworld.
> And I would like to know whats a "cc1". I found it in the code at
> several places.
'cc1' is the actual compiler. It's actually a part of clang.exe, but you
activate it with the '-cc1' switch. What you want to do is debug
clang.exe, but pass it the -cc1 so you can debug the compiler itself. If
you don't pass -cc1, you'll start the driver instead, which spawns the
compiler. You can get around that by debugging child processes, too, but
I think it's easier to just run clang -cc1.

There is a "Lexer" class which is used to read tokens from a source
file, and a "Parser" class which does the parsing. There's also a
"Preprocessor" object which handles preprocessor directives.

The important method in the Lexer class is the Lex() method. It
retrieves the next token. The parser calls this method every time it
wants a token.

The Parser object starts by parsing top-level declarations (i.e. ones at
global scope), which is initiated with the ParseTopLevelDecl() method.

If you set breakpoints on these methods, you can step through the Lexer
and Parser at your leisure.

Chip



More information about the cfe-dev mailing list