Skip to content

release/22.x: [Sema] Fix ICE due to incorrect _Bool handling in format string checking (#174684)#178133

Closed
llvmbot wants to merge 1 commit intollvm:release/22.xfrom
llvmbot:issue174684
Closed

release/22.x: [Sema] Fix ICE due to incorrect _Bool handling in format string checking (#174684)#178133
llvmbot wants to merge 1 commit intollvm:release/22.xfrom
llvmbot:issue174684

Conversation

@llvmbot
Copy link
Copy Markdown
Member

@llvmbot llvmbot commented Jan 27, 2026

Backport 15365d3

Requested by: @zyn0217

@llvmbot
Copy link
Copy Markdown
Member Author

llvmbot commented Jan 27, 2026

@cor3ntin What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from cor3ntin January 27, 2026 07:59
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 27, 2026
@llvmbot
Copy link
Copy Markdown
Member Author

llvmbot commented Jan 27, 2026

@llvm/pr-subscribers-clang

Author: None (llvmbot)

Changes

Backport 15365d3

Requested by: @zyn0217


Full diff: https://github.com/llvm/llvm-project/pull/178133.diff

2 Files Affected:

  • (modified) clang/lib/AST/FormatString.cpp (+1-1)
  • (modified) clang/test/Sema/format-strings.c (+32)
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index d4cb89b43ae87..36c5f57671631 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -371,7 +371,7 @@ static clang::analyze_format_string::ArgType::MatchKind
 matchesSizeTPtrdiffT(ASTContext &C, QualType T, QualType E) {
   using MatchKind = clang::analyze_format_string::ArgType::MatchKind;
 
-  if (!T->isIntegerType())
+  if (!T->isIntegerType() || T->isBooleanType())
     return MatchKind::NoMatch;
 
   if (C.hasSameType(T, E))
diff --git a/clang/test/Sema/format-strings.c b/clang/test/Sema/format-strings.c
index 5280549adc3d7..cdac83fc0bb91 100644
--- a/clang/test/Sema/format-strings.c
+++ b/clang/test/Sema/format-strings.c
@@ -983,3 +983,35 @@ void test_promotion(void) {
   // pointers
   printf("%s", i); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
 }
+
+void test_bool(_Bool b, _Bool* bp)
+{
+  printf("%zu", b); // expected-warning-re{{format specifies type 'size_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+  printf("%td", b); // expected-warning-re{{format specifies type 'ptrdiff_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+  printf("%jd", b); // expected-warning-re{{format specifies type 'intmax_t' (aka '{{.+}}') but the argument has type '_Bool'}}
+  printf("%lld", b); // expected-warning{{format specifies type 'long long' but the argument has type '_Bool'}}
+  printf("%ld", b); // expected-warning{{format specifies type 'long' but the argument has type '_Bool'}}
+  printf("%d", b); // promoted from _Bool to int
+  printf("%hhd", b); // promoted from _Bool to int
+  printf("%hd", b); // promoted from _Bool to int
+#if !defined(__Fuchsia__) && !defined(__ANDROID__) //'%n' specifier not supported on this platform
+  // The n conversion specifier only supports signed types
+  printf("%zn", bp); // expected-warning-re{{format specifies type 'signed size_t *' (aka '{{.+}}') but the argument has type '_Bool *'}}
+  printf("%jn", bp); // expected-warning-re{{format specifies type 'intmax_t *' (aka '{{.+}}') but the argument has type '_Bool *'}}
+  printf("%lln", bp); // expected-warning{{format specifies type 'long long *' but the argument has type '_Bool *'}}
+  printf("%ln", bp); // expected-warning{{format specifies type 'long *' but the argument has type '_Bool *'}}
+  printf("%n", bp); // expected-warning{{format specifies type 'int *' but the argument has type '_Bool *'}}
+  printf("%hhn", bp); // expected-warning{{format specifies type 'signed char *' but the argument has type '_Bool *'}}
+  printf("%hn", bp); // belong to -Wformat-type-confusion
+#endif
+  printf("%c", b); // expected-warning{{using '%c' format specifier, but argument has boolean value}}
+  printf("%s", b); // expected-warning{{format specifies type 'char *' but the argument has type '_Bool'}}
+  printf("%d", b); // promoted from _Bool to int
+  printf("%o", b); // promoted from _Bool to int
+  printf("%x", b); // promoted from _Bool to int
+  printf("%u", b); // promoted from _Bool to int
+  printf("%f", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+  printf("%e", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+  printf("%a", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+  printf("%g", b); // expected-warning{{format specifies type 'double' but the argument has type '_Bool'}}
+}

@c-rhodes c-rhodes moved this from Needs Triage to Needs Review in LLVM Release Status Jan 27, 2026
@dyung
Copy link
Copy Markdown
Collaborator

dyung commented Jan 28, 2026

Note that this change alone is causing a lit test failure on 32-bit arm buildbots. See #174684 for details.

@c-rhodes c-rhodes moved this from Needs Review to Needs Test Fix in LLVM Release Status Jan 28, 2026
@c-rhodes
Copy link
Copy Markdown
Collaborator

c-rhodes commented Feb 2, 2026

test was fixed in #178450

@cor3ntin
Copy link
Copy Markdown
Contributor

cor3ntin commented Feb 2, 2026

This pr should include the changes in #178450 then

@c-rhodes
Copy link
Copy Markdown
Collaborator

c-rhodes commented Feb 4, 2026

@zyn0217 any plans to fix this test? This has been open over a week now and rc3 is in less than a week

@zyn0217
Copy link
Copy Markdown
Contributor

zyn0217 commented Feb 4, 2026

Sorry I missed this. I'll replace it with a manual backport PR

@zyn0217 zyn0217 closed this Feb 4, 2026
@zyn0217
Copy link
Copy Markdown
Contributor

zyn0217 commented Feb 4, 2026

New PR in #179637

@c-rhodes c-rhodes moved this from Needs Test Fix to Won't Merge in LLVM Release Status Feb 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

Status: Won't Merge

Development

Successfully merging this pull request may close these issues.

6 participants