[clang] [clang] Consistently store format specifiers in execution encoding (PR #195876)
Sergei Barannikov via cfe-commits
cfe-commits at lists.llvm.org
Tue May 5 08:51:47 PDT 2026
https://github.com/s-barannikov created https://github.com/llvm/llvm-project/pull/195876
`DecomposePrintfHandler::HandlePrintfSpecifier()` parses the format string and collects specifiers into `Specs`. In most cases the collected specifiers are in the execution encoding, but there were to places that used string literals in "host" encoding.
Change them to use a part of the parsed StringLiteral instead so that `Specs` always contain text in the execution encoding. This is achieved by adding `getCharacters()` method to `OptionalAmount` class, following `ConversionSpecifier::getCharacters()`.
This is to make #169803 smaller and is an NFC before that PR lands.
>From f1e66fb9f9136d6987117ade7397e71d172a021e Mon Sep 17 00:00:00 2001
From: Sergei Barannikov <barannikov88 at gmail.com>
Date: Tue, 5 May 2026 18:44:06 +0300
Subject: [PATCH] [clang] Consistently store format specifiers in execution
encoding
`DecomposePrintfHandler::HandlePrintfSpecifier()` parses the format
string and collects specifiers into `Specs`. In most cases the collected
specifiers are in the execution encoding, but there were to places that
used string literals in "host" encoding.
Change them to use a part of the parsed StringLiteral instead so that
`Specs` always contain text in the execution encoding. This is achieved
by adding `getCharacters()` method to `OptionalAmount` class, following
`ConversionSpecifier::getCharacters()`.
This is to make #169803 smaller and is an NFC before that PR lands.
---
clang/include/clang/AST/FormatString.h | 4 ++++
clang/lib/Sema/SemaChecking.cpp | 22 +++++++++++-----------
2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 586d9f0f8feb0..a3382e1a1d007 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -396,6 +396,10 @@ class OptionalAmount {
return length + UsesDotPrefix;
}
+ StringRef getCharacters() const {
+ return StringRef(start - UsesDotPrefix, length + UsesDotPrefix);
+ }
+
ArgType getArgType(ASTContext &Ctx) const;
void toString(raw_ostream &os) const;
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 12f77d021eb0d..4706fa5d3cde0 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -8707,16 +8707,16 @@ bool DecomposePrintfHandler::HandlePrintfSpecifier(
const auto &FieldWidth = FS.getFieldWidth();
if (!FieldWidth.isInvalid() && FieldWidth.hasDataArgument()) {
FieldWidthIndex = Specs.size();
- Specs.emplace_back(getSpecifierRange(startSpecifier, specifierLen),
- getLocationOfByte(FieldWidth.getStart()),
- analyze_format_string::LengthModifier::None, "*",
- FieldWidth.getArgType(S.Context),
- EquatableFormatArgument::FAR_FieldWidth,
- EquatableFormatArgument::SS_None,
- FieldWidth.usesPositionalArg()
- ? FieldWidth.getPositionalArgIndex() - 1
- : FieldWidthIndex,
- 0);
+ Specs.emplace_back(
+ getSpecifierRange(startSpecifier, specifierLen),
+ getLocationOfByte(FieldWidth.getStart()),
+ analyze_format_string::LengthModifier::None, FieldWidth.getCharacters(),
+ FieldWidth.getArgType(S.Context),
+ EquatableFormatArgument::FAR_FieldWidth,
+ EquatableFormatArgument::SS_None,
+ FieldWidth.usesPositionalArg() ? FieldWidth.getPositionalArgIndex() - 1
+ : FieldWidthIndex,
+ 0);
}
// precision?
const auto &Precision = FS.getPrecision();
@@ -8725,7 +8725,7 @@ bool DecomposePrintfHandler::HandlePrintfSpecifier(
Specs.emplace_back(
getSpecifierRange(startSpecifier, specifierLen),
getLocationOfByte(Precision.getStart()),
- analyze_format_string::LengthModifier::None, ".*",
+ analyze_format_string::LengthModifier::None, Precision.getCharacters(),
Precision.getArgType(S.Context), EquatableFormatArgument::FAR_Precision,
EquatableFormatArgument::SS_None,
Precision.usesPositionalArg() ? Precision.getPositionalArgIndex() - 1
More information about the cfe-commits
mailing list