From 8e2634ae7ee58b0b3e476fb9edc6d92d4e288b62 Mon Sep 17 00:00:00 2001 From: kongo2002 Date: Sun, 4 Mar 2012 19:59:26 +0100 Subject: [PATCH 1/3] add include directory functionality to cpp --- autoload/syntastic/c.vim | 34 ++++++++++++++++++++++++++++++++++ syntax_checkers/c.vim | 31 ++----------------------------- syntax_checkers/cpp.vim | 6 ++++-- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/autoload/syntastic/c.vim b/autoload/syntastic/c.vim index c7232df4..8699a172 100644 --- a/autoload/syntastic/c.vim +++ b/autoload/syntastic/c.vim @@ -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() diff --git a/syntax_checkers/c.vim b/syntax_checkers/c.vim index 8bd486b6..740ad746 100644 --- a/syntax_checkers/c.vim +++ b/syntax_checkers/c.vim @@ -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 diff --git a/syntax_checkers/cpp.vim b/syntax_checkers/cpp.vim index 1bacf936..7b5ff21a 100644 --- a/syntax_checkers/cpp.vim +++ b/syntax_checkers/cpp.vim @@ -53,12 +53,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 From d81b360f645474a057057d1888b4e3902e8c9087 Mon Sep 17 00:00:00 2001 From: kongo2002 Date: Sun, 4 Mar 2012 20:04:15 +0100 Subject: [PATCH 2/3] add missing doc comment --- syntax_checkers/cpp.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/syntax_checkers/cpp.vim b/syntax_checkers/cpp.vim index 7b5ff21a..7166c44b 100644 --- a/syntax_checkers/cpp.vim +++ b/syntax_checkers/cpp.vim @@ -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. From 441506c06177125ddb714ed84240c3184804bab4 Mon Sep 17 00:00:00 2001 From: kongo2002 Date: Sun, 4 Mar 2012 21:08:04 +0100 Subject: [PATCH 3/3] cpp: add option to remove include errors --- syntax_checkers/cpp.vim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/syntax_checkers/cpp.vim b/syntax_checkers/cpp.vim index 7166c44b..5fc6111e 100644 --- a/syntax_checkers/cpp.vim +++ b/syntax_checkers/cpp.vim @@ -45,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 @@ -93,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