Skip to content

[clang][bytecode] Fix diagnostic difference with opaque call cmps #129702

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
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,14 @@ inline bool CmpHelper<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
}
}

static inline bool IsOpaqueConstantCall(const CallExpr *E) {
unsigned Builtin = E->getBuiltinCallee();
return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
Builtin == Builtin::BI__builtin_function_start);
}

template <>
inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
using BoolT = PrimConv<PT_Bool>::T;
Expand Down Expand Up @@ -1065,11 +1073,19 @@ inline bool CmpHelperEQ<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
for (const auto &P : {LHS, RHS}) {
if (P.isZero())
continue;
if (BothNonNull && P.pointsToLiteral() &&
isa<StringLiteral>(P.getDeclDesc()->asExpr())) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_literal_comparison);
return false;
if (BothNonNull && P.pointsToLiteral()) {
const Expr *E = P.getDeclDesc()->asExpr();
if (isa<StringLiteral>(E)) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_literal_comparison);
return false;
} else if (const auto *CE = dyn_cast<CallExpr>(E);
CE && IsOpaqueConstantCall(CE)) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_opaque_call_comparison)
<< P.toDiagnosticString(S.getASTContext());
return false;
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions clang/test/AST/ByteCode/builtin-functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,8 @@ namespace shufflevector {

namespace FunctionStart {
void a(void) {}
static_assert(__builtin_function_start(a) == a, ""); // ref-error {{not an integral constant expression}} \
// ref-note {{comparison against opaque constant address '&__builtin_function_start(a)'}} \
// expected-error {{static assertion failed}}
static_assert(__builtin_function_start(a) == a, ""); // both-error {{not an integral constant expression}} \
// both-note {{comparison against opaque constant address '&__builtin_function_start(a)'}}
}

namespace BuiltinInImplicitCtor {
Expand Down