1 "============================================================================
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.
11 "============================================================================
13 " In order to also check header files add this to your .vimrc:
14 " (this usually creates a .gch file in your source directory)
16 " let g:syntastic_c_check_header = 1
18 " To disable the search of included header files after special
19 " libraries like gtk and glib add this line to your .vimrc:
21 " let g:syntastic_c_no_include_search = 1
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.
30 " let g:syntastic_c_auto_refresh_includes = 1
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:
36 " let b:syntastic_c_cflags = ' -I/usr/include/libsoup-2.4'
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:
42 " let g:syntastic_c_include_dirs = [ 'includes', 'headers' ]
44 " Moreover it is possible to add additional compiler options to the syntax
45 " checking execution via the variable 'g:syntastic_c_compiler_options':
47 " let g:syntastic_c_compiler_options = ' -ansi'
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:
53 " let g:syntastic_c_remove_include_errors = 1
55 if exists('loaded_c_syntax_checker')
58 let loaded_c_syntax_checker = 1
67 " default include directories
68 let s:default_includes = [ '.', '..', 'include', 'includes',
69 \ '../include', '../includes' ]
71 " uniquify the input list
72 function! s:Unique(list)
75 if index(l, elem) == -1
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
87 if exists('g:syntastic_c_include_dirs')
88 call extend(include_dirs, g:syntastic_c_include_dirs)
91 return join(map(s:Unique(include_dirs), '"-I" . v:val'), ' ')
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'
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()
112 " add optional user-defined compiler options
113 if exists('g:syntastic_c_compiler_options')
114 let makeprg .= g:syntastic_c_compiler_options
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()
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()
131 let makeprg .= b:syntastic_c_includes
135 " use the user-defined cflags
136 let makeprg .= b:syntastic_c_cflags
140 let errors = SyntasticMake({ 'makeprg': makeprg,
141 \ 'errorformat': errorformat })
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(''))
153 let &cpo = s:save_cpo
156 " vim: set et sts=4 sw=4: