From 3352a6c9df427204bcbffba78fdf65906d3966d0 Mon Sep 17 00:00:00 2001 From: w0rp Date: Mon, 17 Jul 2017 00:07:18 +0100 Subject: [PATCH] #711 - Make the clangtidy executable configurable --- ale_linters/cpp/clangtidy.vim | 50 +++++++--------- doc/ale-cpp.txt | 9 +++ .../test_clang_tidy_command_callback.vader | 60 ++++++++++++++++--- test/test_c_import_paths.vader | 3 +- 4 files changed, 85 insertions(+), 37 deletions(-) diff --git a/ale_linters/cpp/clangtidy.vim b/ale_linters/cpp/clangtidy.vim index 94f5767..5ff345f 100644 --- a/ale_linters/cpp/clangtidy.vim +++ b/ale_linters/cpp/clangtidy.vim @@ -2,51 +2,43 @@ " gagbo " 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 - \ : '' - else - let l:extra_options = ' -p ' . ale#Escape(l:user_build_dir) - endif + " 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') + \ : '' - 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, diff --git a/doc/ale-cpp.txt b/doc/ale-cpp.txt index 7483339..ed852b4 100644 --- a/doc/ale-cpp.txt +++ b/doc/ale-cpp.txt @@ -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| diff --git a/test/command_callback/test_clang_tidy_command_callback.vader b/test/command_callback/test_clang_tidy_command_callback.vader index 46d8a3a..dc66859 100644 --- a/test/command_callback/test_clang_tidy_command_callback.vader +++ b/test/command_callback/test_clang_tidy_command_callback.vader @@ -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('')) diff --git a/test/test_c_import_paths.vader b/test/test_c_import_paths.vader index 862ec15..dac73f0 100644 --- a/test/test_c_import_paths.vader +++ b/test/test_c_import_paths.vader @@ -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(''))