Skip to content

Commit

Permalink
[clang][bytecode] Don't discard all void-typed expressions (llvm#105625)
Browse files Browse the repository at this point in the history
For void-types InitListExprs, we need to diagnose them as invalid. But
only if we are _not_ discarding.
  • Loading branch information
tbaederr authored Aug 22, 2024
1 parent e338936 commit c79d1fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
23 changes: 9 additions & 14 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,15 +1318,6 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
template <class Emitter>
bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
const Expr *ArrayFiller, const Expr *E) {

QualType QT = E->getType();

if (const auto *AT = QT->getAs<AtomicType>())
QT = AT->getValueType();

if (QT->isVoidType())
return this->emitInvalid(E);

// Handle discarding first.
if (DiscardResult) {
for (const Expr *Init : Inits) {
Expand All @@ -1336,6 +1327,13 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
return true;
}

QualType QT = E->getType();
if (const auto *AT = QT->getAs<AtomicType>())
QT = AT->getValueType();

if (QT->isVoidType())
return this->emitInvalid(E);

// Primitive values.
if (std::optional<PrimType> T = classify(QT)) {
assert(!DiscardResult);
Expand Down Expand Up @@ -3251,12 +3249,9 @@ template <class Emitter> bool Compiler<Emitter>::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<unsigned> LocalIndex = allocateLocal(E);
if (!LocalIndex)
return false;
Expand Down
1 change: 1 addition & 0 deletions clang/test/AST/ByteCode/literals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c79d1fa

Please sign in to comment.