[flang-commits] [flang] 9fb2db1 - [flang] Retain spaces when preprocessing fixed-form source (#112417)
via flang-commits
flang-commits at lists.llvm.org
Tue Oct 15 14:23:48 PDT 2024
Author: Peter Klausler
Date: 2024-10-15T14:23:45-07:00
New Revision: 9fb2db1e1f42ae10a9d8c1d9410b5f4e719fdac0
URL: https://github.com/llvm/llvm-project/commit/9fb2db1e1f42ae10a9d8c1d9410b5f4e719fdac0
DIFF: https://github.com/llvm/llvm-project/commit/9fb2db1e1f42ae10a9d8c1d9410b5f4e719fdac0.diff
LOG: [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 https://github.com/llvm/llvm-project/issues/112279.
Added:
Modified:
flang/lib/Parser/parsing.cpp
flang/lib/Parser/prescan.cpp
flang/lib/Parser/prescan.h
flang/test/Parser/continuation-in-conditional-compilation.f
flang/test/Preprocessing/pp029.F
flang/test/Preprocessing/pp031.F
flang/test/Preprocessing/pp041.F
flang/test/Preprocessing/renaming.F
Removed:
################################################################################
diff --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index d8448e4c527ac7..e2381a6b8ffa3e 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -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$");
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 47260c0680463d..1d2f1e97668792 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -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_},
@@ -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)) {
@@ -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_)) {
diff --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h
index c50bf231e3c705..08041f93b14b6c 100644
--- a/flang/lib/Parser/prescan.h
+++ b/flang/lib/Parser/prescan.h
@@ -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;
@@ -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};
diff --git a/flang/test/Parser/continuation-in-conditional-compilation.f b/flang/test/Parser/continuation-in-conditional-compilation.f
index 35eecbc0f16ea4..987112301e335c 100644
--- a/flang/test/Parser/continuation-in-conditional-compilation.f
+++ b/flang/test/Parser/continuation-in-conditional-compilation.f
@@ -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
diff --git a/flang/test/Preprocessing/pp029.F b/flang/test/Preprocessing/pp029.F
index 4ca87dd20f157a..1f8533ab08cd6c 100644
--- a/flang/test/Preprocessing/pp029.F
+++ b/flang/test/Preprocessing/pp029.F
@@ -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\
diff --git a/flang/test/Preprocessing/pp031.F b/flang/test/Preprocessing/pp031.F
index 4813c40208a9fe..3ad0bde9e50c09 100644
--- a/flang/test/Preprocessing/pp031.F
+++ b/flang/test/Preprocessing/pp031.F
@@ -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
diff --git a/flang/test/Preprocessing/pp041.F b/flang/test/Preprocessing/pp041.F
index 3f1f3c6a2aeba2..cee3c5d3e490b5 100644
--- a/flang/test/Preprocessing/pp041.F
+++ b/flang/test/Preprocessing/pp041.F
@@ -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
diff --git a/flang/test/Preprocessing/renaming.F b/flang/test/Preprocessing/renaming.F
index 1bef18116901e7..c39ab6fb029a05 100644
--- a/flang/test/Preprocessing/renaming.F
+++ b/flang/test/Preprocessing/renaming.F
@@ -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)
More information about the flang-commits
mailing list