[flang-commits] [PATCH] D117148: [flang] Fix handling of space between # and name in preprocessor stringification

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Jan 12 13:55:35 PST 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a reviewer: sscalpone.
klausler requested review of this revision.

When preprocessing "# ARG" in function-like macro expansion,
the preprocessor needs to pop the previously-pushed '#' token
from the end of the resulting token sequence after detecting the
argument name.  The code to do this was just wrong in a couple of
ways.


https://reviews.llvm.org/D117148

Files:
  flang/lib/Parser/preprocessor.cpp
  flang/test/Preprocessing/pp045.F
  flang/test/Preprocessing/pp131.F90


Index: flang/test/Preprocessing/pp131.F90
===================================================================
--- /dev/null
+++ flang/test/Preprocessing/pp131.F90
@@ -0,0 +1,9 @@
+! RUN: %flang -E %s 2>&1 | FileCheck %s
+! CHECK: n = "foobar" // "baz"
+! # stringizing works in FLM
+#define STR(x) # x
+#define MAC(a,b) STR(a ## b)
+program main
+  character(6) n
+  n = MAC(foo, bar) // STR( baz )
+end
Index: flang/test/Preprocessing/pp045.F
===================================================================
--- /dev/null
+++ flang/test/Preprocessing/pp045.F
@@ -0,0 +1,9 @@
+! RUN: %flang -E %s 2>&1 | FileCheck %s
+! CHECK: n = "foobar" // "baz"
+* # stringizing works in FLM
+#define STR(x) # x
+#define MAC(a,b) STR(a ## b)
+      program main
+      character(6) n
+      n = MAC(foo, bar) // STR( baz )
+      end
Index: flang/lib/Parser/preprocessor.cpp
===================================================================
--- flang/lib/Parser/preprocessor.cpp
+++ flang/lib/Parser/preprocessor.cpp
@@ -169,8 +169,9 @@
           replacement_.TokenAt(prev - 1)[0] ==
               '#') { // stringify argument without macro replacement
         std::size_t resultSize{result.SizeInTokens()};
-        while (resultSize > 0 && result.TokenAt(resultSize - 1).empty()) {
+        while (resultSize > 0 && result.TokenAt(resultSize - 1).IsBlank()) {
           result.pop_back();
+          --resultSize;
         }
         CHECK(resultSize > 0 &&
             result.TokenAt(resultSize - 1) == replacement_.TokenAt(prev - 1));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117148.399441.patch
Type: text/x-patch
Size: 1536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220112/e9c6e135/attachment.bin>


More information about the flang-commits mailing list