From 6dbcbe247e9670ce4979abd68eaa1b60301fc912 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 8 Aug 2026 00:33:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20vpi=20stub=20=E6=B3=A8=E5=85=A5=E6=9C=9F?= =?UTF-8?q?=E9=97=B4=E4=BF=AE=E6=AD=A3=E9=94=99=E8=AF=AF=E5=BD=92=E5=9B=A0?= =?UTF-8?q?=EF=BC=8C=E5=A4=9A=E7=BA=A7=E5=A0=86=E6=A0=88=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E4=B8=8A=E4=B8=8B=E6=96=87=E4=B8=8E=E8=A2=AB?= =?UTF-8?q?=E8=AF=BB=20stub=20=E7=9C=9F=E5=AE=9E=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/lib/StubGen/VpiReader.vp | 21 ++++++++++++++++++++- App/lib/core/Handles/HandlesType.vp | 9 ++++++++- App/lib/core/StubMerger.vp | 5 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/App/lib/StubGen/VpiReader.vp b/App/lib/StubGen/VpiReader.vp index 5f09873..b6361ce 100644 --- a/App/lib/StubGen/VpiReader.vp +++ b/App/lib/StubGen/VpiReader.vp @@ -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 diff --git a/App/lib/core/Handles/HandlesType.vp b/App/lib/core/Handles/HandlesType.vp index 0224440..e25de0c 100644 --- a/App/lib/core/Handles/HandlesType.vp +++ b/App/lib/core/Handles/HandlesType.vp @@ -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: diff --git a/App/lib/core/StubMerger.vp b/App/lib/core/StubMerger.vp index e18af2f..c1a8615 100644 --- a/App/lib/core/StubMerger.vp +++ b/App/lib/core/StubMerger.vp @@ -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)