[PATCH] D153623: [clang][Sema] Add fixit for scoped enum format error
Alex Brachet via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 14 09:25:07 PDT 2023
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG563a23c824db: [clang][Sema] Add fixit for scoped enum format error (authored by abrachet).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Changed prior to commit:
https://reviews.llvm.org/D153623?vs=538669&id=540455#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D153623/new/
https://reviews.llvm.org/D153623
Files:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
clang/test/FixIt/format.cpp
Index: clang/test/FixIt/format.cpp
===================================================================
--- /dev/null
+++ 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<int>("
+ // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:25-[[@LINE-2]]:25}:")"
+
+ printf("%hd", N::E::One);
+ // CHECK: "static_cast<short>("
+
+ printf("%hu", N::E::One);
+ // CHECK: "static_cast<unsigned short>("
+}
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -11077,12 +11077,15 @@
assert(Match != ArgType::MatchPromotion);
// Look through unscoped enums to their underlying type.
bool IsEnum = false;
+ bool IsScopedEnum = false;
if (auto EnumTy = ExprTy->getAs<EnumType>()) {
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 @@
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 @@
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<FixItHint,4> 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<CStyleCastExpr>(E)) {
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -417,6 +417,9 @@
^ ~~~~~~
- ``-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
-------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153623.540455.patch
Type: text/x-patch
Size: 3298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230714/a8dab62b/attachment.bin>
More information about the cfe-commits
mailing list