From 563a23c824db22da3ea378e8179fcc7072e897ec Mon Sep 17 00:00:00 2001 From: Alex Brachet Date: Fri, 14 Jul 2023 16:23:22 +0000 Subject: [PATCH] [clang][Sema] Add fixit for scoped enum format error This helps transition code bases to handle the new warning added in 3632e2f5179 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( ) ``` Differential Revision: https://reviews.llvm.org/D153623 --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaChecking.cpp | 15 ++++++++++++--- clang/test/FixIt/format.cpp | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 clang/test/FixIt/format.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 0423c054bd0b..d55757183d58 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -417,6 +417,9 @@ Improvements to Clang's diagnostics ^ ~~~~~~ - ``-Wformat`` cast fix-its will now suggest ``static_cast`` instead of C-style casts for C++ code. +- ``-Wformat`` will no longer suggest a no-op fix-it for fixing scoped enum format + warnings. Instead, it will suggest casting the enum object to the type specified + in the format string. Bug Fixes in This Version ------------------------- diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 66a1a8f3f90e..b09d0383b4bd 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -11077,12 +11077,15 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, assert(Match != ArgType::MatchPromotion); // Look through unscoped enums to their underlying type. bool IsEnum = false; + bool IsScopedEnum = false; if (auto EnumTy = ExprTy->getAs()) { if (EnumTy->isUnscopedEnumerationType()) { ExprTy = EnumTy->getDecl()->getIntegerType(); // This controls whether we're talking about the underlying type or not, // which we only want to do when it's an unscoped enum. IsEnum = true; + } else { + IsScopedEnum = true; } } @@ -11148,7 +11151,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, CharSourceRange SpecRange = getSpecifierRange(StartSpecifier, SpecifierLen); - if (IntendedTy == ExprTy && !ShouldNotPrintDirectly) { + if (IntendedTy == ExprTy && !ShouldNotPrintDirectly && !IsScopedEnum) { unsigned Diag; switch (Match) { case ArgType::Match: @@ -11185,11 +11188,17 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS, SmallString<16> CastBuf; llvm::raw_svector_ostream CastFix(CastBuf); CastFix << (S.LangOpts.CPlusPlus ? "static_cast<" : "("); - IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); + if (IsScopedEnum) { + CastFix << AT.getRepresentativeType(S.Context).getAsString( + S.Context.getPrintingPolicy()); + } else { + IntendedTy.print(CastFix, S.Context.getPrintingPolicy()); + } CastFix << (S.LangOpts.CPlusPlus ? ">" : ")"); SmallVector Hints; - if (!AT.matchesType(S.Context, IntendedTy) || ShouldNotPrintDirectly) + if ((!AT.matchesType(S.Context, IntendedTy) && !IsScopedEnum) || + ShouldNotPrintDirectly) Hints.push_back(FixItHint::CreateReplacement(SpecRange, os.str())); if (const CStyleCastExpr *CCast = dyn_cast(E)) { diff --git a/clang/test/FixIt/format.cpp b/clang/test/FixIt/format.cpp new file mode 100644 index 000000000000..9cc4c2600eb6 --- /dev/null +++ b/clang/test/FixIt/format.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s 2>&1 | FileCheck %s + +extern "C" int printf(const char *, ...); + +namespace N { + enum class E { One }; +} + +void a() { + printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast(" + // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")" + + printf("%hd", N::E::One); + // CHECK: "static_cast(" + + printf("%hu", N::E::One); + // CHECK: "static_cast(" +}