]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/handlers/cppcheck.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / handlers / cppcheck.vim
1 " Description: Handle errors for cppcheck.
2
3 function! ale#handlers#cppcheck#GetCwd(buffer) abort
4     let [l:dir, l:json_path] = ale#c#FindCompileCommands(a:buffer)
5
6     return !empty(l:dir) ? l:dir : ''
7 endfunction
8
9 function! ale#handlers#cppcheck#GetBufferPathIncludeOptions(buffer) abort
10     let l:buffer_path_include = ''
11
12     " Get path to this buffer so we can include it into cppcheck with -I
13     " This could be expanded to get more -I directives from the compile
14     " command in compile_commands.json, if it's found.
15     let l:buffer_path = fnamemodify(bufname(a:buffer), ':p:h')
16     let l:buffer_path_include = ' -I' . ale#Escape(l:buffer_path)
17
18     return l:buffer_path_include
19 endfunction
20
21 function! ale#handlers#cppcheck#GetCompileCommandsOptions(buffer) abort
22     " The compile_commands.json doesn't apply to headers and cppheck will
23     " bail out if it cannot find a file matching the filter, below. Skip out
24     " now, for headers. Also, suppress FPs; cppcheck is not meant to
25     " process lone header files.
26     let b:buffer_name = bufname(a:buffer)
27     let b:file_extension = fnamemodify(b:buffer_name, ':e')
28
29     if b:file_extension is# 'h' || b:file_extension is# 'hpp'
30         return ale#handlers#cppcheck#GetBufferPathIncludeOptions(a:buffer)
31         \   . ' --suppress=unusedStructMember'
32     endif
33
34     " If the current buffer is modified, using compile_commands.json does no
35     " good, so include the file's directory instead. It's not quite as good as
36     " using --project, but is at least equivalent to running cppcheck on this
37     " file manually from the file's directory.
38     let l:modified = getbufvar(a:buffer, '&modified')
39
40     if l:modified
41         return ''
42     endif
43
44     " Search upwards from the file for compile_commands.json.
45     "
46     " If we find it, we'll `cd` to where the compile_commands.json file is,
47     " then use the file to set up import paths, etc.
48     let [l:dir, l:json_path] = ale#c#FindCompileCommands(a:buffer)
49
50     " By default, cppcheck processes every config in compile_commands.json.
51     " Use --file-filter to limit to just the buffer file.
52     return !empty(l:json_path)
53     \   ? '--project=' . ale#Escape(l:json_path[len(l:dir) + 1: ]) . ' --file-filter=' . ale#Escape(bufname(a:buffer))
54     \   : ''
55 endfunction
56
57 function! ale#handlers#cppcheck#HandleCppCheckFormat(buffer, lines) abort
58     " Look for lines like the following.
59     "
60     "test.cpp:974:6: error:inconclusive Array 'n[3]' accessed at index 3, which is out of bounds. [arrayIndexOutOfBounds]\
61     "    n[3]=3;
62     "     ^
63     "" OR if cppcheck doesn't support {column} or {inconclusive:text}:
64     "test.cpp:974:{column}: error:{inconclusive:inconclusive} Array 'n[3]' accessed at index 3, which is out of bounds. [arrayIndexOutOfBounds]\
65     "    n[3]=3;
66     "     ^
67     "
68     "" OR if using the misra addon:
69     "test.c:1:16: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.7]\'
70     "void test( int parm ) {}
71     "               ^
72     let l:pattern = '\v(\f+):(\d+):(\d+|\{column\}): (\w+):(\{inconclusive:inconclusive\})? ?(.*) \[(%(\w[-.]?)+)\]\'
73     let l:output = []
74
75     for l:match in ale#util#GetMatches(a:lines, l:pattern)
76         if ale#path#IsBufferPath(a:buffer, l:match[1])
77             call add(l:output, {
78             \   'lnum':     str2nr(l:match[2]),
79             \   'col':      match(l:match[3],'{column}') >= 0 ? 1 : str2nr(l:match[3]),
80             \   'type':     l:match[4] is# 'error' ? 'E' : 'W',
81             \   'sub_type': l:match[4] is# 'style' ? 'style' : '',
82             \   'text':     l:match[6],
83             \   'code':     l:match[7]
84             \})
85         endif
86     endfor
87
88     return l:output
89 endfunction