Skip to content

[flang] Retain spaces when preprocessing fixed-form source #112417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/lib/Parser/parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
messages_, *currentCooked_, preprocessor_, options.features};
prescanner.set_fixedForm(options.isFixedForm)
.set_fixedFormColumnLimit(options.fixedFormColumns)
.set_preprocessingOnly(options.prescanAndReformat)
.set_expandIncludeLines(!options.prescanAndReformat ||
options.expandIncludeLinesInPreprocessedOutput)
.AddCompilerDirectiveSentinel("dir$");
Expand Down
8 changes: 5 additions & 3 deletions flang/lib/Parser/prescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Prescanner::Prescanner(const Prescanner &that, Preprocessor &prepro,
bool isNestedInIncludeDirective)
: messages_{that.messages_}, cooked_{that.cooked_}, preprocessor_{prepro},
allSources_{that.allSources_}, features_{that.features_},
preprocessingOnly_{that.preprocessingOnly_},
expandIncludeLines_{that.expandIncludeLines_},
isNestedInIncludeDirective_{isNestedInIncludeDirective},
backslashFreeFormContinuation_{that.backslashFreeFormContinuation_},
inFixedForm_{that.inFixedForm_},
Expand Down Expand Up @@ -288,8 +290,8 @@ void Prescanner::Statement() {
break;
case LineClassification::Kind::Source:
if (inFixedForm_) {
if (preprocessed->HasBlanks(/*after column*/ 6)) {
preprocessed->RemoveBlanks(/*after column*/ 6);
if (!preprocessingOnly_ && preprocessed->HasBlanks()) {
preprocessed->RemoveBlanks();
}
} else {
while (SourceLineContinuation(*preprocessed)) {
Expand Down Expand Up @@ -622,7 +624,7 @@ const char *Prescanner::SkipCComment(const char *p) const {

bool Prescanner::NextToken(TokenSequence &tokens) {
CHECK(at_ >= start_ && at_ < limit_);
if (InFixedFormSource()) {
if (InFixedFormSource() && !preprocessingOnly_) {
SkipSpaces();
} else {
if (*at_ == '/' && IsCComment(at_)) {
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Parser/prescan.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Prescanner {
Preprocessor &preprocessor() { return preprocessor_; }
common::LanguageFeatureControl &features() { return features_; }

Prescanner &set_preprocessingOnly(bool yes) {
preprocessingOnly_ = yes;
return *this;
}
Prescanner &set_expandIncludeLines(bool yes) {
expandIncludeLines_ = yes;
return *this;
Expand Down Expand Up @@ -213,6 +217,7 @@ class Prescanner {
Preprocessor &preprocessor_;
AllSources &allSources_;
common::LanguageFeatureControl features_;
bool preprocessingOnly_{false};
bool expandIncludeLines_{true};
bool isNestedInIncludeDirective_{false};
bool backslashFreeFormContinuation_{false};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! RUN: %flang_fc1 -fopenmp -fopenacc -E %s 2>&1 | FileCheck %s
program main
! CHECK: k01=1+1
! CHECK: k01=1+ 1
k01=1+
!$ & 1

Expand Down
2 changes: 1 addition & 1 deletion flang/test/Preprocessing/pp029.F
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: if (777 .eq. 777) then
! CHECK: if (77 7.eq. 777) then
* \ newline allowed in #define
integer, parameter :: KWM = 666
#define KWM 77\
Expand Down
4 changes: 2 additions & 2 deletions flang/test/Preprocessing/pp031.F
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: if (777//Ccomment.eq.777)then
! CHECK: print *, 'pp031.F no: ', 777//Ccomment
! CHECK: if (777 // C comment.eq. 777) then
! CHECK: print *, 'pp031.F no: ', 777 // C comment
* // C++ comment NOT erased from #define
integer, parameter :: KWM = 666
#define KWM 777 // C comment
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Preprocessing/pp041.F
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! RUN: %flang -E %s 2>&1 | FileCheck %s
! CHECK: j = 666WMj=j+1WM211
! CHECK: j = 666WMj= j+ 1WM211
* use KWM expansion as continuation indicators
#define KWM 0
#define KWM2 1
Expand Down
2 changes: 1 addition & 1 deletion flang/test/Preprocessing/renaming.F
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! RUN: %flang -E %s | FileCheck %s
! CHECK: ((1)*10000+(11)*100)
! CHECK: ((1) * 10000 + (11) * 100)
! Ensure that a keyword-like macro can be used to rename a
! function-like macro.
#define TO_VERSION2(MAJOR, MINOR) ((MAJOR) * 10000 + (MINOR) * 100)
Expand Down
Loading