[flang] Diagnose fixed form statement that begins with continuation line
authorPeter Klausler <pklausler@nvidia.com>
Mon, 9 Jan 2023 23:53:39 +0000 (15:53 -0800)
committerPeter Klausler <pklausler@nvidia.com>
Sat, 28 Jan 2023 18:16:43 +0000 (10:16 -0800)
A fixed form continuation line should not be the first line of a statement;
emit a warning if it looks like one is so that the programmer can look
for a missing card.

Differential Revision: https://reviews.llvm.org/D142763

flang/lib/Parser/prescan.cpp
flang/test/Preprocessing/pp044.F

index ae8decf..9da30d2 100644 (file)
@@ -269,9 +269,9 @@ void Prescanner::NextLine() {
 }
 
 void Prescanner::LabelField(TokenSequence &token) {
-  const char *bad{nullptr};
   int outCol{1};
   const char *start{at_};
+  std::optional<int> badColumn;
   for (; *at_ != '\n' && column_ <= 6; ++at_) {
     if (*at_ == '\t') {
       ++at_;
@@ -282,18 +282,24 @@ void Prescanner::LabelField(TokenSequence &token) {
         !(*at_ == '0' && column_ == 6)) { // '0' in column 6 becomes space
       EmitChar(token, *at_);
       ++outCol;
-      if (!bad && !IsDecimalDigit(*at_)) {
-        bad = at_;
+      if (!badColumn && (column_ == 6 || !IsDecimalDigit(*at_))) {
+        badColumn = column_;
       }
     }
     ++column_;
   }
-  if (bad && !preprocessor_.IsNameDefined(token.CurrentOpenToken())) {
-    Say(GetProvenance(bad),
-        "Character in fixed-form label field must be a digit"_warn_en_US);
+  if (badColumn && !preprocessor_.IsNameDefined(token.CurrentOpenToken())) {
+    Say(GetProvenance(start + *badColumn - 1),
+        *badColumn == 6
+            ? "Statement should not begin with a continuation line"_warn_en_US
+            : "Character in fixed-form label field must be a digit"_warn_en_US);
     token.clear();
-    at_ = start;
-    return;
+    if (*badColumn < 6) {
+      at_ = start;
+      column_ = 1;
+      return;
+    }
+    outCol = 1;
   }
   if (outCol == 1) { // empty label field
     // Emit a space so that, if the line is rescanned after preprocessing,
index 6304fa4..5307534 100644 (file)
@@ -1,14 +1,15 @@
 ! RUN: %flang -E %s 2>&1 | FileCheck %s
 ! CHECK-NOT:z = 111
-* #define directive amid continuations
-      integer, parameter :: KWM = 222, KWM111 = 333, KWM222 = 555
-      integer, parameter :: KWMKWM = 333
+! CHECK:warning: Statement should not begin with a continuation line
+! CHECK:j=111+444
+* #define directive amid continuations.
+      integer, parameter :: KWM = 222
       integer, parameter :: z = KWM
 #define KWM 111
-     +KWM+444
-      if (z .EQ. 777) then
+     ,j=KWM+444
+      if (z .EQ. 222 .AND. j .EQ. 555) then
         print *, 'yes'
       else
-        print *, 'no', z
+        print *, 'no', z, _4
       end if
       end