-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.lua
executable file
·144 lines (121 loc) · 4.41 KB
/
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env luajit
local arg = arg
local assert = assert
local print = print
local require = require
local tostring = tostring
local string = require("string")
local os = require("os")
----------
assert(arg[1], "Usage: "..arg[0].." <filename> ...")
local cl = require("ljclang")
arg[0] = nil
local tu = cl.createIndex():parse(arg, {"DetailedPreprocessingRecord"})
-- NOTE: we don't need to keep the Index_t reference around, test this.
collectgarbage()
if (tu == nil) then
print('TU is nil')
os.exit(1)
end
local cur = tu:cursor()
assert(cur==cur)
assert(cur ~= nil)
assert(cur:kindnum() == "CXCursor_TranslationUnit")
assert(cur:haskind("TranslationUnit"))
-- print("TU: "..cur:name()..", "..cur:displayName())
-- local fn = arg[1]:gsub(".*/","")
-- print(fn.." in TU: "..tu:file(fn)..", "..tu:file(arg[1]))
-- local diags = tu:diagnostics()
-- for i=1,#diags do
-- local d = diags[i]
-- print("diag "..i..": "..d.category..", "..d.text)
-- end
-- local V = cl.ChildVisitResult
-- local ourtab = {}
-- local visitor = cl.regCursorVisitor(
-- function(cur, parent)
-- ourtab[#ourtab+1] = cl.Cursor(cur)
-- if (cur:haskind("EnumConstantDecl")) then
-- print(string.format("%s: %d", cur:name(), cur:enumval()))
-- end
-- local isdef = (cur:haskind("FunctionDecl")) and cur:isDefinition()
-- -- print(string.format("[%3d] %50s <- %s", tonumber(cur:kindnum()), tostring(cur), tostring(parent)))
-- print(string.format("%3d [%12s%s] %50s <- %s %s", #ourtab, cur:kind(),
-- isdef and " (def)" or "", tostring(cur), tostring(parent), tostring(cur:type())))
-- if (cur:haskind("CXXMethod")) then
-- print("("..cur:access()..")")
-- end
-- return V.Continue
-- end)
-- cur:children(visitor)
function recurse(cur, indent, visited)
if cur:haskind("MacroExpansion") then
return
end
indent = indent or ''
local f, b, e = cur:location('offset')
local tag = string.format('%s:%d:%d', f or '?', b or 0, e or 0)
if visited then
if visited[tag] then
print(indent..'CYCLE')
return
end
visited[tag] = true
end
local isdef = (cur:haskind("FunctionDecl")) and cur:isDefinition()
local file, row, col, erow, ecol = cur:presumedLocation()
local type = cur:type()
print(string.format("%s[%12s%s] %50s <- %s <%s> (%s::%d:%d::%d:%d - %s)", indent, cur:kind(),
isdef and " (def)" or "", tostring(cur), tostring(type), type and tonumber(type:kindnum()),
file or '?', row or 0, col or 0, erow or 0, ecol or 0, tag))
if cur:haskind("TypedefDecl") then
local tdtype = cur:typedefType()
local name = tdtype:name()
local ptr, arr = name:match('^[^*]*([^[]*)(%[.*)')
local pre, post = '', ''
while true do
print(string.format("%sTypedef type: %s <%s>", indent..' ', tdtype, tonumber(tdtype:kindnum())))
print('postfix', ptr, arr)
if tdtype:haskind('ConstantArray') then
post = post .. '['..tdtype:arraySize()..']'
tdtype = tdtype:arrayElementType()
elseif tdtype:haskind('Pointer') then
local ptr = '*'
if tdtype:isConstQualified() then
ptr = ptr .. 'const '
end
pre = ptr .. pre
tdtype = tdtype:pointee()
else
break
end
end
local typedecl = tdtype:declaration()
if typedecl:haskind('StructDecl') then
if typedecl:name() ~= '' then
print('typedef struct '..typedecl:name()..' '..(ptr or '')..cur:name()..(arr or '')..';')
end
end
if not visited then
recurse(tdtype:declaration(), indent .. ' TYPEDEFTYPE-> ', { [tag] = true })
end
end
if cur:haskind("MacroDefinition") then
local indent = indent..' '
local toks = cur:_tokens()
if toks then
for k, v in pairs(toks) do
print(string.format("%s%s: %s", indent, k, v))
end
end
end
if cur:haskind("TypeRef") and not visited then
local visited = { [tag] = true }
recurse(cur:type():declaration(), indent .. ' TYPE-> ', visited)
end
local kids = cur:children()
for i, k in ipairs(kids) do
recurse(k, indent .. ' ', visited)
end
end
recurse(cur)