mirror of
https://github.com/dense-analysis/ale.git
synced 2026-02-12 13:11:49 +08:00
#3318 Refactor C flag parsing to set up for quoting arguments
This commit is contained in:
@@ -8,6 +8,11 @@ let s:sep = has('win32') ? '\' : '/'
|
||||
" Set just so tests can override it.
|
||||
let g:__ale_c_project_filenames = ['.git/HEAD', 'configure', 'Makefile', 'CMakeLists.txt']
|
||||
|
||||
let g:ale_c_build_dir_names = get(g:, 'ale_c_build_dir_names', [
|
||||
\ 'build',
|
||||
\ 'bin',
|
||||
\])
|
||||
|
||||
function! s:CanParseMakefile(buffer) abort
|
||||
" Something somewhere seems to delete this setting in tests, so ensure we
|
||||
" always have a default value.
|
||||
@@ -115,16 +120,14 @@ function! ale#c#ExpandAtArgs(path_prefix, raw_split_lines) abort
|
||||
return l:out_lines
|
||||
endfunction
|
||||
|
||||
function! ale#c#ParseCFlags(path_prefix, cflag_line) abort
|
||||
let l:cflags_list = []
|
||||
|
||||
let l:raw_split_lines = ale#c#ShellSplit(a:cflag_line)
|
||||
function! ale#c#ParseCFlags(path_prefix, should_quote, raw_arguments) abort
|
||||
" Expand @file arguments now before parsing
|
||||
let l:split_lines = ale#c#ExpandAtArgs(a:path_prefix, l:raw_split_lines)
|
||||
let l:arguments = ale#c#ExpandAtArgs(a:path_prefix, a:raw_arguments)
|
||||
let l:arguments_to_use = []
|
||||
let l:option_index = 0
|
||||
|
||||
while l:option_index < len(l:split_lines)
|
||||
let l:option = l:split_lines[l:option_index]
|
||||
while l:option_index < len(l:arguments)
|
||||
let l:option = l:arguments[l:option_index]
|
||||
let l:option_index = l:option_index + 1
|
||||
|
||||
" Include options, that may need relative path fix
|
||||
@@ -138,7 +141,7 @@ function! ale#c#ParseCFlags(path_prefix, cflag_line) abort
|
||||
let l:arg = join(split(l:option, '\zs')[2:], '')
|
||||
let l:option = '-I'
|
||||
else
|
||||
let l:arg = l:split_lines[l:option_index]
|
||||
let l:arg = l:arguments[l:option_index]
|
||||
let l:option_index = l:option_index + 1
|
||||
endif
|
||||
|
||||
@@ -151,21 +154,21 @@ function! ale#c#ParseCFlags(path_prefix, cflag_line) abort
|
||||
\)
|
||||
endif
|
||||
|
||||
call add(l:cflags_list, l:option)
|
||||
call add(l:cflags_list, l:arg)
|
||||
call add(l:arguments_to_use, l:option)
|
||||
call add(l:arguments_to_use, l:arg)
|
||||
" Options with arg that can be grouped with the option or separate
|
||||
elseif stridx(l:option, '-D') == 0 || stridx(l:option, '-B') == 0
|
||||
call add(l:cflags_list, l:option)
|
||||
call add(l:arguments_to_use, l:option)
|
||||
|
||||
if l:option is# '-D' || l:option is# '-B'
|
||||
call add(l:cflags_list, l:split_lines[l:option_index])
|
||||
call add(l:arguments_to_use, l:arguments[l:option_index])
|
||||
let l:option_index = l:option_index + 1
|
||||
endif
|
||||
" Options that have an argument (always separate)
|
||||
elseif l:option is# '-iprefix' || stridx(l:option, '-iwithprefix') == 0
|
||||
\ || l:option is# '-isysroot' || l:option is# '-imultilib'
|
||||
call add(l:cflags_list, l:option)
|
||||
call add(l:cflags_list, l:split_lines[l:option_index])
|
||||
call add(l:arguments_to_use, l:option)
|
||||
call add(l:arguments_to_use, l:arguments[l:option_index])
|
||||
let l:option_index = l:option_index + 1
|
||||
" Options without argument
|
||||
elseif (stridx(l:option, '-W') == 0 && stridx(l:option, '-Wa,') != 0 && stridx(l:option, '-Wl,') != 0 && stridx(l:option, '-Wp,') != 0)
|
||||
@@ -177,11 +180,11 @@ function! ale#c#ParseCFlags(path_prefix, cflag_line) abort
|
||||
\ || stridx(l:option, '-nostdinc') == 0 || stridx(l:option, '-iplugindir=') == 0
|
||||
\ || stridx(l:option, '--sysroot=') == 0 || l:option is# '--no-sysroot-suffix'
|
||||
\ || stridx(l:option, '-m') == 0
|
||||
call add(l:cflags_list, l:option)
|
||||
call add(l:arguments_to_use, l:option)
|
||||
endif
|
||||
endwhile
|
||||
|
||||
return join(l:cflags_list, ' ')
|
||||
return join(l:arguments_to_use, ' ')
|
||||
endfunction
|
||||
|
||||
function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort
|
||||
@@ -203,7 +206,7 @@ function! ale#c#ParseCFlagsFromMakeOutput(buffer, make_output) abort
|
||||
let l:makefile_path = ale#path#FindNearestFile(a:buffer, 'Makefile')
|
||||
let l:makefile_dir = fnamemodify(l:makefile_path, ':p:h')
|
||||
|
||||
return ale#c#ParseCFlags(l:makefile_dir, l:cflag_line)
|
||||
return ale#c#ParseCFlags(l:makefile_dir, 0, ale#c#ShellSplit(l:cflag_line))
|
||||
endfunction
|
||||
|
||||
" Given a buffer number, find the project directory containing
|
||||
@@ -333,14 +336,16 @@ function! s:GetLookupFromCompileCommandsFile(compile_commands_file) abort
|
||||
return l:empty
|
||||
endfunction
|
||||
|
||||
function! ale#c#GetCompileCommand(json_item) abort
|
||||
if has_key(a:json_item, 'command')
|
||||
return a:json_item.command
|
||||
elseif has_key(a:json_item, 'arguments')
|
||||
return join(a:json_item.arguments, ' ')
|
||||
" Get [should_quote, arguments] from either 'command' or 'arguments'
|
||||
" 'arguments' should be quoted later, the split 'command' strings should not.
|
||||
function! s:GetArguments(json_item) abort
|
||||
if has_key(a:json_item, 'arguments')
|
||||
return [1, a:json_item.arguments]
|
||||
elseif has_key(a:json_item, 'command')
|
||||
return [0, ale#c#ShellSplit(a:json_item.command)]
|
||||
endif
|
||||
|
||||
return ''
|
||||
return [0, []]
|
||||
endfunction
|
||||
|
||||
function! ale#c#ParseCompileCommandsFlags(buffer, file_lookup, dir_lookup) abort
|
||||
@@ -398,7 +403,9 @@ function! ale#c#ParseCompileCommandsFlags(buffer, file_lookup, dir_lookup) abort
|
||||
\ && l:filename[-len(l:source_file):] is? l:source_file
|
||||
\ )
|
||||
\)
|
||||
return ale#c#ParseCFlags(l:item.directory, ale#c#GetCompileCommand(l:item))
|
||||
let [l:should_quote, l:args] = s:GetArguments(l:item)
|
||||
|
||||
return ale#c#ParseCFlags(l:item.directory, l:should_quote, l:args)
|
||||
endif
|
||||
endfor
|
||||
|
||||
@@ -406,7 +413,9 @@ function! ale#c#ParseCompileCommandsFlags(buffer, file_lookup, dir_lookup) abort
|
||||
let l:filename = ale#path#GetAbsPath(l:item.directory, l:item.file)
|
||||
|
||||
if ale#path#Simplify(fnamemodify(l:filename, ':h')) is? l:dir
|
||||
return ale#c#ParseCFlags(l:item.directory, ale#c#GetCompileCommand(l:item))
|
||||
let [l:should_quote, l:args] = s:GetArguments(l:item)
|
||||
|
||||
return ale#c#ParseCFlags(l:item.directory, l:should_quote, l:args)
|
||||
endif
|
||||
endfor
|
||||
|
||||
@@ -517,8 +526,3 @@ function! ale#c#IncludeOptions(include_paths) abort
|
||||
|
||||
return join(l:option_list)
|
||||
endfunction
|
||||
|
||||
let g:ale_c_build_dir_names = get(g:, 'ale_c_build_dir_names', [
|
||||
\ 'build',
|
||||
\ 'bin',
|
||||
\])
|
||||
|
||||
Reference in New Issue
Block a user