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