Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cppcheck implementation #3

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/run-update-script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
cd clang-format/build/linux
./build-docker-linux.sh

- name: Update cppcheck
run: |
cd cppcheck/build/linux
./build-cppcheck-linux.sh

- name: Git Auto Commit
uses: stefanzweifel/[email protected]
with:
Expand All @@ -38,6 +43,11 @@ jobs:
run: |
./clang-format/build/windows/build-clang-format.bat

- name: Update cppcheck
run: |
cd cppcheck/build/windows
./build-cppcheck.bat

- name: Git Auto Commit
uses: stefanzweifel/[email protected]
with:
Expand All @@ -58,6 +68,11 @@ jobs:
run: |
./clang-format/build/mac/build-clang-format-mac.sh

- name: Update cppcheck
run: |
cd cppcheck/build/mac
./build-cppcheck-mac.sh

- name: Git Auto Commit
uses: stefanzweifel/[email protected]
with:
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
entry: ./clang-format/bin/clang-format-hook
language: script
files: \.(c|cc|cxx|cpp|h|tcc)$

- id: cppcheck
name: cppcheck
description: Ensure files are cppcheck compliant
entry: ./cppcheck/bin/cppcheck-hook
language: script
files: \.(c|cc|cxx|cpp|h|tcc)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Custom pre-commit hooks for the pre-commit framework
The hooks present in this repository are:
* clang-format (self-contained hook not reliant on clang-format being available externally)
* Cppcheck (self-contained hook not reliant on cppcheck being available externally)

To use the hooks copy this to your .pre-commit-config.yaml:
```yaml
- repo: https://github.com/mantidproject/pre-commit-hooks
rev: main
hooks:
- id: clang-format
- id: cppcheck
```
Binary file modified clang-format/bin/clang-format-darwin
Binary file not shown.
Binary file modified clang-format/bin/clang-format.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion clang-format/build/linux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ This is best built using Docker. Change to this directory and run:
> build-docker-linux.sh
```

The new binary will be placed in `llvm-build/bin`.
The new binary will be placed in the bin folder.
Binary file added cppcheck/bin/cppcheck-darwin
Binary file not shown.
69 changes: 69 additions & 0 deletions cppcheck/bin/cppcheck-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
"""
pre-commit cppcheck hook
============================

Runs cppcheck on the given file and exits with a non-zero code if it is a valid cppcheck file
"""
# std imports
import argparse
import os
import subprocess
import sys

USAGE = 'cppcheck-hook <file> [<file> ...]'

DESCRIPTION = '''
Runs cppcheck on the given file and exits with a non-zero code if any changes were made.
'''

if sys.platform.startswith('win32'):
CPPCHECK_EXE = os.path.join(os.path.dirname(__file__), 'cppcheck.exe')
elif sys.platform.startswith('linux'):
CPPCHECK_EXE = os.path.join(os.path.dirname(__file__), 'cppcheck-linux-64')
elif sys.platform.startswith('darwin'):
CPPCHECK_EXE = os.path.join(os.path.dirname(__file__), 'cppcheck-darwin')
else:
raise RuntimeError('Unknown platform "{}"'.format(sys.platform))


class main:
def __init__(self):
super().__init__()
parser = argparse.ArgumentParser(usage=USAGE, description=DESCRIPTION)
parser.add_argument('files', nargs='+', help='File to run cppcheck on.')
parser.add_argument('suppressions_list', nargs=1, help='File containing paths for ignorable ')
parser.add_argument('std', nargs=1, help='The cpp standard to be judged against')
self.args = parser.parse_args()

def main(self):
# return non-zero exit code if files will not compliant
return 1 if self.cppcheck_all() else 0

def cppcheck_all(self):
invalid = map(self.cppcheck, self.args.files)
return any(invalid)

def cppcheck(self, filepath):
return_val = self.run_cppcheck_exe(filepath)
pass

def run_cppcheck_exe(self, filepath):
p = subprocess.Popen([CPPCHECK_EXE,
f'--suppressions-list=${self.args.suppressions_list}',
f'--std=${self.args.std}',
'--enable=all',
'--inline-suppr',
'--max-configs=120',
'--quiet',
filepath],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError(stderr)
return stdout


if __name__ == '__main__':
sys.exit(main().main())
Binary file added cppcheck/bin/cppcheck-linux-64
Binary file not shown.
Binary file added cppcheck/bin/cppcheck.exe
Binary file not shown.
7 changes: 7 additions & 0 deletions cppcheck/build/linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Build Statically Linked Executable on Linux

Change to this directory and run:

```shell
> build-cppcheck-linux.sh
```
35 changes: 35 additions & 0 deletions cppcheck/build/linux/build-cppcheck-linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# Compiles Cppcheck
set -ex

# tarballs
CPPCHECK=https://github.com/danmar/cppcheck/archive/2.3.tar.gz
TAR_FILE=$(basename ${CPPCHECK})

# extract source
mkdir cppcheck
cd cppcheck
wget --quiet ${CPPCHECK}
tar --extract --gz --strip-components=1 --file ${TAR_FILE}
# patch CMakeLists.txt to statically link to libgcc and libstdc++ to clang-format exe
sed -i -e 's@${CLANG_FORMAT_LIB_DEPS}@${CLANG_FORMAT_LIB_DEPS} -static-libgcc -static-libstdc++@' CMakeLists.txt

# build
cd ..
mkdir cppcheck-build
cd cppcheck-build
cmake ../cppcheck \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_GUI=OFF \
-DHAVE_RULES=OFF \
-DUSE_MATCHCOMPILER=ON
cmake --build .

# Move binary
mv bin/cppcheck ../../../bin/cppcheck-linux-64
cd ..

# Cleanup
rm -rf cppcheck
rm -rf ${TAR_FILE}
rm -rf cppcheck-build
7 changes: 7 additions & 0 deletions cppcheck/build/mac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Build Statically Linked Executable on macOS

Change to this directory and run:

```shell
> build-cppcheck-mac.sh
```
34 changes: 34 additions & 0 deletions cppcheck/build/mac/build-cppcheck-mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
# Compiles Cppcheck
set -ex

# tarballs
CPPCHECK=https://github.com/danmar/cppcheck/archive/2.3.tar.gz
TAR_FILE=$(basename ${CPPCHECK})

# extract source
mkdir cppcheck
cd cppcheck
wget --quiet ${CPPCHECK}
tar --extract --gz --strip-components=1 --file ${TAR_FILE}
cd ..


# build
mkdir cppcheck-build
cd cppcheck-build
cmake ../cppcheck \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_GUI=OFF \
-DHAVE_RULES=OFF \
-DUSE_MATCHCOMPILER=ON
cmake --build .

# Move binary
mv bin/cppcheck ../../../bin/cppcheck-darwin
cd ..

# Cleanup
rm -rf cppcheck
rm -rf ${TAR_FILE}
rm -rf cppcheck-build
13 changes: 13 additions & 0 deletions cppcheck/build/windows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Building Statically Linked Executable on Windows

Requirements:
- Visual Studio 2019 Community Edition installed in the standard location
- [7-zip](https://www.7-zip.org/download.html) installed in *C:\Program Files\7-Zip*

From the commandline change to this directory and run:

```shell
> build-clang-format
```

The new binary will be placed in `llvm-build\bin`.
45 changes: 45 additions & 0 deletions cppcheck/build/windows/build-cppcheck.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@setlocal enableextensions
:: build statically linked executable

:: enable VC environment. activates cmake, ninja
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 || call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
:: assume 7z is installed in standard path
set PATH=C:\Program Files\7-Zip;%PATH%
set SCRIPTDIR=%~dp0
set CPPCHECK_URL=https://github.com/danmar/cppcheck/archive/2.3.tar.gz
set CPPCHECK_TGZ=cppcheck.tar.gz

:: sources
mkdir cppcheck
cd cppcheck
curl -L %CPPCHECK_URL% -o %CPPCHECK_TGZ%
call:extract-tgz %CPPCHECK_TGZ%
cd ..

:: build
mkdir cppcheck-build
cd cppcheck-build
cmake ^
-DBUILD_GUI=OFF ^
-DHAVE_RULES=OFF ^
-DUSE_MATCHCOMPILER=ON ^
../cppcheck/cppcheck-2.3
cmake --build . --config Release
copy /Y bin\Release\cppcheck.exe ..\..\..\bin\

:: cleanup
cd ..
rmdir /S /Q cppcheck
rmdir /S /Q cppcheck-build

goto:eof


:extract-tgz
:: Using the -J filter on tar directly seems to hang...
set _tgzfile=%~1
set _tarfile=%_tgzfile:~0,-3%
del /Q %_tarfile%
7z -bb0 x %_tgzfile%
7z -bb0 x %_tarfile%
goto:eof