]> git.r.bdr.sh - rbdr/dotfiles/blob - vim/syntax_checkers/c.vim
8bd486b666af7e6460a9c9da034f1669c423ddf0
[rbdr/dotfiles] / vim / syntax_checkers / c.vim
1 "============================================================================
2 "File: c.vim
3 "Description: Syntax checking plugin for syntastic.vim
4 "Maintainer: Gregor Uhlenheuer <kongo2002 at gmail dot com>
5 "License: This program is free software. It comes without any warranty,
6 " to the extent permitted by applicable law. You can redistribute
7 " it and/or modify it under the terms of the Do What The Fuck You
8 " Want To Public License, Version 2, as published by Sam Hocevar.
9 " See http://sam.zoy.org/wtfpl/COPYING for more details.
10 "
11 "============================================================================
12
13 " In order to also check header files add this to your .vimrc:
14 " (this usually creates a .gch file in your source directory)
15 "
16 " let g:syntastic_c_check_header = 1
17 "
18 " To disable the search of included header files after special
19 " libraries like gtk and glib add this line to your .vimrc:
20 "
21 " let g:syntastic_c_no_include_search = 1
22 "
23 " To enable header files being re-checked on every file write add the
24 " following line to your .vimrc. Otherwise the header files are checked only
25 " one time on initially loading the file.
26 " In order to force syntastic to refresh the header includes simply
27 " unlet b:syntastic_c_includes. Then the header files are being re-checked on
28 " the next file write.
29 "
30 " let g:syntastic_c_auto_refresh_includes = 1
31 "
32 " Alternatively you can set the buffer local variable b:syntastic_c_cflags.
33 " If this variable is set for the current buffer no search for additional
34 " libraries is done. I.e. set the variable like this:
35 "
36 " let b:syntastic_c_cflags = ' -I/usr/include/libsoup-2.4'
37 "
38 " In order to add some custom include directories that should be added to the
39 " gcc command line you can add those to the global variable
40 " g:syntastic_c_include_dirs. This list can be used like this:
41 "
42 " let g:syntastic_c_include_dirs = [ 'includes', 'headers' ]
43 "
44 " Moreover it is possible to add additional compiler options to the syntax
45 " checking execution via the variable 'g:syntastic_c_compiler_options':
46 "
47 " let g:syntastic_c_compiler_options = ' -ansi'
48 "
49 " Using the global variable 'g:syntastic_c_remove_include_errors' you can
50 " specify whether errors of files included via the g:syntastic_c_include_dirs'
51 " setting are removed from the result set:
52 "
53 " let g:syntastic_c_remove_include_errors = 1
54
55 if exists('loaded_c_syntax_checker')
56 finish
57 endif
58 let loaded_c_syntax_checker = 1
59
60 if !executable('gcc')
61 finish
62 endif
63
64 let s:save_cpo = &cpo
65 set cpo&vim
66
67 " default include directories
68 let s:default_includes = [ '.', '..', 'include', 'includes',
69 \ '../include', '../includes' ]
70
71 " uniquify the input list
72 function! s:Unique(list)
73 let l = []
74 for elem in a:list
75 if index(l, elem) == -1
76 let l = add(l, elem)
77 endif
78 endfor
79 return l
80 endfunction
81
82 " get the gcc include directory argument depending on the default
83 " includes and the optional user-defined 'g:syntastic_c_include_dirs'
84 function! s:GetIncludeDirs()
85 let include_dirs = s:default_includes
86
87 if exists('g:syntastic_c_include_dirs')
88 call extend(include_dirs, g:syntastic_c_include_dirs)
89 endif
90
91 return join(map(s:Unique(include_dirs), '"-I" . v:val'), ' ')
92 endfunction
93
94 function! SyntaxCheckers_c_GetLocList()
95 let makeprg = 'gcc -fsyntax-only -std=gnu99 '.shellescape(expand('%')).
96 \ ' '.s:GetIncludeDirs()
97 let errorformat = '%-G%f:%s:,%-G%f:%l: %#error: %#(Each undeclared '.
98 \ 'identifier is reported only%.%#,%-G%f:%l: %#error: %#for '.
99 \ 'each function it appears%.%#,%-GIn file included%.%#,'.
100 \ '%-G %#from %f:%l\,,%f:%l:%c: %m,%f:%l: %trror: %m,%f:%l: %m'
101
102 " determine whether to parse header files as well
103 if expand('%') =~? '.h$'
104 if exists('g:syntastic_c_check_header')
105 let makeprg = 'gcc -c '.shellescape(expand('%')).
106 \ ' '.s:GetIncludeDirs()
107 else
108 return []
109 endif
110 endif
111
112 " add optional user-defined compiler options
113 if exists('g:syntastic_c_compiler_options')
114 let makeprg .= g:syntastic_c_compiler_options
115 endif
116
117 " check if the user manually set some cflags
118 if !exists('b:syntastic_c_cflags')
119 " check whether to search for include files at all
120 if !exists('g:syntastic_c_no_include_search') ||
121 \ g:syntastic_c_no_include_search != 1
122 " refresh the include file search if desired
123 if exists('g:syntastic_c_auto_refresh_includes') &&
124 \ g:syntastic_c_auto_refresh_includes != 0
125 let makeprg .= syntastic#c#SearchHeaders()
126 else
127 " search for header includes if not cached already
128 if !exists('b:syntastic_c_includes')
129 let b:syntastic_c_includes = syntastic#c#SearchHeaders()
130 endif
131 let makeprg .= b:syntastic_c_includes
132 endif
133 endif
134 else
135 " use the user-defined cflags
136 let makeprg .= b:syntastic_c_cflags
137 endif
138
139 " process makeprg
140 let errors = SyntasticMake({ 'makeprg': makeprg,
141 \ 'errorformat': errorformat })
142
143 " filter the processed errors if desired
144 if exists('g:syntastic_c_remove_include_errors') &&
145 \ g:syntastic_c_remove_include_errors != 0
146 return filter(errors,
147 \ 'has_key(v:val, "bufnr") && v:val["bufnr"]=='.bufnr(''))
148 else
149 return errors
150 endif
151 endfunction
152
153 let &cpo = s:save_cpo
154 unlet s:save_cpo
155
156 " vim: set et sts=4 sw=4: