From fbf8ccb882eb7819011fa837ea2b4930ed4fa9c7 Mon Sep 17 00:00:00 2001 From: w0rp Date: Thu, 22 Jun 2017 14:08:58 +0100 Subject: [PATCH] Fix #677 - Ignore errors from other files for cppcheck --- autoload/ale/handlers/cppcheck.vim | 15 +++++----- test/handler/test_common_handlers.vader | 21 ------------- test/handler/test_cppcheck_handler.vader | 38 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 test/handler/test_cppcheck_handler.vader diff --git a/autoload/ale/handlers/cppcheck.vim b/autoload/ale/handlers/cppcheck.vim index f5df58b7..b365c794 100644 --- a/autoload/ale/handlers/cppcheck.vim +++ b/autoload/ale/handlers/cppcheck.vim @@ -4,16 +4,17 @@ function! ale#handlers#cppcheck#HandleCppCheckFormat(buffer, lines) abort " Look for lines like the following. " " [test.cpp:5]: (error) Array 'a[10]' accessed at index 10, which is out of bounds - let l:pattern = '^\[.\{-}:\(\d\+\)\]: (\(.\{-}\)) \(.\+\)' + let l:pattern = '\v^\[(.+):(\d+)\]: \(([a-z]+)\) (.+)$' let l:output = [] for l:match in ale#util#GetMatches(a:lines, l:pattern) - call add(l:output, { - \ 'lnum': l:match[1] + 0, - \ 'col': 0, - \ 'text': l:match[3] . ' (' . l:match[2] . ')', - \ 'type': l:match[2] ==# 'error' ? 'E' : 'W', - \}) + if ale#path#IsBufferPath(a:buffer, l:match[1]) + call add(l:output, { + \ 'lnum': str2nr(l:match[2]), + \ 'type': l:match[3] ==# 'error' ? 'E' : 'W', + \ 'text': l:match[4], + \}) + endif endfor return l:output diff --git a/test/handler/test_common_handlers.vader b/test/handler/test_common_handlers.vader index 9a273946..65026d80 100644 --- a/test/handler/test_common_handlers.vader +++ b/test/handler/test_common_handlers.vader @@ -177,24 +177,3 @@ Execute (Unix format functions should handle Windows paths): \ 'C:\Users\w0rp\AppData\Local\Temp\Xyz123.go:27: foo', \ 'C:\Users\w0rp\AppData\Local\Temp\Xyz123.go:53:10: foo', \ ]) - -Execute (HandleCppCheckFormat should handle some example lines of output): - AssertEqual - \ [ - \ { - \ 'lnum': 5, - \ 'col': 0, - \ 'type': 'W', - \ 'text': 'Variable a is assigned a value that is never used. (style)', - \ }, - \ { - \ 'lnum': 12, - \ 'col': 0, - \ 'type': 'E', - \ 'text': 'Array a[10] accessed at index 10, which is out of bounds. (error)', - \ }, - \ ], - \ ale#handlers#cppcheck#HandleCppCheckFormat(42, [ - \ '[/tmp/test.c:5]: (style) Variable a is assigned a value that is never used.', - \ '[/tmp/test.c:12]: (error) Array a[10] accessed at index 10, which is out of bounds.' - \ ]) diff --git a/test/handler/test_cppcheck_handler.vader b/test/handler/test_cppcheck_handler.vader new file mode 100644 index 00000000..51efad4e --- /dev/null +++ b/test/handler/test_cppcheck_handler.vader @@ -0,0 +1,38 @@ +Before: + silent! cd /testplugin/test/handler + let g:dir = getcwd() + +After: + silent execute 'cd ' . fnameescape(g:dir) + unlet! g:dir + +Execute(Basic errors should be handled by cppcheck): + call ale#test#SetFilename('test.cpp') + + AssertEqual + \ [ + \ { + \ 'lnum': 5, + \ 'type': 'E', + \ 'text': 'Array ''a[10]'' accessed at index 10, which is out of bounds', + \ }, + \ { + \ 'lnum': 7, + \ 'type': 'W', + \ 'text': 'Some other problem', + \ }, + \ ], + \ ale#handlers#cppcheck#HandleCppCheckFormat(bufnr(''), [ + \ '[test.cpp:5]: (error) Array ''a[10]'' accessed at index 10, which is out of bounds', + \ '[test.cpp:7]: (warning) Some other problem', + \ ]) + +Execute(Problems from other files should be ignored by cppcheck): + call ale#test#SetFilename('test.cpp') + + AssertEqual + \ [ + \ ], + \ ale#handlers#cppcheck#HandleCppCheckFormat(bufnr(''), [ + \ '[bar.cpp:5]: (error) Array ''a[10]'' accessed at index 10, which is out of bounds', + \ ])