Skip to content

Conversation

klausler
Copy link
Contributor

I implemented legacy "token pasting" via line continuation for

call prefix&
&MACRO&
&suffix(1)

in a recent patch; this patch addresses the related cases

call prefix&
&MACRO&
&(1)

and

call &
&MACRO&
&suffix(1)

Fixes the latest #79590.

@klausler klausler requested a review from clementval January 26, 2024 18:29
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Jan 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 26, 2024

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

I implemented legacy "token pasting" via line continuation for

call prefix&
&MACRO&
&suffix(1)

in a recent patch; this patch addresses the related cases

call prefix&
&MACRO&
&(1)

and

call &
&MACRO&
&suffix(1)

Fixes the latest #79590.


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+15-5)
  • (modified) flang/test/Preprocessing/pp134.F90 (+13-2)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index f7f22177a7d0bfa..26c6dd5a7ffe9c7 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -630,9 +630,11 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
     preventHollerith_ = false;
   } else if (IsLegalInIdentifier(*at_)) {
     int parts{1};
+    const char *afterLast{nullptr};
     do {
       EmitChar(tokens, *at_);
       ++at_, ++column_;
+      afterLast = at_;
       if (SkipToNextSignificantCharacter() && IsLegalIdentifierStart(*at_)) {
         tokens.CloseToken();
         ++parts;
@@ -640,12 +642,20 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
     } while (IsLegalInIdentifier(*at_));
     if (parts >= 3) {
       // Subtlety: When an identifier is split across three or more continuation
-      // lines, its parts are kept as distinct pp-tokens so that macro
-      // operates on them independently.  This trick accommodates the historic
-      // practice of using line continuation for token pasting after
-      // replacement.
+      // lines (or two continuation lines, immediately preceded or followed
+      // by '&' free form continuation line markers, its parts are kept as
+      // distinct pp-tokens so that macro operates on them independently.
+      // This trick accommodates the historic practice of using line
+      // continuation for token pasting after replacement.
     } else if (parts == 2) {
-      tokens.ReopenLastToken();
+      if ((start > start_ && start[-1] == '&') ||
+          (afterLast < limit_ && *afterLast == '&')) {
+        // call &                call foo&
+        //   &MACRO&      OR       &MACRO&
+        //   &foo(...)             &(...)
+      } else {
+        tokens.ReopenLastToken();
+      }
     }
     if (InFixedFormSource()) {
       SkipSpaces();
diff --git a/flang/test/Preprocessing/pp134.F90 b/flang/test/Preprocessing/pp134.F90
index 01e7b010d426ece..1b13e6cb91d7550 100644
--- a/flang/test/Preprocessing/pp134.F90
+++ b/flang/test/Preprocessing/pp134.F90
@@ -1,9 +1,20 @@
 ! RUN: %flang -E %s 2>&1 | FileCheck %s
-! CHECK: print *, ADC
+! CHECK: print *, ADC, 1
+! CHECK: print *, AD, 1
+! CHECK: print *, DC, 1
+! CHECK: print *, AB
 #define B D
 implicit none
 real ADC
 print *, A&
   &B&
-  &C
+  &C, 1
+print *, A&
+  &B&
+  &, 1
+print *, &
+  &B&
+  &C, 1
+print *, A&
+  &B
 end

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

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

LGTM

I implemented legacy "token pasting" via line continuation for

  call prefix&
    &MACRO&
    &suffix(1)

in a recent patch; this patch addresses the related cases

  call prefix&
    &MACRO&
    &(1)

and

  call &
    &MACRO&
    &suffix(1)

and

  call prefix&
    &MACRO

Fixes the latest llvm#79590.
@klausler klausler changed the title [flang] Handle more use cases reported for issues/79590 [flang] Handle more use cases reported for issues/78797 Jan 29, 2024
Copy link
Contributor

@mjklemm mjklemm left a comment

Choose a reason for hiding this comment

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

This PR looks good to me.

@bcornille
Copy link

Is there anything holding up this PR?

@clementval
Copy link
Contributor

Is there anything holding up this PR?

Peter is in PTO right now.

mjklemm added a commit to mjklemm/llvm-project that referenced this pull request Feb 7, 2024
@klausler klausler merged commit 1db2859 into llvm:main Feb 20, 2024
@klausler klausler deleted the bug78797a branch February 20, 2024 21:21
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.

5 participants