-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang][bytecode] Fix CXXConstructExpr for multidim arrays #164760
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
Conversation
This is a thing apparently. Fixes llvm#153803
|
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesThis is a thing apparently. Fixes #153803 Full diff: https://github.com/llvm/llvm-project/pull/164760.diff 2 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 6b989276e6d7d..3ae5bf774d906 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -3274,34 +3274,43 @@ bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
}
if (T->isArrayType()) {
- const ConstantArrayType *CAT =
- Ctx.getASTContext().getAsConstantArrayType(E->getType());
- if (!CAT)
- return false;
-
- size_t NumElems = CAT->getZExtSize();
const Function *Func = getFunction(E->getConstructor());
if (!Func)
return false;
- // FIXME(perf): We're calling the constructor once per array element here,
- // in the old intepreter we had a special-case for trivial constructors.
- for (size_t I = 0; I != NumElems; ++I) {
- if (!this->emitConstUint64(I, E))
- return false;
- if (!this->emitArrayElemPtrUint64(E))
- return false;
+ if (!this->emitDupPtr(E))
+ return false;
- // Constructor arguments.
- for (const auto *Arg : E->arguments()) {
- if (!this->visit(Arg))
- return false;
+ std::function<bool(QualType)> initArrayDimension;
+ initArrayDimension = [&](QualType T) -> bool {
+ if (!T->isArrayType()) {
+ // Constructor arguments.
+ for (const auto *Arg : E->arguments()) {
+ if (!this->visit(Arg))
+ return false;
+ }
+
+ return this->emitCall(Func, 0, E);
}
- if (!this->emitCall(Func, 0, E))
+ const ConstantArrayType *CAT =
+ Ctx.getASTContext().getAsConstantArrayType(T);
+ if (!CAT)
return false;
- }
- return true;
+ QualType ElemTy = CAT->getElementType();
+ unsigned NumElems = CAT->getZExtSize();
+ for (size_t I = 0; I != NumElems; ++I) {
+ if (!this->emitConstUint64(I, E))
+ return false;
+ if (!this->emitArrayElemPtrUint64(E))
+ return false;
+ if (!initArrayDimension(ElemTy))
+ return false;
+ }
+ return this->emitPopPtr(E);
+ };
+
+ return initArrayDimension(E->getType());
}
return false;
diff --git a/clang/test/AST/ByteCode/arrays.cpp b/clang/test/AST/ByteCode/arrays.cpp
index 22a4b41041eb3..eaf9559e40cda 100644
--- a/clang/test/AST/ByteCode/arrays.cpp
+++ b/clang/test/AST/ByteCode/arrays.cpp
@@ -820,3 +820,14 @@ namespace FAM {
return 1;
}
}
+
+namespace MultiDimConstructExpr {
+ struct a {
+ a *p = this;
+ };
+ struct b {
+ a m[3][3];
+ };
+ constexpr b d;
+ static_assert(d.m[2][1].p == &d.m[2][1]);
+}
|
This is a thing apparently. Fixes llvm#153803
This is a thing apparently. Fixes llvm#153803
This is a thing apparently. Fixes llvm#153803
| if (!T->isArrayType()) { | ||
| // Constructor arguments. | ||
| for (const auto *Arg : E->arguments()) { | ||
| if (!this->visit(Arg)) |
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.
Does this if statement and it's body get tested by the tests? It does not look like it but maybe I misunderstand.
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.
The return false case is hit when direct expression evaluation fails or when we hit the size limit of the bytecode emitter. It's not tested in the here attached test case I think, but at least the expression case is tested in an existing test.
| if (!this->visit(Arg)) | ||
| return false; | ||
| std::function<bool(QualType)> initArrayDimension; | ||
| initArrayDimension = [&](QualType T) -> bool { |
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.
Why not just initialize initArrayDimension directly?
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.
I don't thinkt that works if it calls itself.
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.
I completely missed that aspect but that is what Deducing This is for, you pass in this auto &self.
Edit: never mind, not available in C++17 except as an extension :-(
This is a thing apparently.
Fixes #153803