Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented dynamic compilation options. #406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions plugin/clang_complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,12 @@ for s:flag in values(s:flagInfo)
endfor
let s:flagPattern = '\%(' . join(s:flagPatterns, '\|') . '\)'


function! s:processFilename(filename, root)
" Handle Unix absolute path
if matchstr(a:filename, '\C^[''"\\]\=/') != ''
let l:filename = a:filename
" Handle Windows absolute path
elseif s:isWindows()
elseif s:isWindows()
\ && matchstr(a:filename, '\C^"\=[a-zA-Z]:[/\\]') != ''
let l:filename = a:filename
" Convert relative path to absolute path
Expand All @@ -267,7 +266,7 @@ function! s:processFilename(filename, root)
let l:filename = shellescape(a:root) . a:filename
endif
endif

return l:filename
endfunction

Expand All @@ -286,21 +285,59 @@ function! s:parseConfig()

let l:opts = readfile(l:local_conf)
for l:opt in l:opts
" Ensure passed filenames are absolute. Only performed on flags which
" require a filename/directory as an argument, as specified in s:flagInfo
if matchstr(l:opt, '\C^\s*' . s:flagPattern . '\s*') != ''
let l:flag = substitute(l:opt, '\C^\s*\(' . s:flagPattern . '\).*'
\ , '\1', 'g')
let l:flag = substitute(l:flag, '^\(.\{-}\)\s*$', '\1', 'g')
let l:filename = substitute(l:opt,
\ '\C^\s*' . s:flagPattern . '\(.\{-}\)\s*$',
\ '\1', 'g')
let l:filename = s:processFilename(l:filename, l:root)
let l:opt = s:flagInfo[l:flag].output . l:filename
let b:clang_user_options .= ' ' . s:parseOption(l:opt, l:root)
endfor
endfunction

function! s:parseOption(opt, root)
let l:opt = a:opt

" If a line is wrapped with backticks (`), take the content between the
" backticks and execute it as a shell command. Take the output to stdout
" and process the output as if they were ordinary lines in the
" .clang_complete file. If the substring '<<FILE>>'' is encountered, it will
" be replaced with the absolute path of the file being edited.
if matchstr(l:opt, '^\s*`.\{-}`\s*$') != ''
let l:filename = expand('%:p')
" Make path safe to be used as a shell argument. Either by surrounding with
" double quotes on Windows, or using shellescape() on Unix-like systems.
if s:isWindows()
let l:filename = '"' . l:filename . '"'
else
let l:filename = shellescape(l:filename)
endif

let b:clang_user_options .= ' ' . l:opt
endfor
let l:cmd = substitute(l:opt, '^\s*`\(.\{-}\)`\s*$', '\1', '')
let l:cmd = substitute(l:cmd, '<<FILE>>', l:filename, 'g')

let l:dynamic_options = ''
let l:opts = system(l:cmd)

if v:shell_error != 0
echoe '`' . l:cmd . '` failed to run successfully.'
else
for l:opt in split(l:opts, '\n')
let l:dynamic_options .= ' ' . s:parseOption(l:opt, a:root)
endfor
endif

return l:dynamic_options
endif

" Ensure passed filenames are absolute. Only performed on flags which
" require a filename/directory as an argument, as specified in s:flagInfo
if matchstr(l:opt, '\C^\s*' . s:flagPattern) != ''
let l:flag = substitute(l:opt, '\C^\s*\(' . s:flagPattern . '\).*'
\ , '\1', 'g')
let l:flag = substitute(l:flag, '^\(.\{-}\)\s*$', '\1', 'g')
let l:filename = substitute(l:opt,
\ '\C^\s*' . s:flagPattern . '\(.\{-}\)\s*$',
\ '\1', 'g')
let l:filename = s:processFilename(l:filename, a:root)
let l:opt = s:flagInfo[l:flag].output . l:filename
endif

return l:opt
endfunction

function! s:findCompilationDatase(cdb)
Expand Down