Skip to content

Conversation

@alexfh
Copy link
Contributor

@alexfh alexfh commented Jun 5, 2025

CXXParenListInitExpr arguments would lose casts leading to incorrect types being used (e.g. only 32 bits of a 64 bit value being initialized). See #138518 (comment) and #138518 (comment) for details and context.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 5, 2025
@alexfh alexfh requested review from cor3ntin and erichkeane June 5, 2025 15:15
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-clang

Author: Alexander Kornienko (alexfh)

Changes

CXXParenListInitExpr arguments would lose casts leading to incorrect types being
used (e.g. only 32 bits of a 64 bit value being initialized). See
#138518 (comment) and
#138518 (comment) for
details and context.


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+5)
  • (added) clang/test/SemaCXX/paren-list-init-expr.cpp (+23)
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index 84521a3f80ff4..516015302bb39 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2160,6 +2160,11 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
            "paren init for non-call init");
     Exprs = MultiExprArg(List->getExprs(), List->getNumExprs());
   }
+  if (auto *List = dyn_cast_or_null<CXXParenListInitExpr>(Initializer)) {
+    assert(InitStyle == CXXNewInitializationStyle::Parens &&
+           "paren init for non-call init");
+    Exprs = List->getInitExprs();
+  }
 
   // C++11 [expr.new]p15:
   //   A new-expression that creates an object of type T initializes that
diff --git a/clang/test/SemaCXX/paren-list-init-expr.cpp b/clang/test/SemaCXX/paren-list-init-expr.cpp
new file mode 100644
index 0000000000000..7d7f46be7a8fd
--- /dev/null
+++ b/clang/test/SemaCXX/paren-list-init-expr.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -ast-dump %s | FileCheck %s
+struct Node {
+  long val;
+};
+template <bool>
+void CallNew() {
+    new Node(0);
+}
+// CHECK-LABEL: FunctionTemplateDecl {{.*}} CallNew
+// CHECK: |-FunctionDecl {{.*}} CallNew 'void ()'
+// CHECK:  `-CXXNewExpr {{.*}} 'operator new' 'void *(unsigned long)'
+// CHECK:  `-CXXParenListInitExpr {{.*}} 'Node'
+// CHECK:  `-ImplicitCastExpr {{.*}} 'long' <IntegralCast>
+// CHECK:  `-IntegerLiteral {{.*}} 'int' 0
+// CHECK: `-FunctionDecl {{.*}} used CallNew 'void ()' implicit_instantiation
+// CHECK:   |-TemplateArgument integral 'true'
+// CHECK:   `-CXXNewExpr {{.*}} 'operator new' 'void *(unsigned long)'
+// CHECK:   `-CXXParenListInitExpr {{.*}} 'Node'
+// CHECK:   `-ImplicitCastExpr {{.*}} 'long' <IntegralCast>
+// CHECK:   `-IntegerLiteral {{.*}} 'int' 0
+void f() {
+    (void)CallNew<true>; 
+}

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 nit, else this LGTM. @cor3ntin should have a quick look, but I'm happy with this.

Thank you so much!

@alexfh
Copy link
Contributor Author

alexfh commented Jun 5, 2025

@cor3ntin PTAL

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@alexfh alexfh merged commit f532167 into llvm:main Jun 5, 2025
10 of 11 checks passed
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.

4 participants