Skip to content

Commit 563a23c

Browse files
committed
[clang][Sema] Add fixit for scoped enum format error
This helps transition code bases to handle the new warning added in 3632e2f Before: ``` clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat] 10 | printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} | ~~ ^~~~~~~~~ | %d ``` After: ``` clang/test/FixIt/format.cpp:10:16: warning: format specifies type 'int' but the argument has type 'N::E' [-Wformat] 10 | printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} | ~~ ^~~~~~~~~ | static_cast<int>( ) ``` Differential Revision: https://reviews.llvm.org/D153623
1 parent 3c0a136 commit 563a23c

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ Improvements to Clang's diagnostics
417417
^ ~~~~~~
418418
- ``-Wformat`` cast fix-its will now suggest ``static_cast`` instead of C-style casts
419419
for C++ code.
420+
- ``-Wformat`` will no longer suggest a no-op fix-it for fixing scoped enum format
421+
warnings. Instead, it will suggest casting the enum object to the type specified
422+
in the format string.
420423

421424
Bug Fixes in This Version
422425
-------------------------

clang/lib/Sema/SemaChecking.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11077,12 +11077,15 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1107711077
assert(Match != ArgType::MatchPromotion);
1107811078
// Look through unscoped enums to their underlying type.
1107911079
bool IsEnum = false;
11080+
bool IsScopedEnum = false;
1108011081
if (auto EnumTy = ExprTy->getAs<EnumType>()) {
1108111082
if (EnumTy->isUnscopedEnumerationType()) {
1108211083
ExprTy = EnumTy->getDecl()->getIntegerType();
1108311084
// This controls whether we're talking about the underlying type or not,
1108411085
// which we only want to do when it's an unscoped enum.
1108511086
IsEnum = true;
11087+
} else {
11088+
IsScopedEnum = true;
1108611089
}
1108711090
}
1108811091

@@ -11148,7 +11151,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1114811151

1114911152
CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen);
1115011153

11151-
if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) {
11154+
if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) {
1115211155
unsigned Diag;
1115311156
switch (Match) {
1115411157
case ArgType::Match:
@@ -11185,11 +11188,17 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
1118511188
SmallString<16> CastBuf;
1118611189
llvm::raw_svector_ostream CastFix(CastBuf);
1118711190
CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "(");
11188-
IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
11191+
if (IsScopedEnum) {
11192+
CastFix << AT.getRepresentativeType(S.Context).getAsString(
11193+
S.Context.getPrintingPolicy());
11194+
} else {
11195+
IntendedTy.print(CastFix, S.Context.getPrintingPolicy());
11196+
}
1118911197
CastFix << (S.LangOpts.CPlusPlus ? ">" : ")");
1119011198

1119111199
SmallVector<FixItHint,4> Hints;
11192-
if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly)
11200+
if ((!AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) ||
11201+
ShouldNotPrintDirectly)
1119311202
Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str()));
1119411203

1119511204
if (const CStyleCastExpr *CCast = dyn_cast<CStyleCastExpr>(E)) {

clang/test/FixIt/format.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s 2>&1 | FileCheck %s
2+
3+
extern "C" int printf(const char *, ...);
4+
5+
namespace N {
6+
enum class E { One };
7+
}
8+
9+
void a() {
10+
printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
11+
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
12+
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")"
13+
14+
printf("%hd", N::E::One);
15+
// CHECK: "static_cast<short>("
16+
17+
printf("%hu", N::E::One);
18+
// CHECK: "static_cast<unsigned short>("
19+
}

0 commit comments

Comments
 (0)