Add cuda nvcc linter (#874)

* add cuda nvcc linter
This commit is contained in:
BlahGeek 2017-09-08 02:23:58 +08:00 committed by w0rp
parent 555d23c035
commit 73d031d7ea
6 changed files with 148 additions and 0 deletions

View File

@ -75,6 +75,7 @@ formatting.
| Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/) | | Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/) |
| C | [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [gcc](https://gcc.gnu.org/), [clang](http://clang.llvm.org/), [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html)| | C | [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [gcc](https://gcc.gnu.org/), [clang](http://clang.llvm.org/), [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html)|
| C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) !!, [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) !!, [gcc](https://gcc.gnu.org/), [clang-format](https://clang.llvm.org/docs/ClangFormat.html)| | C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) !!, [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) !!, [gcc](https://gcc.gnu.org/), [clang-format](https://clang.llvm.org/docs/ClangFormat.html)|
| CUDA | [nvcc](http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html) |
| C# | [mcs](http://www.mono-project.com/docs/about-mono/languages/csharp/) | | C# | [mcs](http://www.mono-project.com/docs/about-mono/languages/csharp/) |
| Chef | [foodcritic](http://www.foodcritic.io/) | | Chef | [foodcritic](http://www.foodcritic.io/) |
| CMake | [cmakelint](https://github.com/richq/cmake-lint) | | CMake | [cmakelint](https://github.com/richq/cmake-lint) |

56
ale_linters/cuda/nvcc.vim Normal file
View File

@ -0,0 +1,56 @@
" Author: blahgeek <i@blahgeek.com>
" Description: NVCC linter for cuda files
call ale#Set('cuda_nvcc_executable', 'nvcc')
call ale#Set('cuda_nvcc_options', '-std=c++11')
function! ale_linters#cuda#nvcc#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'cuda_nvcc_executable')
endfunction
function! ale_linters#cuda#nvcc#GetCommand(buffer) abort
" Unused: use ale#util#nul_file
" let l:output_file = tempname() . '.ii'
" call ale#engine#ManageFile(a:buffer, l:output_file)
return ale#Escape(ale_linters#cuda#nvcc#GetExecutable(a:buffer))
\ . ' -cuda '
\ . ale#c#IncludeOptions(ale#c#FindLocalHeaderPaths(a:buffer))
\ . ale#Var(a:buffer, 'cuda_nvcc_options') . ' %s'
\ . ' -o ' . g:ale#util#nul_file
endfunction
function! ale_linters#cuda#nvcc#HandleNVCCFormat(buffer, lines) abort
" Look for lines like the following.
"
" test.cu(8): error: argument of type "void *" is incompatible with parameter of type "int *"
let l:pattern = '\v^([^:\(\)]+):?\(?(\d+)\)?:(\d+)?:?\s*\w*\s*(error|warning): (.+)$'
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
let l:item = {
\ 'lnum': str2nr(l:match[2]),
\ 'type': l:match[4] =~# 'error' ? 'E' : 'W',
\ 'text': l:match[5],
\ 'filename': fnamemodify(l:match[1], ':p'),
\}
if !empty(l:match[3])
let l:item.col = str2nr(l:match[3])
endif
call add(l:output, l:item)
endfor
return l:output
endfunction
call ale#linter#Define('cuda', {
\ 'name': 'nvcc',
\ 'output_stream': 'stderr',
\ 'executable_callback': 'ale_linters#cuda#nvcc#GetExecutable',
\ 'command_callback': 'ale_linters#cuda#nvcc#GetCommand',
\ 'callback': 'ale_linters#cuda#nvcc#HandleNVCCFormat',
\ 'lint_file': 1,
\})

25
doc/ale-cuda.txt Normal file
View File

@ -0,0 +1,25 @@
===============================================================================
ALE CUDA Integration *ale-cuda-options*
===============================================================================
NVCC *ale-cuda-nvcc*
g:ale_cuda_nvcc_executable *g:ale_cuda_nvcc_executable*
*b:ale_cuda_nvcc_executable*
Type: |String|
Default: `'nvcc'`
This variable can be changed to use a different executable for nvcc.
Currently only nvcc 8.0 is supported.
g:ale_cuda_nvcc_options *g:ale_cuda_nvcc_options*
*b:ale_cuda_nvcc_options*
Type: |String|
Default: `'-std=c++11'`
This variable can be changed to modify flags given to nvcc.
===============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

View File

@ -32,6 +32,8 @@ CONTENTS *ale-contents*
cpplint.............................|ale-cpp-cpplint| cpplint.............................|ale-cpp-cpplint|
gcc.................................|ale-cpp-gcc| gcc.................................|ale-cpp-gcc|
clang-format........................|ale-cpp-clangformat| clang-format........................|ale-cpp-clangformat|
cuda..................................|ale-cuda-options|
nvcc................................|ale-cuda-nvcc|
css...................................|ale-css-options| css...................................|ale-css-options|
prettier............................|ale-css-prettier| prettier............................|ale-css-prettier|
stylelint...........................|ale-css-stylelint| stylelint...........................|ale-css-stylelint|
@ -201,6 +203,7 @@ Notes:
* Bourne Shell: `shell` (-n flag), `shellcheck` * Bourne Shell: `shell` (-n flag), `shellcheck`
* C: `cppcheck`, `cpplint`!!, `gcc`, `clang`, `clangtidy`!!, `clang-format` * C: `cppcheck`, `cpplint`!!, `gcc`, `clang`, `clangtidy`!!, `clang-format`
* C++ (filetype cpp): `clang`, `clangcheck`!!, `clangtidy`!!, `cppcheck`, `cpplint`!!, `gcc`, `clang-format` * C++ (filetype cpp): `clang`, `clangcheck`!!, `clangtidy`!!, `cppcheck`, `cpplint`!!, `gcc`, `clang-format`
* CUDA: `nvcc`!!
* C#: `mcs` * C#: `mcs`
* Chef: `foodcritic` * Chef: `foodcritic`
* CMake: `cmakelint` * CMake: `cmakelint`

View File

@ -0,0 +1,34 @@
Before:
Save g:ale_cuda_nvcc_executable
Save g:ale_cuda_nvcc_options
unlet! g:ale_cuda_nvcc_executable
unlet! b:ale_cuda_nvcc_executable
unlet! g:ale_cuda_nvcc_options
unlet! b:ale_cuda_nvcc_options
runtime ale_linters/cuda/nvcc.vim
After:
Restore
unlet! b:ale_cuda_nvcc_executable
unlet! b:ale_cuda_nvcc_options
call ale#linter#Reset()
Execute(The executable should be configurable):
AssertEqual 'nvcc', ale_linters#cuda#nvcc#GetExecutable(bufnr(''))
let b:ale_cuda_nvcc_executable = 'foobar'
AssertEqual 'foobar', ale_linters#cuda#nvcc#GetExecutable(bufnr(''))
Execute(The executable should be used in the command):
AssertEqual
\ ale#Escape('nvcc') . ' -cuda -std=c++11 %s -o /dev/null',
\ ale_linters#cuda#nvcc#GetCommand(bufnr(''))
let b:ale_cuda_nvcc_executable = 'foobar'
AssertEqual
\ ale#Escape('foobar') . ' -cuda -std=c++11 %s -o /dev/null',
\ ale_linters#cuda#nvcc#GetCommand(bufnr(''))

View File

@ -0,0 +1,29 @@
Execute(The cuda nvcc handler should parse errors from multiple files for NVCC 8.0):
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'type': 'E',
\ 'text': 'this declaration has no storage class or type specifier',
\ 'filename': '/tmp/cudatest/test.cu',
\ },
\ {
\ 'lnum': 2,
\ 'type': 'E',
\ 'text': 'attribute "global" does not apply here',
\ 'filename': '/tmp/cudatest/common.h',
\ },
\ {
\ 'lnum': 2,
\ 'type': 'E',
\ 'text': 'expected a ";"',
\ 'filename': '/tmp/cudatest/common.h',
\ },
\ ],
\ ale_linters#cuda#nvcc#HandleNVCCFormat(0, [
\ '/tmp/cudatest/test.cu(1): error: this declaration has no storage class or type specifier',
\ '/tmp/cudatest/common.h(2): error: attribute "global" does not apply here',
\ '/tmp/cudatest/common.h(2): error: expected a ";"',
\ 'At end of source: warning: parsing restarts here after previous syntax error',
\ '3 errors detected in the compilation of "/tmp/tmpxft_00003a9f_00000000-7_test.cpp1.ii".',
\ ])