[flang-commits] [PATCH] D86554: [flang] Don't completely left-justify fixed-form tokenization

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Aug 25 10:28:05 PDT 2020


klausler created this revision.
klausler added reviewers: sscalpone, PeteSteinfeld.
klausler added a project: Flang.
Herald added a reviewer: jdoerfert.
Herald added a reviewer: DavidTruby.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
klausler requested review of this revision.

If the label field is empty, and macro replacement occurs,
the rescanned text might be misclassified as a comment card
if it happens to begin with a C or a D.  Insert a leading
space into these otherwise empty label fields.

Fixes https://bugs.llvm.org/show_bug.cgi?id=47173


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D86554

Files:
  flang/lib/Parser/prescan.cpp
  flang/lib/Parser/prescan.h
  flang/test/Preprocessing/fixed-rescan.F
  flang/test/Preprocessing/pp029.F


Index: flang/test/Preprocessing/pp029.F
===================================================================
--- flang/test/Preprocessing/pp029.F
+++ flang/test/Preprocessing/pp029.F
@@ -1,5 +1,5 @@
 ! RUN: %f18 -E %s 2>&1 | FileCheck %s
-! CHECK: if(77 7.eq.777)then
+! CHECK: if(777.eq.777)then
 * \ newline allowed in #define
       integer, parameter :: KWM = 666
 #define KWM 77\
Index: flang/test/Preprocessing/fixed-rescan.F
===================================================================
--- /dev/null
+++ flang/test/Preprocessing/fixed-rescan.F
@@ -0,0 +1,7 @@
+! RUN: %f18 -E %s | FileCheck %s
+! CHECK: callbar
+! Ensure that rescanned lines after macro replacement are not
+! misinterpreted as fixed-form comments when they start with C or D.
+#define foo bar
+      call foo
+      end
Index: flang/lib/Parser/prescan.h
===================================================================
--- flang/lib/Parser/prescan.h
+++ flang/lib/Parser/prescan.h
@@ -147,7 +147,7 @@
                     common::LanguageFeature::ClassicCComments)));
   }
 
-  void LabelField(TokenSequence &, int outCol = 1);
+  void LabelField(TokenSequence &);
   void SkipToEndOfLine();
   bool MustSkipToEndOfLine() const;
   void NextChar();
Index: flang/lib/Parser/prescan.cpp
===================================================================
--- flang/lib/Parser/prescan.cpp
+++ flang/lib/Parser/prescan.cpp
@@ -245,8 +245,9 @@
   }
 }
 
-void Prescanner::LabelField(TokenSequence &token, int outCol) {
+void Prescanner::LabelField(TokenSequence &token) {
   const char *bad{nullptr};
+  int outCol{1};
   for (; *at_ != '\n' && column_ <= 6; ++at_) {
     if (*at_ == '\t') {
       ++at_;
@@ -256,20 +257,26 @@
     if (*at_ != ' ' &&
         !(*at_ == '0' && column_ == 6)) { // '0' in column 6 becomes space
       EmitChar(token, *at_);
+      ++outCol;
       if (!bad && !IsDecimalDigit(*at_)) {
         bad = at_;
       }
-      ++outCol;
     }
     ++column_;
   }
-  if (outCol > 1) {
+  if (outCol == 1) { // empty label field
+    // Emit a space so that, if the line is rescanned after preprocessing,
+    // a leading 'C' or 'D' won't be left-justified and then accidentally
+    // misinterpreted as a comment card.
+    EmitChar(token, ' ');
+    ++outCol;
+  } else {
     if (bad && !preprocessor_.IsNameDefined(token.CurrentOpenToken())) {
       Say(GetProvenance(bad),
           "Character in fixed-form label field must be a digit"_en_US);
     }
-    token.CloseToken();
   }
+  token.CloseToken();
   SkipToNextSignificantCharacter();
   if (IsDecimalDigit(*at_)) {
     Say(GetProvenance(at_),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86554.287705.patch
Type: text/x-patch
Size: 2620 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20200825/37df26f0/attachment.bin>


More information about the flang-commits mailing list