Skip to content

Commit d3d8b8b

Browse files
authored
Merge pull request #144 from google/clangformat_exitcode
Ignore non-zero exit code from clang-format --version
2 parents 0bae1b3 + e8c4b55 commit d3d8b8b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

autoload/codefmt/clangformat.vim

+21-6
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,35 @@ function! s:ClangFormatHasAtLeastVersion(minimum_version) abort
2323
return 0
2424
endif
2525

26-
let l:version_output =
27-
\ maktaba#syscall#Create([l:executable, '--version']).Call().stdout
26+
let l:syscall = maktaba#syscall#Create([l:executable, '--version'])
27+
" Call with throw_errors disabled because some versions of clang-format
28+
" misbehave and return exit code 1 along with the successful version
29+
" output (see https://github.com/google/vim-codefmt/issues/84).
30+
let l:version_output = l:syscall.Call(0).stdout
2831
let l:version_string = matchstr(l:version_output, '\v\d+(.\d+)+')
32+
" If no version string was matched, cached version will be an empty list.
2933
let s:clang_format_version = map(split(l:version_string, '\.'), 'v:val + 0')
3034
endif
31-
let l:length = min([len(a:minimum_version), len(s:clang_format_version)])
35+
" Always fail check if version couldn't be fetched.
36+
if empty(s:clang_format_version)
37+
return 0
38+
endif
39+
" Compare each dotted version value in turn.
40+
let l:length = max([len(a:minimum_version), len(s:clang_format_version)])
3241
for i in range(l:length)
33-
if a:minimum_version[i] < s:clang_format_version[i]
42+
" Consider missing version places as zero (e.g. 7 = 7.0 = 7.0.0).
43+
let l:detected_value = get(s:clang_format_version, i, 0)
44+
let l:minimum_value = get(a:minimum_version, i, 0)
45+
" Any place value above or below than its minimum means entire version is
46+
" above or below the minimum.
47+
if l:detected_value > l:minimum_value
3448
return 1
35-
elseif a:minimum_version[i] > s:clang_format_version[i]
49+
elseif l:detected_value < l:minimum_value
3650
return 0
3751
endif
3852
endfor
39-
return len(a:minimum_version) <= len(s:clang_format_version)
53+
" All version numbers were equal, so version was at least minimum.
54+
return 1
4055
endfunction
4156

4257

0 commit comments

Comments
 (0)