Skip to content

Commit 37d7fe7

Browse files
committed
Revert "[Sema] Use underlying type of scoped enum for -Wformat diagnostics (llvm#67378)"
This reverts commit 69c8c96.
1 parent 181dd79 commit 37d7fe7

File tree

4 files changed

+19
-67
lines changed

4 files changed

+19
-67
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,10 @@ Improvements to Clang's diagnostics
497497
- ``-Wformat`` cast fix-its will now suggest ``static_cast`` instead of C-style casts
498498
for C++ code.
499499
- ``-Wformat`` will no longer suggest a no-op fix-it for fixing scoped enum format
500-
warnings. Instead, it will suggest casting the enum object based on its
501-
underlying type.
500+
warnings. Instead, it will suggest casting the enum object to the type specified
501+
in the format string.
502+
- Clang contexpr evaluator now displays notes as well as an error when a constructor
503+
of a base class is not called in the constructor of its derived class.
502504

503505
- ``-Wzero-as-null-pointer-constant`` diagnostic is no longer emitted when using ``__null``
504506
(or, more commonly, ``NULL`` when the platform defines it as ``__null``) to be more consistent

clang/lib/Sema/SemaChecking.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11500,15 +11500,12 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1150011500
ImplicitMatch == ArgType::NoMatchTypeConfusion)
1150111501
Match = ImplicitMatch;
1150211502
assert(Match != ArgType::MatchPromotion);
11503-
1150411503
// Look through unscoped enums to their underlying type.
1150511504
bool IsEnum = false;
1150611505
bool IsScopedEnum = false;
11507-
QualType IntendedTy = ExprTy;
1150811506
if (auto EnumTy = ExprTy->getAs<EnumType>()) {
11509-
IntendedTy = EnumTy->getDecl()->getIntegerType();
1151011507
if (EnumTy->isUnscopedEnumerationType()) {
11511-
ExprTy = IntendedTy;
11508+
ExprTy = EnumTy->getDecl()->getIntegerType();
1151211509
// This controls whether we're talking about the underlying type or not,
1151311510
// which we only want to do when it's an unscoped enum.
1151411511
IsEnum = true;
@@ -11520,6 +11517,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1152011517
// %C in an Objective-C context prints a unichar, not a wchar_t.
1152111518
// If the argument is an integer of some kind, believe the %C and suggest
1152211519
// a cast instead of changing the conversion specifier.
11520+
QualType IntendedTy = ExprTy;
1152311521
if (isObjCContext() &&
1152411522
FS.getConversionSpecifier().getKind() == ConversionSpecifier::CArg) {
1152511523
if (ExprTy->isIntegralOrUnscopedEnumerationType() &&
@@ -11555,10 +11553,8 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1155511553
std::tie(CastTy, CastTyName) = shouldNotPrintDirectly(S.Context, IntendedTy, E);
1155611554
if (!CastTy.isNull()) {
1155711555
// %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") &&
1156211558
(AT.isSizeT() || AT.isPtrdiffT()) &&
1156311559
AT.matchesType(S.Context, CastTy))
1156411560
Match = ArgType::NoMatchPedantic;
@@ -11613,15 +11609,20 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1161311609
// should be printed as 'long' for 64-bit compatibility.)
1161411610
// Rather than emitting a normal format/argument mismatch, we want to
1161511611
// 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).
1161711613
SmallString<16> CastBuf;
1161811614
llvm::raw_svector_ostream CastFix(CastBuf);
1161911615
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+
}
1162111622
CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
1162211623

1162311624
SmallVector<FixItHint,4> Hints;
11624-
if (AT.matchesType(S.Context, IntendedTy) != ArgType::Match ||
11625+
if ((!AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) ||
1162511626
ShouldNotPrintDirectly)
1162611627
Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
1162711628

@@ -11649,7 +11650,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1164911650
Hints.push_back(FixItHint::CreateInsertion(After, ")"));
1165011651
}
1165111652

11652-
if (ShouldNotPrintDirectly && !IsScopedEnum) {
11653+
if (ShouldNotPrintDirectly) {
1165311654
// The expression has a type that should not be printed directly.
1165411655
// We extract the name from the typedef because we don't want to show
1165511656
// the underlying type in the diagnostic.

clang/test/FixIt/format-darwin-enum-class.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

clang/test/FixIt/format.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,21 @@ struct S {
1212
N::E Type;
1313
};
1414

15-
using uint32_t = unsigned;
16-
enum class FixedE : uint32_t { Two };
17-
1815
void a(N::E NEVal, S *SPtr, S &SRef) {
1916
printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
2017
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
2118
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")"
2219

2320
printf("%hd", N::E::One); // expected-warning{{format specifies type 'short' but the argument has type 'N::E'}}
24-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
25-
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
26-
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
21+
// CHECK: "static_cast<short>("
2722

2823
printf("%hu", N::E::One); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'N::E'}}
29-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
30-
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
31-
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
24+
// CHECK: "static_cast<unsigned short>("
3225

3326
LOG("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
3427
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
3528
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
3629

37-
LOG("%s", N::E::One); // expected-warning{{format specifies type 'char *' but the argument has type 'N::E'}}
38-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:10}:"%d"
39-
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"static_cast<int>("
40-
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:22-[[@LINE-3]]:22}:")"
41-
4230
printf("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
4331
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
4432
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:21-[[@LINE-2]]:21}:")"
@@ -70,8 +58,4 @@ void a(N::E NEVal, S *SPtr, S &SRef) {
7058
SRef.Type);
7159
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
7260
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
73-
74-
printf("%u", FixedE::Two); //expected-warning{{format specifies type 'unsigned int' but the argument has type 'FixedE'}}
75-
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<uint32_t>("
76-
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:27-[[@LINE-2]]:27}:")"
7761
}

0 commit comments

Comments
 (0)