<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58069>58069</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Assertion failure in `clang::CFG::buildCFG` on `goto` into preceding block
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          creuter-rws
      </td>
    </tr>
</table>

<pre>
    When producing a Clang CFG from an AST via the C++ API function
`clang::CFG::buildCFG()`, I am getting an assertion failure when the
input source code contains a `goto` that jumps back into the body of
an `if` statement.  I have not confirmed that it is **only** an `if`
statement.

[Here](https://github.com/llvm/llvm-project/files/9678554/clang-tooling-crash.zip) is a small program that reproduces the issue.

The assertion message is `"L iterator is not reachable from F
iterator."` in function `LocalScope::const_iterator::distance()` in 
source file `clang/lib/Analysis/CFG.cpp` (around line 390).

Here is the troublesome input:

```C
struct bloop {int a; char *b; };

int foo(int a, int c, int d) {
    if (a) {
        struct bloop t1;
    lbl1:                   // destination
        t1.a = 42;
    }

    if (d)
        goto lbl1;          // Jump

    return c;
}
```

I've reproduced this using both LLVM 13.0.0 and a version built from
the main branch tip as of around a month and a half ago.

My guess is that an earlier pass is failing to create or complete the
target label, causing the later pass that updates forward labels to
also try for it.  This fails because the `goto` appears after the
destination, resulting in a invalid `distance()` result.

The attached small program uses the Clang libs to parse a small
program and print the generated CFG.  When built against Clang libs
with assertions enabled and run with the above example (named
`simple2.c` in the zip file), it dies with the assertion failure.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVU1v4zYQ_TXyZRBBlj918MFJkN0UWaDABu2xoCRK4i5NCiSVNP31fSPKsr05FBUEfZCcx5k3M4-lrT8Of3bSUO9sPVTKtCToQQu8H56-UOPsiYSh4_dXelOCQifpIcnvcdPx92dqBlMFZU2SPSbZMdlmFZsmqyNu2MePclC65r98n-Q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