[clang] [clang][bytecode] Fix ToType/FromType diagnostic ordering (PR #116988)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 07:26:45 PST 2024


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

We need to check the ToType first, then the FromType. Additionally, remove qualifiers from the parent type of the field we're emitting a note for.

>From 328b8fc8faf08cf68036f3bdc67bcfeeb5cfd8c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 20 Nov 2024 15:36:11 +0100
Subject: [PATCH] [clang][bytecode] Fix ToType/FromType diagnostic ordering

We need to check the ToType first, then the FromType. Additionally,
remove qualifiers from the parent type of the field we're emitting
a note for.
---
 clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp |  7 +++----
 clang/test/AST/ByteCode/builtin-bit-cast.cpp    | 11 +++++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
index 7e8853d3469317..b1230f92ddf1d4 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp
@@ -254,7 +254,7 @@ static bool CheckBitcastType(InterpState &S, CodePtr OpPC, QualType T,
   };
   auto note = [&](int Construct, QualType NoteType, SourceRange NoteRange) {
     S.Note(NoteRange.getBegin(), diag::note_constexpr_bit_cast_invalid_subtype)
-        << NoteType << Construct << T << NoteRange;
+        << NoteType << Construct << T.getUnqualifiedType() << NoteRange;
     return false;
   };
 
@@ -388,11 +388,10 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
   QualType FromType = FromPtr.getType();
   QualType ToType = ToPtr.getType();
 
-  if (!CheckBitcastType(S, OpPC, FromType, /*IsToType=*/false))
-    return false;
-
   if (!CheckBitcastType(S, OpPC, ToType, /*IsToType=*/true))
     return false;
+  if (!CheckBitcastType(S, OpPC, FromType, /*IsToType=*/false))
+    return false;
 
   BitcastBuffer Buffer;
   readPointerToBuffer(S.getContext(), FromPtr, Buffer,
diff --git a/clang/test/AST/ByteCode/builtin-bit-cast.cpp b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
index 60e8c3a615c5e6..3c6cd0aa36e7fe 100644
--- a/clang/test/AST/ByteCode/builtin-bit-cast.cpp
+++ b/clang/test/AST/ByteCode/builtin-bit-cast.cpp
@@ -145,6 +145,17 @@ namespace Fail {
                                                 // both-note {{initializer of 'a' is not a constant expression}}
 }
 
+namespace ToPtr {
+  struct S {
+    const int *p = nullptr;
+  };
+  struct P {
+    const int *p; // both-note {{invalid type 'const int *' is a member of 'ToPtr::P'}}
+  };
+  constexpr P p = __builtin_bit_cast(P, S{}); // both-error {{must be initialized by a constant expression}} \
+                                              // both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
+}
+
 namespace NullPtr {
   constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
   static_assert(N == nullptr);



More information about the cfe-commits mailing list