﻿# Analysis of C and C\+\+ projects based on JSON Compilation Database

## General information

One of the ways to represent the structure of a C\+\+ project is the [JSON Compilation Database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) format\. It's a file that contains the compilation parameters necessary to create object files from the source code of a project\. Usually, the file has the name `compile_commands.json`\. A compilation database in JSON\-format consists of an array of "command objects", where each command object specifies one way a translation unit is compiled in the project\. 

You can use the `compile_commands.json` file to compile a project or analyze the project by third\-party utilities\. The PVS\-Studio C and C\+\+ analyzer works with this format as well\.

## Running the analysis and getting the report

To analyze the project on Linux and macOS, you need to use `pvs-studio-analyzer` utility\. To analyze the project on Windows, use `CompilerCommandsAnalyzer.exe` utility\. The utility is usually located in the `C:\Program Files (x86)\PVS-Studio` folder\. Read more information about `CompilerCommandsAnalyzer` and `pvs-studio-analyzer` [here](https://pvs-studio.com/en/docs/manual/6615/)\.

![Analyze_Cpp_Projects_with_JSON_Compilation_DB/image1.png](https://import.viva64.com/docx/blog/Analyze_Cpp_Projects_with_JSON_Compilation_DB/image1.png)

**Important:** The project must be successfully compiled and built to be analyzed\.

To start the analysis and get the report, you need to run two commands\.

The command example for Linux and macOS:

```cpp
pvs-studio-analyzer analyze -f path_to_compile_commands.json \
                            -o pvs.log -e excludepath -j<N>

plog-converter -a GA:1,2 -t tasklist -o project.tasks pvs.log
```

The command example for Windows:

```cpp
CompilerCommandsAnalyzer.exe analyze ^
                             -f path_to_compile_commands.json ^
                             -o pvs.log -e exclude-path -j<N>

PlogConverter.exe -a GA:1,2 -t Plog -o path_to_output_directory ^
    -n analysis_report pvs.log
```

If you run the analysis from the directory with the `compile_commands.json` file, you may disable the `-f` flag\.

To exclude directories with third\-party libraries and/or tests from the analysis, you can use the `-e` flag\.  If there are several paths, it's necessary to write the `-e` flag for each path:

```cpp
-e third-party -e tests
```

The analysis can be parallelized into multiple threads with the help of `-j` flag\.

More detailed instructions for utilities on Linux/macOS and Windows are available [here](https://pvs-studio.com/en/docs/manual/0036/) and [here](https://pvs-studio.com/en/docs/manual/0038/)\.

## How to generate compile\_commands\.json

If by default the project does not contain `compile_commands.json`, you can choose one of the ways to generate such a file\.

### CMake project

To generate `compile_commands.json`, add one flag to the CMake call:

```cpp
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=On .
```

It's possible to create the `compile_commands.json` file only if the generator supports the JSON format\. For example, such generators are [Makefile](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) and [Ninja](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html):

```cpp
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=On -G Ninja .
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=On -G "NMake Makefiles" .
```

To use the Ninja generator for Windows, it is often required to execute commands from the Visual Studio developer's command line \(e\.g\., **x64 Native Tools Command Prompt for VS**, etc\.\)\.

### Ninja project

If the project is built directly with [Ninja](https://ninja-build.org/) and there is a `build.ninja` file in the project folder, use the following command to generate `compile_commands.json`:

```cpp
ninja -t compdb > compile_commands.json
```

### QBS project

To generate `compile_commands.json` in a project that use Qt Build System, execute the following command:

```cpp
qbs generate --generator clangdb
```

### Text Toolkit utility

Having trouble getting the `compile_commands.json` file using GNU make? Try [Text Toolkit](https://texttoolkit.com/compilation-database-generator)\. You can generate a compilation database either using the Web interface \(only for Linux and macOS\), or by launching a Python script\. To generate a database online, take the following steps:

1. run the command `make -nwi > output.txt`;
1. copy the contents of the `output.txt` file and paste them to the window on the Text Toolkit website;
1. click the **Generate** button to generate the compilation database in JSON format;
1. copy the obtained commands to the `compile_commands.json` file\.

To generate the `compile_commands.json` using Python, you need to clone a [repository](https://github.com/viva64/texttoolkit) from GitHub and run the following command:

```cpp
ninja -nv | python path_to_texttoolkit_dir\cdg.py
```

### Bear utility \(only for Linux and macOS\)

The [Bear](https://github.com/rizsotto/Bear) \(version 2\.4 or higher\) utility collects compilation parameters by intercepting the compiler calls during project build\. To generate `compile_commands.json`, run the following command

```cpp
bear -- <build_command>
```

The `build_command` can be any build command such as `make all` or `./build.sh`\.

### intercept\-build utility \(only for Linux and macOS\)

The 'intercept\-build' utility in [scan\-build](https://github.com/rizsotto/scan-build) is similar to the Bear utility\. The command to generate `compile_commands.json`:

```cpp
intercept-build <build_command>
```

### Compilation Database Generator utility \(only for Linux and macOS\)

[Compile Database Generator](https://github.com/nickdiego/compiledb) \(compiledb\) is a utility that generates compilation databases for Makefile\-based build systems\. The example of the `compile_commands.json` generation:

```cpp
compiledb -n make
```

The `-n` flag means that the build won't happen \(dry run\)\.

### Xcode project \(macOS only\)

With the [xcpretty](https://github.com/xcpretty/xcpretty) utility, you can generate `compile_commands.json`\. To do this, run the following command:

```cpp
xcodebuild [flags] | xcpretty -r json-compilation-database
```

### qmake project

To generate `compile_commands.json` in the project that uses qmake, you can use IDE QtCreator version 4\.8 or higher\. Open the desired project and select **Build \> Generate Compilation Database for %project\_name%** in the menu bar:

![Analyze_Cpp_Projects_with_JSON_Compilation_DB/image2.png](https://import.viva64.com/docx/blog/Analyze_Cpp_Projects_with_JSON_Compilation_DB/image2.png)

The generated `compile_commands.json` file will be in the project's build directory\.

Note: this method of obtaining `compile_commands.json` does not have automation\. We recommend to use this method only to evaluate the analyzer\.

### SCons project

To generate `compile_commands.json` in a project that uses the [SCons](https://scons.org/) build system, add the following lines to the [SConstruct](https://www.scons.org/doc/0.96/HTML/scons-user/x325.html) file \(this is an analog of [Makefile](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html) for the [Make](https://www.gnu.org/software/make/) utility\) in the project directory:



```cpp
env = Environment(COMPILATIONDB_USE_ABSPATH=True)
env.Tool('compilation_db')
env.CompilationDatabase()
env.Program('programm_for_build.c')
```

After that, to create the `compile_commands.json` file, run the following command in the project directory \(where the [SConstruct](https://www.scons.org/doc/0.96/HTML/scons-user/x325.html) file is located\):

```cpp
scons -Q
```

For a detailed guide on how to create `compile_commands.json` in [SCons](https://scons.org/), please consult [the relevant section of the SCons documentation](https://scons.org/doc/production/HTML/scons-user/ch27.html)\.

### Bazel project

To generate `compile_commands.json` in a project that uses the [Bazel](https://bazel.build/) build system, use the [bazel\-compile\-commands\-extractor](https://github.com/hedronvision/bazel-compile-commands-extractor) utility \(this is a utility that does not require a full build of the project, and is based on [Action Graph Query \(aquery\)](https://bazel.build/query/aquery)\)\. You can find complete setup instructions [here](https://github.com/hedronvision/bazel-compile-commands-extractor/blob/main/README.md)\. 

There are several other options for creating a `compile_commands.json` file for a [Bazel](https://bazel.build/) project besides [bazel\-compile\-commands\-extractor](https://github.com/hedronvision/bazel-compile-commands-extractor):

* [github\.com/google/kythe: tools/cpp/generate\_compilation\_database\.sh](https://github.com/kythe/kythe/blob/f215df07e18d1d99535a2839b197a81130fcfd90/tools/cpp/generate_compilation_database.sh)\. It uses experimental\_action\_listener to create a compilation database;
* [github\.com/grailbio/bazel\-compilation\-database](https://github.com/grailbio/bazel-compilation-database)\. It is faster than experimental\_action\_listener by Kythe, easier to set up and does not require a full build, but is less efficient\. This repository has been frozen since March 17, 2024;
* [github\.com/stackb/bazel\-stack\-vscode\-cc](https://github.com/stackb/bazel-stack-vscode-cc)\. An extension for VS Code that adds a command to create `compile_commands.json` for a project\.