Vim和Cppcheck应该使用哪种错误格式? [英] Which error format should be used for Vim and Cppcheck?

查看:302
本文介绍了Vim和Cppcheck应该使用哪种错误格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下脚本将Cppcheck与gVim集成:

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>

通常这非常好,但是使用Cppcheck检查错误的指针时,此脚本有时会带来麻烦。

Normally this is very good, but this script sometimes creates trouble when checking wrong pointers with Cppcheck.

例如,我有以下C代码:

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;
}

快速修复列表显示:

|| 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)

之后很多Vim错误,在新缓冲区中创建了一个新文件'11]-> [test_cppcheck.c'。当我双击第一个错误时,无法从quickfix窗口执行任何操作。

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?

推荐答案

没有错误的原始格式,这是猜测,但是我认为您需要添加'错误格式'定义的替代形式(这些用逗号分隔):

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:您还应该对'makeprg':setlocal >选项也将其限制为当前缓冲区。

PS: You should also use :setlocal for the 'makeprg' option to restrict it to the current buffer, too.

这篇关于Vim和Cppcheck应该使用哪种错误格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆