Skip to content

Factor common code for quoting a builtin name #120835

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
Jan 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
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/Builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Context {
/// e.g. "__builtin_abs".
llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }

/// Return a quoted name for the specified builtin for use in diagnostics.
std::string getQuotedName(unsigned ID) const;

/// Get the type descriptor string for the specified builtin.
const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }

Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ByteCode/InterpBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void diagnoseNonConstexprBuiltin(InterpState &S, CodePtr OpPC,
if (S.getLangOpts().CPlusPlus11)
S.CCEDiag(Loc, diag::note_constexpr_invalid_function)
<< /*isConstexpr=*/0 << /*isConstructor=*/0
<< ("'" + S.getASTContext().BuiltinInfo.getName(ID) + "'").str();
<< S.getASTContext().BuiltinInfo.getQuotedName(ID);
else
S.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
}
Expand Down Expand Up @@ -1948,7 +1948,7 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
!isOneByteCharacterType(PtrB.getType()))) {
S.FFDiag(S.Current->getSource(OpPC),
diag::note_constexpr_memcmp_unsupported)
<< ("'" + ASTCtx.BuiltinInfo.getName(ID) + "'").str() << PtrA.getType()
<< ASTCtx.BuiltinInfo.getQuotedName(ID) << PtrA.getType()
<< PtrB.getType();
return false;
}
Expand Down
15 changes: 7 additions & 8 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9858,7 +9858,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (Info.getLangOpts().CPlusPlus11)
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
else
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
[[fallthrough]];
Expand Down Expand Up @@ -9903,8 +9903,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
// FIXME: We can compare the bytes in the correct order.
if (IsRawByte && !isOneByteCharacterType(CharTy)) {
Info.FFDiag(E, diag::note_constexpr_memchr_unsupported)
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str()
<< CharTy;
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy;
return false;
}
// Figure out what value we're actually looking for (after converting to
Expand Down Expand Up @@ -9966,7 +9965,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (Info.getLangOpts().CPlusPlus11)
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
else
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
[[fallthrough]];
Expand Down Expand Up @@ -13241,7 +13240,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (Info.getLangOpts().CPlusPlus11)
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
else
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
[[fallthrough]];
Expand All @@ -13266,7 +13265,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (Info.getLangOpts().CPlusPlus11)
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
else
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
[[fallthrough]];
Expand Down Expand Up @@ -13321,8 +13320,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
!(isOneByteCharacterType(CharTy1) && isOneByteCharacterType(CharTy2))) {
// FIXME: Consider using our bit_cast implementation to support this.
Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported)
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str()
<< CharTy1 << CharTy2;
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy1
<< CharTy2;
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Basic/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
}
}

std::string Builtin::Context::getQuotedName(unsigned ID) const {
return (llvm::Twine("'") + getName(ID) + "'").str();
}

unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
if (!WidthPos)
Expand Down
Loading