#782 - Use compile commands for matching source files for headers

This commit is contained in:
w0rp 2019-04-15 21:38:11 +01:00
parent 7f31065fce
commit 23a8208498
No known key found for this signature in database
GPG key ID: 0FC1ECAA8C81CD83
2 changed files with 117 additions and 6 deletions

View file

@ -219,9 +219,32 @@ function! ale#c#ParseCompileCommandsFlags(buffer, file_lookup, dir_lookup) abort
" Search for an exact file match first.
let l:basename = tolower(expand('#' . a:buffer . ':t'))
let l:file_list = get(a:file_lookup, l:basename, [])
" A source file matching the header filename.
let l:source_file = ''
if empty(l:file_list) && l:basename =~? '\.h$\|\.hpp$'
for l:suffix in ['.c', '.cpp']
let l:key = fnamemodify(l:basename, ':r') . l:suffix
let l:file_list = get(a:file_lookup, l:key, [])
if !empty(l:file_list)
let l:source_file = l:key
break
endif
endfor
endif
for l:item in l:file_list
if bufnr(l:item.file) is a:buffer && has_key(l:item, 'command')
" Load the flags for this file, or for a source file matching the
" header file.
if has_key(l:item, 'command')
\&& (
\ bufnr(l:item.file) is a:buffer
\ || (
\ !empty(l:source_file)
\ && l:item.file[-len(l:source_file):] is? l:source_file
\ )
\)
return ale#c#ParseCFlags(l:item.directory, l:item.command)
endif
endfor

View file

@ -153,10 +153,10 @@ Execute(ParseCompileCommandsFlags should tolerate empty values):
AssertEqual '', ale#c#ParseCompileCommandsFlags(bufnr(''), {}, {})
Execute(ParseCompileCommandsFlags should parse some basic flags):
noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
AssertEqual
\ '-I' . '/usr/include/xmms2',
\ '-I/usr/include/xmms2',
\ ale#c#ParseCompileCommandsFlags(bufnr(''), { "xmms2-mpris.c": [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
@ -168,7 +168,7 @@ Execute(ParseCompileCommandsFlags should parse some basic flags):
\ ] }, {})
Execute(ParseCompileCommandsFlags should tolerate items without commands):
noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
AssertEqual
\ '',
@ -180,10 +180,10 @@ Execute(ParseCompileCommandsFlags should tolerate items without commands):
\ ] }, {})
Execute(ParseCompileCommandsFlags should fall back to files in the same directory):
noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.c'))
AssertEqual
\ '-I' . '/usr/include/xmms2',
\ '-I/usr/include/xmms2',
\ ale#c#ParseCompileCommandsFlags(bufnr(''), {}, { "src": [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
@ -194,6 +194,94 @@ Execute(ParseCompileCommandsFlags should fall back to files in the same director
\ },
\ ] })
Execute(ParseCompileCommandsFlags should take commands from matching .c files for .h files):
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.h'))
AssertEqual
\ '-I/usr/include/xmms2',
\ ale#c#ParseCompileCommandsFlags(
\ bufnr(''),
\ {
\ 'xmms2-mpris.c': [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
\ 'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
\ 'command': '/usr/bin/cc -I' . '/usr/include/xmms2'
\ . ' -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
\ . ' -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
\ },
\ ],
\ },
\ {
\ },
\ )
Execute(ParseCompileCommandsFlags should take commands from matching .cpp files for .hpp files):
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.hpp'))
AssertEqual
\ '-I/usr/include/xmms2',
\ ale#c#ParseCompileCommandsFlags(
\ bufnr(''),
\ {
\ 'xmms2-mpris.cpp': [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
\ 'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
\ 'command': '/usr/bin/cc -I' . '/usr/include/xmms2'
\ . ' -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
\ . ' -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
\ },
\ ],
\ },
\ {
\ },
\ )
Execute(ParseCompileCommandsFlags should take commands from matching .cpp files for .h files):
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/xmms2-mpris.h'))
AssertEqual
\ '-I/usr/include/xmms2',
\ ale#c#ParseCompileCommandsFlags(
\ bufnr(''),
\ {
\ 'xmms2-mpris.cpp': [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
\ 'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
\ 'command': '/usr/bin/cc -I' . '/usr/include/xmms2'
\ . ' -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
\ . ' -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.cpp',
\ },
\ ],
\ },
\ {
\ },
\ )
Execute(ParseCompileCommandsFlags should not take commands from .c files for .h files with different names):
silent noautocmd execute 'file! ' . fnameescape(ale#path#Simplify('/foo/bar/xmms2-mpris/src/other.h'))
AssertEqual
\ '',
\ ale#c#ParseCompileCommandsFlags(
\ bufnr(''),
\ {
\ 'xmms2-mpris.c': [
\ {
\ 'directory': '/foo/bar/xmms2-mpris',
\ 'file': (has('win32') ? 'C:' : '') . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
\ 'command': '/usr/bin/cc -I' . '/usr/include/xmms2'
\ . ' -o CMakeFiles/xmms2-mpris.dir/src/xmms2-mpris.c.o'
\ . ' -c ' . '/foo/bar/xmms2-mpris/src/xmms2-mpris.c',
\ },
\ ],
\ },
\ {
\ },
\ )
Execute(ParseCFlags should handle parenthesis and quotes):
AssertEqual
\ '-Dgoal=9 -Dtest1="('' '')" file1.o -Dtest2=''(` `)'' file2.o -Dtest3=`(" ")` file3.o',