* Shell commands should by called async with the help of a command chain

* The makefile parser unit test should only test the cflag parser itself
#1167
This commit is contained in:
roel0 2018-03-20 21:49:31 +01:00
parent 38953c4626
commit 18d0aeb1a0
8 changed files with 92 additions and 63 deletions

View file

@ -6,7 +6,6 @@ RUN install_vim -tag v8.0.0027 -build \
ENV PACKAGES="\ ENV PACKAGES="\
bash \ bash \
git \ git \
make \
python \ python \
py-pip \ py-pip \
" "

View file

@ -3,16 +3,15 @@
call ale#Set('c_clang_executable', 'clang') call ale#Set('c_clang_executable', 'clang')
call ale#Set('c_clang_options', '-std=c11 -Wall') call ale#Set('c_clang_options', '-std=c11 -Wall')
call ale#Set('c_clang_parse_makefile', 0)
function! ale_linters#c#clang#GetExecutable(buffer) abort function! ale_linters#c#clang#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'c_clang_executable') return ale#Var(a:buffer, 'c_clang_executable')
endfunction endfunction
function! ale_linters#c#clang#GetCommand(buffer) abort function! ale_linters#c#clang#GetCommand(buffer, output) abort
let l:cflags = [] let l:cflags = []
if g:ale_c_clang_parse_makefile if !empty(a:output)
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ') let l:cflags = join(ale#c#ParseMakefile(a:buffer, join(a:output, '\n')), ' ')
endif endif
if empty(l:cflags) if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer)) let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
@ -33,6 +32,9 @@ call ale#linter#Define('c', {
\ 'name': 'clang', \ 'name': 'clang',
\ 'output_stream': 'stderr', \ 'output_stream': 'stderr',
\ 'executable_callback': 'ale_linters#c#clang#GetExecutable', \ 'executable_callback': 'ale_linters#c#clang#GetExecutable',
\ 'command_callback': 'ale_linters#c#clang#GetCommand', \ 'command_chain': [
\ {'callback': 'ale#c#ParseMakefile', 'output_stream': 'stdout'},
\ {'callback': 'ale_linters#c#clang#GetCommand'}
\ ],
\ 'callback': 'ale#handlers#gcc#HandleGCCFormat', \ 'callback': 'ale#handlers#gcc#HandleGCCFormat',
\}) \})

View file

@ -3,16 +3,15 @@
call ale#Set('c_gcc_executable', 'gcc') call ale#Set('c_gcc_executable', 'gcc')
call ale#Set('c_gcc_options', '-std=c11 -Wall') call ale#Set('c_gcc_options', '-std=c11 -Wall')
call ale#Set('c_gcc_parse_makefile', 0)
function! ale_linters#c#gcc#GetExecutable(buffer) abort function! ale_linters#c#gcc#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'c_gcc_executable') return ale#Var(a:buffer, 'c_gcc_executable')
endfunction endfunction
function! ale_linters#c#gcc#GetCommand(buffer) abort function! ale_linters#c#gcc#GetCommand(buffer, output) abort
let l:cflags = [] let l:cflags = []
if g:ale_c_gcc_parse_makefile if !empty(a:output)
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ') let l:cflags = join(ale#c#ParseCFlags(a:buffer, join(a:output, '\n')), ' ')
endif endif
if empty(l:cflags) if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer)) let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
@ -33,6 +32,9 @@ call ale#linter#Define('c', {
\ 'name': 'gcc', \ 'name': 'gcc',
\ 'output_stream': 'stderr', \ 'output_stream': 'stderr',
\ 'executable_callback': 'ale_linters#c#gcc#GetExecutable', \ 'executable_callback': 'ale_linters#c#gcc#GetExecutable',
\ 'command_callback': 'ale_linters#c#gcc#GetCommand', \ 'command_chain': [
\ {'callback': 'ale#c#ParseMakefile', 'output_stream': 'stdout'},
\ {'callback': 'ale_linters#c#gcc#GetCommand'}
\ ],
\ 'callback': 'ale#handlers#gcc#HandleGCCFormat', \ 'callback': 'ale#handlers#gcc#HandleGCCFormat',
\}) \})

View file

@ -1,6 +1,7 @@
" Author: gagbo <gagbobada@gmail.com>, w0rp <devw0rp@gmail.com> " Author: gagbo <gagbobada@gmail.com>, w0rp <devw0rp@gmail.com>
" Description: Functions for integrating with C-family linters. " Description: Functions for integrating with C-family linters.
call ale#Set('c_parse_makefile', 0)
let s:sep = has('win32') ? '\' : '/' let s:sep = has('win32') ? '\' : '/'
function! ale#c#FindProjectRoot(buffer) abort function! ale#c#FindProjectRoot(buffer) abort
@ -22,27 +23,30 @@ function! ale#c#FindProjectRoot(buffer) abort
return '' return ''
endfunction endfunction
function! ale#c#ParseCFlags(project_root, stdout_make) abort function! ale#c#ParseCFlagsToList(buffer, cflags) abort
let l:cflags_list = [] let l:project_root = ale#c#FindProjectRoot(a:buffer)
let l:cflags = split(a:stdout_make) let l:previous_option = ''
let l:shell_option = 0 let l:shell_option = 0
let l:macro_option = 0 let l:macro_option = 0
let l:previous_option = '' let l:cflags_list = []
for l:option in l:cflags
for l:option in a:cflags
" Check if cflag contained spaces " Check if cflag contained spaces
if l:shell_option || stridx(l:option, '=`') >= 0 if l:shell_option || stridx(l:option, '=`') >= 0
" Cflag contained shell command with spaces (ex. -D='date +%s') " Cflag contained shell command with spaces (ex. -D='date +%s')
let l:shell_option = 1 let l:shell_option = 1
let l:previous_option .= l:option . ' ' let l:previous_option .= l:option
if l:option[-1: -1] isnot? '`' if l:option[-1: -1] isnot? '`'
let l:previous_option .= ' '
continue continue
endif endif
let l:shell_option = 0 let l:shell_option = 0
elseif l:macro_option || stridx(l:option, '$((') > 0 elseif l:macro_option || stridx(l:option, '$((') > 0
" Cflag contained macro with spaces (ex -Da=$(( 4 * 20 ))) " Cflag contained macro with spaces (ex -Da=$(( 4 * 20 )))
let l:macro_option = 1 let l:macro_option = 1
let l:previous_option .= l:option . ' ' let l:previous_option .= l:option
if stridx(l:option, '))') < 0 if stridx(l:option, '))') < 0
let l:previous_option .= ' '
continue continue
endif endif
let l:macro_option = 0 let l:macro_option = 0
@ -54,7 +58,7 @@ function! ale#c#ParseCFlags(project_root, stdout_make) abort
" Fix relative paths if needed " Fix relative paths if needed
if stridx(l:option, '-I') >= 0 if stridx(l:option, '-I') >= 0
if stridx(l:option, '-I' . s:sep) < 0 if stridx(l:option, '-I' . s:sep) < 0
let l:option = '-I' . a:project_root . s:sep . l:option[2:] let l:option = '-I' . l:project_root . s:sep . l:option[2:]
endif endif
endif endif
" Parse the cflag " Parse the cflag
@ -68,21 +72,33 @@ function! ale#c#ParseCFlags(project_root, stdout_make) abort
return l:cflags_list return l:cflags_list
endfunction endfunction
function! ale#c#ParseMakefile(buffer) abort function! ale#c#ParseCFlags(buffer, stdout_make) abort
let l:project_root = ale#c#FindProjectRoot(a:buffer) if g:ale_c_parse_makefile
let l:project_cflags = [] for l:cflags in split(a:stdout_make, '\n')
if stridx(l:cflags, expand('#' . a:buffer . '...'))
let l:cflags = split(l:cflags)
break
endif
endfor
if !empty(l:cflags)
return ale#c#ParseCFlagsToList(a:buffer, l:cflags)
endif
endif
retur []
endfunction
if !empty(l:project_root) function! ale#c#ParseMakefile(buffer) abort
if !empty(globpath(l:project_root, 'Makefile', 0)) if g:ale_c_parse_makefile
let l:stdout_make = system('cd '. l:project_root . ' && make -n') let l:project_root = ale#c#FindProjectRoot(a:buffer)
for l:object in split(l:stdout_make, '\n') let l:project_cflags = []
if stridx(l:object, expand('#' . a:buffer . '...'))
return ale#c#ParseCFlags(l:project_root, l:object) if !empty(l:project_root)
endif if !empty(globpath(l:project_root, 'Makefile', 0))
endfor return 'cd '. l:project_root . ' && make -n'
endif
endif endif
endif endif
return [] return ''
endfunction endfunction
" Given a buffer number, search for a project root, and output a List " Given a buffer number, search for a project root, and output a List

View file

@ -30,10 +30,10 @@ Execute(The executable should be configurable):
Execute(The executable should be used in the command): Execute(The executable should be used in the command):
AssertEqual AssertEqual
\ ale#Escape('clang') . b:command_tail, \ ale#Escape('clang') . b:command_tail,
\ ale_linters#c#clang#GetCommand(bufnr('')) \ ale_linters#c#clang#GetCommand(bufnr(''), [])
let b:ale_c_clang_executable = 'foobar' let b:ale_c_clang_executable = 'foobar'
AssertEqual AssertEqual
\ ale#Escape('foobar') . b:command_tail, \ ale#Escape('foobar') . b:command_tail,
\ ale_linters#c#clang#GetCommand(bufnr('')) \ ale_linters#c#clang#GetCommand(bufnr(''), [])

View file

@ -30,10 +30,10 @@ Execute(The executable should be configurable):
Execute(The executable should be used in the command): Execute(The executable should be used in the command):
AssertEqual AssertEqual
\ ale#Escape('gcc') . b:command_tail, \ ale#Escape('gcc') . b:command_tail,
\ ale_linters#c#gcc#GetCommand(bufnr('')) \ ale_linters#c#gcc#GetCommand(bufnr(''), [])
let b:ale_c_gcc_executable = 'foobar' let b:ale_c_gcc_executable = 'foobar'
AssertEqual AssertEqual
\ ale#Escape('foobar') . b:command_tail, \ ale#Escape('foobar') . b:command_tail,
\ ale_linters#c#gcc#GetCommand(bufnr('')) \ ale_linters#c#gcc#GetCommand(bufnr(''), [])

View file

@ -42,7 +42,7 @@ Execute(The C GCC handler should include 'include' directories for projects with
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/include')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/include')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#gcc#GetCommand(bufnr('')) \ , ale_linters#c#gcc#GetCommand(bufnr(''), [])
Execute(The C GCC handler should include 'include' directories for projects with a configure file): Execute(The C GCC handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -55,7 +55,7 @@ Execute(The C GCC handler should include 'include' directories for projects with
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/configure_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/configure_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/configure_project/include')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/configure_project/include')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#gcc#GetCommand(bufnr('')) \ , ale_linters#c#gcc#GetCommand(bufnr(''), [])
Execute(The C GCC handler should include root directories for projects with .h files in them): Execute(The C GCC handler should include root directories for projects with .h files in them):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -68,7 +68,7 @@ Execute(The C GCC handler should include root directories for projects with .h f
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#gcc#GetCommand(bufnr('')) \ , ale_linters#c#gcc#GetCommand(bufnr(''), [])
Execute(The C GCC handler should include root directories for projects with .hpp files in them): Execute(The C GCC handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -81,7 +81,7 @@ Execute(The C GCC handler should include root directories for projects with .hpp
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#gcc#GetCommand(bufnr('')) \ , ale_linters#c#gcc#GetCommand(bufnr(''), [])
Execute(The C Clang handler should include 'include' directories for projects with a Makefile): Execute(The C Clang handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
@ -94,7 +94,7 @@ Execute(The C Clang handler should include 'include' directories for projects wi
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/include')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/include')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#clang#GetCommand(bufnr('')) \ , ale_linters#c#clang#GetCommand(bufnr(''), [])
Execute(The C Clang handler should include 'include' directories for projects with a configure file): Execute(The C Clang handler should include 'include' directories for projects with a configure file):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
@ -107,7 +107,7 @@ Execute(The C Clang handler should include 'include' directories for projects wi
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#clang#GetCommand(bufnr('')) \ , ale_linters#c#clang#GetCommand(bufnr(''), [])
Execute(The C Clang handler should include root directories for projects with .h files in them): Execute(The C Clang handler should include root directories for projects with .h files in them):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
@ -120,7 +120,7 @@ Execute(The C Clang handler should include root directories for projects with .h
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/h_file_project')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#clang#GetCommand(bufnr('')) \ , ale_linters#c#clang#GetCommand(bufnr(''), [])
Execute(The C Clang handler should include root directories for projects with .hpp files in them): Execute(The C Clang handler should include root directories for projects with .hpp files in them):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/clang.vim
@ -133,7 +133,7 @@ Execute(The C Clang handler should include root directories for projects with .h
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project/subdir')) . ' ' \ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project/subdir')) . ' '
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project')) . ' ' \ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/hpp_file_project')) . ' '
\ . ' -' \ . ' -'
\ , ale_linters#c#clang#GetCommand(bufnr('')) \ , ale_linters#c#clang#GetCommand(bufnr(''), [])
Execute(The C++ GCC handler should include 'include' directories for projects with a Makefile): Execute(The C++ GCC handler should include 'include' directories for projects with a Makefile):
runtime! ale_linters/cpp/gcc.vim runtime! ale_linters/cpp/gcc.vim

View file

@ -1,15 +1,13 @@
Before: Before:
Save g:ale_c_parse_makefile
Save g:ale_c_gcc_options Save g:ale_c_gcc_options
Save g:ale_c_gcc_parse_makefile
Save g:ale_c_clang_options Save g:ale_c_clang_options
Save g:ale_c_clang_parse_makefile
Save g:ale_cpp_gcc_options Save g:ale_cpp_gcc_options
Save g:ale_cpp_clang_options Save g:ale_cpp_clang_options
call ale#test#SetDirectory('/testplugin/test') call ale#test#SetDirectory('/testplugin/test')
let g:ale_c_gcc_parse_makefile=1 let g:ale_c_parse_makefile=1
let g:ale_c_clang_parse_makefile=1
let g:ale_c_gcc_options = '' let g:ale_c_gcc_options = ''
let g:ale_c_clang_options = '' let g:ale_c_clang_options = ''
let g:ale_cpp_gcc_options = '' let g:ale_cpp_gcc_options = ''
@ -35,32 +33,44 @@ Execute(Move .git/HEAD to a temp dir):
call delete(g:head_filename) call delete(g:head_filename)
endif endif
Execute(The C GCC handler should include directories specified in the include path for projects with a Makefile): Execute(The CFlags parser should be able to parse include directives):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c') call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual AssertEqual
\ ale#Escape('gcc') \ ['-I/testplugin/test/test_c_projects/makefile_project/subdir']
\ . ' -S -x c -fsyntax-only ' \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -c file.c')
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir'))
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir/'))[1:-3] . ' '
\ . ' -'
\ , ale_linters#c#gcc#GetCommand(bufnr(''))
Execute(The C++ Clang handler should include directories specified in the include path for projects with a Makefile): Execute(The CFlags parser should be able to parse macro directives):
runtime! ale_linters/c/clang.vim runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c') call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual AssertEqual
\ ale#Escape('clang') \ ['-I/testplugin/test/test_c_projects/makefile_project/subdir',
\ . ' -S -x c -fsyntax-only ' \ '-DTEST=1']
\ . '-iquote ' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir')) \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=1 -c file.c')
\ . ' -I' . ale#Escape(ale#path#Simplify(g:dir . '/test_c_projects/makefile_project/subdir'))[1:-2] . ' '
\ . ' -'
\ , ale_linters#c#clang#GetCommand(bufnr(''))
Execute(The CFlags parser should be able to parse macro directives with spaces):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-I/testplugin/test/test_c_projects/makefile_project/subdir',
\ '-DTEST=$(( 2 * 4 ))']
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=$(( 2 * 4 )) -c file.c')
Execute(The CFlags parser should be able to parse shell directives with spaces):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-I/testplugin/test/test_c_projects/makefile_project/subdir',
\ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=`date +%s` -c file.c')
Execute(Move .git/HEAD back): Execute(Move .git/HEAD back):
if !empty(g:head_filename) if !empty(g:head_filename)