Clang has a number of classes and functions inside its implementation. Most of the time it makes me to be confused to identify the interconnection among classes and functions. Because many functions may be called from different header files. The main thing what i want to know is about main methods of clang. I have seen 4 main functions in clang. But it is difficult for me to identify in which main methods are other functions called. To make it clear let me talk about one class(example StmtPrinter.cpp class). This class prints back source code from AST. Under this class their are two functions i.e PrintPretty(…) and DumpPretty(…). Suppose I want to use printPretty(…) and it is called at different functions. Where is the main method of clang that calls this function to print the source code? Can someone tell me how it works. I tried to greped out on command line but failed to get the main method that calls this function. Even I want to know the command to use this function.
Sorry, it might be silly question, but I failed to get it out. Thanks in advance for your help!.
If you look at Stmt.h, you will find the main declarations for printPretty and dumpPretty (line 305). They are member functions of the Stmt class, so assuming you have a Stmt instance you can just call “S->dumpPretty(Context);”
It’s difficult to give a general rule for everything in Clang – as you know it is quite a complex source base. But hopefully this has helped?
Thanks a lot for your help and sorry for my late reply. Your information is helpful, however what I want to know is the main method of clang that calls the printpretty() function. As you mentioned, lets have S ->printpretty(context). We can see this at StmtPrinter.cpp class as I mentioned earlier. My question is which main method of clang this function is called. As we know at run time there is at least on main method that calls other functions. Assume we want to print the source code of AST. We will run clang on command line. we will use a command(I don’t know the command) that calls the print pretty function. Hope you get my idea. So, which main method/function of clang is calling this function? Thanks again.
Ah, I understand your question better now. Clang has an option “-ast-print” that does what you want. It is a cc1 option though, so you can’t access it directly through the driver.
$ clang –cc1 –help
Will show it, and to use it:
$ clang –Xclang –ast-print –S x.c –o –
The “-Xclang” forces the next argument to be passed directly to the cc1 executable, which will recognise it.
Hope this helps,
Thanks for the command line option. But where is the function called? I mean in which main method of clang it is called? We know that every function must be called at least at one main method/function to be executed. Let me make it clear like this way:
int main ()//main method
So, which main method is calling our printpretty() function? Hope now its clear for you.
The best way to find out where a function is called from is to run
Clang in a debugger, set a breakpoint in the function you are asking
about (StmtPrinter::PrintStmt), and look at the stack trace.
I can’t use debugger. Because am using Linux command line to run clang. There is no debugging option at Linux. Thanks for the idea.
Just type “gdb –args” before your command line.
You’ll need to unwrap the driver invocation from the actual cc1 invocation by giving the –### argument to clang. This will cause it to print out its cc1 command line:
./bin/clang –ast-print –S test.c –o - -###
Then take that and run it under gdb:
Gdb –args “./bin/clang” “-cc1” …
And set a breakpoint where you care:
(gdb) b StmtPrinter.cpp:49
Breakpoint 1, (anonymous namespace)::StmtPrinter::PrintStmt (this=0xffffabd0, S=0xad9a438, SubIndent=2)
Thanks a lot james and Hans, It is really helpful. Have great time!.
Huh, don't you have gdb installed?