Skip to content

Commit 4a146eb

Browse files
committed
Tools: Add cppcheck instructions. Update clang-tidy instructions.
1 parent b5edb75 commit 4a146eb

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

docs/tools/README.md

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,23 @@ Next time you execute `git commit`, the hooks will run automatically.
5353
`clang-tidy` is a clang-based C++ linter tool for diagnosing and fixing typical programming errors, like style violations or bugs.
5454
(See the [list of checks](https://clang.llvm.org/extra/clang-tidy/checks/list.html).)
5555

56-
### Prerequisites for using clang-tidy
56+
To use `clang-tidy`, you need to have O2Physics compiled and a valid symbolic link `compile_commands.json` in the O2Physics directory pointing to the `alice/sw/BUILD/.../O2Physics` directory.
5757

58-
- You need to have O2Physics successfully compiled.
59-
- Verify that there is a valid symbolic link `compile_commands.json` in the O2Physics directory pointing to the `alice/sw/BUILD/.../O2Physics` directory.
60-
61-
### Tips
62-
63-
#### Naming conventions
58+
### Checking naming conventions
6459

6560
The [`readability-identifier-naming`](https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html) check can fix deviations from the [naming conventions](https://rawgit.com/AliceO2Group/CodingGuidelines/master/naming_formatting.html).
6661

67-
#### Cleaning `#include`s
62+
### Cleaning `#include`s
6863

6964
The [`misc-include-cleaner`](https://clang.llvm.org/extra/clang-tidy/checks/misc/include-cleaner.html) check can fix missing and unused `#include`s.
7065
This helps to apply the [Include What You Use](https://github.com/AliceO2Group/O2Physics/issues/8357) principle which allows to maintain header dependencies clean.
7166

72-
#### Testing (and fixing) many files at once
67+
### Testing (and fixing) many files at once
7368

74-
Here is an example of how to run the `misc-include-cleaner` check in parallel on all `.h` and `.cxx` files in the current directory.
69+
Here is an example of how to run the `misc-include-cleaner` check in parallel on all `.h`, `.cxx`, `.C` files in the current directory.
7570

7671
```bash
77-
parallel "clang-tidy --fix -checks=-*,misc-include-cleaner {}; echo \"{} \$?\"" ::: $(find -name "*.h" -o -name "*.cxx") > "clang-tidy.log"
72+
parallel "clang-tidy --fix -checks=-*,misc-include-cleaner {}; echo \"{} \$?\"" ::: "$(find . -name "*.h" -o -name "*.cxx" -o -name "*.C")" > "clang-tidy.log"
7873
```
7974

8075
The [`parallel`](https://www.gnu.org/software/parallel/) command is used to parallelise the execution of the `clang-tidy` command for all files.
@@ -83,6 +78,67 @@ For each file, `clang-tidy` will first try to compile it and then run the enable
8378
The messages are redirected into `clang-tidy.log`.
8479
The file name and the exit code are printed below the output of `clang-tidy` so that you can get the list of files for which `clang-tidy` failed with `grep " 1$" "clang-tidy.log"`.
8580

81+
## [cppcheck](https://cppcheck.sourceforge.io/)
82+
83+
`cppcheck` is a static analysis tool for C/C++ code that detects bugs, undefined behaviour, and dangerous coding constructs that compilers typically miss.
84+
85+
`cppcheck` can analyse individual files (file mode) or entire projects (project mode).
86+
The two modes give slightly different results so one can consider using both for maximum coverage.
87+
88+
### Using cppcheck in file mode
89+
90+
The file mode is used in the MegaLinter check on GitHub.
91+
92+
To use this mode, provide paths of files you want to analyse in the following way:
93+
94+
```bash
95+
cppcheck --language=c++ --std=c++20 --enable=style --check-level=exhaustive --suppressions-list=cppcheck_config file1 file2 ... 2> "err.log"
96+
```
97+
98+
The report will be stored in the `err.log` file.
99+
100+
To run a parallelised analysis of all `.h`, `.cxx`, `.C` files in the current directory, execute:
101+
102+
```bash
103+
parallel "cppcheck --language=c++ --std=c++20 --enable=style --check-level=exhaustive --suppressions-list=cppcheck_config {}" ::: "$(find . -name "*.h" -o -name "*.cxx" -o -name "*.C")" 2> "err.log"
104+
```
105+
106+
Note: It is possible to parallelise the execution with the `-j` option instead but it usually produces less results than analysing files independently.
107+
108+
### Using cppcheck in project mode
109+
110+
To use this mode, you need to have O2Physics compiled and a valid symbolic link `compile_commands.json` in the O2Physics directory pointing to the `alice/sw/BUILD/.../O2Physics` directory.
111+
112+
Instead of providing file paths, provide the path to the project compilation database and use the `-j` option for parallelisation:
113+
114+
```bash
115+
cppcheck --language=c++ --std=c++20 --enable=style --check-level=exhaustive --suppressions-list=cppcheck_config --project=compile_commands.json -j $(nproc) 2> "err.log"
116+
```
117+
118+
### Generating browsable HTML output
119+
120+
Run `cppcheck` with the additional option `--xml` which will store the output in the XML format.
121+
122+
If you used the file mode, the output file will contain an XML header and a footer for each analysed file which makes the output file invalid.
123+
To keep only the first header and the last footer, you can extract the relevant parts of the file with:
124+
125+
```bash
126+
logfile_raw="err.log"
127+
logfile_merged="err_merged.log"
128+
head -4 "${logfile_raw}" > "${logfile_merged}"
129+
grep -vE "<(\?xml |/?results|cppcheck |/?errors)" "${logfile_raw}" >> "${logfile_merged}"
130+
tail -2 "${logfile_raw}" >> "${logfile_merged}"
131+
```
132+
133+
You can generate the HTML report (providing the correct `err` file) with:
134+
135+
```bash
136+
cppcheck-htmlreport --title=O2Physics --report-dir=cppcheck-report --source-dir=. --file=err.log
137+
```
138+
139+
The HTML files will be stored in the `cppcheck-report` directory.
140+
You can browse them in your web browser by opening the `index.html` file.
141+
86142
## [Visual Studio Code (VS Code)](https://code.visualstudio.com/)
87143

88144
Integrated development environment

0 commit comments

Comments
 (0)