Extended unit tests + simplified parsing algoritme #1167

This commit is contained in:
roel0 2018-03-27 10:18:24 +02:00
parent cf62ef7b07
commit dfb3e194d7
2 changed files with 107 additions and 51 deletions

View File

@ -24,44 +24,30 @@ function! ale#c#FindProjectRoot(buffer) abort
endfunction endfunction
function! ale#c#ParseCFlagsToList(path_prefix, cflags) abort function! ale#c#ParseCFlagsToList(path_prefix, cflags) abort
let l:previous_option = ''
let l:shell_option = 0
let l:macro_option = 0
let l:cflags_list = [] let l:cflags_list = []
let l:previous_options = []
for l:option in a:cflags for l:option in a:cflags
call add(l:previous_options, l:option)
" Check if cflag contained spaces " Check if cflag contained a '-' and should not have been splitted
if l:shell_option || stridx(l:option, '=`') >= 0 let l:option_list = split(l:option, '\zs')
" Cflag contained shell command with spaces (ex. -D='date +%s') if l:option_list[-1] isnot# ' '
let l:shell_option = 1
let l:previous_option .= l:option
if l:option[-1: -1] isnot? '`'
let l:previous_option .= ' '
continue continue
endif 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
if stridx(l:option, '))') < 0
let l:previous_option .= ' '
continue
endif
let l:macro_option = 0
endif
if l:previous_option isnot? '' let l:option = join(l:previous_options, '-')
let l:option = l:previous_option let l:previous_options = []
let l:previous_option = ''
endif let l:option = '-' . substitute(l:option, '^\s*\(.\{-}\)\s*$', '\1', '')
" 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 \ stridx(l:option, '-I' . s:sep) < 0
let l:option = '-I' . a:path_prefix . s:sep . l:option[2:] let l:rel_path = join(split(l:option, '\zs')[2:], '')
endif let l:rel_path = substitute(l:rel_path, '"', '', 'g')
let l:rel_path = substitute(l:rel_path, '''', '', 'g')
let l:option = ale#Escape('-I' . a:path_prefix .
\ s:sep . l:rel_path)
endif endif
" Parse the cflag " Parse the cflag
@ -81,11 +67,11 @@ function! ale#c#ParseCFlags(buffer, stdout_make) abort
return [] return []
endif endif
let l:buffer_filename = expand('#' . a:buffer . '...') let l:buffer_filename = expand('#' . a:buffer . ':t')
let l:cflags = []
for l:cflags in split(a:stdout_make, '\n') for l:lines in split(a:stdout_make, '\\n')
if stridx(l:cflags, l:buffer_filename) if stridx(l:lines, l:buffer_filename) >= 0
let l:cflags = split(l:cflags) let l:cflags = split(l:lines, '-')
break break
endif endif
endfor endfor

View File

@ -25,7 +25,7 @@ Execute(The CFlags parser should be able to parse include directives):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')] \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'))]
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -c file.c') \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -c file.c')
Execute(The CFlags parser should be able to parse macro directives): Execute(The CFlags parser should be able to parse macro directives):
@ -34,7 +34,7 @@ Execute(The CFlags parser should be able to parse macro directives):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-DTEST=1'] \ '-DTEST=1']
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=1 -c file.c') \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=1 -c file.c')
@ -44,7 +44,7 @@ Execute(The CFlags parser should be able to parse macro directives with spaces):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-DTEST=$(( 2 * 4 ))'] \ '-DTEST=$(( 2 * 4 ))']
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=$(( 2 * 4 )) -c file.c') \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=$(( 2 * 4 )) -c file.c')
@ -54,7 +54,7 @@ Execute(The CFlags parser should be able to parse shell directives with spaces):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-DTEST=`date +%s`'] \ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=`date +%s` -c file.c') \ , ale#c#ParseCFlags(bufnr(''), 'gcc -Isubdir -DTEST=`date +%s` -c file.c')
@ -64,10 +64,10 @@ Execute(The CFlagsToList parser should be able to parse multiple cflags):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-DTEST=`date +%s`'] \ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'), \ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Isubdir -DTEST=`date +%s` -c file.c')) \ split('gcc -Isubdir -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #2): Execute(The CFlagsToList parser should be able to parse multiple cflags #2):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -75,13 +75,13 @@ Execute(The CFlagsToList parser should be able to parse multiple cflags #2):
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
\ ['-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ [ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include'), \ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`'] \ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'), \ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Isubdir ' . \ split('gcc -Isubdir ' .
\ '-I'. ale#path#Simplify('kernel/include') . \ '-I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c')) \ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #3): Execute(The CFlagsToList parser should be able to parse multiple cflags #3):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -90,13 +90,13 @@ Execute(The CFlagsToList parser should be able to parse multiple cflags #3):
AssertEqual AssertEqual
\ ['-Dgoal=9', \ ['-Dgoal=9',
\ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include'), \ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`'] \ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'), \ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Isubdir ' . \ split('gcc -Dgoal=9 -Isubdir ' .
\ '-I'. ale#path#Simplify('kernel/include') . \ '-I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c')) \ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #4): Execute(The CFlagsToList parser should be able to parse multiple cflags #4):
runtime! ale_linters/c/gcc.vim runtime! ale_linters/c/gcc.vim
@ -105,10 +105,80 @@ Execute(The CFlagsToList parser should be able to parse multiple cflags #4):
AssertEqual AssertEqual
\ ['-Dgoal=9', \ ['-Dgoal=9',
\ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir'), \ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include'), \ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`'] \ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'), \ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' . \ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' .
\ '-I'. ale#path#Simplify('kernel/include') . \ '-I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c')) \ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #5):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-Dgoal=9',
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir with spaces')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' .
\ '-I"dir with spaces"' . ' -I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #6):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-Dgoal=9',
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir with spaces')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' .
\ '-I''dir with spaces''' . ' -I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #7):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-Dgoal=9',
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir with spaces')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir-with-dash')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' .
\ '-I''dir with spaces''' . ' -Idir-with-dash' .
\ ' -I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c', '-'))
Execute(The CFlagsToList parser should be able to parse multiple cflags #8):
runtime! ale_linters/c/gcc.vim
call ale#test#SetFilename('test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ ['-Dgoal=9',
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/subdir')),
\ '-Dmacro-with-dash',
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir with spaces')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/dir-with-dash')),
\ ale#Escape('-I' . ale#path#Simplify(g:dir. '/test_c_projects/makefile_project/kernel/include')),
\ '-DTEST=`date +%s`']
\ , ale#c#ParseCFlagsToList(ale#path#Simplify(g:dir. '/test_c_projects/makefile_project'),
\ split('gcc -Dgoal=9 -Tlinkerfile.ld blabla -Isubdir ' .
\ '-Dmacro-with-dash ' .
\ '-I''dir with spaces''' . ' -Idir-with-dash' .
\ ' -I'. ale#path#Simplify('kernel/include') .
\ ' -DTEST=`date +%s` -c file.c', '-'))