[clang] [clang][bytecode] Reject assignments in C (PR #136126)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 17 04:04:12 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Similar to what the current interpreter does.

---
Full diff: https://github.com/llvm/llvm-project/pull/136126.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+5-1) 
- (added) clang/test/AST/ByteCode/c2y.c (+23) 


``````````diff
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;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/136126


More information about the cfe-commits mailing list