[clang] Carving out -Wformat warning about scoped enums into a subwarning (PR #88595)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 16 15:50:54 PDT 2024
https://github.com/ZijunZhaoCCK updated https://github.com/llvm/llvm-project/pull/88595
>From 29d2d19c9dea9e3f818a34c97f95bd7c93b9433b Mon Sep 17 00:00:00 2001
From: Zijun <zijunzhao at google.com>
Date: Sat, 13 Apr 2024 00:53:38 +0000
Subject: [PATCH 1/3] Carving out -Wformat warning about scoped enums into a
subwarning
Make it part of -Wformat-pedantic.
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++--
clang/test/Sema/format-strings-scanf.c | 2 +-
clang/test/Sema/format-strings-signedness.c | 6 +++---
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 774d2b53a38252..4ba27d62208da4 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9827,7 +9827,7 @@ def warn_scanf_nonzero_width : Warning<
def warn_format_conversion_argument_type_mismatch : Warning<
"format specifies type %0 but the argument has "
"%select{type|underlying type}2 %1">,
- InGroup<Format>;
+ InGroup<FormatPedantic>;
def warn_format_conversion_argument_type_mismatch_pedantic : Extension<
warn_format_conversion_argument_type_mismatch.Summary>,
InGroup<FormatPedantic>;
@@ -9840,7 +9840,7 @@ def warn_format_conversion_argument_type_mismatch_confusion : Warning<
def warn_format_argument_needs_cast : Warning<
"%select{values of type|enum values with underlying type}2 '%0' should not "
"be used as format arguments; add an explicit cast to %1 instead">,
- InGroup<Format>;
+ InGroup<FormatPedantic>;
def warn_format_argument_needs_cast_pedantic : Warning<
warn_format_argument_needs_cast.Summary>,
InGroup<FormatPedantic>, DefaultIgnore;
diff --git a/clang/test/Sema/format-strings-scanf.c b/clang/test/Sema/format-strings-scanf.c
index eb5b8ec36bf7a4..9bdc46bfeebc3b 100644
--- a/clang/test/Sema/format-strings-scanf.c
+++ b/clang/test/Sema/format-strings-scanf.c
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat-nonliteral %s
// Test that -Wformat=0 works:
-// RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 %s
+// RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 -Wno-format-pedantic %s
#include <stdarg.h>
typedef __SIZE_TYPE__ size_t;
diff --git a/clang/test/Sema/format-strings-signedness.c b/clang/test/Sema/format-strings-signedness.c
index d5a8140d9ef8a0..ccd4d72c2d2635 100644
--- a/clang/test/Sema/format-strings-signedness.c
+++ b/clang/test/Sema/format-strings-signedness.c
@@ -12,8 +12,8 @@
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s
// Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
-// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s
-// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s
+// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify %s
+// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify %s
// okay-no-diagnostics
int printf(const char *restrict format, ...);
@@ -218,5 +218,5 @@ void test_printf_unsigned_priX16(uint16_t x) {
void test_suppress(int x)
{
#pragma GCC diagnostic ignored "-Wformat"
- printf("%u", x);
+ printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
}
>From 46e8d902d92b71e9bad2321321eed3de489e837f Mon Sep 17 00:00:00 2001
From: Zijun <zijunzhao at google.com>
Date: Sat, 13 Apr 2024 00:53:38 +0000
Subject: [PATCH 2/3] Carving out -Wformat warning about scoped enums into a
subwarning
Make it part of -Wformat-pedantic.
Fixes #81647
---
clang/test/Sema/format-strings-signedness.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/test/Sema/format-strings-signedness.c b/clang/test/Sema/format-strings-signedness.c
index ccd4d72c2d2635..a06884cb9fe090 100644
--- a/clang/test/Sema/format-strings-signedness.c
+++ b/clang/test/Sema/format-strings-signedness.c
@@ -11,7 +11,7 @@
// (gcc compat).
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s
-// Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
+// Verify that -Wformat-signedness with -Wno-format are still reported (gcc compat).
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify %s
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify %s
// okay-no-diagnostics
@@ -213,7 +213,7 @@ void test_printf_unsigned_priX16(uint16_t x) {
printf("PRIX16: %" "X" /*PRIX16*/ "\n", x); // no-warning
}
-// Verify that we can suppress a -Wformat-signedness warning by ignoring
+// Verify that we can not suppress a -Wformat-signedness warning by ignoring
// -Wformat (gcc compat).
void test_suppress(int x)
{
>From e1fc0fafaf731f8695e42d18bfac6624dee4213a Mon Sep 17 00:00:00 2001
From: Zijun <zijunzhao at google.com>
Date: Tue, 16 Apr 2024 22:36:53 +0000
Subject: [PATCH 3/3] Carving out -Wformat warning about scoped enums into a
subwarning
Make it part of -Wformat-pedantic.
Fixes #81647
---
.../clang/Basic/DiagnosticSemaKinds.td | 4 +--
clang/lib/Sema/SemaChecking.cpp | 10 ++++---
clang/test/FixIt/format-darwin-enum-class.cpp | 4 +--
clang/test/FixIt/format.cpp | 27 ++++++++++---------
clang/test/Sema/format-strings-scanf.c | 2 +-
clang/test/Sema/format-strings-signedness.c | 8 +++---
clang/test/SemaCXX/format-strings.cpp | 4 +--
7 files changed, 32 insertions(+), 27 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e066898d6c51fa..44f802c0c28e84 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -9826,7 +9826,7 @@ def warn_scanf_nonzero_width : Warning<
def warn_format_conversion_argument_type_mismatch : Warning<
"format specifies type %0 but the argument has "
"%select{type|underlying type}2 %1">,
- InGroup<FormatPedantic>;
+ InGroup<Format>;
def warn_format_conversion_argument_type_mismatch_pedantic : Extension<
warn_format_conversion_argument_type_mismatch.Summary>,
InGroup<FormatPedantic>;
@@ -9839,7 +9839,7 @@ def warn_format_conversion_argument_type_mismatch_confusion : Warning<
def warn_format_argument_needs_cast : Warning<
"%select{values of type|enum values with underlying type}2 '%0' should not "
"be used as format arguments; add an explicit cast to %1 instead">,
- InGroup<FormatPedantic>;
+ InGroup<Format>;
def warn_format_argument_needs_cast_pedantic : Warning<
warn_format_argument_needs_cast.Summary>,
InGroup<FormatPedantic>, DefaultIgnore;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 8e21811b67d900..278fb515b68ac3 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -12764,10 +12764,14 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
// In this case, the expression could be printed using a different
// specifier, but we've decided that the specifier is probably correct
// and we should cast instead. Just use the normal warning message.
+
+ unsigned Diag = IsScopedEnum ?
+ diag::warn_format_conversion_argument_type_mismatch_pedantic :
+ diag::warn_format_conversion_argument_type_mismatch;
+
EmitFormatDiagnostic(
- S.PDiag(diag::warn_format_conversion_argument_type_mismatch)
- << AT.getRepresentativeTypeName(S.Context) << ExprTy << IsEnum
- << E->getSourceRange(),
+ S.PDiag(Diag) << AT.getRepresentativeTypeName(S.Context) << ExprTy
+ << IsEnum << E->getSourceRange(),
E->getBeginLoc(), /*IsStringLocation*/ false, SpecRange, Hints);
}
}
diff --git a/clang/test/FixIt/format-darwin-enum-class.cpp b/clang/test/FixIt/format-darwin-enum-class.cpp
index 5aa1a80d8614c2..6d0bb80e982d7e 100644
--- a/clang/test/FixIt/format-darwin-enum-class.cpp
+++ b/clang/test/FixIt/format-darwin-enum-class.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify -Wformat %s
-// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify -Wformat-pedantic %s
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -fdiagnostics-parseable-fixits -Wformat-pedantic %s 2>&1 | FileCheck %s
extern "C" int printf(const char * restrict, ...);
diff --git a/clang/test/FixIt/format.cpp b/clang/test/FixIt/format.cpp
index 4e6573a4f9e54e..f724af4d808c34 100644
--- a/clang/test/FixIt/format.cpp
+++ b/clang/test/FixIt/format.cpp
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 -fsyntax-only -verify -Wformat %s
-// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -Wformat-pedantic %s 2>&1 | FileCheck %s
+// expected-no-diagnostics
extern "C" int printf(const char *, ...);
#define LOG(...) printf(__VA_ARGS__)
@@ -16,45 +17,45 @@ using uint32_t = unsigned;
enum class FixedE : uint32_t { Two };
void a(N::E NEVal, S *SPtr, S &SRef) {
- printf("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ printf("%d", N::E::One);
// 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); // expected-warning{{format specifies type 'short' but the argument has type 'N::E'}}
+ printf("%hd", N::E::One);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
- printf("%hu", N::E::One); // expected-warning{{format specifies type 'unsigned short' but the argument has type 'N::E'}}
+ printf("%hu", N::E::One);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:14}:"%d"
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:17-[[@LINE-2]]:17}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:26-[[@LINE-3]]:26}:")"
- LOG("%d", N::E::One); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ LOG("%d", N::E::One);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:22-[[@LINE-2]]:22}:")"
- LOG("%s", N::E::One); // expected-warning{{format specifies type 'char *' but the argument has type 'N::E'}}
+ LOG("%s", N::E::One);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:10}:"%d"
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:22-[[@LINE-3]]:22}:")"
- printf("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ printf("%d", NEVal);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:21-[[@LINE-2]]:21}:")"
- LOG("%d", NEVal); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ LOG("%d", NEVal);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:13}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:")"
printf(
"%d",
- SPtr->Type // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ SPtr->Type
);
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
- LOG( // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ LOG(
"%d",
SPtr->Type
);
@@ -62,16 +63,16 @@ void a(N::E NEVal, S *SPtr, S &SRef) {
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
printf("%d",
- SRef.Type); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ SRef.Type);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
- LOG("%d", // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
+ LOG("%d",
SRef.Type);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
- printf("%u", FixedE::Two); //expected-warning{{format specifies type 'unsigned int' but the argument has type 'FixedE'}}
+ printf("%u", FixedE::Two);
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:16-[[@LINE-1]]:16}:"static_cast<uint32_t>("
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:27-[[@LINE-2]]:27}:")"
}
diff --git a/clang/test/Sema/format-strings-scanf.c b/clang/test/Sema/format-strings-scanf.c
index 9bdc46bfeebc3b..eb5b8ec36bf7a4 100644
--- a/clang/test/Sema/format-strings-scanf.c
+++ b/clang/test/Sema/format-strings-scanf.c
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -std=c11 -fsyntax-only -verify -Wformat-nonliteral %s
// Test that -Wformat=0 works:
-// RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 -Wno-format-pedantic %s
+// RUN: %clang_cc1 -std=c11 -fsyntax-only -Werror -Wformat=0 %s
#include <stdarg.h>
typedef __SIZE_TYPE__ size_t;
diff --git a/clang/test/Sema/format-strings-signedness.c b/clang/test/Sema/format-strings-signedness.c
index a06884cb9fe090..89d6dd96d13040 100644
--- a/clang/test/Sema/format-strings-signedness.c
+++ b/clang/test/Sema/format-strings-signedness.c
@@ -11,9 +11,9 @@
// (gcc compat).
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat -verify=okay %s
-// Verify that -Wformat-signedness with -Wno-format are still reported (gcc compat).
-// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify %s
-// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify %s
+// Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
+// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s
+// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -std=c11 -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s
// okay-no-diagnostics
int printf(const char *restrict format, ...);
@@ -218,5 +218,5 @@ void test_printf_unsigned_priX16(uint16_t x) {
void test_suppress(int x)
{
#pragma GCC diagnostic ignored "-Wformat"
- printf("%u", x); // expected-warning{{format specifies type 'unsigned int' but the argument has type 'int'}}
+ printf("%u", x);
}
diff --git a/clang/test/SemaCXX/format-strings.cpp b/clang/test/SemaCXX/format-strings.cpp
index f554e905d6455b..48cf23999a94f7 100644
--- a/clang/test/SemaCXX/format-strings.cpp
+++ b/clang/test/SemaCXX/format-strings.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -Wformat-pedantic -fblocks %s
// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++98 %s
-// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -fblocks -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -Wformat-non-iso -Wformat-pedantic -fblocks -std=c++11 %s
#include <stdarg.h>
More information about the cfe-commits
mailing list