The LLVM Project Blog

LLVM Project News and Details from the Trenches

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!