changeset 36245:368dd9765c4d

runtime(compiler): add cppcheck linter compiler plugin Commit: https://github.com/vim/vim/commit/af449f69c7cc9f0ffafaa6e0d028dccd1c358763 Author: Konfekt <Konfekt@users.noreply.github.com> Date: Sat Oct 5 17:09:21 2024 +0200 runtime(compiler): add cppcheck linter compiler plugin closes: https://github.com/vim/vim/issues/15804 Signed-off-by: Konfekt <Konfekt@users.noreply.github.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sat, 05 Oct 2024 17:15:03 +0200
parents ddf626d18d9e
children de8d90f97a4a
files runtime/compiler/cppcheck.vim runtime/doc/quickfix.txt
diffstat 2 files changed, 67 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/runtime/compiler/cppcheck.vim
@@ -0,0 +1,40 @@
+" vim compiler file
+" Compiler:	cppcheck (C++ static checker)
+" Maintainer:   Vincent B. (twinside@free.fr)
+" Last Change:  2024 Oct 4 by @Konfekt
+
+if exists("cppcheck")
+  finish
+endif
+let current_compiler = "cppcheck"
+
+let s:cpo_save = &cpo
+set cpo-=C
+
+if !exists('g:c_cppcheck_params')
+  let g:c_cppcheck_params = '--verbose --force --inline-suppr'
+        \ ..' '..'--enable=warning,style,performance,portability,information,missingInclude'
+        \ ..' '..(executable('getconf') ? '-j' .. systemlist('getconf _NPROCESSORS_ONLN')[0] : '')
+  let s:undo_compiler = 'unlet! g:c_cppcheck_params'
+endif
+
+let &l:makeprg = 'cppcheck --quiet'
+      \ ..' --template="{file}:{line}:{column}: {severity}: [{id}] {message} {callstack}"'
+      \ ..' '..get(b:, 'c_cppcheck_params',
+      \            g:c_cppcheck_params..' '..(&filetype ==# 'cpp' ? ' --language=c++' : ''))
+      \ ..' '..get(b:, 'c_cppcheck_includes', get(g:, 'c_cppcheck_includes',
+      \	          (filereadable('compile_commands.json') ? '--project=compile_commands.json' :
+      \	          (empty(&path) ? '' : '-I')..join(map(filter(split(&path, ','), 'isdirectory(v:val)'),'shellescape(v:val)'), ' -I'))))
+silent CompilerSet makeprg
+
+CompilerSet errorformat=
+  \%f:%l:%c:\ %tarning:\ %m,
+  \%f:%l:%c:\ %trror:\ %m,
+  \%f:%l:%c:\ %tnformation:\ %m,
+  \%f:%l:%c:\ %m,
+  \%.%#\ :\ [%f:%l]\ %m
+
+exe get(s:, 'undo_compiler', '')
+
+let &cpo = s:cpo_save
+unlet s:cpo_save
--- a/runtime/doc/quickfix.txt
+++ b/runtime/doc/quickfix.txt
@@ -1,4 +1,4 @@
-*quickfix.txt*  For Vim version 9.1.  Last change: 2024 Sep 10
+*quickfix.txt*  For Vim version 9.1.  Last change: 2024 Oct 05
 
 
 		  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1278,6 +1278,32 @@ For writing a compiler plugin, see |writ
 
 Use the |compiler-make| plugin to undo the effect of a compiler plugin.
 
+CPPCHECK			*quickfix-cppcheck* *compiler-cppcheck*
+
+Use g/b:`c_cppcheck_params` to set cppcheck parameters. The global
+settings by default include
+
+- `--verbose`: Enables verbose output.
+- `--force`: Forces checking of all configurations.
+- `--inline-suppr`: Allows inline suppressions.
+- `--enable=...`: Enables specific checks like warnings, style, performance,
+  portability, information, and missing includes.
+- `-j`: Utilizes multiple processors if available, determined by the
+  `getconf` command if available (requires omitting the unusedFunction check)
+
+For C++ files (`filetype == 'cpp'`), the `--language=c++` option is added to
+ensure Cppcheck treats the file as C++.
+
+If compile_commands.json is present in the current directory, it is added as a
+`--project` parameter to the command line. Otherwise, by default the
+directories in &path are passed as include directories. These can be set by
+g/b:`c_cppcheck_includes` as a list of `-I` flags. Tim Pope's vim-apathy
+plug-in [0] can expand &path. To also append the folders in a git repo use >
+
+  let &l:path = join(systemlist('git ls-tree -d --name-only -r HEAD'), ',')
+
+[0] https://github.com/tpope/vim-apathy
+
 DOTNET							*compiler-dotnet*
 
 The .NET CLI compiler outputs both errors and warnings by default. The output