问题
I use the following script to integrate Cppcheck with gVim:
" vimcppcheck.vim
" ===================================================================
" Code Checking with cppcheck (1)
" ===================================================================
function! Cppcheck_1()
set makeprg=cppcheck\ --enable=all\ %
setlocal errorformat=[%f:%l]:%m
let curr_dir = expand('%:h')
if curr_dir == ''
let curr_dir = '.'
endif
echo curr_dir
execute 'lcd ' . curr_dir
execute 'make'
execute 'lcd -'
exe ":botright cwindow"
:copen
endfunction
:menu Build.Code\ Checking.cppcheck :cclose<CR>:update<CR>:call Cppcheck_1() <cr>
Normally this is very good, but this script sometimes creates trouble when checking wrong pointers with Cppcheck.
For example, I have the following C code:
/* test_cppcheck.c */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *ptr01;
*ptr01 = (int *)malloc((size_t)10 * sizeof(int)); /* FIXME: I intensionally written *ptr01 instead of ptr01 */
if(ptr01==NULL) {
fprintf(stderr, "\ndynamic memory allocation failed\n");
exit(EXIT_FAILURE);
}
free(ptr01);
ptr01 = NULL;
}
The quickfix list shows:
|| Checking test_cppcheck.c...
H:\codes\test_cppcheck.c:11] -> [test_cppcheck.c|12| (warning) Possible null pointer dereference: ptr01 - otherwise it is redundant to check it against null.
H:\codes\test_cppcheck.c|11| (error) Uninitialized variable: ptr01
H:\codes\test_cppcheck.c|16| (error) Uninitialized variable: ptr01
H:\codes\test_cppcheck.c|12| (error) Uninitialized variable: ptr01
|| Checking usage of global functions..
|| (information) Cppcheck cannot find all the include files (use --check-config for details)
After a lot of Vim errors, a new file '11] -> [test_cppcheck.c' is created in a new buffer. When I double-click the first error, nothing can be done from the quickfix window. It is because of the errorformat as much as I know.
The ->
instead of :
is creating all the trouble, although I know minor tweaking of this script will fix this issue, but I am tired of doing so.
Please try this first. How can I deal with this?
回答1:
Without the original format of the error this is guesswork, but I think you need to add an alternative to the 'errorformat'
definition (these are comma-separated):
setlocal errorformat=[%f:%l]\ ->\ %m,[%f:%l]:%m
PS: You should also use :setlocal
for the 'makeprg'
option to restrict it to the current buffer, too.
回答2:
Now I'm using the script below, and it's working perfectly as I expected.
This can be a general solution to everyone who is interested to integrate Cppcheck with Vim.
Of course, this script can be improved a lot. But it's a starting point for them.
" vimcppcheck.vim
" ===================================================================
" Code Checking with cppcheck (1)
" Thanks to Mr. Ingo Karkat
" http://stackoverflow.com/questions/19157270/vim-cppcheck-which-errorformat-to-use
" ===================================================================
function! Cppcheck_1()
setlocal makeprg=cppcheck\ --enable=all\ %
" earlier it was: " setlocal errorformat=[%f:%l]:%m
" fixed by an advise by Mr. Ingo Karkat
setlocal errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m
let curr_dir = expand('%:h')
if curr_dir == ''
let curr_dir = '.'
endif
echo curr_dir
execute 'lcd ' . curr_dir
execute 'make'
execute 'lcd -'
exe ":botright cwindow"
:copen
endfunction
:menu Build.Code\ Checking.cppcheck :cclose<CR>:update<CR>:call Cppcheck_1() <cr>
来源:https://stackoverflow.com/questions/19157270/which-error-format-should-be-used-for-vim-and-cppcheck