Migrating from v1¶
This page lists what changes when moving from cpp-linter v1 (the Python package) to v2 (this Rust project). Most options keep their names, short flags and meaning, so many command lines work unchanged. The sections below cover the differences.
Installing v2¶
v2 keeps the package name and the command of v1. On PyPI it is still cpp-linter, and it still
installs a cpp-linter command. Once 2.0 is released, pip install --upgrade cpp-linter moves to
v2. To stay on v1, pin the version:
pip install "cpp-linter<2"
v2 can also be installed with cargo, as a pre-built binary, or from npm. See Quick start for these commands and for how to install the current release candidate.
cpp-linter-action v2 keeps using cpp-linter v1, so workflows that use the action do not change until they move to a new major version of the action.
Python API¶
v1 is a Python package, so some scripts import its modules, such as cpp_linter.rest_api. The v2
Python package only provides cpp_linter.main(). Scripts that import other v1 modules need to run
cpp-linter as a command or call main() instead. main() takes an optional list of strings that
starts with the program name, like sys.argv:
import cpp_linter
cpp_linter.main(["cpp-linter", "--style=file", "src/main.cpp"])
If cpp-linter fails, main() raises an OSError. Invalid arguments and --help exit the Python
process, as they exit the command.
Deprecated options¶
| v1 option | v2 behavior | What to do |
|---|---|---|
-j, --jobs |
Files are always analyzed in parallel. The value is still parsed as an integer, then ignored with a deprecation warning. | Remove the option. |
Renamed or merged options¶
| v1 option | v2 option | Notes |
|---|---|---|
-d, --tidy-review |
-P, --pr-review |
v2 posts one review with suggestions from both clang-tidy and clang-format. The old names still work as aliases of --pr-review, so -d true now also enables clang-format suggestions. |
-m, --format-review |
-P, --pr-review |
Same as above. -m true now also enables clang-tidy suggestions. |
-F, --fix |
-F, --gen-patch |
See Fixes are written to a patch. --fix is kept as an alias. |
The old names can still be given together, as in v1 (for example
--tidy-review=true --format-review=false). Reviews are enabled if any of the given values is
true.
Changed defaults¶
| Option | v1 default | v2 default |
|---|---|---|
-l, --lines-changed-only |
false |
true |
-i, --ignore |
.github |
.github and target |
-c, --tidy-checks |
includes clang-analyzer-* |
no clang-analyzer-* |
Because --lines-changed-only is true by default and implies
--files-changed-only, v2 analyzes only the added lines of changed files
unless told otherwise. To keep the v1 behavior of analyzing every line of every source file,
including files under target, pass:
cpp-linter --lines-changed-only=false --files-changed-only=false --ignore=.github
The default --tidy-checks no longer adds the Clang Static Analyzer checks
(clang-analyzer-*), because they can make clang-tidy many times slower. clang-tidy 22 and newer
also leave them out of their own defaults, so with those versions they only run if a .clang-tidy
file or --tidy-checks enables them. clang-tidy 21 and older still enable them by default; add
-clang-analyzer-* to --tidy-checks to turn them off there. To keep them with any version, add
them back:
cpp-linter --tidy-checks="boost-*,bugprone-*,performance-*,readability-*,portability-*,modernize-*,clang-analyzer-*,cppcoreguidelines-*"
Invalid option values stop the run¶
v1 accepted any value for --lines-changed-only and treated an unknown one
as false. It also kept going when --version was not a version or a path.
v2 checks these values before it starts. --lines-changed-only takes true, false or diff, and
--version takes a version requirement or a path. Any other value prints an error and exits with
code 2, so a typo in a workflow input fails the job instead of being ignored.
Fixes are written to a patch¶
In v1, --fix applied clang-format fixes in place, editing the files in the working tree.
In v2, cpp-linter never modifies the working tree. --gen-patch (alias --fix)
writes one patch file that combines the clang-format and clang-tidy fixes. Its path is printed in
the log and, in CI, set as the fix-patch-path output. By default the patch is written to
.cpp-linter-cache/auto-fix.patch in the --repo-root folder.
--gen-patch-path (or the GEN_PATCH_PATH environment variable) chooses another
path and implies --gen-patch. To apply the fixes locally:
export GEN_PATCH_PATH=fixes.patch
cpp-linter
# The patch is only written when there are fixes.
if [ -f "$GEN_PATCH_PATH" ]; then git apply "$GEN_PATCH_PATH"; fi
A clang-tidy fix can also change a file that was not analyzed. Those parts are left out of the patch and listed as skipped in the log and in the comments, so apply them by hand.
Clang tools are installed for you¶
v1 expects clang-format and clang-tidy to be installed already, for example with
clang-tools-pip. --version only selects which
installed version to use.
In v2, -V, --version also accepts a version requirement (for example 16 or
>=10, <13). If no matching tools are found on the PATH or in the cache, v2 installs them.
It tries the system package manager first when allowed (on by default in CI, see
--mod-sys and --no-mod-sys), then PyPI wheels, then static binaries
built by cpp-linter. A path to an install location works as in v1. With a blank string (the
default), v2 uses the installed tools and does not install anything, so the run fails if they are
missing.
Running -V without a value prints the cpp-linter version, like the version subcommand.
When clang-format fails¶
In v1, when clang-format exited with an error for a file (for example because of a bad
--style value, or a .clang-format key that the installed version does not know), the
error was only logged at debug level and the file passed as formatted.
In v2, such a file is logged as a warning and counted in the clang-format-checks-failed output.
If clang-format fails on every file it runs on, the configuration is most likely wrong, so
cpp-linter exits with an error.
Relative paths¶
In v2, a relative path given to --database, --gen-patch-path,
--summary-output-file or --sarif-file is resolved against
the current working directory, not against --repo-root.
--databasebehaves as in v1 1.15 and later.--summary-output-filewas resolved against--repo-rootin v1. If you set--repo-rootto a sub-folder and pass a relative summary path, prefix it with that sub-folder or use an absolute path.
With the default --repo-root of ., nothing changes.
New in v2¶
--sarif-filewrites clang-tidy diagnostics as a SARIF 2.1.0 file, for example to upload to GitHub code scanning.--code-quality-filewrites clang-format and clang-tidy findings as a GitLab Code Quality report, to show them in GitLab merge requests.--gen-patchand--gen-patch-pathproduce one patch of the clang-format and clang-tidy fixes, described above.--mod-sysand--no-mod-syscontrol whether system package managers may be used to install the clang tools.