Skip to content

Commit 51295d6

Browse files
authored
[clang][bytecode] Reject assignments in C (#136126)
Similar to what the current interpreter does.
1 parent a84a6f7 commit 51295d6

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,12 @@ bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
863863
return this->VisitPointerArithBinOp(BO);
864864
}
865865

866-
// Assignmentes require us to evalute the RHS first.
866+
// Assignments require us to evalute the RHS first.
867867
if (BO->getOpcode() == BO_Assign) {
868+
// We don't support assignments in C.
869+
if (!Ctx.getLangOpts().CPlusPlus)
870+
return this->emitInvalid(BO);
871+
868872
if (!visit(RHS) || !visit(LHS))
869873
return false;
870874
if (!this->emitFlip(*LT, *RT, BO))

clang/test/AST/ByteCode/c2y.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple x86_64-linux %s -std=c2y -verify=expected,both -fexperimental-new-constant-interpreter
2+
// RUN: %clang_cc1 -triple x86_64-linux %s -std=c2y -verify=ref,both
3+
4+
// both-no-diagnostics
5+
6+
struct S {
7+
int x;
8+
char c;
9+
float f;
10+
};
11+
12+
#define DECL_BUFFER(Ty, Name) alignas(Ty) unsigned char Name[sizeof(Ty)]
13+
14+
struct T {
15+
DECL_BUFFER(struct S, buffer);
16+
};
17+
18+
int quorble() {
19+
DECL_BUFFER(struct T, buffer);
20+
((struct S *)((struct T *)buffer)->buffer)->x = 12;
21+
const struct S *s_ptr = (struct S *)((struct T *)buffer)->buffer;
22+
return s_ptr->x;
23+
}

0 commit comments

Comments
 (0)