Merge pull request #197 from kongo2002/cpp_include_dirs

Add include directories to c++
This commit is contained in:
Martin Grenfell 2012-03-04 14:46:25 -08:00
commit 7a5bca349c
3 changed files with 64 additions and 32 deletions

View File

@ -38,6 +38,40 @@ function! s:Init()
call s:RegHandler('php\.h', 'syntastic#c#CheckPhp', [])
endfunction
" default include directories
let s:default_includes = [ '.', '..', 'include', 'includes',
\ '../include', '../includes' ]
" uniquify the input list
function! s:Unique(list)
let l = []
for elem in a:list
if index(l, elem) == -1
let l = add(l, elem)
endif
endfor
return l
endfunction
" get the gcc include directory argument depending on the default
" includes and the optional user-defined 'g:syntastic_c_include_dirs'
function! syntastic#c#GetIncludeDirs(cpp)
let include_dirs = copy(s:default_includes)
if a:cpp == 1
if exists('g:syntastic_cpp_include_dirs')
call extend(include_dirs, g:syntastic_cpp_include_dirs)
endif
else
if exists('g:syntastic_c_include_dirs')
call extend(include_dirs, g:syntastic_c_include_dirs)
endif
endif
return join(map(s:Unique(include_dirs), '"-I" . v:val'), ' ')
endfunction
" search the first 100 lines for include statements that are
" given in the handlers dictionary
function! syntastic#c#SearchHeaders()

View File

@ -64,36 +64,9 @@ endif
let s:save_cpo = &cpo
set cpo&vim
" default include directories
let s:default_includes = [ '.', '..', 'include', 'includes',
\ '../include', '../includes' ]
" uniquify the input list
function! s:Unique(list)
let l = []
for elem in a:list
if index(l, elem) == -1
let l = add(l, elem)
endif
endfor
return l
endfunction
" get the gcc include directory argument depending on the default
" includes and the optional user-defined 'g:syntastic_c_include_dirs'
function! s:GetIncludeDirs()
let include_dirs = s:default_includes
if exists('g:syntastic_c_include_dirs')
call extend(include_dirs, g:syntastic_c_include_dirs)
endif
return join(map(s:Unique(include_dirs), '"-I" . v:val'), ' ')
endfunction
function! SyntaxCheckers_c_GetLocList()
let makeprg = 'gcc -fsyntax-only -std=gnu99 '.shellescape(expand('%')).
\ ' '.s:GetIncludeDirs()
\ ' '.syntastic#c#GetIncludeDirs(0)
let errorformat = '%-G%f:%s:,%-G%f:%l: %#error: %#(Each undeclared '.
\ 'identifier is reported only%.%#,%-G%f:%l: %#error: %#for '.
\ 'each function it appears%.%#,%-GIn file included%.%#,'.
@ -103,7 +76,7 @@ function! SyntaxCheckers_c_GetLocList()
if expand('%') =~? '.h$'
if exists('g:syntastic_c_check_header')
let makeprg = 'gcc -c '.shellescape(expand('%')).
\ ' '.s:GetIncludeDirs()
\ ' '.syntastic#c#GetIncludeDirs(0)
else
return []
endif

View File

@ -20,6 +20,12 @@
"
" let g:syntastic_cpp_no_include_search = 1
"
" In order to add some custom include directories that should be added to the
" gcc command line you can add those to the global variable
" g:syntastic_cpp_include_dirs. This list can be used like this:
"
" let g:syntastic_cpp_include_dirs = [ 'includes', 'headers' ]
"
" To enable header files being re-checked on every file write add the
" following line to your .vimrc. Otherwise the header files are checked only
" one time on initially loading the file.
@ -39,6 +45,12 @@
" checking execution via the variable 'g:syntastic_cpp_compiler_options':
"
" let g:syntastic_cpp_compiler_options = ' -std=c++0x'
"
" Using the global variable 'g:syntastic_cpp_remove_include_errors' you can
" specify whether errors of files included via the
" g:syntastic_cpp_include_dirs' setting are removed from the result set:
"
" let g:syntastic_cpp_remove_include_errors = 1
if exists('loaded_cpp_syntax_checker')
finish
@ -53,12 +65,14 @@ let s:save_cpo = &cpo
set cpo&vim
function! SyntaxCheckers_cpp_GetLocList()
let makeprg = 'g++ -fsyntax-only '.shellescape(expand('%'))
let makeprg = 'g++ -fsyntax-only '.shellescape(expand('%')).
\ ' ' . syntastic#c#GetIncludeDirs(1)
let errorformat = '%-G%f:%s:,%f:%l:%c: %m,%f:%l: %m'
if expand('%') =~? '\%(.h\|.hpp\|.hh\)$'
if exists('g:syntastic_cpp_check_header')
let makeprg = 'g++ -c '.shellescape(expand('%'))
let makeprg = 'g++ -c '.shellescape(expand('%')).
\ ' ' . syntastic#c#GetIncludeDirs(1)
else
return []
endif
@ -85,7 +99,18 @@ function! SyntaxCheckers_cpp_GetLocList()
let makeprg .= b:syntastic_cpp_cflags
endif
return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
" process makeprg
let errors = SyntasticMake({ 'makeprg': makeprg,
\ 'errorformat': errorformat })
" filter the processed errors if desired
if exists('g:syntastic_cpp_remove_include_errors') &&
\ g:syntastic_cpp_remove_include_errors != 0
return filter(errors,
\ 'has_key(v:val, "bufnr") && v:val["bufnr"]=='.bufnr(''))
else
return errors
endif
endfunction
let &cpo = s:save_cpo