From c79d1fa540390f6e37e1ea326153559eeadd0de6 Mon Sep 17 00:00:00 2001 From: Timm Baeder Date: Thu, 22 Aug 2024 09:51:16 +0200 Subject: [PATCH] [clang][bytecode] Don't discard all void-typed expressions (#105625) For void-types InitListExprs, we need to diagnose them as invalid. But only if we are _not_ discarding. --- clang/lib/AST/ByteCode/Compiler.cpp | 23 +++++++++-------------- clang/test/AST/ByteCode/literals.cpp | 1 + 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 6d05f75131640a..10f3222726fd43 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -1318,15 +1318,6 @@ bool Compiler::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { template bool Compiler::visitInitList(ArrayRef Inits, const Expr *ArrayFiller, const Expr *E) { - - QualType QT = E->getType(); - - if (const auto *AT = QT->getAs()) - QT = AT->getValueType(); - - if (QT->isVoidType()) - return this->emitInvalid(E); - // Handle discarding first. if (DiscardResult) { for (const Expr *Init : Inits) { @@ -1336,6 +1327,13 @@ bool Compiler::visitInitList(ArrayRef Inits, return true; } + QualType QT = E->getType(); + if (const auto *AT = QT->getAs()) + QT = AT->getValueType(); + + if (QT->isVoidType()) + return this->emitInvalid(E); + // Primitive values. if (std::optional T = classify(QT)) { assert(!DiscardResult); @@ -3251,12 +3249,9 @@ template bool Compiler::visit(const Expr *E) { if (E->getType().isNull()) return false; - if (E->getType()->isVoidType()) - return this->discard(E); - // Create local variable to hold the return value. - if (!E->isGLValue() && !E->getType()->isAnyComplexType() && - !classify(E->getType())) { + if (!E->getType()->isVoidType() && !E->isGLValue() && + !E->getType()->isAnyComplexType() && !classify(E->getType())) { std::optional LocalIndex = allocateLocal(E); if (!LocalIndex) return false; diff --git a/clang/test/AST/ByteCode/literals.cpp b/clang/test/AST/ByteCode/literals.cpp index a46f6ed747ec2f..2329d4d973f01d 100644 --- a/clang/test/AST/ByteCode/literals.cpp +++ b/clang/test/AST/ByteCode/literals.cpp @@ -46,6 +46,7 @@ static_assert(Failed2 == 0, ""); // both-error {{not an integral constant expres // both-note {{initializer of 'Failed2' is not a constant expression}} const int x = *(volatile int*)0x1234; +static_assert((void{}, true), ""); namespace ScalarTypes { constexpr int ScalarInitInt = int();