【问题标题】:Which error format should be used for Vim and Cppcheck?Vim 和 Cppcheck 应该使用哪种错误格式?
【发布时间】:2013-10-03 10:51:23
【问题描述】:

我使用以下脚本将 Cppcheck 与 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 检查错误的指针时,此脚本有时会产生麻烦。

例如,我有以下 C 代码:

/* 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 窗口什么也做不了。据我所知,这是因为错误格式。

-&gt; 而不是 : 正在制造所有麻烦,虽然我知道对这个脚本稍作调整可以解决这个问题,但我已经厌倦了这样做。

请先试试这个。我该如何处理?

【问题讨论】:

    标签: vim cppcheck


    【解决方案1】:

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

    setlocal errorformat=[%f:%l]\ ->\ %m,[%f:%l]:%m
    

    PS:您还应该使用:setlocal 来设置'makeprg' 选项,以将其也限制在当前缓冲区中。

    【讨论】:

    • 谢谢你非常非常非常。这很好用,它很棒而且很棒。但我不知道如何将 :setlocal 用于 'makeprg' 选项以将其限制为当前缓冲区,但我得到了答案。再次感谢您。
    • 我的意思是,在您的Cppcheck_1() 函数中,将set makeprg... 更改为setlocal makeprg...
    • 谢谢,我会改的。
    • 现在我将 set makeprg... 更改为 setlocal makeprg...,现在我的脚本更加健壮,因为它不会意外打开新缓冲区。谢谢。
    【解决方案2】:

    现在我正在使用下面的脚本,它按我的预期完美运行。

    对于所有有兴趣将 Cppcheck 与 Vim 集成的人来说,这可能是一个通用的解决方案。

    当然,这个脚本可以改进很多。但这对他们来说是一个起点。

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

    【讨论】:

      猜你喜欢
      • 2012-01-11
      • 2010-10-09
      • 2019-05-31
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多