[flang-commits] [flang] a5320f8 - [flang] Allow C style comments in continuation lines (#214336)

via flang-commits flang-commits at lists.llvm.org
Fri Aug 28 06:06:35 PDT 2026


Author: Leandro Lupori
Date: 2026-08-28T10:06:30-03:00
New Revision: a5320f84aabb530b941cea3af07427e001302b6e

URL: https://github.com/llvm/llvm-project/commit/a5320f84aabb530b941cea3af07427e001302b6e
DIFF: https://github.com/llvm/llvm-project/commit/a5320f84aabb530b941cea3af07427e001302b6e.diff

LOG: [flang] Allow C style comments in continuation lines (#214336)

With this change, C-style comments in continuation lines are accepted
and treated as space.

This causes some previously-accepted code to be rejected now, such as
`/* c */ &`, which is now equivalent to ` &`, already invalid before.

Known limitation / follow-up: comment-only continuation lines (e.g.
`i&`, `/* c */`, `= 5`) are now accepted with no `-pedantic` warning,
whereas comments with code on the same line still warn. This is
consistent with the long-standing behavior for standalone comment
lines, which have never warned either. A uniform fix in the
comment-line classification path would be good follow-up material,
but is out of scope for this PR.

Fixes #129455

Added: 
    flang/test/Parser/continuation-before-c-comment01.f90
    flang/test/Parser/continuation-before-c-comment02.f90

Modified: 
    flang/lib/Parser/prescan.cpp
    flang/lib/Parser/prescan.h

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 95d1febd68b98..db4b50a492002 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -630,11 +630,7 @@ void Prescanner::SkipCComments() {
   while (true) {
     if (IsCComment(at_)) {
       if (const char *after{SkipCComment(at_)}) {
-        column_ += after - at_;
-        // May have skipped over one or more newlines; relocate the start of
-        // the next line.
-        nextLine_ = at_ = after;
-        NextLine();
+        UpdateSourcePositionAfterSkip(after);
       } else {
         // Don't emit any messages about unclosed C-style comments, because
         // the sequence /* can appear legally in a FORMAT statement.  There's
@@ -650,6 +646,13 @@ void Prescanner::SkipCComments() {
   }
 }
 
+void Prescanner::WarnCComment(const char *at) {
+  if (features_.ShouldWarn(LanguageFeature::ClassicCComments)) {
+    Say(LanguageFeature::ClassicCComments, GetProvenance(at),
+        "nonstandard usage: C-style comment"_port_en_US);
+  }
+}
+
 void Prescanner::SkipSpaces() {
   while (IsSpaceOrTab(at_)) {
     NextChar();
@@ -664,11 +667,31 @@ const char *Prescanner::SkipWhiteSpace(const char *p) {
   return p;
 }
 
+// If `cComment` is non-null, C-style comments are skipped, and `*cComment`
+// is set to point to the first C-style comment found, or to null if none
+// were found.
+// If `cComment` is null, C-style comments are not skipped.
+//
+// The returned first C-style comment is used to emit a warning if that
+// comment is actually consumed. This is consistent with how C-style
+// comment warnings are emitted elsewhere: only the first one is warned
+// about, then SkipCComments is called, which may skip multiple C-style
+// comments.
 const char *Prescanner::SkipWhiteSpaceIncludingEmptyMacros(
-    const char *p) const {
+    const char *p, const char **cComment) const {
+  const char *firstCComment{nullptr};
   while (true) {
     if (int n{IsSpaceOrTab(p)}) {
       p += n;
+    } else if (cComment && IsCComment(p)) {
+      if (const char *after{SkipCComment(p)}) {
+        if (!firstCComment) {
+          firstCComment = p;
+        }
+        p = after;
+      } else {
+        break;
+      }
     } else if (preprocessor_.AnyDefinitions() && IsLegalIdentifierStart(*p)) {
       // Skip keyword macros with empty definitions
       const char *q{p + 1};
@@ -685,6 +708,9 @@ const char *Prescanner::SkipWhiteSpaceIncludingEmptyMacros(
       break;
     }
   }
+  if (cComment) {
+    *cComment = firstCComment;
+  }
   return p;
 }
 
@@ -718,6 +744,26 @@ const char *Prescanner::SkipCComment(const char *p) const {
   return p;
 }
 
+// When skipping over C-style comments, one or more newlines may be skipped.
+// Adjust current position, column, and next line in source.
+void Prescanner::UpdateSourcePositionAfterSkip(const char *after) {
+  if (at_ >= after) {
+    return;
+  }
+  const char *nl{after - 1};
+  while (nl > at_ && *nl != '\n') {
+    --nl;
+  }
+  if (*nl == '\n' && after >= nextLine_) {
+    column_ = after - nl;
+    nextLine_ = nl + 1;
+    NextLine();
+  } else {
+    column_ += after - at_;
+  }
+  at_ = after;
+}
+
 bool Prescanner::NextToken(TokenSequence &tokens) {
   CHECK(at_ >= start_ && at_ < limit_);
   if (InFixedFormSource() && !preprocessingOnly_) {
@@ -726,10 +772,7 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
     if (*at_ == '/' && IsCComment(at_)) {
       // Recognize and skip over classic C style /*comments*/ when
       // outside a character literal.
-      if (features_.ShouldWarn(LanguageFeature::ClassicCComments)) {
-        Say(LanguageFeature::ClassicCComments, GetCurrentProvenance(),
-            "nonstandard usage: C-style comment"_port_en_US);
-      }
+      WarnCComment(at_);
       SkipCComments();
     }
     if (IsSpaceOrTab(at_)) {
@@ -1355,6 +1398,7 @@ bool Prescanner::SkipCommentLine(bool afterAmpersand) {
   } else {
     auto lineClass{ClassifyLine(nextLine_)};
     if (lineClass.kind == LineClassification::Kind::Comment) {
+      nextLine_ += lineClass.payloadOffset; // advance to '!' or newline
       NextLine();
       return true;
     } else if (lineClass.kind ==
@@ -1479,13 +1523,9 @@ constexpr bool IsDirective(const char *match, const char *dir) {
   return true;
 }
 
-const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
+const char *Prescanner::GetFreeFormContinuationLine(
+    bool ampersand, const char *p) {
   const char *lineStart{nextLine_};
-  const char *p{lineStart};
-  if (p >= limit_) {
-    return nullptr;
-  }
-  p = SkipWhiteSpaceIncludingEmptyMacros(p);
   if (InCompilerDirective()) {
     if (InConditionalLine()) {
       if (preprocessingOnly_) {
@@ -1571,6 +1611,21 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
   }
 }
 
+const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
+  const char *lineStart{nextLine_};
+  const char *p{lineStart};
+  if (p >= limit_) {
+    return nullptr;
+  }
+  const char *cComment;
+  p = SkipWhiteSpaceIncludingEmptyMacros(lineStart, &cComment);
+  p = GetFreeFormContinuationLine(ampersand, p);
+  if (p && cComment) {
+    WarnCComment(cComment);
+  }
+  return p;
+}
+
 bool Prescanner::FixedFormContinuation(bool atNewline) {
   // N.B. We accept '&' as a continuation indicator in fixed form, too,
   // but not in a character literal.
@@ -1609,8 +1664,8 @@ bool Prescanner::FreeFormContinuation() {
   }
   do {
     if (const char *cont{FreeFormContinuationLine(ampersand)}) {
-      BeginSourceLine(cont);
-      NextLine();
+      UpdateSourcePositionAfterSkip(cont);
+      tabInCurrentLine_ = false;
       return true;
     }
   } while (SkipCommentLine(ampersand));
@@ -1720,7 +1775,8 @@ Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
 
 std::optional<Prescanner::LineClassification>
 Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
-  if (const char *p{SkipWhiteSpaceIncludingEmptyMacros(start)};
+  if (const char *p{
+          SkipWhiteSpaceIncludingEmptyMacros(start, /*cComment=*/nullptr)};
       p && *p == '!') {
     if (auto lnClass{IsCompilerDirectiveSentinelAfterKeywordMacro(p + 1)}) {
       if (lnClass->kind == LineClassification::Kind::CompilerDirective) {
@@ -1962,7 +2018,7 @@ bool Prescanner::CompilerDirectiveContinuation(
   if (nextContinuation) {
     // What follows is !DIR$ & xxx; skip over the & so that it
     // doesn't cause a spurious continuation.
-    at_ = nextContinuation;
+    UpdateSourcePositionAfterSkip(nextContinuation);
   } else {
     // What follows looks like a source line before macro expansion,
     // but might become a directive continuation afterwards.
@@ -2018,10 +2074,9 @@ bool Prescanner::SourceLineContinuation(TokenSequence &tokens) {
       NextLine();
       return true;
     } else if (const char *nextContinuation{FreeFormContinuationLine(true)}) {
-      BeginSourceLine(nextLine_);
-      NextLine();
+      UpdateSourcePositionAfterSkip(nextContinuation);
+      tabInCurrentLine_ = false;
       TokenSequence followingTokens;
-      at_ = nextContinuation;
       while (NextToken(followingTokens)) {
       }
       if (auto followingPrepro{

diff  --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h
index 034d5915b09a6..656b3c98b6c73 100644
--- a/flang/lib/Parser/prescan.h
+++ b/flang/lib/Parser/prescan.h
@@ -229,11 +229,14 @@ class Prescanner {
   // True when input flowed to a continuation line
   bool SkipToNextSignificantCharacter();
   void SkipCComments();
+  void WarnCComment(const char *at);
   void SkipSpaces();
   static const char *SkipWhiteSpace(const char *);
-  const char *SkipWhiteSpaceIncludingEmptyMacros(const char *) const;
+  const char *SkipWhiteSpaceIncludingEmptyMacros(
+      const char *, const char **) const;
   const char *SkipWhiteSpaceAndCComments(const char *) const;
   const char *SkipCComment(const char *) const;
+  void UpdateSourcePositionAfterSkip(const char *);
   bool NextToken(TokenSequence &);
   bool HandleExponent(TokenSequence &);
   bool HandleKindSuffix(TokenSequence &);
@@ -248,6 +251,7 @@ class Prescanner {
   void FortranInclude(const char *quote);
   const char *IsPreprocessorDirectiveLine(const char *) const;
   const char *FixedFormContinuationLine(bool atNewline);
+  const char *GetFreeFormContinuationLine(bool ampersand, const char *p);
   const char *FreeFormContinuationLine(bool ampersand);
   bool IsImplicitContinuation() const;
   bool FixedFormContinuation(bool atNewline);

diff  --git a/flang/test/Parser/continuation-before-c-comment01.f90 b/flang/test/Parser/continuation-before-c-comment01.f90
new file mode 100644
index 0000000000000..d7355a682bba3
--- /dev/null
+++ b/flang/test/Parser/continuation-before-c-comment01.f90
@@ -0,0 +1,74 @@
+! RUN: %flang_fc1 -fopenmp -fdebug-unparse %s 2>&1 | FileCheck %s
+! Continuation before C style comment.
+
+integer :: i
+
+! Single line comment.
+! CHECK: i=1
+/* comment 1 */
+i&
+/* comment 2 */
+=1
+! CHECK: i=2
+  /* comment 1 */
+  i&
+  /* comment 2 */
+  =2
+! CHECK: i=3
+  /* comment 1 */
+  i&  
+  /* comment 2 */  
+  =3
+! CHECK: i=4
+  /* comment 1 */
+  i&  /* inline comment */  
+  /* comment 2 */  
+  =4
+! CHECK: i=42
+i&
+/* c */ &= 42
+! CHECK: i=44
+i = 43 &
+/* c */ & + 1
+
+! Multi-line comment.
+! CHECK: i=5
+i&
+/* c
+*/ = 5
+
+! Compiler directive.
+! CHECK: !$OMP PARALLEL
+! CHECK: !$OMP END PARALLEL
+!$omp para&
+/* comment */
+!$omp llel
+!$omp end parallel
+! CHECK: !$OMP PARALLEL
+! CHECK: !$OMP END PARALLEL
+!$omp para&
+/* multi
+ * line */
+!$omp llel
+!$omp end parallel
+! CHECK: !$OMP PARALLEL DO
+! CHECK: !$OMP END PARALLEL DO
+!$omp parallel do &
+/* c */ !$omp private(i)
+  do i = 1, 10
+  end do
+!$omp end parallel do
+
+! Source line continuation after macro expansion.
+! CHECK: i=12
+! CHECK: i=14
+! CHECK: PRINT *, "pass"
+#define CONT &
+i = 6 CONT
+/* comment */
++ 6
+i = 7 CONT
+/* multi
+ * line */ + 7
+print *,'pass'
+end

diff  --git a/flang/test/Parser/continuation-before-c-comment02.f90 b/flang/test/Parser/continuation-before-c-comment02.f90
new file mode 100644
index 0000000000000..f8e92ed668377
--- /dev/null
+++ b/flang/test/Parser/continuation-before-c-comment02.f90
@@ -0,0 +1,35 @@
+! Continuation before C style comment.
+! RUN: rm -rf %t && split-file %s %t
+! RUN: %python %S/../Semantics/test_errors.py %t/err01.f90 %flang_fc1 -pedantic -Werror
+! RUN: %python %S/../Semantics/test_errors.py %t/err02.f90 %flang_fc1 -fopenmp -pedantic
+
+!--- err01.f90
+i&
+! ERROR: nonstandard usage: C-style comment [-Wclassic-c-comments]
+/* c */ = &
+! ERROR: nonstandard usage: C-style comment [-Wclassic-c-comments]
+/* d */ 1
+end
+
+!--- err02.f90
+i&
+! ERROR: nonstandard usage: C-style comment [-Wclassic-c-comments]
+! ERROR: expected '('
+/* c */ &
+! ERROR: expected declaration construct
+= 2
+
+i&
+! ERROR: expected '('
+! ERROR: nonstandard usage: C-style comment [-Wclassic-c-comments]
+/* c */ & ! d
+! ERROR: expected declaration construct
+= 3
+
+!$omp parallel do &
+! ERROR: nonstandard usage: C-style comment [-Wclassic-c-comments]
+/* c */ !$omp private(i)
+  do i = 1, 10
+  end do
+!$omp end parallel do
+end


        


More information about the flang-commits mailing list