-
Notifications
You must be signed in to change notification settings - Fork 11.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Clang][Sema] Fix templated array size calculation. #96464
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: None (awson) ChangesThe last attempt to fix #41441 has been reverted immediately. Here I'm trying the simplest idea I've been able to come with: skip handling dependent case in The original test (borrowed form #89036) passes. Also I've created and added to the tests a minimal repro of the code #89036 fails on. This (obviously) also passes. Full diff: https://github.com/llvm/llvm-project/pull/96464.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index f3af8dee6b090..2f79540faea00 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -2174,7 +2174,8 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal,
// Per C++0x [expr.new]p5, the type being constructed may be a
// typedef of an array type.
- if (!ArraySize) {
+ // Dependent case will be handled separately.
+ if (!ArraySize && !AllocType->isDependentType()) {
if (const ConstantArrayType *Array
= Context.getAsConstantArrayType(AllocType)) {
ArraySize = IntegerLiteral::Create(Context, Array->getSize(),
diff --git a/clang/test/SemaCXX/GH41441.cpp b/clang/test/SemaCXX/GH41441.cpp
new file mode 100644
index 0000000000000..7a6260fef91b5
--- /dev/null
+++ b/clang/test/SemaCXX/GH41441.cpp
@@ -0,0 +1,46 @@
+// RUN: %clang --target=x86_64-pc-linux -S -fno-discard-value-names -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 %s -fsyntax-only -verify
+
+namespace std {
+ using size_t = decltype(sizeof(int));
+};
+void* operator new[](std::size_t, void*) noexcept;
+
+// CHECK: call void @llvm.memset.p0.i64(ptr align 1 %x, i8 0, i64 8, i1 false)
+// CHECK: call void @llvm.memset.p0.i64(ptr align 16 %x, i8 0, i64 32, i1 false)
+template <typename TYPE>
+void f()
+{
+ typedef TYPE TArray[8];
+
+ TArray x;
+ new(&x) TArray();
+}
+
+template <typename T>
+void f1() {
+ int (*x)[1] = new int[1][1];
+}
+template void f1<char>();
+void f2() {
+ int (*x)[1] = new int[1][1];
+}
+
+int main()
+{
+ f<char>();
+ f<int>();
+}
+
+// expected-no-diagnostics
+template <typename T> struct unique_ptr {unique_ptr(T* p){}};
+
+template <typename T>
+unique_ptr<T> make_unique(unsigned long long n) {
+ return unique_ptr<T>(new T[n]());
+}
+
+auto boro(int n){
+ typedef double HistoryBuffer[4];
+ return make_unique<HistoryBuffer>(n);
+}
|
I've borrowed size-calculation test from PR89036 and added another test, which PR89036 fails on.
I see you actively reviewed (related, failed) #89036. Could you look into this? The idea of the fix is extremely simple: if we want to do something once, but mistakenly do it twice, just don't do it the first time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for catching this! Can you please add a release note to clang/docs/ReleaseNotes.rst
so users know about the fix?
I think these changes otherwise look good.
It seems reasonable to me, but I want @cor3ntin to review this, he's the one who got his foot stuck in array bounds for a while :D However, this patch needs a few changes: 1- A better patch title, perhaps improved commit message |
|
Our 'merge' process is a squash & Merge, so it really means "edit the github title and description to be more descriptive, and that is what will be picked up". So no need to modify the actual commits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, feel free to merge.
@shafik ? |
The last attempt to fix #41441 has been reverted immediately.
Here I'm trying the simplest idea I've been able to come with: skip handling dependent case in
BuildCXXNew
.The original test (borrowed form #89036) passes.
Also I've created and added to the tests a minimal repro of the code #89036 fails on. This (obviously) also passes.