summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-05-02 21:18:17 +0100
committerw0rp <devw0rp@gmail.com>2017-05-02 21:18:17 +0100
commiteb8bd26776fbdc57f0162d729260c4847645e145 (patch)
tree04fba5687126d64ff8cdc68f8a195ba7ee55f0b0
parent36314aeaf2c1c5e272d7f3e5d696b5fc348081fd (diff)
downloadale-eb8bd26776fbdc57f0162d729260c4847645e145.zip
#392 - Detect and use compile_commands.json for cppcheck
-rw-r--r--ale_linters/c/cppcheck.vim17
-rw-r--r--ale_linters/cpp/cppcheck.vim17
-rw-r--r--test/command_callback/cppcheck_paths/one/compile_commands.json0
-rw-r--r--test/command_callback/cppcheck_paths/one/two/three/file.c0
-rw-r--r--test/command_callback/cppcheck_paths/one/two/three/file.cpp0
-rw-r--r--test/command_callback/test_cppcheck_command_callbacks.vader40
6 files changed, 72 insertions, 2 deletions
diff --git a/ale_linters/c/cppcheck.vim b/ale_linters/c/cppcheck.vim
index 7da5b3ab..90805914 100644
--- a/ale_linters/c/cppcheck.vim
+++ b/ale_linters/c/cppcheck.vim
@@ -5,7 +5,22 @@
let g:ale_c_cppcheck_options = get(g:, 'ale_c_cppcheck_options', '--enable=style')
function! ale_linters#c#cppcheck#GetCommand(buffer) abort
- return 'cppcheck -q --language=c '
+ " Search upwards from the file for compile_commands.json.
+ "
+ " If we find it, we'll `cd` to where the compile_commands.json file is,
+ " then use the file to set up import paths, etc.
+ let l:compile_commmands_path = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
+
+ let l:cd_command = !empty(l:compile_commmands_path)
+ \ ? ale#path#CdString(fnamemodify(l:compile_commmands_path, ':h'))
+ \ : ''
+ let l:compile_commands_option = !empty(l:compile_commmands_path)
+ \ ? '--project=compile_commands.json '
+ \ : ''
+
+ return l:cd_command
+ \ . 'cppcheck -q --language=c '
+ \ . l:compile_commands_option
\ . ale#Var(a:buffer, 'c_cppcheck_options')
\ . ' %t'
endfunction
diff --git a/ale_linters/cpp/cppcheck.vim b/ale_linters/cpp/cppcheck.vim
index 49cab0d9..2255f63f 100644
--- a/ale_linters/cpp/cppcheck.vim
+++ b/ale_linters/cpp/cppcheck.vim
@@ -5,7 +5,22 @@
let g:ale_cpp_cppcheck_options = get(g:, 'ale_cpp_cppcheck_options', '--enable=style')
function! ale_linters#cpp#cppcheck#GetCommand(buffer) abort
- return 'cppcheck -q --language=c++ '
+ " Search upwards from the file for compile_commands.json.
+ "
+ " If we find it, we'll `cd` to where the compile_commands.json file is,
+ " then use the file to set up import paths, etc.
+ let l:compile_commmands_path = ale#path#FindNearestFile(a:buffer, 'compile_commands.json')
+
+ let l:cd_command = !empty(l:compile_commmands_path)
+ \ ? ale#path#CdString(fnamemodify(l:compile_commmands_path, ':h'))
+ \ : ''
+ let l:compile_commands_option = !empty(l:compile_commmands_path)
+ \ ? '--project=compile_commands.json '
+ \ : ''
+
+ return l:cd_command
+ \ . 'cppcheck -q --language=c++ '
+ \ . l:compile_commands_option
\ . ale#Var(a:buffer, 'cpp_cppcheck_options')
\ . ' %t'
endfunction
diff --git a/test/command_callback/cppcheck_paths/one/compile_commands.json b/test/command_callback/cppcheck_paths/one/compile_commands.json
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/cppcheck_paths/one/compile_commands.json
diff --git a/test/command_callback/cppcheck_paths/one/two/three/file.c b/test/command_callback/cppcheck_paths/one/two/three/file.c
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/cppcheck_paths/one/two/three/file.c
diff --git a/test/command_callback/cppcheck_paths/one/two/three/file.cpp b/test/command_callback/cppcheck_paths/one/two/three/file.cpp
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/test/command_callback/cppcheck_paths/one/two/three/file.cpp
diff --git a/test/command_callback/test_cppcheck_command_callbacks.vader b/test/command_callback/test_cppcheck_command_callbacks.vader
new file mode 100644
index 00000000..665b4f13
--- /dev/null
+++ b/test/command_callback/test_cppcheck_command_callbacks.vader
@@ -0,0 +1,40 @@
+Before:
+ silent! cd /testplugin/test/command_callback
+ let b:dir = getcwd()
+
+After:
+ silent execute 'cd ' . fnameescape(b:dir)
+ unlet! b:dir
+ call ale#linter#Reset()
+
+Execute(The default C cppcheck command should be correct):
+ runtime ale_linters/c/cppcheck.vim
+
+ AssertEqual
+ \ 'cppcheck -q --language=c --enable=style %t',
+ \ ale_linters#c#cppcheck#GetCommand(bufnr(''))
+
+Execute(cppcheck for C should detect compile_commands.json files):
+ runtime ale_linters/c/cppcheck.vim
+ cd cppcheck_paths/one
+
+ AssertEqual
+ \ 'cd ' . fnameescape(b:dir . '/cppcheck_paths/one') . ' && '
+ \ . 'cppcheck -q --language=c --project=compile_commands.json --enable=style %t',
+ \ ale_linters#c#cppcheck#GetCommand(bufnr(''))
+
+Execute(The default C++ cppcheck command should be correct):
+ runtime ale_linters/cpp/cppcheck.vim
+
+ AssertEqual
+ \ 'cppcheck -q --language=c++ --enable=style %t',
+ \ ale_linters#cpp#cppcheck#GetCommand(bufnr(''))
+
+Execute(cppcheck for C++ should detect compile_commands.json files):
+ runtime ale_linters/cpp/cppcheck.vim
+ cd cppcheck_paths/one
+
+ AssertEqual
+ \ 'cd ' . fnameescape(b:dir . '/cppcheck_paths/one') . ' && '
+ \ . 'cppcheck -q --language=c++ --project=compile_commands.json --enable=style %t',
+ \ ale_linters#cpp#cppcheck#GetCommand(bufnr(''))