Skip to content

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

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
Nov 21, 2024

Conversation

tbaederr
Copy link
Contributor

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.

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.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 20, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp (+3-4)
  • (modified) clang/test/AST/ByteCode/builtin-bit-cast.cpp (+11)
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);

@tbaederr tbaederr merged commit 476b208 into llvm:main Nov 21, 2024
11 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