* 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="\
bash \
git \
make \
python \
py-pip \
"

View file

@ -3,16 +3,15 @@
call ale#Set('c_clang_executable', 'clang')
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
return ale#Var(a:buffer, 'c_clang_executable')
endfunction
function! ale_linters#c#clang#GetCommand(buffer) abort
let l:cflags = []
if g:ale_c_clang_parse_makefile
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ')
function! ale_linters#c#clang#GetCommand(buffer, output) abort
let l:cflags = []
if !empty(a:output)
let l:cflags = join(ale#c#ParseMakefile(a:buffer, join(a:output, '\n')), ' ')
endif
if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
@ -33,6 +32,9 @@ call ale#linter#Define('c', {
\ 'name': 'clang',
\ 'output_stream': 'stderr',
\ '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',
\})

View file

@ -3,16 +3,15 @@
call ale#Set('c_gcc_executable', 'gcc')
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
return ale#Var(a:buffer, 'c_gcc_executable')
endfunction
function! ale_linters#c#gcc#GetCommand(buffer) abort
function! ale_linters#c#gcc#GetCommand(buffer, output) abort
let l:cflags = []
if g:ale_c_gcc_parse_makefile
let l:cflags = join(ale#c#ParseMakefile(a:buffer), ' ')
if !empty(a:output)
let l:cflags = join(ale#c#ParseCFlags(a:buffer, join(a:output, '\n')), ' ')
endif
if empty(l:cflags)
let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
@ -33,6 +32,9 @@ call ale#linter#Define('c', {
\ 'name': 'gcc',
\ 'output_stream': 'stderr',
\ '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',
\})

View file

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

View file

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