[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
Fri Nov 22 01:28:06 PST 2024


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

>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 1/2] [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

>From 636b14f273fe266651ad933276afb8ab9caf0dfb Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <joseph942010 at gmail.com>
Date: Fri, 22 Nov 2024 04:27:59 -0500
Subject: [PATCH 2/2] Rename var: Mte -> MTE

Co-authored-by: Sirraide <aeternalmail at gmail.com>
---
 clang/lib/Sema/SemaChecking.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index e36cb318c61885..a49605e4867651 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12048,8 +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());
+    } else if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(OriginalE))
+      Exprs.push_back(MTE->getSubExpr());
   } while (!Exprs.empty());
 }
 



More information about the cfe-commits mailing list