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

Conversation

klausler
Copy link
Contributor

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.

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 llvm#112279.
@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2024

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/112417.diff

8 Files Affected:

  • (modified) flang/lib/Parser/parsing.cpp (+1)
  • (modified) flang/lib/Parser/prescan.cpp (+5-3)
  • (modified) flang/lib/Parser/prescan.h (+5)
  • (modified) flang/test/Parser/continuation-in-conditional-compilation.f (+1-1)
  • (modified) flang/test/Preprocessing/pp029.F (+1-1)
  • (modified) flang/test/Preprocessing/pp031.F (+2-2)
  • (modified) flang/test/Preprocessing/pp041.F (+1-1)
  • (modified) flang/test/Preprocessing/renaming.F (+1-1)
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)

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly and looks good.

@klausler klausler merged commit 9fb2db1 into llvm:main Oct 15, 2024
11 checks passed
@klausler klausler deleted the bug112279 branch October 15, 2024 21:23
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
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 llvm#112279.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:parser flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Flang] When there are multiple statements on a line, the preprocessor does not work correctly.
3 participants