Skip to content

Conversation

@klausler
Copy link
Contributor

@klausler klausler commented Sep 2, 2024

Codes using traditional C preprocessors will sometimes put a keyword macro name in a free form continuation line in order to get macro replacement of part of an identifier, as in

call subr_&
&N&
&(1.)

where N is a keyword macro. f18 already handles this case, but not when there is white space between the macro name and the following continuation marker character '&'. Allow white space to appear.

Fixes #106931.

Codes using traditional C preprocessors will sometimes put a keyword
macro name in a free form continuation line in order to get macro
replacement of part of an identifier, as in

  call subr_&
    &N&
    &(1.)

where N is a keyword macro.  f18 already handles this case, but not
when there is white space between the macro name and the following
continuation marker character '&'.  Allow white space to appear.

Fixes llvm#106931.
@llvmbot
Copy link
Member

llvmbot commented Sep 2, 2024

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

Codes using traditional C preprocessors will sometimes put a keyword macro name in a free form continuation line in order to get macro replacement of part of an identifier, as in

call subr_&
&N&
&(1.)

where N is a keyword macro. f18 already handles this case, but not when there is white space between the macro name and the following continuation marker character '&'. Allow white space to appear.

Fixes #106931.


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+8-4)
  • (modified) flang/test/Preprocessing/pp134.F90 (+8-4)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 804ada7d11e020..dc41f444020659 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -712,12 +712,16 @@ bool Prescanner::NextToken(TokenSequence &tokens) {
       // Subtlety: When an identifier is split across three or more continuation
       // 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.
+      // distinct pp-tokens so that macro replacement operates on them
+      // independently.  This trick accommodates the historic practice of
+      // using line continuation for token pasting after replacement.
     } else if (parts == 2) {
+      if (afterLast && afterLast < limit_) {
+        afterLast = SkipWhiteSpace(afterLast);
+      }
       if ((start > start_ && start[-1] == '&') ||
-          (afterLast < limit_ && (*afterLast == '&' || *afterLast == '\n'))) {
+          (afterLast && afterLast < limit_ &&
+              (*afterLast == '&' || *afterLast == '\n'))) {
         // call &                call foo&        call foo&
         //   &MACRO&      OR       &MACRO&   OR     &MACRO
         //   &foo(...)             &(...)
diff --git a/flang/test/Preprocessing/pp134.F90 b/flang/test/Preprocessing/pp134.F90
index bc34767224fa03..213baad900b602 100644
--- a/flang/test/Preprocessing/pp134.F90
+++ b/flang/test/Preprocessing/pp134.F90
@@ -1,7 +1,8 @@
 ! RUN: %flang -E %s 2>&1 | FileCheck %s
 ! CHECK: print *, ADC, 1
-! CHECK: print *, AD, 1
-! CHECK: print *, DC, 1
+! CHECK: print *, AD, 2
+! CHECK: print *, DC, 3
+! CHECK: print *, AD(1), 4
 ! CHECK: print *, AD
 ! CHECK: print *, AB
 #define B D
@@ -12,10 +13,13 @@
   &C, 1
 print *, A&
   &B&
-  &, 1
+  &, 2
 print *, &
   &B&
-  &C, 1
+  &C, 3
+print *, A&
+  &B &
+  &(1), 4
 print *, A&
   &B
 print *, A&

@klausler klausler merged commit 1324789 into llvm:main Sep 4, 2024
@klausler klausler deleted the bug106931 branch September 4, 2024 17:55
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] Preprocessor error when the name to be replaced contains trailing blank in a continuation line

3 participants