robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
5.3 KiB240 linesraw
1-- Checks mismatches between "EXX" error codes (E123, E1234) defined in C sources and those
2-- documented in `runtime/doc/*.txt`.
3--
4-- Usage: nvim -l scripts/linterrcodes.lua
5
6--- Error codes allowed to appear in more than one place. Value is the exact expected
7--- occurrence count. A mismatch (actual > or < expected) is reported, to avoid
8--- accidental duplicates from slipping in.
9--- @type table<string, integer>
10local dup_allowed = {
11  E109 = 2,
12  E1098 = 2,
13  E110 = 2,
14  E112 = 2,
15  E114 = 2,
16  E115 = 2,
17  E1159 = 2,
18  E116 = 2,
19  E121 = 2,
20  E1502 = 4,
21  E1510 = 2,
22  E151 = 2,
23  E155 = 3,
24  E158 = 2,
25  E170 = 2,
26  E173 = 2,
27  E180 = 2,
28  E212 = 2,
29  E216 = 2,
30  E298 = 3,
31  E303 = 3,
32  E312 = 2,
33  E317 = 4,
34  E319 = 2,
35  E367 = 3,
36  E423 = 3,
37  E474 = 60,
38  E475 = 6,
39  E482 = 3,
40  E484 = 2,
41  E488 = 2,
42  E5009 = 3,
43  E502 = 2,
44  E503 = 3,
45  E504 = 2,
46  E505 = 2,
47  E509 = 2,
48  E5101 = 2,
49  E5102 = 2,
50  E5108 = 6,
51  E5111 = 2,
52  E513 = 2,
53  E521 = 2,
54  E546 = 2,
55  E588 = 2,
56  E678 = 2,
57  E685 = 5,
58  E697 = 2,
59  E703 = 2,
60  E716 = 2,
61  E723 = 2,
62  E724 = 3,
63  E728 = 2,
64  E741 = 2,
65  E742 = 2,
66  E745 = 2,
67  E798 = 2,
68  E805 = 2,
69  E856 = 2,
70  E867 = 2,
71  E900 = 3,
72  E903 = 2,
73  E905 = 2,
74  E906 = 2,
75  E948 = 2,
76  E970 = 2,
77  E974 = 2,
78  E996 = 5,
79}
80
81--- Runs a command, returns stdout lines. Errors on non-zero exit.
82--- @param cmd string[]
83--- @return string[]
84local function run(cmd)
85  local result = vim.system(cmd, { text = true }):wait()
86  if result.code ~= 0 then
87    error('command failed: ' .. table.concat(cmd, ' ') .. '\n' .. (result.stderr or ''))
88  end
89  return vim.split(result.stdout, '\n', { trimempty = true })
90end
91
92--- Extracts error codes from a line of C source, excluding hex literals (0xE000),
93--- identifiers (FOO_E123), and inline comments (`//`).
94--- @param line string
95--- @return string[]
96local function extract_codes(line)
97  local codes = {} --- @type string[]
98  local cmt = line:find('//')
99  for pos, code in line:gmatch('()(E%d%d%d%d?)') do
100    --- @cast pos integer
101    local in_comment = cmt and pos > cmt
102    -- Preceded by a word char means the `E` is part of something else.
103    local prev = pos > 1 and line:sub(pos - 1, pos - 1) or ''
104    local in_word = prev:match('[%w_]') ~= nil
105    if not in_comment and not in_word then
106      codes[#codes + 1] = code
107    end
108  end
109  return codes
110end
111
112--- @return table<string, true> Set of error codes documented in help docs.
113local function collect_help_codes()
114  local lines = run({
115    'git',
116    'grep',
117    '-hE',
118    [[\*E[0-9]{3,4}\*]],
119    '--',
120    'runtime/doc/*.txt',
121  })
122  local codes = {} --- @type table<string, true>
123  for _, line in ipairs(lines) do
124    for code in line:gmatch('E%d%d%d%d?') do
125      codes[code] = true
126    end
127  end
128  return codes
129end
130
131--- @return table<string, string[]> Map of error code to its occurrences in C sources.
132local function collect_c_codes()
133  local lines = run({
134    'git',
135    'grep',
136    '-nE',
137    'E[0-9]{3,4}',
138    '--',
139    'src/nvim/*.c',
140    'src/nvim/*.h',
141  })
142  local codes = {} --- @type table<string, string[]>
143  for _, line in ipairs(lines) do
144    for _, code in ipairs(extract_codes(line)) do
145      codes[code] = codes[code] or {}
146      table.insert(codes[code], line)
147    end
148  end
149  return codes
150end
151
152--- @param a string
153--- @param b string
154--- @return boolean
155local function errcode_lt(a, b)
156  return tonumber(a:sub(2)) < tonumber(b:sub(2))
157end
158
159--- @param c_codes table<string, string[]>
160--- @param help_codes table<string, true>
161--- @return integer missing Number of codes missing from help docs.
162--- @return integer dups Number of codes with unexpected duplicate usage.
163local function report(c_codes, help_codes)
164  local missing = {} --- @type string[]
165  for code in pairs(c_codes) do
166    if not help_codes[code] then
167      missing[#missing + 1] = code
168    end
169  end
170  table.sort(missing, errcode_lt)
171
172  local dup_codes = {} --- @type string[]
173  for code, occurrences in pairs(c_codes) do
174    local allowed = dup_allowed[code]
175    if allowed then
176      -- Whitelisted: only flag if the actual count doesn't match the expected count.
177      if #occurrences ~= allowed then
178        dup_codes[#dup_codes + 1] = code
179      end
180    elseif #occurrences > 1 then
181      dup_codes[#dup_codes + 1] = code
182    end
183  end
184  table.sort(dup_codes, errcode_lt)
185
186  if #missing > 0 then
187    print('Error codes missing from help docs:')
188    for _, code in ipairs(missing) do
189      print('  ' .. code)
190    end
191    print('')
192  end
193
194  if #dup_codes > 0 then
195    print('Error codes used in more than one place:')
196    for _, code in ipairs(dup_codes) do
197      print(string.format('  %s (%d occurrences):', code, #c_codes[code]))
198      for _, loc in ipairs(c_codes[code]) do
199        print('    ' .. loc)
200      end
201    end
202    print('')
203  end
204
205  local max_code = 0
206  for code in pairs(c_codes) do
207    local n = tonumber(code:sub(2)) or 0
208    if n > max_code then
209      max_code = n
210    end
211  end
212
213  local n_errcodes = 0
214  for _ in pairs(c_codes) do
215    n_errcodes = n_errcodes + 1
216  end
217
218  print(
219    string.format(
220      'errcodes=%d dup-codes=%d missing-help=%d highest=E%d',
221      n_errcodes,
222      #dup_codes,
223      #missing,
224      max_code
225    )
226  )
227
228  return #missing, #dup_codes
229end
230
231local function main()
232  local help_codes = collect_help_codes()
233  local c_codes = collect_c_codes()
234  local missing, dups = report(c_codes, help_codes)
235  if missing > 0 or dups > 0 then
236    os.exit(1)
237  end
238end
239
240main()