syntastic/syntax_checkers/c.vim

107 lines
3.2 KiB
VimL
Raw Normal View History

"============================================================================
"File: c.vim
"Description: Syntax checking plugin for syntastic.vim
"Maintainer: Gregor Uhlenheuer <kongo2002 at gmail dot com>
"License: This program is free software. It comes without any warranty,
" to the extent permitted by applicable law. You can redistribute
" it and/or modify it under the terms of the Do What The Fuck You
" Want To Public License, Version 2, as published by Sam Hocevar.
" See http://sam.zoy.org/wtfpl/COPYING for more details.
"
"============================================================================
" in order to also check header files add this to your .vimrc:
" (this usually creates a .gch file in your source directory)
"
" let g:syntastic_c_check_header = 1
2009-12-20 14:08:31 -05:00
if exists('loaded_c_syntax_checker')
finish
endif
let loaded_c_syntax_checker = 1
2009-12-20 14:08:31 -05:00
if !executable('gcc')
finish
endif
2010-03-25 19:16:38 -04:00
" initialize handlers
function! s:Init()
let s:handlers = []
call s:RegHandler('\%(gtk\|glib\)', s:CheckGtk())
call s:RegHandler('ruby', s:CheckRuby())
unlet! s:RegHandler
endfunction
function! SyntaxCheckers_c_GetLocList()
2009-12-20 13:02:08 -05:00
let makeprg = 'gcc -fsyntax-only %'
let errorformat = '%-G%f:%s:,%f:%l: %m'
2009-12-20 14:08:31 -05:00
if expand('%') =~? '.h$'
if exists('g:syntastic_c_check_header')
let makeprg = 'gcc -c %'
else
return []
endif
endif
2010-03-25 19:16:38 -04:00
let makeprg .= s:SearchHeaders(s:handlers)
2010-02-08 10:44:31 -05:00
return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
endfunction
2010-02-08 10:44:31 -05:00
2010-03-25 19:16:38 -04:00
" search the first 100 lines for include statements that are
" given in the s:handlers dictionary
function! s:SearchHeaders(handlers)
let includes = ''
let found = {}
for i in range(100)
for handler in a:handlers
if !has_key(found, handler["func"])
if getline(i) =~? '^#include.*' . handler["regex"]
let includes .= handler["func"]
let found[handler["func"]] = 1
2010-02-08 10:44:31 -05:00
endif
endif
endfor
2010-03-25 19:16:38 -04:00
endfor
return includes
endfunction
" try to find the gtk headers with 'pkg-config'
function! s:CheckGtk()
if executable('pkg-config')
if !exists('s:gtk_flags')
let s:gtk_flags = system('pkg-config --cflags gtk+-2.0')
let s:gtk_flags = substitute(s:gtk_flags, "\n", '', '')
let s:gtk_flags = ' ' . s:gtk_flags
endif
return s:gtk_flags
2010-02-08 10:44:31 -05:00
endif
return ''
endfunction
2010-03-25 18:21:19 -04:00
2010-03-25 19:16:38 -04:00
" try to find the headers with 'rbconfig'
2010-03-25 18:21:19 -04:00
function! s:CheckRuby()
if executable('ruby')
2010-03-25 19:16:38 -04:00
if !exists('s:ruby_flags')
let s:ruby_flags = system('ruby -r rbconfig -e '
\ . '''puts Config::CONFIG["archdir"]''')
let s:ruby_flags = substitute(s:ruby_flags, "\n", '', '')
let s:ruby_flags = ' -I' . s:ruby_flags
endif
return s:ruby_flags
2010-03-25 18:21:19 -04:00
endif
return ''
endfunction
2010-03-25 19:16:38 -04:00
" return a handler dictionary object
function! s:RegHandler(regex, function)
let handler = {}
let handler["regex"] = a:regex
let handler["func"] = a:function
call add(s:handlers, handler)
endfunction
call s:Init()