diff options
author | w0rp <w0rp@users.noreply.github.com> | 2018-03-27 09:55:43 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-27 09:55:43 +0100 |
commit | 018831d601a6fc53216ad448a91bb76b0ac4d8e3 (patch) | |
tree | 5a34d094612bc4acbfc5500ef21282ac45a1adbd /autoload | |
parent | 27c5faeafe055954b6e3164467844e78f7a07e55 (diff) | |
parent | dfb3e194d7a05b747c77d312a72e5149595bbcef (diff) | |
download | ale-018831d601a6fc53216ad448a91bb76b0ac4d8e3.zip |
Merge pull request #1434 from roel0/master
Automatically determine build flags by parsing `make -n` output #1167
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/c.vim | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/autoload/ale/c.vim b/autoload/ale/c.vim index f6ad7deb..5ab10f00 100644 --- a/autoload/ale/c.vim +++ b/autoload/ale/c.vim @@ -1,6 +1,7 @@ -" Author: gagbo <gagbobada@gmail.com>, w0rp <devw0rp@gmail.com> +" Author: gagbo <gagbobada@gmail.com>, w0rp <devw0rp@gmail.com>, roel0 <postelmansroel@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,6 +23,88 @@ function! ale#c#FindProjectRoot(buffer) abort return '' endfunction +function! ale#c#ParseCFlagsToList(path_prefix, cflags) abort + let l:cflags_list = [] + let l:previous_options = [] + + for l:option in a:cflags + call add(l:previous_options, l:option) + " Check if cflag contained a '-' and should not have been splitted + let l:option_list = split(l:option, '\zs') + if l:option_list[-1] isnot# ' ' + continue + endif + + let l:option = join(l:previous_options, '-') + let l:previous_options = [] + + let l:option = '-' . substitute(l:option, '^\s*\(.\{-}\)\s*$', '\1', '') + + " Fix relative paths if needed + if stridx(l:option, '-I') >= 0 && + \ stridx(l:option, '-I' . s:sep) < 0 + let l:rel_path = join(split(l:option, '\zs')[2:], '') + 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 + + " Parse the cflag + if stridx(l:option, '-I') >= 0 || + \ stridx(l:option, '-D') >= 0 + if index(l:cflags_list, l:option) < 0 + call add(l:cflags_list, l:option) + endif + endif + endfor + + return l:cflags_list +endfunction + +function! ale#c#ParseCFlags(buffer, stdout_make) abort + if !g:ale_c_parse_makefile + return [] + endif + + let l:buffer_filename = expand('#' . a:buffer . ':t') + let l:cflags = [] + for l:lines in split(a:stdout_make, '\\n') + if stridx(l:lines, l:buffer_filename) >= 0 + let l:cflags = split(l:lines, '-') + break + endif + endfor + + let l:makefile_path = ale#path#FindNearestFile(a:buffer, 'Makefile') + return ale#c#ParseCFlagsToList(fnamemodify(l:makefile_path, ':p:h'), l:cflags) +endfunction + +function! ale#c#GetCFlags(buffer, output) abort + let l:cflags = ' ' + + if g:ale_c_parse_makefile && !empty(a:output) + let l:cflags = join(ale#c#ParseCFlags(a:buffer, join(a:output, '\n')), ' ') . ' ' + endif + + if l:cflags is# ' ' + let l:cflags = ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer)) + endif + + return l:cflags +endfunction + +function! ale#c#GetMakeCommand(buffer) abort + if g:ale_c_parse_makefile + let l:makefile_path = ale#path#FindNearestFile(a:buffer, 'Makefile') + if !empty(l:makefile_path) + return 'cd '. fnamemodify(l:makefile_path, ':p:h') . ' && make -n' + endif + endif + + return '' +endfunction + " Given a buffer number, search for a project root, and output a List " of directories to include based on some heuristics. " |