[clang] [clang][bytecode] Fix IntegralAP::is{Positive, Negative} (PR #105924)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 23 22:19:28 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

This depends on signed-ness.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/IntegralAP.h (+10-2) 
- (modified) clang/test/AST/ByteCode/intap.cpp (+9) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/IntegralAP.h b/clang/lib/AST/ByteCode/IntegralAP.h
index b8aa21038256c7..209b0af7da5f30 100644
--- a/clang/lib/AST/ByteCode/IntegralAP.h
+++ b/clang/lib/AST/ByteCode/IntegralAP.h
@@ -136,8 +136,16 @@ template <bool Signed> class IntegralAP final {
   APValue toAPValue(const ASTContext &) const { return APValue(toAPSInt()); }
 
   bool isZero() const { return V.isZero(); }
-  bool isPositive() const { return V.isNonNegative(); }
-  bool isNegative() const { return !V.isNonNegative(); }
+  bool isPositive() const {
+    if constexpr (Signed)
+      return V.isNonNegative();
+    return true;
+  }
+  bool isNegative() const {
+    if constexpr (Signed)
+      return !V.isNonNegative();
+    return false;
+  }
   bool isMin() const { return V.isMinValue(); }
   bool isMax() const { return V.isMaxValue(); }
   static constexpr bool isSigned() { return Signed; }
diff --git a/clang/test/AST/ByteCode/intap.cpp b/clang/test/AST/ByteCode/intap.cpp
index d4440124856915..d0ad641fe508cb 100644
--- a/clang/test/AST/ByteCode/intap.cpp
+++ b/clang/test/AST/ByteCode/intap.cpp
@@ -104,6 +104,15 @@ static_assert(INT128_MAX == 0, ""); // expected-error {{failed}} \
                                     // ref-note {{evaluates to '170141183460469231731687303715884105727 == 0'}}
 static const __int128_t INT128_MIN = -INT128_MAX - 1;
 
+
+namespace PointerArithmeticOverflow {
+  int n;
+  constexpr int *p = (&n + 1) + (unsigned __int128)-1; // expected-error {{constant expression}} \
+                                                       // expected-note {{cannot refer to element 3402}} \
+                                                       // ref-error {{constant expression}} \
+                                                       // ref-note {{cannot refer to element 3402}}
+}
+
 namespace i128 {
 
   constexpr int128_t I128_1 = 12;

``````````

</details>


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


More information about the cfe-commits mailing list