robin/laufey

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

Justin M. KeyesMerge #38117 from noib3/fix-lua-lints-in-srcdce8bdd247

master
5.1 KiB177 linesraw
1local grammar = require('gen.c_grammar').grammar
2
3--- @param fname string
4--- @return string?
5local function read_file(fname)
6  local f = io.open(fname, 'r')
7  if not f then
8    return
9  end
10  local contents = f:read('*a')
11  f:close()
12  return contents
13end
14
15--- @param fname string
16--- @param contents string[]
17local function write_file(fname, contents)
18  local contents_s = table.concat(contents, '\n') .. '\n'
19  local fcontents = read_file(fname)
20  if fcontents == contents_s then
21    return
22  end
23  local f = assert(io.open(fname, 'w'))
24  f:write(contents_s)
25  f: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)
32  if fname:find('.*/src/nvim/.*%.c$') then
33    -- Add an IWYU pragma comment if the corresponding .h file exists.
34    local header_fname = fname:sub(1, -3) .. '.h'
35    local header_f = io.open(header_fname, 'r')
36    if header_f then
37      header_f:close()
38      return (header_fname:gsub('.*/src/nvim/', 'nvim/'))
39    end
40  elseif non_static_fname:find('/include/api/private/dispatch_wrappers%.h%.generated%.h$') then
41    return 'nvim/api/private/dispatch.h'
42  elseif non_static_fname:find('/include/ui_events_call%.h%.generated%.h$') then
43    return 'nvim/ui.h'
44  elseif non_static_fname:find('/include/ui_events_client%.h%.generated%.h$') then
45    return 'nvim/ui_client.h'
46  elseif non_static_fname:find('/include/ui_events_remote%.h%.generated%.h$') then
47    return 'nvim/api/ui.h'
48  end
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
55  d = d:gsub('/%*.-%*/', '')
56  d = d:gsub('//.-\n', '\n')
57  d = d:gsub('# .-\n', '')
58  d = d:gsub('\n', ' ')
59  d = d:gsub('%s+', ' ')
60  d = d:gsub(' ?%( ?', '(')
61  d = d:gsub(' ?, ?', ', ')
62  d = d:gsub(' ?(%*+) ?', ' %1')
63  d = d:gsub(' ?(FUNC_ATTR_)', ' %1')
64  d = d:gsub(' $', '')
65  d = d:gsub('^ ', '')
66  return 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)
74  local non_static = {} --- @type string[]
75  local static = {} --- @type string[]
76
77  local any_static = false
78  for _, node in ipairs(grammar:match(text)) do
79    if node[1] == 'proto' then
80      local node_text = text:sub(node.pos, node.endpos - 1)
81      local declaration = process_decl(node_text)
82
83      if node.static then
84        if not any_static and declaration:find('FUNC_ATTR_') then
85          any_static = true
86        end
87        static[#static + 1] = declaration
88      else
89        non_static[#non_static + 1] = 'DLLEXPORT ' .. declaration
90      end
91    end
92  end
93
94  return static, non_static, any_static
95end
96
97local usage = [[
98Usage:
99
100    gen_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()
108  local fname = arg[1]
109  local static_fname = arg[2]
110  local non_static_fname = arg[3]
111  local static_basename = arg[4]
112
113  if fname == '--help' or #arg < 4 then
114    print(usage)
115    os.exit()
116  end
117
118  local text = assert(read_file(fname))
119
120  local static_decls, non_static_decls, any_static = gen_declarations(text)
121
122  local static = {} --- @type string[]
123  if fname:find('.*/src/nvim/.*%.h$') then
124    static[#static + 1] = ('// IWYU pragma: private, include "%s"'):format(
125      fname:gsub('.*/src/nvim/', 'nvim/')
126    )
127  end
128  vim.list_extend(static, {
129    '#define DEFINE_FUNC_ATTRIBUTES',
130    '#include "nvim/func_attr.h"',
131    '#undef DEFINE_FUNC_ATTRIBUTES',
132  })
133  vim.list_extend(static, static_decls)
134  vim.list_extend(static, {
135    '#define DEFINE_EMPTY_ATTRIBUTES',
136    '#include "nvim/func_attr.h"  // IWYU pragma: export',
137    '',
138  })
139
140  write_file(static_fname, static)
141
142  if any_static then
143    local orig_text = assert(read_file(fname))
144    local pat = '\n#%s?include%s+"' .. static_basename .. '"\n'
145    local pat_comment = '\n#%s?include%s+"' .. static_basename .. '"%s*//'
146    if not orig_text:find(pat) and not orig_text:find(pat_comment) then
147      error(('fail: missing include for %s in %s'):format(static_basename, fname))
148    end
149  end
150
151  if non_static_fname ~= 'SKIP' then
152    local non_static = {} --- @type string[]
153    local iwyu_non_static = add_iwyu_non_static(fname, non_static_fname)
154    if iwyu_non_static then
155      non_static[#non_static + 1] = ('// IWYU pragma: private, include "%s"'):format(
156        iwyu_non_static
157      )
158    end
159    vim.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    })
171    vim.list_extend(non_static, non_static_decls)
172    non_static[#non_static + 1] = '#include "nvim/func_attr.h"'
173    write_file(non_static_fname, non_static)
174  end
175end
176
177return main()