llvm / llvm/llvm-project

[flang] CookedSource::GetSourcePositionRange returns nullopt for labeled-CONTINUE DO terminator

Open
#196,291 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

When the last executable statement of a procedure is a labeled `CONTINUE` that terminates a label-DO construct, or any labeled `continue`, `Fortran::parser::CookedSource::GetSourcePositionRange()` returns `std::nullopt` for that statement's `CharBlock source`.

### AI Disclosure

I used Claude Code to help me diagnose this issue, generate an MWE, and file this issue. I have read through everything and can confirm that it appears to be accurate.

### Reproducer

`label_do_at_end.f90`:
```fortran
subroutine label_do_at_end(n, total)
implicit none
integer, intent(in) :: n
integer, intent(out) :: total
integer :: i
total = 0
do 10 i = 1, n
total = total + i
10 continue
end subroutine label_do_at_end
```

A flang frontend plugin walks the parse tree and calls `parsing->allCooked().GetSourcePositionRange(stmt.source)` on each `Fortran::parser::Statement` in the procedure's execution-part block. For the labeled `10 continue` at the end of the body, the call returns nullopt.

### Expected

`GetSourcePositionRange` returns a valid `(begin, end)` `SourcePosition` pair, since the statement appears in the original source.

### Observed

Returns `std::nullopt`.

### Versions tested

- flang-new 19.1.5 (`ab4b5a2db582958af1ee308a790cfdb42bd24720`)
- flang-new 22.1.4 (Homebrew)

The reproducer compiles, links, and runs cleanly with both `flang-new` and `gfortran`.

### Context

Encountered while building [SALT](https://github.com/ParaToolsInc/salt), a Fortran source-to-source instrumentor. Tracking: https://github.com/ParaToolsInc/salt/issues/31.

We have implemented a work around, which may even be superior (getting the last line of the enclosing program unit and the contains statement, if one is present) but this was unexpected behavior and would be nice to have an ending line number for the executable statement even in this case.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.