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

Frama-C技术问询:如何获取循环结束标记}的位置

How to Get the Position of Loop Closing Braces in Code Analysis?

Got it, let's break this down. The issue you're hitting is super common when working with compiler intermediate representations (IRs)—syntactic elements like closing braces } often don't get represented as separate statements or instructions because they don't correspond to any executable logic. Here's how you can approach solving this:

First, let's restate your example code for clarity:

void kernel(int ni, int nj, int nk, float alpha, float *tmp, float *A, float *B) { 
    int i, j, k; 
    for (i = 0; i < ni; i++) { 
        for (j = 0; j < nj; j++) { 
            tmp[i * nj + j] = 0.0f; 
            for (k = 0; k < nk; ++k) { 
                tmp[i * nj + j] += alpha * A[i * nk + k] * B[k * nj + j]; 
            } 
        } 
    } 
}

Why Your Current Approaches Aren't Working

The reason you can't find the } in stmt.preds, stmt.succs, or stmt.bstmts is that these structures represent the control flow of the program, not its raw syntax. Closing braces don't alter the execution path—they just group code logically—so IRs (which focus on executable operations) discard them entirely. The get_all_stmt_last_stmts function returns break statements because those are actual control-flow instructions, not syntactic markers.

Solutions to Get the Brace Position

Here are three reliable ways to track down the closing brace's location:

  1. Work with the Source AST (Abstract Syntax Tree)
    Most code analysis tools that parse source code into an AST retain full syntactic metadata, including brace positions. For example, in Clang's AST, ForStmt nodes have a getLocEnd() method that directly returns the source location of the loop's closing brace. This is the most straightforward approach if your tool has access to the AST instead of just lower-level IR.

  2. Infer from the Loop Body's Last Statement (IR Workaround)
    If you're stuck using IR:

    • Fetch the list of statements inside the loop body via stmt.bstmts and take the final one.
    • Retrieve the end source position of that last statement (most IR nodes include debug metadata linking them to source locations).
    • The closing brace will be immediately after this position in the original source code.
  3. Use Source-to-IR Mapping Utilities
    Many tools offer utilities to map IR ranges back to source code regions. You can get the full range of the loop in IR (from the loop header to the last instruction in the body), then query the corresponding source region. From there, you can scan forward (or use the tool's built-in syntactic analysis) to locate the matching closing brace.

The core takeaway is that closing braces are syntactic sugar—they don't affect program execution, so IRs ignore them. To find their positions, you need to work with a representation that preserves syntactic details (like the AST) or infer their location from surrounding code elements that do have source position data.

内容的提问来源于stack exchange,提问作者R. Fomba

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:36:24