这个过程简单的分为几步。git
第一步:写一个简单的小程序,hello.c。内容以下:github
#include<stdio.h>
int main() {
printf("Hello world!\n");
}
小程序
第二步:找出如何在LLVM里面输出信息,最后选择采用llvm::errs(),等于采用了LLVM的错误机制。包括llvm::errs()所需的头文件。具体内容以下:函数
//shining add endcode
注:为了标明代码是我本身添加的,因此在添加的log信息先后都加了注释。get
第三步:采用clang3.9.0代码,在我知道的几个点随意添加了几个log信息,信息内容如上一步所示,主要是标明插入log的函数的相关信息,都放在了函数的第一条语句处。编译该clang以后,使用命令clang hello.c -o hello编译hello.c,能够获得以下输出信息:源码
clang/tools/driver/driver.cpp/main()/shining_addit
clang/tools/driver/driver.cpp/main()/shining_add
clang/tools/driver/cc1_main.cpp/cc1_main()/shining_add
clang/lib/CodeGen/CodeGenAction.cpp/CodeGenAction::ExecuteAction()/shining_add
clang/lib/Sema/Sema.cpp/Sema::Sema()/shining_add
clang/lib/Parse/ParseAST.cpp/ParseAST(Sema)/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
clang/lib/Lex/Lexer.cpp/Lexer::InitLexer()/shining_add
基本上我所插入的log信息都打印出来了,这里能够看到clang编译一个简单的程序,所经历的大的点。后续会在此基础之上,再分块进行分析代码和流程。开这么个头,方便本身,也为想读clang源码的朋友们提供一个思路。这个思路简单易行,根据我提供的几个点,也能够继续深刻去分析本身感兴趣的地方。
io
代码和修改后的代码,我也建了一个代码库,地址:https://github.com/shining1984/clang_code_comment编译