[flang-commits] [flang] [flang] Accept C-style comments in label fields (PR #207012)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Thu Sep 17 15:46:02 PDT 2026


================
@@ -1424,18 +1469,43 @@ bool Prescanner::SkipCommentLine(bool afterAmpersand) {
   return false;
 }
 
-const char *Prescanner::FixedFormContinuationLine(bool atNewline) {
+const char *Prescanner::FixedFormContinuationLine(
+    bool atNewline, const char *&cComment, const char *&unterminatedCComment) {
+  cComment = nullptr;
+  unterminatedCComment = nullptr;
   if (IsAtEnd()) {
     return nullptr;
   }
   tabInCurrentLine_ = false;
   char col1{*nextLine_};
+  const char *afterWhiteSpace{SkipWhiteSpace(nextLine_)};
+  const char *afterCComment{nullptr};
+  if (preprocessingEnabled_ && IsCComment(afterWhiteSpace)) {
+    afterCComment = SkipCComment(afterWhiteSpace);
+    if (afterCComment == nullptr) {
+      unterminatedCComment = afterWhiteSpace;
+    } else {
+      cComment = afterWhiteSpace;
+    }
+  }
+  std::uint64_t maxLineLength{static_cast<std::uint64_t>(limit_ - nextLine_)};
+  std::uint64_t n{maxLineLength < 5 ? maxLineLength - 1 : 4};
+  int trailingSpaces{0};
+  for (std::uint64_t i{afterCComment
+               ? static_cast<std::uint64_t>(afterCComment - nextLine_)
+               : 1};
+      i <= n && nextLine_[i] == ' '; ++i) {
+    ++trailingSpaces;
+  }
+  bool cCommentAndSpaces{afterCComment &&
+      afterCComment - nextLine_ + trailingSpaces == 5 &&
----------------
eugeneepshteyn wrote:

(Interesting comment from AI agent)

This comparison mixes a byte offset with a column count, and a leading tab breaks it: `afterCComment - nextLine_` counts bytes, but a tab occupies one byte while advancing past column 1. For a continuation-candidate line starting `<TAB>/**/`, the sum is 5, `cCommentAndSpaces` is satisfied, and `col6 = nextLine_ + 5` lands on whatever the user wrote after the comment — which is then consumed as the continuation marker. The result is silent wrong code:

```console
$ printf '      integer :: x, y\n      y = 3\n      x = y\n\t/**/+2\n      print *, x\n      end\n' > tab-cont.f
$ flang -fc1 -cpp -fdebug-unparse tab-cont.f
 INTEGER x, y
  y=3_4
  x=__builtin_int(y2,kind=4)
 PRINT *, x
END PROGRAM
```

The user's `+` was eaten as the continuation marker and `2` was glued onto `y`, producing a reference to an undeclared, implicitly typed `y2` — with no diagnostic. Current main rejects this line (`error: expected an executable statement`). The `trailingSpaces` loop above only matches `' '`, and `HasTabInLabelField` — which this PR added for exactly this hazard on the label-field path — is not consulted here.

Suggested fix: also require the prefix to be tab-free, e.g. add `&& !HasTabInLabelField(nextLine_, limit_)` to this conjunction (ideally checked before the `SkipCComment` lookahead), and please add the reproducer above as a regression test.


https://github.com/llvm/llvm-project/pull/207012


More information about the flang-commits mailing list