Skip to content

Commit acf091c

Browse files
committed
fix output error
1 parent e0c2f03 commit acf091c

File tree

2 files changed

+90
-20
lines changed

2 files changed

+90
-20
lines changed

script/plugins/ffi/init.lua

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
local searchCode = require 'plugins.ffi.searchCode'
2-
local cdefRerence = require 'plugins.ffi.cdefRerence'
3-
local cdriver = require 'plugins.ffi.c-parser.cdriver'
4-
local util = require 'plugins.ffi.c-parser.util'
5-
local utility = require 'utility'
6-
local SDBMHash = require 'SDBMHash'
7-
local config = require 'config'
8-
local fs = require 'bee.filesystem'
9-
local scope = require 'workspace.scope'
1+
local searchCode = require 'plugins.ffi.searchCode'
2+
local cdefRerence = require 'plugins.ffi.cdefRerence'
3+
local cdriver = require 'plugins.ffi.c-parser.cdriver'
4+
local util = require 'plugins.ffi.c-parser.util'
5+
local utility = require 'utility'
6+
local SDBMHash = require 'SDBMHash'
7+
local config = require 'config'
8+
local fs = require 'bee.filesystem'
9+
local scope = require 'workspace.scope'
1010

11-
local namespace <const> = 'ffi.namespace*.'
11+
local namespace <const> = 'ffi.namespace*.'
1212

1313
--TODO:supprot 32bit ffi, need config
14-
local knownTypes = {
14+
local knownTypes = {
1515
["bool"] = 'boolean',
1616
["char"] = 'integer',
1717
["short"] = 'integer',
@@ -60,10 +60,33 @@ local knownTypes = {
6060
["signedlong"] = 'integer',
6161
}
6262

63-
local constName <const> = 'm'
63+
local blackKeyWord <const> = {
64+
['and'] = "_and",
65+
['do'] = "_do",
66+
['elseif'] = "_elseif",
67+
['end'] = "_end",
68+
['false'] = "_false",
69+
['function'] = "_function",
70+
['in'] = "_in",
71+
['local'] = "_local",
72+
['nil'] = "_nil",
73+
['not'] = "_not",
74+
['or'] = "_or",
75+
['repeat'] = "_repeat",
76+
['then'] = "_then",
77+
['true'] = "_true",
78+
}
79+
80+
local invaildKeyWord <const> = {
81+
const = true,
82+
restrict = true,
83+
volatile = true,
84+
}
85+
86+
local constName <const> = 'm'
6487

6588
---@class ffi.builder
66-
local builder = { switch_ast = utility.switch() }
89+
local builder = { switch_ast = utility.switch() }
6790

6891
function builder:getTypeAst(name)
6992
for i, asts in ipairs(self.globalAsts) do
@@ -95,14 +118,22 @@ function builder:getType(name)
95118
if type(name) == 'table' then
96119
local t = ""
97120
local isStruct
121+
if name.type then
122+
t = t .. name.type .. "@"
123+
name = name.name
124+
end
98125
for _, n in ipairs(name) do
99126
if type(n) == 'table' then
100127
n = n.full_name
101128
end
129+
if invaildKeyWord[n] then
130+
goto continue
131+
end
102132
if not isStruct then
103133
isStruct = self:needDeref(self:getTypeAst(n))
104134
end
105135
t = t .. n
136+
::continue::
106137
end
107138
-- deref 一级指针
108139
if isStruct and t:sub(#t) == '*' then
@@ -142,11 +173,15 @@ local function getArrayType(arr)
142173
return res
143174
end
144175

176+
local function getValidName(name)
177+
return blackKeyWord[name] or name
178+
end
179+
145180
function builder:buildStructOrUnion(lines, tt, name)
146181
lines[#lines+1] = '---@class ' .. self:getType(name)
147182
for _, field in ipairs(tt.fields or {}) do
148183
if field.name and field.type then
149-
lines[#lines+1] = ('---@field %s %s%s'):format(field.name, self:getType(field.type),
184+
lines[#lines+1] = ('---@field %s %s%s'):format(getValidName(field.name), self:getType(field.type),
150185
getArrayType(field.isarray))
151186
end
152187
end
@@ -155,8 +190,9 @@ end
155190
function builder:buildFunction(lines, tt, name)
156191
local param_names = {}
157192
for i, param in ipairs(tt.params or {}) do
158-
lines[#lines+1] = ('---@param %s %s%s'):format(param.name, self:getType(param.type), getArrayType(param.idxs))
159-
param_names[#param_names+1] = param.name
193+
local param_name = getValidName(param.name)
194+
lines[#lines+1] = ('---@param %s %s%s'):format(param_name, self:getType(param.type), getArrayType(param.idxs))
195+
param_names[#param_names+1] = param_name
160196
end
161197
if tt.vararg then
162198
param_names[#param_names+1] = '...'
@@ -175,7 +211,7 @@ function builder:buildTypedef(lines, tt, name)
175211
-- 这个时候没有主类型,只有一个别名,直接创建一个别名结构体
176212
self.switch_ast(def.type, self, lines, def, name)
177213
else
178-
lines[#lines+1] = ('---@alias %s %s'):format(name, self:getType(def))
214+
lines[#lines+1] = ('---@alias %s %s'):format(self:getType(name), self:getType(def))
179215
end
180216
end
181217

test/plugins/ffi/builder.lua

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,44 @@ function TEST(wanted)
3939
end
4040
end
4141

42+
TEST [[
43+
---@alias ffi.namespace*.EVP_MD ffi.namespace*.struct@env_md_st
44+
45+
---@return ffi.namespace*.EVP_MD
46+
function m.EVP_md5() end
47+
]] [[
48+
typedef struct env_md_st EVP_MD;
49+
const EVP_MD *EVP_md5(void);
50+
]]
51+
52+
TEST [[
53+
---@class ffi.namespace*.struct@a
54+
---@field _in integer
55+
]] [[
56+
struct a {
57+
int in;
58+
};
59+
]]
60+
61+
TEST [[
62+
---@param _in integer
63+
function m.test(_in) end
64+
]] [[
65+
void test(int in);
66+
]]
67+
68+
TEST [[
69+
---@alias ffi.namespace*.ENGINE ffi.namespace*.struct@engine_st
70+
---@alias ffi.namespace*.ENGINE1 ffi.namespace*.enum@engine_st1
71+
]] [[
72+
typedef struct engine_st ENGINE;
73+
typedef enum engine_st1 ENGINE1;
74+
]]
75+
4276
TEST [[
4377
---@param a integer[][]
4478
function m.test(a) end
45-
]][[
79+
]] [[
4680
void test(int a[][]);
4781
]]
4882

@@ -76,7 +110,7 @@ TEST [[
76110
m.A = 1
77111
m.C = 5
78112
---@alias ffi.namespace*.enum@a 1 | 2 | 'B' | 'A' | 5 | 'C'
79-
]][[
113+
]] [[
80114
enum a {
81115
A = 1,
82116
B = 2,
@@ -165,7 +199,7 @@ TEST [[
165199
]]
166200

167201
TEST [[
168-
---@alias H ffi.namespace*.void
202+
---@alias ffi.namespace*.H ffi.namespace*.void
169203
170204
function m.test() end
171205
]] [[

0 commit comments

Comments
 (0)