@@ -11500,15 +11500,12 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
11500
11500
ImplicitMatch == ArgType::NoMatchTypeConfusion)
11501
11501
Match = ImplicitMatch;
11502
11502
assert(Match != ArgType::MatchPromotion);
11503
-
11504
11503
// Look through unscoped enums to their underlying type.
11505
11504
bool IsEnum = false;
11506
11505
bool IsScopedEnum = false;
11507
- QualType IntendedTy = ExprTy;
11508
11506
if (auto EnumTy = ExprTy->getAs<EnumType>()) {
11509
- IntendedTy = EnumTy->getDecl()->getIntegerType();
11510
11507
if (EnumTy->isUnscopedEnumerationType()) {
11511
- ExprTy = IntendedTy ;
11508
+ ExprTy = EnumTy->getDecl()->getIntegerType() ;
11512
11509
// This controls whether we're talking about the underlying type or not,
11513
11510
// which we only want to do when it's an unscoped enum.
11514
11511
IsEnum = true;
@@ -11520,6 +11517,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
11520
11517
// %C in an Objective-C context prints a unichar, not a wchar_t.
11521
11518
// If the argument is an integer of some kind, believe the %C and suggest
11522
11519
// a cast instead of changing the conversion specifier.
11520
+ QualType IntendedTy = ExprTy;
11523
11521
if (isObjCContext() &&
11524
11522
FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
11525
11523
if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
@@ -11555,10 +11553,8 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
11555
11553
std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
11556
11554
if (!CastTy.isNull()) {
11557
11555
// %zi/%zu and %td/%tu are OK to use for NSInteger/NSUInteger of type int
11558
- // (long in ASTContext). Only complain to pedants or when they're the
11559
- // underlying type of a scoped enum (which always needs a cast).
11560
- if (!IsScopedEnum &&
11561
- (CastTyName == "NSInteger" || CastTyName == "NSUInteger") &&
11556
+ // (long in ASTContext). Only complain to pedants.
11557
+ if ((CastTyName == "NSInteger" || CastTyName == "NSUInteger") &&
11562
11558
(AT.isSizeT() || AT.isPtrdiffT()) &&
11563
11559
AT.matchesType(S.Context, CastTy))
11564
11560
Match = ArgType::NoMatchPedantic;
@@ -11613,15 +11609,20 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
11613
11609
// should be printed as 'long' for 64-bit compatibility.)
11614
11610
// Rather than emitting a normal format/argument mismatch, we want to
11615
11611
// add a cast to the recommended type (and correct the format string
11616
- // if necessary). We should also do so for scoped enumerations.
11612
+ // if necessary).
11617
11613
SmallString<16> CastBuf;
11618
11614
llvm::raw_svector_ostream CastFix(CastBuf);
11619
11615
CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "(");
11620
- IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
11616
+ if (IsScopedEnum) {
11617
+ CastFix << AT.getRepresentativeType(S.Context).getAsString(
11618
+ S.Context.getPrintingPolicy());
11619
+ } else {
11620
+ IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
11621
+ }
11621
11622
CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
11622
11623
11623
11624
SmallVector<FixItHint,4> Hints;
11624
- if (AT.matchesType(S.Context, IntendedTy) != ArgType::Match ||
11625
+ if ((! AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) ||
11625
11626
ShouldNotPrintDirectly)
11626
11627
Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
11627
11628
@@ -11649,7 +11650,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
11649
11650
Hints.push_back(FixItHint::CreateInsertion(After, ")"));
11650
11651
}
11651
11652
11652
- if (ShouldNotPrintDirectly && !IsScopedEnum ) {
11653
+ if (ShouldNotPrintDirectly) {
11653
11654
// The expression has a type that should not be printed directly.
11654
11655
// We extract the name from the typedef because we don't want to show
11655
11656
// the underlying type in the diagnostic.
0 commit comments