[flang-commits] [flang] [flang][preprocessor] fix use of bitwise-and for logical-and (PR #146758)

via flang-commits flang-commits at lists.llvm.org
Wed Jul 2 11:54:46 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-parser

Author: Andre Kuhlenschmidt (akuhlens)

<details>
<summary>Changes</summary>

The preprocessor used bitwise and to implement logical, this is a bug.

towards #<!-- -->146362

---
Full diff: https://github.com/llvm/llvm-project/pull/146758.diff


2 Files Affected:

- (modified) flang/lib/Parser/preprocessor.cpp (+7-2) 
- (added) flang/test/Parser/issue-146362.2.f90 (+11) 


``````````diff
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
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/146758


More information about the flang-commits mailing list