#711 - Make the clangtidy executable configurable

This commit is contained in:
w0rp 2017-07-17 00:07:18 +01:00
parent fe70742bb9
commit 3352a6c9df
4 changed files with 85 additions and 37 deletions

View File

@ -2,51 +2,43 @@
" gagbo <gagbobada@gmail.com>
" Description: clang-tidy linter for cpp files
call ale#Set('cpp_clangtidy_executable', 'clang-tidy')
" Set this option to check the checks clang-tidy will apply.
let g:ale_cpp_clangtidy_checks = get(g:, 'ale_cpp_clangtidy_checks', ['*'])
call ale#Set('cpp_clangtidy_checks', ['*'])
" Set this option to manually set some options for clang-tidy.
" This will disable compile_commands.json detection.
let g:ale_cpp_clangtidy_options = get(g:, 'ale_cpp_clangtidy_options', '')
" Set this option to manually point to the build directory for clang-tidy.
" This will disable all the other clangtidy_options, since compilation
" flags are contained in the json
let g:ale_c_build_dir = get(g:, 'ale_c_build_dir', '')
call ale#Set('cpp_clangtidy_options', '')
call ale#Set('c_build_dir', '')
function! ale_linters#cpp#clangtidy#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'cpp_clangtidy_executable')
endfunction
function! ale_linters#cpp#clangtidy#GetCommand(buffer) abort
let l:check_list = ale#Var(a:buffer, 'cpp_clangtidy_checks')
let l:check_option = !empty(l:check_list)
\ ? '-checks=' . ale#Escape(join(l:check_list, ',')) . ' '
\ : ''
let l:user_options = ale#Var(a:buffer, 'cpp_clangtidy_options')
let l:user_build_dir = ale#Var(a:buffer, 'c_build_dir')
let l:checks = join(ale#Var(a:buffer, 'cpp_clangtidy_checks'), ',')
let l:build_dir = ale#Var(a:buffer, 'c_build_dir')
" c_build_dir has the priority if defined
if empty(l:user_build_dir)
let l:user_build_dir = ale#c#FindCompileCommands(a:buffer)
if empty(l:build_dir)
let l:build_dir = ale#c#FindCompileCommands(a:buffer)
endif
" We check again if user_builddir stayed empty after the
" c_build_dir_names check
" If we found the compilation database we override the value of
" l:extra_options
if empty(l:user_build_dir)
let l:extra_options = !empty(l:user_options)
\ ? ' -- ' . l:user_options
" Get the extra options if we couldn't find a build directory.
let l:options = empty(l:build_dir)
\ ? ale#Var(a:buffer, 'cpp_clangtidy_options')
\ : ''
else
let l:extra_options = ' -p ' . ale#Escape(l:user_build_dir)
endif
return 'clang-tidy ' . l:check_option . '%s' . l:extra_options
return ale#Escape(ale_linters#cpp#clangtidy#GetExecutable(a:buffer))
\ . (!empty(l:checks) ? ' -checks=' . ale#Escape(l:checks) : '')
\ . ' %s'
\ . (!empty(l:build_dir) ? ' -p ' . ale#Escape(l:build_dir) : '')
\ . (!empty(l:options) ? ' -- ' . l:options : '')
endfunction
call ale#linter#Define('cpp', {
\ 'name': 'clangtidy',
\ 'output_stream': 'stdout',
\ 'executable': 'clang-tidy',
\ 'executable_callback': 'ale_linters#cpp#clangtidy#GetExecutable',
\ 'command_callback': 'ale_linters#cpp#clangtidy#GetCommand',
\ 'callback': 'ale#handlers#gcc#HandleGCCFormat',
\ 'lint_file': 1,

View File

@ -105,6 +105,15 @@ g:ale_cpp_clangtidy_checks *g:ale_cpp_clangtidy_checks*
the shell. The `-checks` flag can be removed entirely by setting this
option to an empty List.
g:ale_cpp_clangtidy_executable *g:ale_cpp_clangtidy_executable*
*b:ale_cpp_clangtidy_executable*
Type: |String|
Default: `'clang-tidy'`
This variable can be changed to use a different executable for clangtidy.
g:ale_cpp_clangtidy_options *g:ale_cpp_clangtidy_options*
*b:ale_cpp_clangtidy_options*
Type: |String|

View File

@ -1,31 +1,77 @@
Before:
Save g:ale_cpp_clangtidy_checks
Save g:ale_cpp_clangtidy_options
Save g:ale_c_build_dir
unlet! g:ale_c_build_dir
unlet! b:ale_c_build_dir
unlet! g:ale_cpp_clangtidy_checks
unlet! b:ale_cpp_clangtidy_checks
unlet! g:ale_cpp_clangtidy_options
unlet! b:ale_cpp_clangtidy_options
runtime ale_linters/cpp/clangtidy.vim
After:
unlet! b:ale_c_build_dir
unlet! b:ale_cpp_clangtidy_checks
unlet! b:ale_cpp_clangtidy_options
unlet! b:ale_cpp_clangtidy_executable
Restore
call ale#linter#Reset()
Execute(The clangtidy command default should be correct):
AssertEqual
\ 'clang-tidy -checks=''*'' %s',
\ ale#Escape('clang-tidy')
\ . ' -checks=''*'' %s',
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(You should be able to remove the -checks option for clang-tidy):
let g:ale_cpp_clangtidy_checks = []
let b:ale_cpp_clangtidy_checks = []
AssertEqual
\ 'clang-tidy %s',
\ ale#Escape('clang-tidy')
\ . ' %s',
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(You should be able to set other checks for clang-tidy):
let g:ale_cpp_clangtidy_checks = ['-*', 'clang-analyzer-*']
let b:ale_cpp_clangtidy_checks = ['-*', 'clang-analyzer-*']
AssertEqual
\ 'clang-tidy -checks=''-*,clang-analyzer-*'' %s',
\ ale#Escape('clang-tidy')
\ . ' -checks=''-*,clang-analyzer-*'' %s',
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(You should be able to manually set compiler flags for clang-tidy):
let g:ale_cpp_clangtidy_options = '-Wall'
let b:ale_cpp_clangtidy_options = '-Wall'
AssertEqual
\ 'clang-tidy -checks=''*'' %s -- -Wall',
\ ale#Escape('clang-tidy')
\ . ' -checks=''*'' %s -- -Wall',
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
\
Execute(The build directory should be configurable):
let b:ale_c_build_dir = '/foo/bar'
AssertEqual
\ ale#Escape('clang-tidy')
\ . ' -checks=''*'' %s -p ' . ale#Escape('/foo/bar'),
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(The build directory setting should override the options):
let b:ale_c_build_dir = '/foo/bar'
let b:ale_cpp_clangtidy_options = '-Wall'
AssertEqual
\ ale#Escape('clang-tidy')
\ . ' -checks=''*'' %s -p ' . ale#Escape('/foo/bar'),
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))
Execute(The executable should be configurable):
let b:ale_cpp_clangtidy_executable = 'foobar'
AssertEqual
\ ale#Escape('foobar')
\ . ' -checks=''*'' %s',
\ ale_linters#cpp#clangtidy#GetCommand(bufnr(''))

View File

@ -266,7 +266,8 @@ Execute(The C++ ClangTidy handler should include json folders for projects with
call ale#test#SetFilename('test_c_projects/json_project/subdir/file.cpp')
AssertEqual
\ 'clang-tidy -checks=''*'' %s '
\ ale#Escape('clang-tidy')
\ . ' -checks=''*'' %s '
\ . '-p ' . ale#Escape(g:dir . '/test_c_projects/json_project/build')
\ , ale_linters#cpp#clangtidy#GetCommand(bufnr(''))