diff --git a/ale_linters/c/clang.vim b/ale_linters/c/clang.vim index ae96ba4..ecfa505 100644 --- a/ale_linters/c/clang.vim +++ b/ale_linters/c/clang.vim @@ -10,11 +10,14 @@ if !exists('g:ale_c_clang_options') endif function! ale_linters#c#clang#GetCommand(buffer) abort + let l:paths = ale#handlers#c#FindLocalHeaderPaths(a:buffer) + " -iquote with the directory the file is in makes #include work for " headers in the same directory. return 'clang -S -x c -fsyntax-only ' - \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) - \ . ' ' . ale#Var(a:buffer, 'c_clang_options') . ' -' + \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' + \ . ale#handlers#c#IncludeOptions(l:paths) + \ . ale#Var(a:buffer, 'c_clang_options') . ' -' endfunction call ale#linter#Define('c', { diff --git a/ale_linters/c/gcc.vim b/ale_linters/c/gcc.vim index 79c6eb2..bcf8017 100644 --- a/ale_linters/c/gcc.vim +++ b/ale_linters/c/gcc.vim @@ -10,11 +10,14 @@ if !exists('g:ale_c_gcc_options') endif function! ale_linters#c#gcc#GetCommand(buffer) abort + let l:paths = ale#handlers#c#FindLocalHeaderPaths(a:buffer) + " -iquote with the directory the file is in makes #include work for " headers in the same directory. return 'gcc -S -x c -fsyntax-only ' - \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) - \ . ' ' . ale#Var(a:buffer, 'c_gcc_options') . ' -' + \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' + \ . ale#handlers#c#IncludeOptions(l:paths) + \ . ale#Var(a:buffer, 'c_gcc_options') . ' -' endfunction call ale#linter#Define('c', { diff --git a/ale_linters/cpp/clang.vim b/ale_linters/cpp/clang.vim index 430903f..953c8a7 100644 --- a/ale_linters/cpp/clang.vim +++ b/ale_linters/cpp/clang.vim @@ -7,11 +7,14 @@ if !exists('g:ale_cpp_clang_options') endif function! ale_linters#cpp#clang#GetCommand(buffer) abort + let l:paths = ale#handlers#c#FindLocalHeaderPaths(a:buffer) + " -iquote with the directory the file is in makes #include work for " headers in the same directory. return 'clang++ -S -x c++ -fsyntax-only ' - \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) - \ . ' ' . ale#Var(a:buffer, 'cpp_clang_options') . ' -' + \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' + \ . ale#handlers#c#IncludeOptions(l:paths) + \ . ale#Var(a:buffer, 'cpp_clang_options') . ' -' endfunction call ale#linter#Define('cpp', { diff --git a/ale_linters/cpp/gcc.vim b/ale_linters/cpp/gcc.vim index e85f189..36e958e 100644 --- a/ale_linters/cpp/gcc.vim +++ b/ale_linters/cpp/gcc.vim @@ -17,11 +17,14 @@ if !exists('g:ale_cpp_gcc_options') endif function! ale_linters#cpp#gcc#GetCommand(buffer) abort + let l:paths = ale#handlers#c#FindLocalHeaderPaths(a:buffer) + " -iquote with the directory the file is in makes #include work for " headers in the same directory. return 'gcc -S -x c++ -fsyntax-only ' - \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) - \ . ' ' . ale#Var(a:buffer, 'cpp_gcc_options') . ' -' + \ . '-iquote ' . ale#Escape(fnamemodify(bufname(a:buffer), ':p:h')) . ' ' + \ . ale#handlers#c#IncludeOptions(l:paths) + \ . ale#Var(a:buffer, 'cpp_gcc_options') . ' -' endfunction call ale#linter#Define('cpp', { diff --git a/autoload/ale/handlers/c.vim b/autoload/ale/handlers/c.vim index d80f5e7..266ab20 100644 --- a/autoload/ale/handlers/c.vim +++ b/autoload/ale/handlers/c.vim @@ -2,7 +2,7 @@ " Desciption: Functions for integrating with C and C++ compilers. function! ale#handlers#c#FindProjectRoot(buffer) abort - for l:project_filename in ['Makefile', 'CMakeLists.txt'] + for l:project_filename in ['configure', 'Makefile', 'CMakeLists.txt'] let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename) if !empty(l:full_path) @@ -55,5 +55,9 @@ function! ale#handlers#c#IncludeOptions(include_paths) abort call add(l:option_list, '-I' . ale#Escape(l:path)) endfor - return join(l:option_list) + if empty(l:option_list) + return '' + endif + + return ' ' . join(l:option_list) . ' ' endfunction diff --git a/test/test_c_import_paths.vader b/test/test_c_import_paths.vader new file mode 100644 index 0000000..66ff6dc --- /dev/null +++ b/test/test_c_import_paths.vader @@ -0,0 +1,228 @@ +Before: + Save g:ale_c_gcc_options + Save g:ale_c_clang_options + Save g:ale_cpp_gcc_options + Save g:ale_cpp_clang_options + + silent! cd /testplugin/test + let g:dir = getcwd() + + let g:ale_c_gcc_options = '' + let g:ale_c_clang_options = '' + let g:ale_cpp_gcc_options = '' + let g:ale_cpp_clang_options = '' + +After: + Restore + + silent execute 'cd ' . fnameescape(g:dir) + unlet! g:dir + call ale#linter#Reset() + +Execute(The C GCC handler should include 'include' directories for projects with a Makefile): + runtime! ale_linters/c/gcc.vim + + cd test_c_projects/makefile_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'gcc -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/makefile_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/makefile_project/include') . ' ' + \ . ' -' + \ , ale_linters#c#gcc#GetCommand(bufnr('')) + +Execute(The C GCC handler should include 'include' directories for projects with a configure file): + runtime! ale_linters/c/gcc.vim + + cd test_c_projects/configure_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'gcc -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/configure_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/configure_project/include') . ' ' + \ . ' -' + \ , ale_linters#c#gcc#GetCommand(bufnr('')) + +Execute(The C GCC handler should include root directories for projects with .h files in them): + runtime! ale_linters/c/gcc.vim + + cd test_c_projects/h_file_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'gcc -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/h_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/h_file_project') . ' ' + \ . ' -' + \ , ale_linters#c#gcc#GetCommand(bufnr('')) + +Execute(The C GCC handler should include root directories for projects with .hpp files in them): + runtime! ale_linters/c/gcc.vim + + cd test_c_projects/hpp_file_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'gcc -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project') . ' ' + \ . ' -' + \ , ale_linters#c#gcc#GetCommand(bufnr('')) + +Execute(The C Clang handler should include 'include' directories for projects with a Makefile): + runtime! ale_linters/c/clang.vim + + cd test_c_projects/makefile_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'clang -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/makefile_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/makefile_project/include') . ' ' + \ . ' -' + \ , ale_linters#c#clang#GetCommand(bufnr('')) + +Execute(The C Clang handler should include 'include' directories for projects with a configure file): + runtime! ale_linters/c/clang.vim + + cd test_c_projects/h_file_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'clang -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/h_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/h_file_project') . ' ' + \ . ' -' + \ , ale_linters#c#clang#GetCommand(bufnr('')) + +Execute(The C Clang handler should include root directories for projects with .h files in them): + runtime! ale_linters/c/clang.vim + + cd test_c_projects/h_file_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'clang -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/h_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/h_file_project') . ' ' + \ . ' -' + \ , ale_linters#c#clang#GetCommand(bufnr('')) + +Execute(The C Clang handler should include root directories for projects with .hpp files in them): + runtime! ale_linters/c/clang.vim + + cd test_c_projects/hpp_file_project/subdir + silent noautocmd file file.c + + AssertEqual + \ 'clang -S -x c -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project') . ' ' + \ . ' -' + \ , ale_linters#c#clang#GetCommand(bufnr('')) + +Execute(The C++ GCC handler should include 'include' directories for projects with a Makefile): + runtime! ale_linters/cpp/gcc.vim + + cd test_c_projects/makefile_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'gcc -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/makefile_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/makefile_project/include') . ' ' + \ . ' -' + \ , ale_linters#cpp#gcc#GetCommand(bufnr('')) + +Execute(The C++ GCC handler should include 'include' directories for projects with a configure file): + runtime! ale_linters/cpp/gcc.vim + + cd test_c_projects/configure_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'gcc -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/configure_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/configure_project/include') . ' ' + \ . ' -' + \ , ale_linters#cpp#gcc#GetCommand(bufnr('')) + +Execute(The C++ GCC handler should include root directories for projects with .h files in them): + runtime! ale_linters/cpp/gcc.vim + + cd test_c_projects/h_file_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'gcc -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/h_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/h_file_project') . ' ' + \ . ' -' + \ , ale_linters#cpp#gcc#GetCommand(bufnr('')) + +Execute(The C++ GCC handler should include root directories for projects with .hpp files in them): + runtime! ale_linters/cpp/gcc.vim + + cd test_c_projects/hpp_file_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'gcc -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project') . ' ' + \ . ' -' + \ , ale_linters#cpp#gcc#GetCommand(bufnr('')) + +Execute(The C++ Clang handler should include 'include' directories for projects with a Makefile): + runtime! ale_linters/cpp/clang.vim + + cd test_c_projects/makefile_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'clang++ -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/makefile_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/makefile_project/include') . ' ' + \ . ' -' + \ , ale_linters#cpp#clang#GetCommand(bufnr('')) + +Execute(The C++ Clang handler should include 'include' directories for projects with a configure file): + runtime! ale_linters/cpp/clang.vim + + cd test_c_projects/configure_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'clang++ -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/configure_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/configure_project/include') . ' ' + \ . ' -' + \ , ale_linters#cpp#clang#GetCommand(bufnr('')) + +Execute(The C++ Clang handler should include root directories for projects with .h files in them): + runtime! ale_linters/cpp/clang.vim + + cd test_c_projects/h_file_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'clang++ -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/h_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/h_file_project') . ' ' + \ . ' -' + \ , ale_linters#cpp#clang#GetCommand(bufnr('')) + +Execute(The C++ Clang handler should include root directories for projects with .hpp files in them): + runtime! ale_linters/cpp/clang.vim + + cd test_c_projects/hpp_file_project/subdir + silent noautocmd file file.cpp + + AssertEqual + \ 'clang++ -S -x c++ -fsyntax-only ' + \ . '-iquote ' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project/subdir') . ' ' + \ . ' -I' . ale#Escape(g:dir . '/test_c_projects/hpp_file_project') . ' ' + \ . ' -' + \ , ale_linters#cpp#clang#GetCommand(bufnr('')) diff --git a/test/test_c_projects/configure_project/Makefile b/test/test_c_projects/configure_project/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/configure_project/configure b/test/test_c_projects/configure_project/configure new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/configure_project/include/test.h b/test/test_c_projects/configure_project/include/test.h new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/configure_project/subdir/Makefile b/test/test_c_projects/configure_project/subdir/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/h_file_project/Makefile b/test/test_c_projects/h_file_project/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/h_file_project/subdir/dummy b/test/test_c_projects/h_file_project/subdir/dummy new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/h_file_project/test.h b/test/test_c_projects/h_file_project/test.h new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/hpp_file_project/Makefile b/test/test_c_projects/hpp_file_project/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/hpp_file_project/subdir/dummy b/test/test_c_projects/hpp_file_project/subdir/dummy new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/hpp_file_project/test.hpp b/test/test_c_projects/hpp_file_project/test.hpp new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/makefile_project/Makefile b/test/test_c_projects/makefile_project/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/makefile_project/include/test.h b/test/test_c_projects/makefile_project/include/test.h new file mode 100644 index 0000000..e69de29 diff --git a/test/test_c_projects/makefile_project/subdir/dummy b/test/test_c_projects/makefile_project/subdir/dummy new file mode 100644 index 0000000..e69de29