fix: vpi stub 注入期间修正错误归因,多级堆栈显示编译上下文与被读 stub 真实位置

This commit is contained in:
TermiNexus committed 2026-08-08 00:33:29 +08:00
1 parent 94ae2aee52
commit 6dbcbe247e
3 files changed
+33 -2

No files matched your search

+20 -1
View File
@@ -463,7 +463,26 @@ def ParseVpiIntoModule(pool: memhub.MemBuddy | t.CPtr,
mod: llvmlite.LLVMModule | t.CPtr,
vpi_path: str,
trans: HT.Translator | t.CPtr) -> int:
"""解析 .vpi 文件,将函数声明注入到当前模块"""
"""解析 .vpi 文件,将函数声明注入到当前模块
解析 stub 期间临时将错误归因切换到被读 stub 文件(.vpi):
注解解析失败时 fatal_error 应指向 stub 及其行号(真实出错位置),
而非当前编译模块(错误归因修复)。
"""
if pool is None or mod is None or vpi_path is None or trans is None:
return 0
saved_cur: str = HandlesType.get_current_file()
HandlesType.set_current_file(vpi_path)
ret: int = _ParseVpiIntoModuleImpl(pool, mod, vpi_path, trans)
HandlesType.set_current_file(saved_cur)
return ret
def _ParseVpiIntoModuleImpl(pool: memhub.MemBuddy | t.CPtr,
mod: llvmlite.LLVMModule | t.CPtr,
vpi_path: str,
trans: HT.Translator | t.CPtr) -> int:
"""解析 .vpi 文件,将函数声明注入到当前模块(ParseVpiIntoModule 实际实现)"""
if pool is None or mod is None or vpi_path is None or trans is None:
return 0
+8 -1
View File
@@ -745,6 +745,11 @@ def set_current_file(file_path: str):
_g_current_file = file_path
def get_current_file() -> str:
"""获取当前编译的文件名(供报错归因使用)"""
return _g_current_file
# ============================================================
# _read_source_line - 从源文件读取第 lineno 行
#
@@ -823,12 +828,14 @@ def fatal_error(node: ast.AST | t.CPtr, msg: str,
for i in range(_g_trace_count):
frame: TraceFrame | t.CPtr = _get_trace_frame(i)
if frame is not None and frame.file_name is not None:
stdio.printf(' File "%s", line %d\n', frame.file_name, frame.lineno)
if frame.lineno > 0:
stdio.printf(' File "%s", line %d\n', frame.file_name, frame.lineno)
src: str = _read_source_line(frame.file_name, frame.lineno)
if src is not None:
stdio.printf(" %s\n", src)
stdlib.free(src)
else:
stdio.printf(' File "%s"\n', frame.file_name)
# 输出当前帧(node 对应的位置)
lineno: int = 0
if node is not None:
+5
View File
@@ -15,6 +15,7 @@ import lib.core.IncludesScanner as IncludesScanner
import lib.core.Handles.HandlesStruct as HandlesStruct
import lib.core.Handles.HandlesImports as HandlesImports
import lib.core.Handles.HandlesTranslator as HandlesTranslator
import lib.core.Handles.HandlesType as HandlesType
import lib.core.BuildPipeline as BuildPipeline
import lib.StubGen.VpiReader as VpiReader
import lib.Projectrans.Utils as Utils
@@ -2084,7 +2085,11 @@ def LoadDepVpiIntoModule(pool: memhub.MemBuddy | t.CPtr,
if not vf.closed:
vf.close()
# 调用 VpiReader 解析 .vpi 并注入 declare
# 错误归因:push 当前编译模块帧,stub 解析期间若 fatal_error,
# 多级堆栈显示 当前模块 → 被读 stub 文件(真实出错位置)
HandlesType.push_trace(HandlesType.get_current_file(), 0)
ret: int = VpiReader.ParseVpiIntoModule(pool, mod, vpi_path, trans)
HandlesType.pop_trace()
if ret == 1:
success_count += 1
stdlib.free(vpi_path)