[flang-commits] [flang] [flang][preprocessor] fix use of bitwise-and for logical-and (PR #146758)
Andre Kuhlenschmidt via flang-commits
flang-commits at lists.llvm.org
Wed Jul 2 13:45:29 PDT 2025
https://github.com/akuhlens updated https://github.com/llvm/llvm-project/pull/146758
>From 3631e247920d8e1c5a1ef186e1abcd91d24dce9e Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Wed, 2 Jul 2025 11:51:05 -0700
Subject: [PATCH 1/2] initial commit
---
flang/lib/Parser/preprocessor.cpp | 9 +++++++--
flang/test/Parser/issue-146362.2.f90 | 11 +++++++++++
2 files changed, 18 insertions(+), 2 deletions(-)
create mode 100644 flang/test/Parser/issue-146362.2.f90
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index ef24c704db885..d79b7d41da83c 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -1276,14 +1276,19 @@ static std::int64_t ExpressionValue(const TokenSequence &token,
left = right >= 64 ? 0 : left >> right;
break;
case BITAND:
- case AND:
left = left & right;
break;
+ // (bool)(1 && 2) != (bool)(1 & 2), we can't use bitwise AND here.
+ case AND:
+ left = left && right;
+ break;
case BITXOR:
left = left ^ right;
break;
- case BITOR:
+ // (bool)(1 || 2) == (bool)(1 | 2), seems like we can use `|` here.
+ // But should we?
case OR:
+ case BITOR:
left = left | right;
break;
case LT:
diff --git a/flang/test/Parser/issue-146362.2.f90 b/flang/test/Parser/issue-146362.2.f90
new file mode 100644
index 0000000000000..db27115ec15d0
--- /dev/null
+++ b/flang/test/Parser/issue-146362.2.f90
@@ -0,0 +1,11 @@
+!RUN: %flang_fc1 -cpp -fdebug-unparse %s | FileCheck %s
+PROGRAM P
+#if (1 && 2)
+ !CHECK: TRUE
+ WRITE(*,*) 'TRUE'
+#else
+ !CHECK-NOT: FALSE
+ WRITE(*,*) 'FALSE'
+#endif
+END PROGRAM
+
>From 6abffad169a40a2a5744876964c11b4f676407bd Mon Sep 17 00:00:00 2001
From: Andre Kuhlenschmidt <akuhlenschmi at nvidia.com>
Date: Wed, 2 Jul 2025 13:44:49 -0700
Subject: [PATCH 2/2] remove using bitwise or for logical or
---
flang/lib/Parser/preprocessor.cpp | 13 ++++++-------
flang/test/Parser/issue-146362.2.f90 | 7 +++++++
2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index d79b7d41da83c..8ef9810463d5a 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -1278,19 +1278,18 @@ static std::int64_t ExpressionValue(const TokenSequence &token,
case BITAND:
left = left & right;
break;
- // (bool)(1 && 2) != (bool)(1 & 2), we can't use bitwise AND here.
- case AND:
- left = left && right;
- break;
case BITXOR:
left = left ^ right;
break;
- // (bool)(1 || 2) == (bool)(1 | 2), seems like we can use `|` here.
- // But should we?
- case OR:
case BITOR:
left = left | right;
break;
+ case AND:
+ left = left && right;
+ break;
+ case OR:
+ left = left || right;
+ break;
case LT:
left = -(left < right);
break;
diff --git a/flang/test/Parser/issue-146362.2.f90 b/flang/test/Parser/issue-146362.2.f90
index db27115ec15d0..d9ad8c7d547f2 100644
--- a/flang/test/Parser/issue-146362.2.f90
+++ b/flang/test/Parser/issue-146362.2.f90
@@ -7,5 +7,12 @@ PROGRAM P
!CHECK-NOT: FALSE
WRITE(*,*) 'FALSE'
#endif
+#if ((1 || 2) != 3)
+ !CHECK: TRUE
+ WRITE(*,*) 'TRUE'
+#else
+ !CHECK-NOT: FALSE
+ WRITE(*,*) 'FALSE'
+#endif
END PROGRAM
More information about the flang-commits
mailing list