[clang] [clang] Warn const integer-overflow of member in temporary struct bound to rvalue reference (PR #117225)

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 11:51:26 PST 2024


https://github.com/JOE1994 created https://github.com/llvm/llvm-project/pull/117225

Fixes #46755

>From 08e1a3388e9d98f2469687d2367472342b05c47e Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Thu, 21 Nov 2024 13:05:20 -0600
Subject: [PATCH] [clang] Warn const integer-overflow of member in temporary
 struct bound to rvalue reference

Fixes #46755
---
 clang/docs/ReleaseNotes.rst             | 3 +++
 clang/lib/Sema/SemaChecking.cpp         | 3 ++-
 clang/test/SemaCXX/integer-overflow.cpp | 6 ++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8c81de341937ca..8820ba0fb24b38 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -578,6 +578,9 @@ Improvements to Clang's diagnostics
 
 - Clang now omits shadowing warnings for parameter names in explicit object member functions (#GH95707).
 
+- For an rvalue reference bound to a temporary struct with an integer member, Clang will detect constant integer overflow
+  in the initializer for the integer member (#GH46755).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 2fd990750ed212..e36cb318c61885 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12048,7 +12048,8 @@ void Sema::CheckForIntOverflow (const Expr *E) {
              New && New->isArray()) {
       if (auto ArraySize = New->getArraySize())
         Exprs.push_back(*ArraySize);
-    }
+    } else if (const auto *Mte = dyn_cast<MaterializeTemporaryExpr>(OriginalE))
+      Exprs.push_back(Mte->getSubExpr());
   } while (!Exprs.empty());
 }
 
diff --git a/clang/test/SemaCXX/integer-overflow.cpp b/clang/test/SemaCXX/integer-overflow.cpp
index d1cc8bee566f6b..73a4e88ee6c098 100644
--- a/clang/test/SemaCXX/integer-overflow.cpp
+++ b/clang/test/SemaCXX/integer-overflow.cpp
@@ -246,4 +246,10 @@ int m() {
     return 0;
 }
 }
+
+namespace GH46755 {
+void f() {
+    struct { int v; } &&r = {512 * 1024 * 1024 * 1024}; // expected-warning {{overflow in expression; result is 0 with type 'int'}}
+}
+}
 #endif



More information about the cfe-commits mailing list