[clang] [clang][bytecode] Reject assignments in C (PR #136126)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 04:03:37 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/136126
Similar to what the current interpreter does.
>From 646321191ac22da6ff4c83031ea995da4a2f8756 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 17 Apr 2025 13:02:45 +0200
Subject: [PATCH] [clang][bytecode] Reject assignments in C
Similar to what the current interpreter does.
---
clang/lib/AST/ByteCode/Compiler.cpp | 6 +++++-
clang/test/AST/ByteCode/c2y.c | 23 +++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
create mode 100644 clang/test/AST/ByteCode/c2y.c
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 157e306e5cdb3..d6c8817610742 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -863,8 +863,12 @@ bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
return this->VisitPointerArithBinOp(BO);
}
- // Assignmentes require us to evalute the RHS first.
+ // Assignments require us to evalute the RHS first.
if (BO->getOpcode() == BO_Assign) {
+ // We don't support assignments in C.
+ if (!Ctx.getLangOpts().CPlusPlus)
+ return this->emitInvalid(BO);
+
if (!visit(RHS) || !visit(LHS))
return false;
if (!this->emitFlip(*LT, *RT, BO))
diff --git a/clang/test/AST/ByteCode/c2y.c b/clang/test/AST/ByteCode/c2y.c
new file mode 100644
index 0000000000000..bbb163ab83833
--- /dev/null
+++ b/clang/test/AST/ByteCode/c2y.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple x86_64-linux %s -std=c2y -verify=expected,both -fexperimental-new-constant-interpreter
+// RUN: %clang_cc1 -triple x86_64-linux %s -std=c2y -verify=ref,both
+
+// both-no-diagnostics
+
+struct S {
+ int x;
+ char c;
+ float f;
+};
+
+#define DECL_BUFFER(Ty, Name) alignas(Ty) unsigned char Name[sizeof(Ty)]
+
+struct T {
+ DECL_BUFFER(struct S, buffer);
+};
+
+int quorble() {
+ DECL_BUFFER(struct T, buffer);
+ ((struct S *)((struct T *)buffer)->buffer)->x = 12;
+ const struct S *s_ptr = (struct S *)((struct T *)buffer)->buffer;
+ return s_ptr->x;
+}
More information about the cfe-commits
mailing list