diff --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h --- a/flang/lib/Parser/prescan.h +++ b/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(); diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp --- a/flang/lib/Parser/prescan.cpp +++ b/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_), diff --git a/flang/test/Preprocessing/fixed-rescan.F b/flang/test/Preprocessing/fixed-rescan.F new file mode 100644 --- /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 --- 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\