camfort / fortran-src

Fortran parsing and static analysis infrastructure
https://hackage.haskell.org/package/fortran-src
Other
44 stars 20 forks source link

Fix reformatting of 73 character long lines #251

Closed ksromanov closed 1 year ago

ksromanov commented 1 year ago

According to http://fortranwiki.org/fortran/show/Continuation+lines ampersand sign should be placed at column 73 and after, but not at column 72. So that & is ignored in fixed-form and is processed in free-form languages.

This patch fixes old behavior, which sometimes produced lines 73 characters long.

We (D. Beer, R. Hidalgo-Charman and myself) found this bug working on random FORTRAN code autogeneration tool. One of the examples of generated files with too long lines:

.........
      if (- (+ (+ (50 / 38)))) then
        x = + (((y - (z - 48)) * ((z + 62) - z)) + (((- z) * x) / (- x)))
        x = 53
        print *, 24
        print *, + (z - (+ (+ (+ 20))))
        x = + y
        x = + (+ x)
        x = + (42 - (((- 82) / (y - y)) / 45))
        print *, (((- (z / 86)) * ((y + y) - (44 + x))) / (- ((- y) - (-&
     & x)))) - z
        x = + z
      else if (z) then
.........
raehik commented 1 year ago

Sorry for the delay in looking at this. Yep, the lookahead I did meant that exactly 73-character lines wouldn't get split. Over-eager splitting doesn't happen here due to newline handling happening earlier. Thanks for the fix and links to docs! Happy to merge.