Skip to content

Commit 476b208

Browse files
authored
[clang][bytecode] Fix ToType/FromType diagnostic ordering (#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.
1 parent e9c561e commit 476b208

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static bool CheckBitcastType(InterpState &S, CodePtr OpPC, QualType T,
254254
};
255255
auto note = [&](int Construct, QualType NoteType, SourceRange NoteRange) {
256256
S.Note(NoteRange.getBegin(), diag::note_constexpr_bit_cast_invalid_subtype)
257-
<< NoteType << Construct << T << NoteRange;
257+
<< NoteType << Construct << T.getUnqualifiedType() << NoteRange;
258258
return false;
259259
};
260260

@@ -388,11 +388,10 @@ bool clang::interp::DoBitCastPtr(InterpState &S, CodePtr OpPC,
388388
QualType FromType = FromPtr.getType();
389389
QualType ToType = ToPtr.getType();
390390

391-
if (!CheckBitcastType(S, OpPC, FromType, /*IsToType=*/false))
392-
return false;
393-
394391
if (!CheckBitcastType(S, OpPC, ToType, /*IsToType=*/true))
395392
return false;
393+
if (!CheckBitcastType(S, OpPC, FromType, /*IsToType=*/false))
394+
return false;
396395

397396
BitcastBuffer Buffer;
398397
readPointerToBuffer(S.getContext(), FromPtr, Buffer,

clang/test/AST/ByteCode/builtin-bit-cast.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ namespace Fail {
145145
// both-note {{initializer of 'a' is not a constant expression}}
146146
}
147147

148+
namespace ToPtr {
149+
struct S {
150+
const int *p = nullptr;
151+
};
152+
struct P {
153+
const int *p; // both-note {{invalid type 'const int *' is a member of 'ToPtr::P'}}
154+
};
155+
constexpr P p = __builtin_bit_cast(P, S{}); // both-error {{must be initialized by a constant expression}} \
156+
// both-note {{bit_cast to a pointer type is not allowed in a constant expression}}
157+
}
158+
148159
namespace NullPtr {
149160
constexpr nullptr_t N = __builtin_bit_cast(nullptr_t, (intptr_t)1u);
150161
static_assert(N == nullptr);

0 commit comments

Comments
 (0)