Skip to content

Commit 3e023f7

Browse files
authored
[clang] Don't crash when -Wformat-signedness specified (#162049)
Fixes #161075. This patch follow the changes in #150962, and fix a crash issue when `-Wformat-signedness` was specificed.
1 parent 898c6b2 commit 3e023f7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8811,8 +8811,10 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
88118811
case ArgType::Match:
88128812
case ArgType::MatchPromotion:
88138813
case ArgType::NoMatchPromotionTypeConfusion:
8814-
case ArgType::NoMatchSignedness:
88158814
llvm_unreachable("expected non-matching");
8815+
case ArgType::NoMatchSignedness:
8816+
Diag = diag::warn_format_conversion_argument_type_mismatch_signedness;
8817+
break;
88168818
case ArgType::NoMatchPedantic:
88178819
Diag = diag::warn_format_conversion_argument_type_mismatch_pedantic;
88188820
break;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wformat -Wformat-signedness %s
2+
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -verify -Wformat -Wformat-signedness %s
3+
4+
// Verify that -Wformat-signedness alone (without -Wformat) trigger the
5+
// warnings. Note in gcc this will not trigger the signedness warnings as
6+
// -Wformat is default off in gcc.
7+
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -verify -Wformat-signedness %s
8+
// RUN: %clang_cc1 -triple=x86_64-pc-win32 -fsyntax-only -verify -Wformat-signedness %s
9+
10+
// Verify that -Wformat-signedness warnings are not reported with only -Wformat
11+
// (gcc compat).
12+
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -Wformat -verify=okay %s
13+
14+
// Verify that -Wformat-signedness with -Wno-format are not reported (gcc compat).
15+
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -Wformat-signedness -Wno-format -verify=okay %s
16+
// RUN: %clang_cc1 -triple=x86_64-pc-linux-gnu -fsyntax-only -Wno-format -Wformat-signedness -verify=okay %s
17+
// okay-no-diagnostics
18+
19+
// Ignore 'GCC requires a function with the 'format' attribute to be variadic'.
20+
#pragma GCC diagnostic ignored "-Wgcc-compat"
21+
namespace GH161075 {
22+
template <typename... Args>
23+
void format(const char *fmt, Args &&...args)
24+
__attribute__((format(printf, 1, 2)));
25+
26+
void do_format() {
27+
bool b = false;
28+
format("%hhi %hhu %hi %hu %i %u", b, b, b, b, b, b); // expected-warning {{format specifies type 'unsigned char' but the argument has type 'bool', which differs in signedness}}
29+
}
30+
} // namespace GH161075

0 commit comments

Comments
 (0)