Skip to content

Commit

Permalink
Add cppcheck integration
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlyanon committed May 2, 2021
1 parent 76e87f6 commit 067c95e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Install clang-tidy
- name: Install static analyzers
if: matrix.os == 'ubuntu'
run: sudo apt-get install clang-tidy -y -q
run: sudo apt-get install clang-tidy cppcheck -y -q

- uses: actions/setup-python@v2
with: { python-version: "3.8" }
Expand Down
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Make sure you have these programs installed:
* Python 3.8 or newer
* CMake 3.19 or newer
* git
* clang-tidy (optional, should be available in PATH as `clang-tidy`)
* [clang-tidy](#clang-tidy) (optional)
* [cppcheck](#cppcheck) (optional)

`cmake-init` consists of a single file that can be run using Python. Python was
chosen for this, because it is cross-platform, convenient for this use-case and
Expand Down Expand Up @@ -91,6 +92,21 @@ and use it locally, but it is recommended.
reason for this is that only [Makefiles and Ninja][7] are supported with CMake
for use with clang-tidy. For other generators, this feature is a no-op.

### cppcheck

[cppcheck][8] is a static analysis tool similar to clang-tidy, however the
overlap in what they detect is minimal, so it's beneficial to use both of them.
This script gives you the option to inherit the `cppcheck` preset in your `dev`
preset, enabling the CMake integration for this tool.

CI will always run cppcheck for you, so it is entirely optional to install and
use it locally, but it is recommended.

**For Windows users**, if you wish to use cppcheck, then you must install
[Ninja][6] and set the `generator` field in your `dev` preset to `Ninja`. The
reason for this is that only [Makefiles and Ninja][9] are supported with CMake
for use with cppcheck. For other generators, this feature is a no-op.

## Usage

* `cmake-init <path>`
Expand Down Expand Up @@ -124,3 +140,5 @@ indirectly from the use or non-use of these files.
[5]: https://clang.llvm.org/extra/clang-tidy/
[6]: https://github.com/ninja-build/ninja
[7]: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CLANG_TIDY.html
[8]: http://cppcheck.sourceforge.net/
[9]: https://cmake.org/cmake/help/latest/prop_tgt/LANG_CPPCHECK.html
16 changes: 15 additions & 1 deletion cmake-init/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def ask(*args, **kwargs):
predicate=lambda v: v in ["y", "n"],
header="This will require you to download clang-tidy locally.",
) == "y",
"use_cppcheck": ask(
"Add cppcheck to local dev preset ([Y]es/[n]o)",
cli_args.use_cppcheck or "y",
mapper=lambda v: v[0:1].lower(),
predicate=lambda v: v in ["y", "n"],
header="This will require you to download cppcheck locally.",
) == "y",
"examples": False,
"os": "win64" if is_windows else "unix",
}
Expand Down Expand Up @@ -280,7 +287,7 @@ def main():
type=os.path.realpath,
help="path to generate to, the name is also derived from this",
)
create_flags = ["type_id", "std", "use_clang_tidy"]
create_flags = ["type_id", "std", "use_clang_tidy", "use_cppcheck"]
p.set_defaults(**{k: "" for k in create_flags})
type_g = p.add_mutually_exclusive_group()
mapping = {
Expand Down Expand Up @@ -308,6 +315,13 @@ def main():
const="n",
help="omit the clang-tidy preset from the dev preset",
)
p.add_argument(
"--no-cppcheck",
action="store_const",
dest="use_cppcheck",
const="n",
help="omit the cppcheck preset from the dev preset",
)
args = p.parse_args()
flags_used = any(getattr(args, k) != "" for k in create_flags)
setattr(args, "flags_used", flags_used)
Expand Down
4 changes: 2 additions & 2 deletions cmake-init/templates/common/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ jobs:
steps:
- uses: actions/checkout@v1

- name: Install clang-tidy
- name: Install static analyzers
if: matrix.os == 'ubuntu'
run: sudo apt-get install clang-tidy -y -q
run: sudo apt-get install clang-tidy cppcheck -y -q

- name: Configure
run: cmake --preset=ci-${{ matrix.os }}{type shared}
Expand Down
9 changes: 8 additions & 1 deletion cmake-init/templates/common/CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"patch": 0
},
"configurePresets": [
{
"name": "cppcheck",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_CPPCHECK": "cppcheck"
}
},
{
"name": "clang-tidy",
"hidden": true,
Expand Down Expand Up @@ -65,7 +72,7 @@
},
{
"name": "ci-ubuntu",
"inherits": ["ci-unix", "clang-tidy"]
"inherits": ["ci-unix", "clang-tidy", "cppcheck"]
},
{
"name": "ci-windows",
Expand Down
2 changes: 1 addition & 1 deletion cmake-init/templates/common/CMakeUserPresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"configurePresets": [
{
"name": "dev",
"inherits": ["ci-%(os)s"{if use_clang_tidy}, "clang-tidy"{end}],
"inherits": ["ci-%(os)s"{if use_clang_tidy}, "clang-tidy"{end}{if use_cppcheck}, "cppcheck"{end}],
"cacheVariables": {
"%(name)s_DEVELOPER_MODE": "ON"
}
Expand Down

0 comments on commit 067c95e

Please sign in to comment.