[clang] [clang][bytecode] Diagnose IntegralToPointer casts to non-void (PR #123619)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 20 06:16:19 PST 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/123619

But keep evaluating. This is what the current interpreter does as well.

>From 3ba917b703c05da898515923e17db03850c807e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 20 Jan 2025 14:19:48 +0100
Subject: [PATCH] [clang][bytecode] Diagnose IntegralToPointer casts to
 non-void

But keep evaluating. This is what the current interpreter does as well.
---
 clang/lib/AST/ByteCode/Interp.h               | 4 ++++
 clang/test/AST/ByteCode/constexpr.c           | 4 ++--
 clang/test/SemaCXX/builtin-assume-aligned.cpp | 1 -
 3 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 58c0256c7d7df8..11e9738eff5dcd 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2693,6 +2693,10 @@ template <PrimType Name, class T = typename PrimConv<Name>::T>
 inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Descriptor *Desc) {
   const T &IntVal = S.Stk.pop<T>();
 
+  if (Desc)
+    S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast)
+        << 2 << S.getLangOpts().CPlusPlus;
+
   S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Desc);
   return true;
 }
diff --git a/clang/test/AST/ByteCode/constexpr.c b/clang/test/AST/ByteCode/constexpr.c
index fed24fa72b2541..af96bf3a06f375 100644
--- a/clang/test/AST/ByteCode/constexpr.c
+++ b/clang/test/AST/ByteCode/constexpr.c
@@ -309,8 +309,8 @@ constexpr const int *V81 = &V80;
 constexpr int *V82 = 0;
 constexpr int *V83 = V82;
 constexpr int *V84 = 42;
-// ref-error at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
-// ref-note at -2 {{this conversion is not allowed in a constant expression}}
+// both-error at -1 {{constexpr variable 'V84' must be initialized by a constant expression}}
+// both-note at -2 {{this conversion is not allowed in a constant expression}}
 // both-error at -3 {{constexpr pointer initializer is not null}}
 constexpr int *V85 = nullptr;
 
diff --git a/clang/test/SemaCXX/builtin-assume-aligned.cpp b/clang/test/SemaCXX/builtin-assume-aligned.cpp
index 85a7faee916181..48bd8414fc50a1 100644
--- a/clang/test/SemaCXX/builtin-assume-aligned.cpp
+++ b/clang/test/SemaCXX/builtin-assume-aligned.cpp
@@ -1,5 +1,4 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s -fexperimental-new-constant-interpreter
 
 int n;
 constexpr int *p = 0;



More information about the cfe-commits mailing list