Skip to content

Commit ad941fa

Browse files
committed
Factor common code for quoting a builtin name
This shows up in several places in order to match the quoting of other uses of the same diagnostic. Handling it centrally simplifies the code and reduces changes if the storage for builtin names changes. This refactoring is extracted out of llvm#120534 as requested in code review.
1 parent 9d031c4 commit ad941fa

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

clang/include/clang/Basic/Builtins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class Context {
102102
/// e.g. "__builtin_abs".
103103
llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }
104104

105+
/// Return a quoted name for the specified builtin for use in diagnostics.
106+
std::string getQuotedName(unsigned ID) const;
107+
105108
/// Get the type descriptor string for the specified builtin.
106109
const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }
107110

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static void diagnoseNonConstexprBuiltin(InterpState &S, CodePtr OpPC,
154154
if (S.getLangOpts().CPlusPlus11)
155155
S.CCEDiag(Loc, diag::note_constexpr_invalid_function)
156156
<< /*isConstexpr=*/0 << /*isConstructor=*/0
157-
<< ("'" + S.getASTContext().BuiltinInfo.getName(ID) + "'").str();
157+
<< S.getASTContext().BuiltinInfo.getQuotedName(ID);
158158
else
159159
S.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr);
160160
}
@@ -1948,7 +1948,7 @@ static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
19481948
!isOneByteCharacterType(PtrB.getType()))) {
19491949
S.FFDiag(S.Current->getSource(OpPC),
19501950
diag::note_constexpr_memcmp_unsupported)
1951-
<< ("'" + ASTCtx.BuiltinInfo.getName(ID) + "'").str() << PtrA.getType()
1951+
<< ASTCtx.BuiltinInfo.getQuotedName(ID) << PtrA.getType()
19521952
<< PtrB.getType();
19531953
return false;
19541954
}

clang/lib/AST/ExprConstant.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9858,7 +9858,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
98589858
if (Info.getLangOpts().CPlusPlus11)
98599859
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
98609860
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
9861-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
9861+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
98629862
else
98639863
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
98649864
[[fallthrough]];
@@ -9903,8 +9903,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
99039903
// FIXME: We can compare the bytes in the correct order.
99049904
if (IsRawByte && !isOneByteCharacterType(CharTy)) {
99059905
Info.FFDiag(E, diag::note_constexpr_memchr_unsupported)
9906-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str()
9907-
<< CharTy;
9906+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy;
99089907
return false;
99099908
}
99109909
// Figure out what value we're actually looking for (after converting to
@@ -9966,7 +9965,7 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
99669965
if (Info.getLangOpts().CPlusPlus11)
99679966
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
99689967
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
9969-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
9968+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
99709969
else
99719970
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
99729971
[[fallthrough]];
@@ -13241,7 +13240,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1324113240
if (Info.getLangOpts().CPlusPlus11)
1324213241
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
1324313242
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
13244-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
13243+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
1324513244
else
1324613245
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
1324713246
[[fallthrough]];
@@ -13266,7 +13265,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1326613265
if (Info.getLangOpts().CPlusPlus11)
1326713266
Info.CCEDiag(E, diag::note_constexpr_invalid_function)
1326813267
<< /*isConstexpr*/ 0 << /*isConstructor*/ 0
13269-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str();
13268+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp);
1327013269
else
1327113270
Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr);
1327213271
[[fallthrough]];
@@ -13321,8 +13320,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1332113320
!(isOneByteCharacterType(CharTy1) && isOneByteCharacterType(CharTy2))) {
1332213321
// FIXME: Consider using our bit_cast implementation to support this.
1332313322
Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported)
13324-
<< ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'").str()
13325-
<< CharTy1 << CharTy2;
13323+
<< Info.Ctx.BuiltinInfo.getQuotedName(BuiltinOp) << CharTy1
13324+
<< CharTy2;
1332613325
return false;
1332713326
}
1332813327

clang/lib/Basic/Builtins.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
163163
}
164164
}
165165

166+
std::string Builtin::Context::getQuotedName(unsigned ID) const {
167+
return (llvm::Twine("'") + getName(ID) + "'").str();
168+
}
169+
166170
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
167171
const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
168172
if (!WidthPos)

0 commit comments

Comments
 (0)