Skip to content

Conversation

@JOE1994
Copy link
Member

@JOE1994 JOE1994 commented Nov 21, 2024

Fixes #46755

@JOE1994 JOE1994 requested a review from zygoloid November 21, 2024 19:51
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2024

@llvm/pr-subscribers-clang

Author: Youngsuk Kim (JOE1994)

Changes

Fixes #46755


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaChecking.cpp (+2-1)
  • (modified) clang/test/SemaCXX/integer-overflow.cpp (+6)
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

@JOE1994 JOE1994 merged commit ef20644 into llvm:main Nov 22, 2024
6 of 9 checks passed
@JOE1994 JOE1994 deleted the clang/46755 branch November 22, 2024 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False negative of -Winteger-overflow

3 participants