From aee0cc45bea7f190c27f0fc06f98a465ecff56fa Mon Sep 17 00:00:00 2001 From: Dan George Date: Wed, 17 Nov 2021 20:27:23 -0800 Subject: Cppcheck buffered file only (#3983) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add cppcheck handler match on misra msg * Use --file-filter cppcheck option Cppcheck recently added --file-filter so that cppcheck only checks the filtered files, even when using --project option, which checks all files in the project, by default. The --ccpcheck-build-dir option didn't help enough (at all?). * Added C test cases Also fixed and assumed typo: foo.c, instead of foo.cpp * Replace hard-coded full path filenames Attempt to fix the windows platform test execution. * Fix typo - foo.c, instead of foo.cpp * Reset buffer var between tests * Handle header files in cppcheck Cppcheck isn't designed to check header files, stand-alone. Daniel Marjamäki suggested using --suppress options to avoid FPs. * Fix Vint complaint in cppcheck handler. * Fix file path in cppcheck handler Co-authored-by: Dan George --- autoload/ale/handlers/cppcheck.vim | 18 +++++++++++++++++- test/linter/test_c_cppcheck.vader | 18 ++++++++++++++++-- test/linter/test_cpp_cppcheck.vader | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/autoload/ale/handlers/cppcheck.vim b/autoload/ale/handlers/cppcheck.vim index bb8c8602..cf69df26 100644 --- a/autoload/ale/handlers/cppcheck.vim +++ b/autoload/ale/handlers/cppcheck.vim @@ -19,6 +19,18 @@ function! ale#handlers#cppcheck#GetBufferPathIncludeOptions(buffer) abort endfunction function! ale#handlers#cppcheck#GetCompileCommandsOptions(buffer) abort + " The compile_commands.json doesn't apply to headers and cppheck will + " bail out if it cannot find a file matching the filter, below. Skip out + " now, for headers. Also, suppress FPs; cppcheck is not meant to + " process lone header files. + let b:buffer_name = bufname(a:buffer) + let b:file_extension = fnamemodify(b:buffer_name, ':e') + + if b:file_extension is# 'h' || b:file_extension is# 'hpp' + return ale#handlers#cppcheck#GetBufferPathIncludeOptions(a:buffer) + \ . ' --suppress=unusedStructMember' + endif + " If the current buffer is modified, using compile_commands.json does no " good, so include the file's directory instead. It's not quite as good as " using --project, but is at least equivalent to running cppcheck on this @@ -34,9 +46,13 @@ function! ale#handlers#cppcheck#GetCompileCommandsOptions(buffer) abort " If we find it, we'll `cd` to where the compile_commands.json file is, " then use the file to set up import paths, etc. let [l:dir, l:json_path] = ale#c#FindCompileCommands(a:buffer) + let b:root_index = len(l:dir) + 1 + let b:buffer_file= bufname(a:buffer) + " By default, cppcheck processes every config in compile_commands.json. + " Use --file-filter to limit to just the buffer file. return !empty(l:json_path) - \ ? '--project=' . ale#Escape(l:json_path[len(l:dir) + 1: ]) + \ ? '--project=' . ale#Escape(l:json_path[b:root_index: ]) . ' --file-filter=' . ale#Escape(b:buffer_file[b:root_index:]) \ : '' endfunction diff --git a/test/linter/test_c_cppcheck.vader b/test/linter/test_c_cppcheck.vader index c84053f8..c0611143 100644 --- a/test/linter/test_c_cppcheck.vader +++ b/test/linter/test_c_cppcheck.vader @@ -22,20 +22,22 @@ Execute(cppcheck for C should detect compile_commands.json files): \ . ' -q --language=c' \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') \ . ' --project=' . ale#Escape('compile_commands.json') + \ . ' --file-filter=' . ale#Escape('foo.c') \ . ' --enable=style %t' Execute(cppcheck for C should detect compile_commands.json files in build directories): - call ale#test#SetFilename('../test-files/cppcheck/with_build_dir/foo.cpp') + call ale#test#SetFilename('../test-files/cppcheck/with_build_dir/foo.c') AssertLinterCwd ale#path#Simplify(g:dir . '/../test-files/cppcheck/with_build_dir') AssertLinter 'cppcheck', ale#Escape('cppcheck') \ . ' -q --language=c' \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') \ . ' --project=' . ale#Escape(ale#path#Simplify('build/compile_commands.json')) + \ . ' --file-filter=' . ale#Escape('foo.c') \ . ' --enable=style %t' Execute(cppcheck for C should include file dir if compile_commands.json file is not found): - call ale#test#SetFilename('../test-files/cppcheck/foo.cpp') + call ale#test#SetFilename('../test-files/cppcheck/foo.c') AssertLinter 'cppcheck', \ ale#Escape('cppcheck') @@ -44,3 +46,15 @@ Execute(cppcheck for C should include file dir if compile_commands.json file is \ . ' --enable=style' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/cppcheck')) \ . ' %t' + +Execute(cppcheck for C header should include file dir and not use compile_commands.json): + call ale#test#SetFilename('../test-files/cppcheck/one/foo.h') + + AssertLinter 'cppcheck', + \ ale#Escape('cppcheck') + \ . ' -q --language=c' + \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') + \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/cppcheck/one')) + \ . ' --suppress=unusedStructMember' + \ . ' --enable=style' + \ . ' %t' diff --git a/test/linter/test_cpp_cppcheck.vader b/test/linter/test_cpp_cppcheck.vader index 62195803..2cde1da4 100644 --- a/test/linter/test_cpp_cppcheck.vader +++ b/test/linter/test_cpp_cppcheck.vader @@ -28,6 +28,7 @@ Execute(cppcheck for C++ should detect compile_commands.json files): \ . ' -q --language=c++' \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') \ . ' --project=' . ale#Escape('compile_commands.json') + \ . ' --file-filter=' . ale#Escape('foo.cpp') \ . ' --enable=style %t' Execute(cppcheck for C++ should detect compile_commands.json files in build directories): @@ -38,6 +39,7 @@ Execute(cppcheck for C++ should detect compile_commands.json files in build dire \ . ' -q --language=c++' \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') \ . ' --project=' . ale#Escape(ale#path#Simplify('build/compile_commands.json')) + \ . ' --file-filter=' . ale#Escape('foo.cpp') \ . ' --enable=style %t' Execute(cppcheck for C++ should include file dir if compile_commands.json file is not found): @@ -51,6 +53,18 @@ Execute(cppcheck for C++ should include file dir if compile_commands.json file i \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/cppcheck')) \ . ' %t' +Execute(cppcheck for C++ header should include file dir and not use compile_commands.json): + call ale#test#SetFilename('../test-files/cppcheck/one/foo.hpp') + + AssertLinter 'cppcheck', + \ ale#Escape('cppcheck') + \ . ' -q --language=c++' + \ . ' --template=' . ale#Escape('{file}:{line}:{column}: {severity}:{inconclusive:inconclusive:} {message} [{id}]\\n{code}') + \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/../test-files/cppcheck/one')) + \ . ' --suppress=unusedStructMember' + \ . ' --enable=style' + \ . ' %t' + Execute(cppcheck for C++ should ignore compile_commands.json file if buffer is modified): call ale#test#SetFilename('../test-files/cppcheck/one/foo.cpp') -- cgit v1.2.3