Skip to content

Commit 001cc34

Browse files
authored
[clang] Add scoped enum support to StreamingDiagnostic (llvm#138089)
This patch adds templated `operator<<` for diagnostics that pass scoped enums, saving people from `llvm::to_underlying()` clutter on the side of emitting the diagnostic. This eliminates 80 out of 220 usages of `llvm::to_underlying()` in Clang. I also backported `std::is_scoped_enum_v` from C++23.
1 parent a6459de commit 001cc34

25 files changed

+105
-125
lines changed

clang/include/clang/Basic/Diagnostic.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,22 @@ operator<<(const StreamingDiagnostic &DB, T *DC) {
14291429
return DB;
14301430
}
14311431

1432+
// Convert scope enums to their underlying type, so that we don't have
1433+
// clutter the emitting code with `llvm::to_underlying()`.
1434+
// We also need to disable implicit conversion for the first argument,
1435+
// because classes that derive from StreamingDiagnostic define their own
1436+
// templated operator<< that accept a wide variety of types, leading
1437+
// to ambiguity.
1438+
template <typename T, typename U>
1439+
inline std::enable_if_t<
1440+
std::is_same_v<std::remove_const_t<T>, StreamingDiagnostic> &&
1441+
llvm::is_scoped_enum_v<std::remove_reference_t<U>>,
1442+
const StreamingDiagnostic &>
1443+
operator<<(const T &DB, U &&SE) {
1444+
DB << llvm::to_underlying(SE);
1445+
return DB;
1446+
}
1447+
14321448
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
14331449
SourceLocation L) {
14341450
DB.AddSourceRange(CharSourceRange::getTokenRange(L));

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,6 @@ enum class AssignmentAction {
220220
Casting,
221221
Passing_CFAudited
222222
};
223-
inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
224-
const AssignmentAction &AA) {
225-
DB << llvm::to_underlying(AA);
226-
return DB;
227-
}
228223

229224
namespace threadSafety {
230225
class BeforeSet;
@@ -15471,12 +15466,6 @@ void Sema::PragmaStack<Sema::AlignPackInfo>::Act(SourceLocation PragmaLocation,
1547115466
llvm::StringRef StackSlotLabel,
1547215467
AlignPackInfo Value);
1547315468

15474-
inline const StreamingDiagnostic &
15475-
operator<<(const StreamingDiagnostic &DB, Sema::StringEvaluationContext Ctx) {
15476-
DB << llvm::to_underlying(Ctx);
15477-
return DB;
15478-
}
15479-
1548015469
} // end namespace clang
1548115470

1548215471
#endif

clang/lib/AST/ODRDiagsEmitter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,8 @@ bool ODRDiagsEmitter::diagnoseSubMismatchObjCMethod(
461461
}
462462
if (FirstMethod->getImplementationControl() !=
463463
SecondMethod->getImplementationControl()) {
464-
DiagError(ControlLevel)
465-
<< llvm::to_underlying(FirstMethod->getImplementationControl());
466-
DiagNote(ControlLevel) << llvm::to_underlying(
467-
SecondMethod->getImplementationControl());
464+
DiagError(ControlLevel) << FirstMethod->getImplementationControl();
465+
DiagNote(ControlLevel) << SecondMethod->getImplementationControl();
468466
return true;
469467
}
470468
if (FirstMethod->isThisDeclarationADesignatedInitializer() !=

clang/lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2578,7 +2578,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
25782578
if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
25792579
D.isFirstDeclarator()) {
25802580
Diag(CommaLoc, diag::err_multiple_template_declarators)
2581-
<< llvm::to_underlying(TemplateInfo.Kind);
2581+
<< TemplateInfo.Kind;
25822582
}
25832583

25842584
// Parse the next declarator.

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3472,7 +3472,7 @@ Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclaration(
34723472
if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
34733473
DeclaratorInfo.isFirstDeclarator()) {
34743474
Diag(CommaLoc, diag::err_multiple_template_declarators)
3475-
<< llvm::to_underlying(TemplateInfo.Kind);
3475+
<< TemplateInfo.Kind;
34763476
}
34773477

34783478
// Parse the next declarator.

clang/lib/Parse/ParsePragma.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,8 @@ void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
23412341
SourceLocation PragmaLocation = Tok.getLocation();
23422342
PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
23432343
if (Tok.isNot(tok::equal)) {
2344-
PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << llvm::to_underlying(SecKind);
2344+
PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal)
2345+
<< SecKind;
23452346
return;
23462347
}
23472348

clang/lib/Parse/Parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void Parser::ConsumeExtraSemi(ExtraSemiKind Kind, DeclSpec::TST TST) {
226226

227227
if (Kind != ExtraSemiKind::AfterMemberFunctionDefinition || HadMultipleSemis)
228228
Diag(StartLoc, diag::ext_extra_semi)
229-
<< llvm::to_underlying(Kind)
229+
<< Kind
230230
<< DeclSpec::getSpecifierName(
231231
TST, Actions.getASTContext().getPrintingPolicy())
232232
<< FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));

clang/lib/Sema/SemaAccess.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,24 +1670,21 @@ Sema::AccessResult Sema::CheckConstructorAccess(SourceLocation UseLoc,
16701670
case InitializedEntity::EK_Base:
16711671
PD = PDiag(diag::err_access_base_ctor);
16721672
PD << Entity.isInheritedVirtualBase()
1673-
<< Entity.getBaseSpecifier()->getType()
1674-
<< llvm::to_underlying(getSpecialMember(Constructor));
1673+
<< Entity.getBaseSpecifier()->getType() << getSpecialMember(Constructor);
16751674
break;
16761675

16771676
case InitializedEntity::EK_Member:
16781677
case InitializedEntity::EK_ParenAggInitMember: {
16791678
const FieldDecl *Field = cast<FieldDecl>(Entity.getDecl());
16801679
PD = PDiag(diag::err_access_field_ctor);
1681-
PD << Field->getType()
1682-
<< llvm::to_underlying(getSpecialMember(Constructor));
1680+
PD << Field->getType() << getSpecialMember(Constructor);
16831681
break;
16841682
}
16851683

16861684
case InitializedEntity::EK_LambdaCapture: {
16871685
StringRef VarName = Entity.getCapturedVarName();
16881686
PD = PDiag(diag::err_access_lambda_capture);
1689-
PD << VarName << Entity.getType()
1690-
<< llvm::to_underlying(getSpecialMember(Constructor));
1687+
PD << VarName << Entity.getType() << getSpecialMember(Constructor);
16911688
break;
16921689
}
16931690

clang/lib/Sema/SemaCUDA.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ bool SemaCUDA::inferTargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
450450
if (Diagnose) {
451451
Diag(ClassDecl->getLocation(),
452452
diag::note_implicit_member_target_infer_collision)
453-
<< (unsigned)CSM << llvm::to_underlying(*InferredTarget)
454-
<< llvm::to_underlying(BaseMethodTarget);
453+
<< (unsigned)CSM << *InferredTarget << BaseMethodTarget;
455454
}
456455
MemberDecl->addAttr(
457456
CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
@@ -496,8 +495,7 @@ bool SemaCUDA::inferTargetForImplicitSpecialMember(CXXRecordDecl *ClassDecl,
496495
if (Diagnose) {
497496
Diag(ClassDecl->getLocation(),
498497
diag::note_implicit_member_target_infer_collision)
499-
<< (unsigned)CSM << llvm::to_underlying(*InferredTarget)
500-
<< llvm::to_underlying(FieldMethodTarget);
498+
<< (unsigned)CSM << *InferredTarget << FieldMethodTarget;
501499
}
502500
MemberDecl->addAttr(
503501
CUDAInvalidTargetAttr::CreateImplicit(getASTContext()));
@@ -713,7 +711,7 @@ void SemaCUDA::checkAllowedInitializer(VarDecl *VD) {
713711
if (InitFnTarget != CUDAFunctionTarget::Host &&
714712
InitFnTarget != CUDAFunctionTarget::HostDevice) {
715713
Diag(VD->getLocation(), diag::err_ref_bad_target_global_initializer)
716-
<< llvm::to_underlying(InitFnTarget) << InitFn;
714+
<< InitFnTarget << InitFn;
717715
Diag(InitFn->getLocation(), diag::note_previous_decl) << InitFn;
718716
VD->setInvalidDecl();
719717
}
@@ -952,8 +950,8 @@ bool SemaCUDA::CheckCall(SourceLocation Loc, FunctionDecl *Callee) {
952950

953951
SemaDiagnosticBuilder(DiagKind, Loc, diag::err_ref_bad_target, Caller,
954952
SemaRef)
955-
<< llvm::to_underlying(IdentifyTarget(Callee)) << /*function*/ 0 << Callee
956-
<< llvm::to_underlying(IdentifyTarget(Caller));
953+
<< IdentifyTarget(Callee) << /*function*/ 0 << Callee
954+
<< IdentifyTarget(Caller);
957955
if (!Callee->getBuiltinID())
958956
SemaDiagnosticBuilder(DiagKind, Callee->getLocation(),
959957
diag::note_previous_decl, Caller, SemaRef)
@@ -1049,8 +1047,7 @@ void SemaCUDA::checkTargetOverload(FunctionDecl *NewFD,
10491047
(NewTarget == CUDAFunctionTarget::Global) ||
10501048
(OldTarget == CUDAFunctionTarget::Global)) {
10511049
Diag(NewFD->getLocation(), diag::err_cuda_ovl_target)
1052-
<< llvm::to_underlying(NewTarget) << NewFD->getDeclName()
1053-
<< llvm::to_underlying(OldTarget) << OldFD;
1050+
<< NewTarget << NewFD->getDeclName() << OldTarget << OldFD;
10541051
Diag(OldFD->getLocation(), diag::note_previous_declaration);
10551052
NewFD->setInvalidDecl();
10561053
break;
@@ -1060,7 +1057,7 @@ void SemaCUDA::checkTargetOverload(FunctionDecl *NewFD,
10601057
(NewTarget == CUDAFunctionTarget::Device &&
10611058
OldTarget == CUDAFunctionTarget::Host)) {
10621059
Diag(NewFD->getLocation(), diag::warn_offload_incompatible_redeclare)
1063-
<< llvm::to_underlying(NewTarget) << llvm::to_underlying(OldTarget);
1060+
<< NewTarget << OldTarget;
10641061
Diag(OldFD->getLocation(), diag::note_previous_declaration);
10651062
}
10661063
}

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8339,8 +8339,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
83398339
} else {
83408340
EmitFormatDiagnostic(
83418341
S.PDiag(diag::warn_non_pod_vararg_with_format_string)
8342-
<< S.getLangOpts().CPlusPlus11 << ExprTy
8343-
<< llvm::to_underlying(CallType)
8342+
<< S.getLangOpts().CPlusPlus11 << ExprTy << CallType
83448343
<< AT.getRepresentativeTypeName(S.Context) << CSR
83458344
<< E->getSourceRange(),
83468345
E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
@@ -8354,16 +8353,15 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
83548353
else if (ExprTy->isObjCObjectType())
83558354
EmitFormatDiagnostic(
83568355
S.PDiag(diag::err_cannot_pass_objc_interface_to_vararg_format)
8357-
<< S.getLangOpts().CPlusPlus11 << ExprTy
8358-
<< llvm::to_underlying(CallType)
8356+
<< S.getLangOpts().CPlusPlus11 << ExprTy << CallType
83598357
<< AT.getRepresentativeTypeName(S.Context) << CSR
83608358
<< E->getSourceRange(),
83618359
E->getBeginLoc(), /*IsStringLocation*/ false, CSR);
83628360
else
83638361
// FIXME: If this is an initializer list, suggest removing the braces
83648362
// or inserting a cast to the target type.
83658363
S.Diag(E->getBeginLoc(), diag::err_cannot_pass_to_vararg_format)
8366-
<< isa<InitListExpr>(E) << ExprTy << llvm::to_underlying(CallType)
8364+
<< isa<InitListExpr>(E) << ExprTy << CallType
83678365
<< AT.getRepresentativeTypeName(S.Context) << E->getSourceRange();
83688366
break;
83698367
}

0 commit comments

Comments
 (0)