robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dce8bdd247
master
1local grammar =require ( 'gen.c_grammar' ). grammar 2 3--- @param fname string 4--- @return string? 5local function read_file ( fname ) 6local f =io . open ( fname , 'r' ) 7if not f then 8return 9end 10local contents =f : read ( '*a' ) 11f : close () 12return contents 13end 14 15--- @param fname string 16--- @param contents string[] 17local function write_file ( fname , contents ) 18local contents_s =table . concat ( contents , '\n' ) .. '\n' 19local fcontents =read_file ( fname ) 20if fcontents == contents_s then 21return 22end 23local f =assert ( io . open ( fname , 'w' )) 24f : write ( contents_s ) 25f : close () 26end 27 28--- @param fname string 29--- @param non_static_fname string 30--- @return string? non_static 31local function add_iwyu_non_static ( fname , non_static_fname ) 32if fname : find ( '.*/src/nvim/.*%.c$' ) then 33-- Add an IWYU pragma comment if the corresponding .h file exists. 34local header_fname =fname : sub ( 1 , - 3 ) .. '.h' 35local header_f =io . open ( header_fname , 'r' ) 36if header_f then 37header_f : close () 38return ( header_fname : gsub ( '.*/src/nvim/' , 'nvim/' )) 39end 40elseif non_static_fname : find ( '/include/api/private/dispatch_wrappers%.h%.generated%.h$' ) then 41return 'nvim/api/private/dispatch.h' 42elseif non_static_fname : find ( '/include/ui_events_call%.h%.generated%.h$' ) then 43return 'nvim/ui.h' 44elseif non_static_fname : find ( '/include/ui_events_client%.h%.generated%.h$' ) then 45return 'nvim/ui_client.h' 46elseif non_static_fname : find ( '/include/ui_events_remote%.h%.generated%.h$' ) then 47return 'nvim/api/ui.h' 48end 49end 50 51--- @param d string 52local function process_decl ( d ) 53-- Comments are really handled by preprocessor, so the following is not 54-- needed 55d =d : gsub ( '/%*.-%*/' , '' ) 56d =d : gsub ( '//.-\n' , '\n' ) 57d =d : gsub ( '# .-\n' , '' ) 58d =d : gsub ( '\n' , ' ' ) 59d =d : gsub ( '%s+' , ' ' ) 60d =d : gsub ( ' ?%( ?' , '(' ) 61d =d : gsub ( ' ?, ?' , ', ' ) 62d =d : gsub ( ' ?(%*+) ?' , ' %1' ) 63d =d : gsub ( ' ?(FUNC_ATTR_)' , ' %1' ) 64d =d : gsub ( ' $' , '' ) 65d =d : gsub ( '^ ' , '' ) 66return d .. ';' 67end 68 69--- @param text string 70--- @return string[] static 71--- @return string[] non_static 72--- @return boolean any_static 73local function gen_declarations ( text ) 74local non_static ={} --- @type string[] 75local static ={} --- @type string[] 76 77local any_static =false 78for _ , node in ipairs ( grammar : match ( text )) do 79if node [ 1 ] == 'proto' then 80local node_text =text : sub ( node . pos , node . endpos - 1 ) 81local declaration =process_decl ( node_text ) 82 83if node . static then 84if not any_static and declaration : find ( 'FUNC_ATTR_' ) then 85any_static =true 86end 87static [ # static + 1 ] =declaration 88else 89non_static [ # non_static + 1 ] ='DLLEXPORT ' .. declaration 90end 91end 92end 93 94return static , non_static , any_static 95end 96 97local usage =[[ 98Usage: 99 100gen_declarations.lua definitions.c static.h non-static.h 101 102Generates declarations for a C file definitions.c, putting declarations for 103static functions into static.h and declarations for non-static functions into 104non-static.h. Also generate an IWYU comment. 105]] 106 107local function main () 108local fname =arg [ 1 ] 109local static_fname =arg [ 2 ] 110local non_static_fname =arg [ 3 ] 111local static_basename =arg [ 4 ] 112 113if fname == '--help' or # arg < 4 then 114( usage ) 115os . exit () 116end 117 118local text =assert ( read_file ( fname )) 119 120local static_decls , non_static_decls , any_static =gen_declarations ( text ) 121 122local static ={} --- @type string[] 123if fname : find ( '.*/src/nvim/.*%.h$' ) then 124static [ # static + 1 ] =( '// IWYU pragma: private, include "%s"' ): format ( 125fname : gsub ( '.*/src/nvim/' , 'nvim/' ) 126) 127end 128vim . list_extend ( static , { 129'#define DEFINE_FUNC_ATTRIBUTES' , 130'#include "nvim/func_attr.h"' , 131'#undef DEFINE_FUNC_ATTRIBUTES' , 132} ) 133vim . list_extend ( static , static_decls ) 134vim . list_extend ( static , { 135'#define DEFINE_EMPTY_ATTRIBUTES' , 136'#include "nvim/func_attr.h" // IWYU pragma: export' , 137'' , 138} ) 139 140write_file ( static_fname , static ) 141 142if any_static then 143local orig_text =assert ( read_file ( fname )) 144local pat ='\n#%s?include%s+"' .. static_basename .. '"\n' 145local pat_comment ='\n#%s?include%s+"' .. static_basename .. '"%s*//' 146if not orig_text : find ( pat ) and not orig_text : find ( pat_comment ) then 147error (( 'fail: missing include for %s in %s' ): format ( static_basename , fname )) 148end 149end 150 151if non_static_fname ~= 'SKIP' then 152local non_static ={} --- @type string[] 153local iwyu_non_static =add_iwyu_non_static ( fname , non_static_fname ) 154if iwyu_non_static then 155non_static [ # non_static + 1 ] =( '// IWYU pragma: private, include "%s"' ): format ( 156iwyu_non_static 157) 158end 159vim . list_extend ( non_static , { 160'#define DEFINE_FUNC_ATTRIBUTES' , 161'#include "nvim/func_attr.h"' , 162'#undef DEFINE_FUNC_ATTRIBUTES' , 163'#ifndef DLLEXPORT' , 164'# ifdef MSWIN' , 165'# define DLLEXPORT __declspec(dllexport)' , 166'# else' , 167'# define DLLEXPORT' , 168'# endif' , 169'#endif' , 170} ) 171vim . list_extend ( non_static , non_static_decls ) 172non_static [ # non_static + 1 ] ='#include "nvim/func_attr.h"' 173write_file ( non_static_fname , non_static ) 174end 175end 176 177return main ()