[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 11:54:16 PDT 2025
https://github.com/akuhlens created https://github.com/llvm/llvm-project/pull/146758
The preprocessor used bitwise and to implement logical, this is a bug.
towards #146362
>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] 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
+
More information about the flang-commits
mailing list