[flang-commits] [flang] ba4cc3b - [flang] Don't completely left-justify fixed-form tokenization

peter klausler via flang-commits flang-commits at lists.llvm.org
Tue Aug 25 10:54:48 PDT 2020


Author: peter klausler
Date: 2020-08-25T10:53:56-07:00
New Revision: ba4cc3b38087eb256ef7a93b14dc7d09be15c511

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

LOG: [flang] Don't completely left-justify fixed-form tokenization

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

Added: 
    flang/test/Preprocessing/fixed-rescan.F

Modified: 
    flang/lib/Parser/prescan.cpp
    flang/lib/Parser/prescan.h
    flang/test/Preprocessing/pp029.F

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 9e90f7f6228f..7fdeaf00d185 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -245,8 +245,9 @@ void Prescanner::NextLine() {
   }
 }
 
-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 @@ void Prescanner::LabelField(TokenSequence &token, int outCol) {
     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_),

diff  --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h
index 595f8f701185..0b5b64792004 100644
--- a/flang/lib/Parser/prescan.h
+++ b/flang/lib/Parser/prescan.h
@@ -147,7 +147,7 @@ class Prescanner {
                     common::LanguageFeature::ClassicCComments)));
   }
 
-  void LabelField(TokenSequence &, int outCol = 1);
+  void LabelField(TokenSequence &);
   void SkipToEndOfLine();
   bool MustSkipToEndOfLine() const;
   void NextChar();

diff  --git a/flang/test/Preprocessing/fixed-rescan.F b/flang/test/Preprocessing/fixed-rescan.F
new file mode 100644
index 000000000000..3d6ba9a8c6f4
--- /dev/null
+++ b/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

diff  --git a/flang/test/Preprocessing/pp029.F b/flang/test/Preprocessing/pp029.F
index bb8efe6c1a2e..9be309f75143 100644
--- a/flang/test/Preprocessing/pp029.F
+++ b/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\


        


More information about the flang-commits mailing list