# Static analysis tools: using Clang in CppDepend

DevFeed: [Static analysis tools: using Clang in CppDepend](<https://devfeed.tech/articles/static-analysis-tools-using-clang-in-cppdepend-42879.md>)

Original publisher: [Read original article](<https://blog.llvm.org/2013/04/static-analysis-tools-using-clang-in.html>)

Author: Issam

Published: 2013-04-15T03:39:00Z

Content type: article

Language: en

Sources: [The LLVM Project Blog](<https://devfeed.tech/sources/the-llvm-project-blog.md>)

Topics: [debugging](<https://devfeed.tech/topics/debugging.md>), [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [clang](<https://devfeed.tech/topics/clang.md>), [Parser](<https://devfeed.tech/topics/parser.md>), [Parsing](<https://devfeed.tech/topics/parsing.md>), [Code](<https://devfeed.tech/topics/code.md>), [Front end](<https://devfeed.tech/topics/frontend.md>), [LLVM](<https://devfeed.tech/topics/llvm.md>), [Tooling](<https://devfeed.tech/topics/tooling.md>), [Development](<https://devfeed.tech/topics/development.md>)

Tags: [back-end](<https://devfeed.tech/tags/back-end.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [clang](<https://devfeed.tech/tags/clang.md>), [code](<https://devfeed.tech/tags/code.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [computer](<https://devfeed.tech/tags/computer.md>), [development](<https://devfeed.tech/tags/development.md>), [front-end](<https://devfeed.tech/tags/front-end.md>), [infrastructure](<https://devfeed.tech/tags/infrastructure.md>), [products](<https://devfeed.tech/tags/products.md>), [static-analysis](<https://devfeed.tech/tags/static-analysis.md>), [structure](<https://devfeed.tech/tags/structure.md>), [syntax](<https://devfeed.tech/tags/syntax.md>), [tools](<https://devfeed.tech/tags/tools.md>)

## AI overview

The article describes how CppDepend adopted Clang as a C/C++ parser for static analysis. It explains Clang's compiler architecture, including its frontend, optimizer, backend, and AST, and discusses implementing custom AST consumers using existing examples.

## Source excerpt

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 understan