Skip to content

Commit 7b4b85b

Browse files
authored
[clang][bytecode] Reject void InitListExpr differently (llvm#105802)
This reverts c79d1fa and 125aa10 Instead, use the previous approach but allow void-typed InitListExprs with 0 initializers.
1 parent 38b8e54 commit 7b4b85b

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,16 @@ bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
13441344
template <class Emitter>
13451345
bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
13461346
const Expr *ArrayFiller, const Expr *E) {
1347+
QualType QT = E->getType();
1348+
if (const auto *AT = QT->getAs<AtomicType>())
1349+
QT = AT->getValueType();
1350+
1351+
if (QT->isVoidType()) {
1352+
if (Inits.size() == 0)
1353+
return true;
1354+
return this->emitInvalid(E);
1355+
}
1356+
13471357
// Handle discarding first.
13481358
if (DiscardResult) {
13491359
for (const Expr *Init : Inits) {
@@ -1353,13 +1363,6 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
13531363
return true;
13541364
}
13551365

1356-
QualType QT = E->getType();
1357-
if (const auto *AT = QT->getAs<AtomicType>())
1358-
QT = AT->getValueType();
1359-
1360-
if (QT->isVoidType())
1361-
return this->emitInvalid(E);
1362-
13631366
// Primitive values.
13641367
if (std::optional<PrimType> T = classify(QT)) {
13651368
assert(!DiscardResult);
@@ -3272,9 +3275,12 @@ template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
32723275
if (E->getType().isNull())
32733276
return false;
32743277

3278+
if (E->getType()->isVoidType())
3279+
return this->discard(E);
3280+
32753281
// Create local variable to hold the return value.
3276-
if (!E->getType()->isVoidType() && !E->isGLValue() &&
3277-
!E->getType()->isAnyComplexType() && !classify(E->getType())) {
3282+
if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
3283+
!classify(E->getType())) {
32783284
std::optional<unsigned> LocalIndex = allocateLocal(E);
32793285
if (!LocalIndex)
32803286
return false;
@@ -5174,7 +5180,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
51745180
// We should already have a pointer when we get here.
51755181
return this->delegate(SubExpr);
51765182
case UO_Deref: // *x
5177-
if (DiscardResult || E->getType()->isVoidType())
5183+
if (DiscardResult)
51785184
return this->discard(SubExpr);
51795185
return this->visit(SubExpr);
51805186
case UO_Not: // ~x

clang/test/AST/ByteCode/c.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ void T1(void) {
298298
enum teste1 test1f(void), (*test1)(void) = test1f; // pedantic-warning {{ISO C forbids forward references to 'enum' types}}
299299
enum teste1 { TEST1 };
300300

301-
302301
void func(void) {
303302
_Static_assert(func + 1 - func == 1, ""); // pedantic-warning {{arithmetic on a pointer to the function type}} \
304303
// pedantic-warning {{arithmetic on pointers to the function type}} \
@@ -313,3 +312,9 @@ void func(void) {
313312
func - 0xdead000000000000UL; // all-warning {{expression result unused}} \
314313
// pedantic-warning {{arithmetic on a pointer to the function type}}
315314
}
315+
316+
void foo3 (void)
317+
{
318+
void* x = 0;
319+
void* y = &*x;
320+
}

0 commit comments

Comments
 (0)