[clang] [clang][bytecode] Narrow pointer in UO_Deref unary operators (PR #113089)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 20 09:17:08 PDT 2024
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/113089
>From 6942291d0f529baa323cfd35e2f5d9b4f818c0f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 20 Oct 2024 13:57:49 +0200
Subject: [PATCH] [clang][bytecode] Narrow pointer in UO_Deref unary operators
Otherwise we treat this like an array element even though we should
treat it as a single object.
---
clang/lib/AST/ByteCode/Compiler.cpp | 12 ++++++++++--
clang/lib/AST/ByteCode/Pointer.cpp | 2 +-
clang/test/AST/ByteCode/cxx98.cpp | 5 +++++
3 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 672fa7fc25d6d0..3f068aa8c189a9 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5738,9 +5738,17 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
// We should already have a pointer when we get here.
return this->delegate(SubExpr);
case UO_Deref: // *x
- if (DiscardResult)
+ if (DiscardResult) {
+ // assert(false);
return this->discard(SubExpr);
- return this->visit(SubExpr);
+ }
+
+ if (!this->visit(SubExpr))
+ return false;
+ if (classifyPrim(SubExpr) == PT_Ptr)
+ return this->emitNarrowPtr(E);
+ return true;
+
case UO_Not: // ~x
if (!T)
return this->emitError(E);
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 75b00dcb2ab242..c9de039c195d94 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -635,7 +635,7 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
// Return the composite type.
APValue Result;
- if (!Composite(getType(), *this, Result))
+ if (!Composite(ResultType, *this, Result))
return std::nullopt;
return Result;
}
diff --git a/clang/test/AST/ByteCode/cxx98.cpp b/clang/test/AST/ByteCode/cxx98.cpp
index 471a58f8e05518..20f98d33c31c4f 100644
--- a/clang/test/AST/ByteCode/cxx98.cpp
+++ b/clang/test/AST/ByteCode/cxx98.cpp
@@ -54,3 +54,8 @@ _Static_assert(a == 0, ""); // both-error {{static assertion expression is not a
struct SelfReference { SelfReference &r; };
extern SelfReference self_reference_1;
SelfReference self_reference_2 = {self_reference_1};
+
+struct PR65784s{
+ int *ptr;
+} const PR65784[] = {(int *)""};
+PR65784s PR65784f() { return *PR65784; }
More information about the cfe-commits
mailing list