You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Frama-C技术问询:如何获取起始行号与起始字符位置?

问题描述

我持有PdgTypes.Node.stmt,当前使用以下代码:

Format.printf "%a"
  Printer.pp_location (Cil_datatype.Stmt.loc PdgTypes.Node.stmt)

输出结果类似mkdir.c:500,但我需要包含起始字符位置的格式,比如mkdir.c:500:23,该如何实现?

解决方案

CIL中的location结构体本身包含了文件名、行号和起始字节位置(对应你要的字符位置),默认的Printer.pp_location只输出到行号,所以你可以直接提取这些字段手动格式化:

let loc = Cil_datatype.Stmt.loc PdgTypes.Node.stmt in
Format.printf "%s:%d:%d" loc.Cil.file loc.Cil.line loc.Cil.byte

如果需要处理部分特殊语句可能缺失完整位置信息的情况,可以加个简单的 fallback 判断:

let loc = Cil_datatype.Stmt.loc PdgTypes.Node.stmt in
match loc.Cil.file, loc.Cil.line, loc.Cil.byte with
| Some file, Some line, Some byte -> Format.printf "%s:%d:%d" file line byte
| _ -> Format.printf "%a" Printer.pp_location loc  (* 回退到默认格式 *)

内容的提问来源于stack exchange,提问作者BaiQi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 08:45:29