Skip to content

Commit be68cfb

Browse files
authored
Merge pull request #339 from vkucera/tools
Tools: Add instructions for fixing include format. Minor improvements.
2 parents 2bb85f6 + 1c3769c commit be68cfb

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ repos:
1010
- id: check-yaml
1111
- id: trailing-whitespace
1212
- repo: https://github.com/pre-commit/mirrors-clang-format
13-
rev: v21.1.5 # clang-format version
13+
rev: v21.1.7 # clang-format version
1414
hooks:
1515
- id: clang-format
1616
- repo: https://github.com/cpplint/cpplint
1717
rev: 2.0.2
1818
hooks:
1919
- id: cpplint
2020
- repo: https://github.com/igorshubovych/markdownlint-cli
21-
rev: v0.45.0
21+
rev: v0.47.0
2222
hooks:
2323
- id: markdownlint-fix
2424
- repo: https://github.com/tcort/markdown-link-check
25-
rev: v3.14.1
25+
rev: v3.14.2
2626
hooks:
2727
- id: markdown-link-check
2828
args: [-q]

docs/tools/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ Next time you execute `git commit`, the hooks will run automatically.
4848
- If the cpplint hook fails, the linter has found issues that need to be fixed.
4949
- Updated files need to be staged with `git add` before attempting `git commit` again.
5050

51-
## [clang-tidy](https://clang.llvm.org/extra/clang-tidy/)
51+
## [Clang-Tidy](https://clang.llvm.org/extra/clang-tidy/)
5252

53-
`clang-tidy` is a clang-based C++ linter tool for diagnosing and fixing typical programming errors, like style violations or bugs.
53+
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-
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.
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

5858
### Checking naming conventions
5959

@@ -69,23 +69,37 @@ This helps to apply the [Include What You Use](https://github.com/AliceO2Group/O
6969
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.
7070

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

7575
The [`parallel`](https://www.gnu.org/software/parallel/) command is used to parallelise the execution of the `clang-tidy` command for all files.
7676

77-
For each file, `clang-tidy` will first try to compile it and then run the enabled check(s) and fix found problems (the `--fix` option).
77+
For each file, Clang-Tidy will first try to compile it and then run the enabled check(s) and fix found problems (the `--fix` option).
7878
The messages are redirected into `clang-tidy.log`.
79-
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"`.
79+
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"`.
8080

81-
## [cppcheck](https://cppcheck.sourceforge.io/)
81+
## Fixing include format
8282

83-
`cppcheck` is a static analysis tool for C/C++ code that detects bugs, undefined behaviour, and dangerous coding constructs that compilers typically miss.
83+
Headers from the same project should be included using quotation marks (`#include "path/header.h"`), all other headers should be included using angle brackets (`#include <path/header.h>`).
84+
Failing to do so can result in picking a wrong header.
85+
See more details in the [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#sf12-prefer-the-quoted-form-of-include-for-files-relative-to-the-including-file-and-the-angle-bracket-form-everywhere-else).
8486

85-
`cppcheck` can analyse individual files (file mode) or entire projects (project mode).
87+
The [`format_includes.awk`](https://github.com/AliceO2Group/O2Physics/blob/master/Scripts/format_includes.awk) script allows to fix the include format in a provided O2Physics file.
88+
89+
To fix the include format in all `.h`, `.cxx` files in the current directory, execute:
90+
91+
```bash
92+
find . -name "*.h" -o -name "*.cxx" | parallel "awk -f Scripts/format_includes.awk \"{}\" > \"{}.tmp\" && mv \"{}.tmp\" \"{}\""
93+
```
94+
95+
## [Cppcheck](https://cppcheck.sourceforge.io/)
96+
97+
Cppcheck is a static analysis tool for C/C++ code that detects bugs, undefined behaviour, and dangerous coding constructs that compilers typically miss.
98+
99+
Cppcheck can analyse individual files (file mode) or entire projects (project mode).
86100
The two modes give slightly different results so one can consider using both for maximum coverage.
87101

88-
### Using cppcheck in file mode
102+
### Using Cppcheck in file mode
89103

90104
The file mode is used in the MegaLinter check on GitHub.
91105

@@ -100,12 +114,12 @@ The report will be stored in the `err.log` file.
100114
To run a parallelised analysis of all `.h`, `.cxx`, `.C` files in the current directory, execute:
101115

102116
```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"
117+
find . -name "*.h" -o -name "*.cxx" -o -name "*.C" | parallel "cppcheck --language=c++ --std=c++20 --enable=style --check-level=exhaustive --suppressions-list=cppcheck_config {}" 2> "err.log"
104118
```
105119

106120
Note: It is possible to parallelise the execution with the `-j` option instead but it usually produces less results than analysing files independently.
107121

108-
### Using cppcheck in project mode
122+
### Using Cppcheck in project mode
109123

110124
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.
111125

@@ -117,7 +131,7 @@ cppcheck --language=c++ --std=c++20 --enable=style --check-level=exhaustive --su
117131

118132
### Generating browsable HTML output
119133

120-
Run `cppcheck` with the additional option `--xml` which will store the output in the XML format.
134+
Run Cppcheck with the additional option `--xml` which will store the output in the XML format.
121135

122136
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.
123137
To keep only the first header and the last footer, you can extract the relevant parts of the file with:

0 commit comments

Comments
 (0)