[flang-commits] [flang] [flang] Handle duplicate backslashes in preprocessor. (PR #115822)

via flang-commits flang-commits at lists.llvm.org
Mon Nov 11 22:18:34 PST 2024


https://github.com/olee7 updated https://github.com/llvm/llvm-project/pull/115822

>From ece4c0658a12815c0eb0114c8f7e8d34f7e34d0e Mon Sep 17 00:00:00 2001
From: dlee67 <keyboardPuncher96 at gmail.com>
Date: Mon, 11 Nov 2024 22:03:59 -0800
Subject: [PATCH] [flang] Handle duplicate backslashes in preprocessor.

---
 flang/lib/Parser/prescan.cpp                |  2 +-
 flang/test/Parser/duplicate-backslashes.f90 | 23 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Parser/duplicate-backslashes.f90

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 1d2f1e97668792..8c991678047ecd 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -843,7 +843,7 @@ void Prescanner::QuotedCharacterLiteral(
     if (*at_ == '\\') {
       if (escapesEnabled) {
         isEscaped = !isEscaped;
-      } else {
+      } else if (!preprocessingOnly_) {
         // The parser always processes escape sequences, so don't confuse it
         // when escapes are disabled.
         insert('\\');
diff --git a/flang/test/Parser/duplicate-backslashes.f90 b/flang/test/Parser/duplicate-backslashes.f90
new file mode 100644
index 00000000000000..896a5e864bc93d
--- /dev/null
+++ b/flang/test/Parser/duplicate-backslashes.f90
@@ -0,0 +1,23 @@
+! RUN: %flang -cpp -E %s -o %t.f90
+! RUN: %flang %t.f90 -o %t
+! RUN: %t | FileCheck %s
+
+program main
+  implicit none
+
+  ! Test single backslash
+  write(*, '(A)') "\"   ! Expected single backslash in output
+  ! CHECK: \
+  ! CHECK-NOT: \\
+
+  ! Test double backslash
+  write(*, '(A)') "\\"   ! Expected double backslashes in output
+  ! CHECK: \\
+  ! CHECK-NOT: \\\\
+
+  ! Test quadruple backslash
+  write(*, '(A)') "\\\\"   ! Expected quadruple backslashes in output
+  ! CHECK: \\\\
+  ! CHECK-NOT: \\\\\\\\
+
+end program main



More information about the flang-commits mailing list