Skip to content

[clang][bytecode] Reject assignments in C #136126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged

Conversation

tbaederr
Copy link
Contributor

Similar to what the current interpreter does.

Similar to what the current interpreter does.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 17, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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

@tbaederr tbaederr merged commit 51295d6 into llvm:main Apr 17, 2025
12 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants