summaryrefslogtreecommitdiff
path: root/autoload/ale/c.vim
blob: 7dc532b31e7f570830f10b11556dd7ceed336e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
" Author: gagbo <gagbobada@gmail.com>, w0rp <devw0rp@gmail.com>
" Description: Functions for integrating with C-family linters.

let s:sep = has('win32') ? '\' : '/'

function! ale#c#FindProjectRoot(buffer) abort
    for l:project_filename in ['.git/HEAD', 'configure', 'Makefile', 'CMakeLists.txt']
        let l:full_path = ale#path#FindNearestFile(a:buffer, l:project_filename)

        if !empty(l:full_path)
            let l:path = fnamemodify(l:full_path, ':h')

            " Correct .git path detection.
            if fnamemodify(l:path, ':t') is# '.git'
                let l:path = fnamemodify(l:path, ':h')
            endif

            return l:path
        endif
    endfor

    return ''
endfunction

function! ale#c#ParseCFlags(project_root, stdout_make) abort
    let l:cflags_list = []
    let l:cflags = split(a:stdout_make)
    let l:shell_option = 0
    let l:macro_option = 0
    let l:previous_option = ''
    for l:option in l:cflags
        " Check if cflag contained spaces
        if l:shell_option || stridx(l:option, '=`') >= 0
            " Cflag contained shell command with spaces (ex. -D='date +%s')
            let l:shell_option = 1
            let l:previous_option .= l:option . ' '
            if l:option[-1: -1] isnot? '`'
                continue
            endif
            let l:shell_option = 0
        elseif l:macro_option || stridx(l:option, '$((') > 0
            " Cflag contained macro with spaces (ex -Da=$(( 4 * 20  )))
            let l:macro_option = 1
            let l:previous_option .= l:option . ' '
            if stridx(l:option, '))') < 0
                continue
            endif
            let l:macro_option = 0
        endif
        if l:previous_option isnot? ''
            let l:option = l:previous_option
            let l:previous_option = ''
        endif
        " Fix relative paths if needed
        if stridx(l:option, '-I') >= 0
            if stridx(l:option, '-I' . s:sep) < 0
                let l:option = '-I' . a:project_root . s:sep . l:option[2:]
            endif
        endif
        " Parse the cflag
        if stridx(l:option, '-I') >= 0 ||
           \ stridx(l:option, '-D') >= 0
            if index(l:cflags_list, l:option) < 0
                call add(l:cflags_list, l:option)
            endif
        endif
    endfor
    return l:cflags_list
endfunction

function! ale#c#ParseMakefile(buffer) abort
    let l:project_root = ale#c#FindProjectRoot(a:buffer)
    let l:project_cflags = []

    if !empty(l:project_root)
        if !empty(globpath(l:project_root, 'Makefile', 0))
            let l:stdout_make = system('cd '. l:project_root . ' && make -n')
            for l:object in split(l:stdout_make, '\n')
                if stridx(l:object, expand('#' . a:buffer . '...'))
                    return ale#c#ParseCFlags(l:project_root, l:object)
                endif
            endfor
        endif
    endif
    return []
endfunction

" Given a buffer number, search for a project root, and output a List
" of directories to include based on some heuristics.
"
" For projects with headers in the project root, the project root will
" be returned.
"
" For projects with an 'include' directory, that directory will be returned.
function! ale#c#FindLocalHeaderPaths(buffer) abort
    let l:project_root = ale#c#FindProjectRoot(a:buffer)

    if empty(l:project_root)
        return []
    endif

    " See if we can find .h files directory in the project root.
    " If we can, that's our include directory.
    if !empty(globpath(l:project_root, '*.h', 0))
        return [l:project_root]
    endif

    " Look for .hpp files too.
    if !empty(globpath(l:project_root, '*.hpp', 0))
        return [l:project_root]
    endif

    " If we find an 'include' directory in the project root, then use that.
    if isdirectory(l:project_root . '/include')
        return [ale#path#Simplify(l:project_root . s:sep . 'include')]
    endif

    return []
endfunction

" Given a List of include paths, create a string containing the -I include
" options for those paths, with the paths escaped for use in the shell.
function! ale#c#IncludeOptions(include_paths) abort
    let l:option_list = []

    for l:path in a:include_paths
        call add(l:option_list, '-I' . ale#Escape(l:path))
    endfor

    if empty(l:option_list)
        return ''
    endif

    return ' ' . join(l:option_list) . ' '
endfunction

let g:ale_c_build_dir_names = get(g:, 'ale_c_build_dir_names', [
\   'build',
\   'bin',
\])

" Given a buffer number, find the build subdirectory with compile commands
" The subdirectory is returned without the trailing /
function! ale#c#FindCompileCommands(buffer) abort
    for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
        for l:dirname in ale#Var(a:buffer, 'c_build_dir_names')
            let l:c_build_dir = l:path . s:sep . l:dirname

            if filereadable(l:c_build_dir . '/compile_commands.json')
                return l:c_build_dir
            endif
        endfor
    endfor

    return ''
endfunction