Skip to content

Commit a69a12d

Browse files
committed
Cleanup references when analyzing multiple times (for LSPs)
1 parent b334929 commit a69a12d

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

lualib/nelua/analyzer.lua

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ local analyzer = {}
2020
local luatype = type
2121

2222
local primtypes = typedefs.primtypes
23+
local builtin_attrs = typedefs.builtin_attrs
24+
local orig_primtypes = {}
25+
local orig_builtin_attrs = {}
26+
tabler.updatecopymt(orig_primtypes, primtypes)
27+
tabler.updatecopymt(orig_builtin_attrs, builtin_attrs)
28+
2329
local visitors = {}
2430
analyzer.visitors = visitors
2531

@@ -3392,6 +3398,19 @@ function visitors.BinaryOp(context, node, opts)
33923398
end
33933399

33943400
function analyzer.analyze(context)
3401+
-- this is necessary to support calling analyzer multiple times (eg in LSPs),
3402+
-- previous analyzer may have filled references in builtin symbols and primtypes
3403+
-- so we cleanup before
3404+
for k,v in pairs(orig_primtypes) do
3405+
local primtype = tabler.mirror(primtypes[k], v)
3406+
if primtype.metafields then
3407+
tabler.clear(primtype.metafields)
3408+
end
3409+
end
3410+
for k,v in pairs(orig_builtin_attrs) do
3411+
tabler.mirror(builtin_attrs[k], v)
3412+
end
3413+
33953414
-- save current analyzing context
33963415
local old_current_context = analyzer.current_context
33973416
analyzer.current_context = context

lualib/nelua/astnode.lua

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ function ASTNode._create(mt, ...)
9393
}, mt)
9494
end
9595

96+
-- Creates unique id counter of AST nodes.
97+
function ASTNode.reset_uid_counter()
98+
uid = 0
99+
end
100+
96101
-- Allows calling ASTNode to create a new node.
97102
getmetatable(ASTNode).__call = ASTNode._create
98103

lualib/nelua/cdefs.lua

+8-8
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ compilers_flags.cc = {
9191
ext = '.c',
9292
}
9393
-- GCC
94-
compilers_flags.gcc = tabler.updatecopy(compilers_flags.cc, {
94+
compilers_flags.gcc = tabler.copyupdate(compilers_flags.cc, {
9595
cflags_base = "-fwrapv -fno-strict-aliasing",
9696
cflags_sanitize = "-Wall -Wextra -fsanitize=address,undefined",
9797
cflags_devel = "-g",
@@ -107,42 +107,42 @@ compilers_flags.gcc = tabler.updatecopy(compilers_flags.cc, {
107107
cmd_defines = '$(cc) -E -dM -x c "$(cfile)" -x none $(cflags)',
108108
})
109109
-- Emscripten CC
110-
compilers_flags.emcc = tabler.updatecopy(compilers_flags.gcc, {
110+
compilers_flags.emcc = tabler.copyupdate(compilers_flags.gcc, {
111111
cflags_release = "-Oz -DNDEBUG",
112112
cflags_maximum_performance = "-O3 -ffast-math -DNDEBUG -fno-plt -flto",
113113
})
114114
-- Clang
115-
compilers_flags.clang = tabler.updatecopy(compilers_flags.gcc, {
115+
compilers_flags.clang = tabler.copyupdate(compilers_flags.gcc, {
116116
cmd_compile = '$(cc) -x c "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags) -o "$(binfile)"',
117117
cmd_info = '$(cc) -E -x c "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags)',
118118
cmd_defines = '$(cc) -E -dM -x c "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags)',
119119
})
120120
-- TCC
121-
compilers_flags.tcc = tabler.updatecopy(compilers_flags.cc, {
121+
compilers_flags.tcc = tabler.copyupdate(compilers_flags.cc, {
122122
cflags_base = "-w",
123123
cflags_devel = "-g",
124124
cflags_debug = "-g",
125125
})
126126
-- C2M
127-
compilers_flags.c2m = tabler.updatecopy(compilers_flags.cc, {
127+
compilers_flags.c2m = tabler.copyupdate(compilers_flags.cc, {
128128
cflags_base = "-w",
129129
cflags_shared_lib = "-c",
130130
})
131131
-- GCC (C++)
132-
compilers_flags['g++'] = tabler.updatecopy(compilers_flags.gcc, {
132+
compilers_flags['g++'] = tabler.copyupdate(compilers_flags.gcc, {
133133
cmd_compile = '$(cc) -x c++ "$(cfile)" -x none $(cflags) -o "$(binfile)"',
134134
cmd_info = '$(cc) -E -x c++ "$(cfile)" -x none $(cflags)',
135135
cmd_defines = '$(cc) -E -dM -x c++ "$(cfile)" -x none $(cflags)',
136136
ext = '.cpp',
137137
})
138138
-- Clang (C++)
139-
compilers_flags['clang++'] = tabler.updatecopy(compilers_flags['g++'], {
139+
compilers_flags['clang++'] = tabler.copyupdate(compilers_flags['g++'], {
140140
cmd_compile = '$(cc) -x c++ "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags) -o "$(binfile)"',
141141
cmd_info = '$(cc) -E -x c++ "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags)',
142142
cmd_defines = '$(cc) -E -dM -x c++ "$(cfile)" -x none -Wno-unused-command-line-argument $(cflags)',
143143
})
144144
-- NVCC (CUDA C++)
145-
compilers_flags['nvcc'] = tabler.updatecopy(compilers_flags.gcc, {
145+
compilers_flags['nvcc'] = tabler.copyupdate(compilers_flags.gcc, {
146146
cflags_base = "",
147147
cmd_compile = '$(cc) -x cu "$(cfile)" $(cflags) -o "$(binfile)"',
148148
cmd_info = '$(cc) -E -x cu "$(cfile)" $(cflags)',

lualib/nelua/utils/tabler.lua

+17-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,23 @@ function tabler.copy(t)
6060
return ot
6161
end
6262

63+
-- Shallow copy for table (including metatables).
64+
function tabler.copymt(t)
65+
local ot = {}
66+
for i,v in next,t do
67+
ot[i] = v
68+
end
69+
return setmetatable(ot, getmetatable(t))
70+
end
71+
72+
-- Update a table with shallow copy of children (including metatables).
73+
function tabler.updatecopymt(dest, src)
74+
for k,v in pairs(src) do dest[k] = tabler.copymt(v) end
75+
return dest
76+
end
77+
6378
-- Shallow copy a table and update its elements.
64-
function tabler.updatecopy(s,t)
79+
function tabler.copyupdate(s, t)
6580
return tabler.update(tabler.copy(s), t)
6681
end
6782

@@ -113,10 +128,7 @@ function tabler.mirror(dst, src)
113128
if rawequal(dst, src) then
114129
return
115130
end
116-
setmetatable(dst, nil)
117-
tabler.clear(dst)
118-
tabler.update(dst, src)
119-
setmetatable(dst, getmetatable(src))
131+
return setmetatable(tabler.update(tabler.clear(setmetatable(dst, nil)), src), getmetatable(src))
120132
end
121133

122134
return tabler

0 commit comments

Comments
 (0)