Skip to content

cppcheck: use file list and add -j option with available processors#73

Open
mdeweerd wants to merge 11 commits into
pocc:masterfrom
mdeweerd:vibe/cppcheck-filelist-c62b93
Open

cppcheck: use file list and add -j option with available processors#73
mdeweerd wants to merge 11 commits into
pocc:masterfrom
mdeweerd:vibe/cppcheck-filelist-c62b93

Conversation

@mdeweerd

@mdeweerd mdeweerd commented Jul 12, 2026

Copy link
Copy Markdown

Why

In CI, I enabled cppcheck caching by adding the --cppcheck-build-dir option, but that did not work as every file was analysed in a separate cppcheck call.

The changes avoids splitting the analysis in multiple batches, and has all the files analysed in a single cppcheck call (which itself creates multiple subprocesses).

Summary of the changes

  • Use --file-list option to pass multiple files to cppcheck in a single invocation
  • Automatically add -j option with number of available processors for parallel analysis
  • Add --cppcheck-build-dir when using -j > 1 to enable unusedFunction check compatibility
  • Always suppress unusedFunction warnings (now safe due to --cppcheck-build-dir handling)
  • Use truly unique temporary directories with tempfile.mkdtemp()
  • Properly clean up temporary directories with shutil.rmtree() even on interruption

Changes

  • Modified hooks/cppcheck.py to use --file-list option instead of running cppcheck once per file
  • Added automatic -j N option with CPU count (only on multi-core systems)
  • Added automatic --cppcheck-build-dir creation when using -j > 1
  • Simplified suppression logic: always add --suppress=unusedFunction (previously conditional)
  • Added proper cleanup of temporary build directories using shutil.rmtree()
  • Updated test filters in tests/test_hooks.py to handle cppcheck warning messages

Extra Info

--suppress=unusedFunction is no longer needed

As we add the --cppcheck-build-dir when missing if -j is > 1, there is no longer an issue with the analysis.

Verification

  • All existing tests pass - except on macos which is unrelated to these changes
  • New behavior properly handles parallel analysis
  • Temporary directories are cleaned up even on interruption
  • No warning messages about -j and unusedFunction conflicts
  • Test on actual project.

mistral-vibe and others added 11 commits July 12, 2026 17:02
- Create a temporary file with the list of all files to check
- Pass the file list to cppcheck using --file-list option
- Automatically add -j option with number of available processors
- Only add -j if not already provided in pre-commit arguments
- Add unit tests for -j option behavior
- Add integration test cases for -j and -j2 options
cppcheck cannot use unusedFunction check with -j (parallel processing).
This fix ensures that when -j is added (either automatically or by user),
the unusedFunction suppression is not included to avoid the conflict.

The test was failing because cppcheck was printing:
'cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check.'

This change removes the unusedFunction suppression when -j is present,
allowing parallel processing to work correctly.
Add regex filters to remove warning messages about unusedFunction check
conflicting with -j option in newer versions of cppcheck.

Closes #
- Add --cppcheck-build-dir when using -j option to enable unusedFunction
- Handle cases where -j is user-provided or auto-added
- Ensure unusedFunction suppression is only added when safe
- Use --file-list for passing multiple files to cppcheck

This fixes the warning: 'cppcheck: unusedFunction check requires --cppcheck-build-dir to be active with -j.'

Closes #
- When -j is 1 or not specified: suppress unusedFunction (no build dir needed)
- When -j > 1 and user didn't provide --cppcheck-build-dir: add it automatically
- When -j > 1 and user provided --cppcheck-build-dir: suppress unusedFunction
- Only auto-add -j if num_cpus > 1 (skip for single-core systems)

This ensures unusedFunction check works correctly in all scenarios.

Closes #
- Create unique temporary directory with tempfile.mkdtemp(prefix='cppcheck_')
- Store build dir path in self.cppcheck_build_dir for cleanup
- Clean up temporary directory after cppcheck runs
- Properly use add_if_missing for --cppcheck-build-dir option

This ensures each cppcheck run gets its own unique build directory.

Closes #
- Use shutil.rmtree() instead of os.rmdir() for recursive directory removal
- Add cleanup_build_dir() method for proper cleanup
- Call cleanup in finally block of run() method
- Call cleanup in finally block of main() for exception safety
- Ensures temp dir is removed even on KeyboardInterrupt or other exceptions

Closes #
Since we now automatically add --cppcheck-build-dir when using -j > 1,
we can safely always suppress unusedFunction just like the other
suppressions (unmatchedSuppression, missingIncludeSystem).

This fixes the test_cppcheck_default_args_added regression test.

Closes #
@mdeweerd mdeweerd marked this pull request as ready for review July 12, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants