[flang] Treat a bare carriage return as whitespace outside literals (#206171)

Free-form (and fixed-form) source files authored or transferred on
Windows can contain a stray carriage return (0x0d or \r) in the interior
of a line, for example between a token and a trailing '&' continuation
marker:

    COMMON/CM1/<CR>       &

flang rejected such files with "bad character (0x0d) in Fortran token",
whereas the other compilers accept them. Carriage returns that form a
clean CRLF line ending were already tolerated; only a CR in the
significant part of a line tripped the error.

The fix is to skip over the stray <CR> outside of character literals.

Assisted-by: AI
This commit is contained in:
Eugene Epshteyn 2026-06-29 12:53:44 -04:00 committed by GitHub
parent 683ddb8369
commit 21725d26de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View file

@ -203,6 +203,10 @@ end
## Extensions, deletions, and legacy features supported by default
* Tabs in source
* A bare carriage return (CR, 0x0d) in the interior of a source line -- e.g.
from a file with Windows line endings that has been mishandled -- is treated
as a blank, except within a character or Hollerith literal where it is
retained.
* `<>` as synonym for `.NE.` and `/=`
* `$` and `@` as legal characters in names
* Initialization in type declaration statements using `/values/`

View file

@ -56,6 +56,8 @@ static inline int IsSpace(const char *p) {
return 1;
} else if (p[0] == '\xc2' && p[1] == '\xa0') { // UTF-8 NBSP
return 2;
} else if (*p == '\r') { // bare carriage return retained in line
return 1;
} else {
return 0;
}

View file

@ -0,0 +1,25 @@
! Tests that a bare carriage return (0x0d) appearing in the interior of a
! source line -- i.e. not as part of a CRLF/CR line ending -- is treated as
! insignificant whitespace outside of character literals, while still being
! preserved verbatim inside character literals.
!
! The carriage returns are generated with printf so that no literal CR byte is
! committed into the repository.
! A mid-line CR before a free-form '&' continuation must compile cleanly.
! RUN: printf 'program p\n integer :: i, j\n common /blk/\r &\n i, j\nend\n' > %t-free.f90
! RUN: %flang_fc1 -fsyntax-only %t-free.f90
! A mid-line CR in fixed-form source must compile cleanly.
! RUN: printf ' program p\n integer i, j\n common /blk/ i\r, j\n end\n' > %t-fixed.f
! RUN: %flang_fc1 -fsyntax-only -ffixed-form %t-fixed.f
! A CR inside a character literal must be preserved: the literal stays three
! characters long and its middle byte remains 0x0d (decimal 13). The quote
! characters around the literal are emitted with printf's octal escape \047 so
! the whole format string stays single-quoted and portable to lit's internal
! shell on all platforms.
! RUN: printf 'module m\n character(*), parameter :: s = \047a\rb\047\n integer, parameter :: slen = len(s)\n integer, parameter :: midval = iachar(s(2:2))\nend module\n' > %t-literal.f90
! RUN: %flang_fc1 -fdebug-unparse %t-literal.f90 | FileCheck %s
! CHECK: slen = 3_4
! CHECK: midval = 13_4