Skip to content

Commit 9fb2db1

Browse files
authored
[flang] Retain spaces when preprocessing fixed-form source (#112417)
When running fixed-form source through the compiler under -E, don't aggressively remove space characters, since the parser won't be parsing the result and some tools might need to see the spaces in the -E preprocessed output. Fixes #112279.
1 parent 38b9dd7 commit 9fb2db1

File tree

8 files changed

+17
-9
lines changed

8 files changed

+17
-9
lines changed

flang/lib/Parser/parsing.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
7575
messages_, *currentCooked_, preprocessor_, options.features};
7676
prescanner.set_fixedForm(options.isFixedForm)
7777
.set_fixedFormColumnLimit(options.fixedFormColumns)
78+
.set_preprocessingOnly(options.prescanAndReformat)
7879
.set_expandIncludeLines(!options.prescanAndReformat ||
7980
options.expandIncludeLinesInPreprocessedOutput)
8081
.AddCompilerDirectiveSentinel("dir$");

flang/lib/Parser/prescan.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Prescanner::Prescanner(const Prescanner &that, Preprocessor &prepro,
3636
bool isNestedInIncludeDirective)
3737
: messages_{that.messages_}, cooked_{that.cooked_}, preprocessor_{prepro},
3838
allSources_{that.allSources_}, features_{that.features_},
39+
preprocessingOnly_{that.preprocessingOnly_},
40+
expandIncludeLines_{that.expandIncludeLines_},
3941
isNestedInIncludeDirective_{isNestedInIncludeDirective},
4042
backslashFreeFormContinuation_{that.backslashFreeFormContinuation_},
4143
inFixedForm_{that.inFixedForm_},
@@ -288,8 +290,8 @@ void Prescanner::Statement() {
288290
break;
289291
case LineClassification::Kind::Source:
290292
if (inFixedForm_) {
291-
if (preprocessed->HasBlanks(/*after column*/ 6)) {
292-
preprocessed->RemoveBlanks(/*after column*/ 6);
293+
if (!preprocessingOnly_ && preprocessed->HasBlanks()) {
294+
preprocessed->RemoveBlanks();
293295
}
294296
} else {
295297
while (SourceLineContinuation(*preprocessed)) {
@@ -622,7 +624,7 @@ const char *Prescanner::SkipCComment(const char *p) const {
622624

623625
bool Prescanner::NextToken(TokenSequence &tokens) {
624626
CHECK(at_ >= start_ && at_ < limit_);
625-
if (InFixedFormSource()) {
627+
if (InFixedFormSource() && !preprocessingOnly_) {
626628
SkipSpaces();
627629
} else {
628630
if (*at_ == '/' && IsCComment(at_)) {

flang/lib/Parser/prescan.h

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class Prescanner {
4848
Preprocessor &preprocessor() { return preprocessor_; }
4949
common::LanguageFeatureControl &features() { return features_; }
5050

51+
Prescanner &set_preprocessingOnly(bool yes) {
52+
preprocessingOnly_ = yes;
53+
return *this;
54+
}
5155
Prescanner &set_expandIncludeLines(bool yes) {
5256
expandIncludeLines_ = yes;
5357
return *this;
@@ -213,6 +217,7 @@ class Prescanner {
213217
Preprocessor &preprocessor_;
214218
AllSources &allSources_;
215219
common::LanguageFeatureControl features_;
220+
bool preprocessingOnly_{false};
216221
bool expandIncludeLines_{true};
217222
bool isNestedInIncludeDirective_{false};
218223
bool backslashFreeFormContinuation_{false};

flang/test/Parser/continuation-in-conditional-compilation.f

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! RUN: %flang_fc1 -fopenmp -fopenacc -E %s 2>&1 | FileCheck %s
22
program main
3-
! CHECK: k01=1+1
3+
! CHECK: k01=1+ 1
44
k01=1+
55
!$ & 1
66

flang/test/Preprocessing/pp029.F

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
! RUN: %flang -E %s 2>&1 | FileCheck %s
2-
! CHECK: if (777 .eq. 777) then
2+
! CHECK: if (77 7.eq. 777) then
33
* \ newline allowed in #define
44
integer, parameter :: KWM = 666
55
#define KWM 77\

flang/test/Preprocessing/pp031.F

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! RUN: %flang -E %s 2>&1 | FileCheck %s
2-
! CHECK: if (777//Ccomment.eq.777)then
3-
! CHECK: print *, 'pp031.F no: ', 777//Ccomment
2+
! CHECK: if (777 // C comment.eq. 777) then
3+
! CHECK: print *, 'pp031.F no: ', 777 // C comment
44
* // C++ comment NOT erased from #define
55
integer, parameter :: KWM = 666
66
#define KWM 777 // C comment

flang/test/Preprocessing/pp041.F

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
! RUN: %flang -E %s 2>&1 | FileCheck %s
2-
! CHECK: j = 666WMj=j+1WM211
2+
! CHECK: j = 666WMj= j+ 1WM211
33
* use KWM expansion as continuation indicators
44
#define KWM 0
55
#define KWM2 1

flang/test/Preprocessing/renaming.F

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
! RUN: %flang -E %s | FileCheck %s
2-
! CHECK: ((1)*10000+(11)*100)
2+
! CHECK: ((1) * 10000 + (11) * 100)
33
! Ensure that a keyword-like macro can be used to rename a
44
! function-like macro.
55
#define TO_VERSION2(MAJOR, MINOR) ((MAJOR) * 10000 + (MINOR) * 100)

0 commit comments

Comments
 (0)