[flang-commits] [flang] 0e811d3 - [flang] Fix handling of space between # and name in preprocessor stringification

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Jan 12 16:02:32 PST 2022


Author: Peter Klausler
Date: 2022-01-12T16:02:17-08:00
New Revision: 0e811d3b66ff85c13629d80c483c5d706eb4d15f

URL: https://github.com/llvm/llvm-project/commit/0e811d3b66ff85c13629d80c483c5d706eb4d15f
DIFF: https://github.com/llvm/llvm-project/commit/0e811d3b66ff85c13629d80c483c5d706eb4d15f.diff

LOG: [flang] Fix handling of space between # and name in preprocessor stringification

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.

Differential Revision: https://reviews.llvm.org/D117148

Added: 
    flang/test/Preprocessing/pp045.F
    flang/test/Preprocessing/pp131.F90

Modified: 
    flang/lib/Parser/preprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index de85e8eda56ab..46b62a4f800df 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -169,8 +169,9 @@ TokenSequence Definition::Apply(
           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));

diff  --git a/flang/test/Preprocessing/pp045.F b/flang/test/Preprocessing/pp045.F
new file mode 100644
index 0000000000000..93c914fbd415e
--- /dev/null
+++ b/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

diff  --git a/flang/test/Preprocessing/pp131.F90 b/flang/test/Preprocessing/pp131.F90
new file mode 100644
index 0000000000000..4deb5f9a5ee99
--- /dev/null
+++ b/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


        


More information about the flang-commits mailing list