Static analysis tools: using Clang in CppDepend

Static analysis tools: using Clang in CppDepend

Static analysis is a method of computer program debugging that is done by examining the code without executing the program. The process provides an understanding of the code structure, can help to ensure that the code adheres to industry standards, and can find bugs not easy to detect.

To develop a C / C++ static analysis tool, a parser is needed to parse the source code. C++ is a very powerful language but its syntax is a little bit complicated, what makes the parser not easy to develop.

When we began the development of CppDepend about four years ago we needed a reliable C / C++ parser. At that time, Clang was an option but was not widely used and we didn’t know if it would ultimately develop into a fully-featured compiler frontend.

Last year, for the major release of CppDepend 3.0, we re-evaluated our C / C++ parser with a goal of getting more reliable results. We checked Clang to see where its evolution went and were very surprised that it now implements virtually all C++'11 features and became very popular. Clang now provide solid infrastructure to write tools that need syntactic and semantic information about a program.

Clang is designed to be modular, and like other compilers it has three phases:

  • The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code.
  • The optimizer: its goal is to do some optimization on the AST generated by the front end.
  • The back end: generates the final code to be executed by the machine, it depends of the target architecture.

In our case we needed only to use the front end parser, and we use the generated AST. For that we added our custom ASTFrontEndAction.
Each ASTFrontEndAction create one or many ASTConsumer as shown by the following dependency gaph:

ASTConsumer is an abstract class, and we have to implement our AST consumer for our specific needs. And to do that we had take a look at the implementation of the existing AST Consumers to understand how they works, and here are all the classes inheriting from ASTConsumer:

And we took ASTPrinter and ASTDumpXML as examples to understand how to implements our specific AST consumer.

Thanks to all the developers behind LLVM and Clang for their big efforts to deliver a powerful compiler, and to thank all open source C / C++ contributors, we are giving you a CppDepend “Pro license” for free. CppDepend can be used to analyze a code base in order to:

• Improve the code base quality.

• Help new contributors to understand the existing code base.

• Automate the code review to be sure that all the coding rules specified by the manager are satisfied.

• Generate custom reports for documentation purpose.

To get your CppDepend Pro license, you can just send us a mail at support@cppdepend.com with the following infos:

• Name of the contributor.

• Link to the patches that you have contributed.

Thanks!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值