[llvm] [llvm][DebugInfo] formatv - Tests (PR #191980)
Konrad Kleine via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 14 02:24:54 PDT 2026
https://github.com/kwk updated https://github.com/llvm/llvm-project/pull/191980
>From 4fed09d36dd4ede6580a59ead201ae553be3604e Mon Sep 17 00:00:00 2001
From: Konrad Kleine <kkleine at redhat.com>
Date: Mon, 13 Apr 2026 16:38:28 +0000
Subject: [PATCH 1/3] [llvm][DebugInfo] formatv - Tests
This is one commit out of of series of commits that deal with the
migration from `format()` to `formatv()` in DebugInfo.
The idea for this commit is to show how `formatv()` format
strings need to be constructed in order to achieve the same output as
with `format()`.
This relates to #35980.
---
llvm/unittests/Support/FormatVariadicTest.cpp | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index a18c20c76eb7a..22054e69676cd 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -412,8 +412,56 @@ TEST(FormatAndFormatvTest, EquivalentHexFormatting) {
formatv("0x{0:x-}", fmt_align(N, AlignStyle::Right, HexDigits, '0'))
.str());
+ // More examples
+
+ EXPECT_EQ("0x0000000abc",
+ printToString(format("0x%0*" PRIx64, 10, 0xABCULL)));
+ EXPECT_EQ("0x0000000abc",
+ formatv("0x{0:x-}", fmt_align(0xABCULL, AlignStyle::Right, 10, '0'))
+ .str());
+
EXPECT_EQ("0x000000fe", printToString(format("0x%8.8" PRIx64, 0xfeULL)));
EXPECT_EQ("0x000000fe", formatv("{0:x+8}", 0xfeULL).str());
+ EXPECT_EQ("0x000000fe", formatv("{0:x8}", 0xfeULL).str());
+
+ EXPECT_EQ("000000fe", printToString(format("%08" PRIx64, 0xfeULL)));
+ EXPECT_EQ("000000fe", formatv("{0:x-8}", 0xfeULL).str());
+
+ EXPECT_EQ("0x000000fb", printToString(format("0x%08.08" PRIx64, 0xfbULL)));
+ EXPECT_EQ("0x000000fb", formatv("{0:x+8}", 0xfbULL).str());
+
+ EXPECT_EQ("123", printToString(format("%x", 0x123)));
+ EXPECT_EQ("123", formatv("{0:x-}", 0x123).str());
+}
+
+TEST(FormatAndFormatvTest, StringPadding) {
+ // The expected string has 9 spaces after "abcd" and a closing bracket.
+ EXPECT_EQ("[abcd ]",
+ printToString(format("[%s%*c", "abcd", 10, ']')));
+ // With formatv we can use padding but the closing bracket can be included in
+ // the format string and the padding needs to be one off.
+ EXPECT_EQ("[abcd ]",
+ formatv("[{0}]", fmt_pad("abcd", 0, 10 - 1)).str());
+}
+
+TEST(FormatAndFormatvTest, Alignment) {
+ EXPECT_EQ("250 ", printToString(format("%-15" PRIu32, 0xfa)));
+ EXPECT_EQ("250 ", formatv("{0, -15}", 0xfa).str());
+ EXPECT_EQ("250 ",
+ formatv("{0}", fmt_align(0xfa, AlignStyle::Left, 15)).str());
+
+ EXPECT_EQ(" 123", printToString(format("%5u", 123)));
+ EXPECT_EQ(" 123", formatv("{0,5:d}", 123).str());
+ EXPECT_EQ(" 123",
+ formatv("{0}", fmt_align(123, AlignStyle::Right, 5)).str());
+
+ EXPECT_EQ("001", printToString(format("%03d", 1)));
+ EXPECT_EQ("001", formatv("{0,0+3}", 1).str());
+
+ EXPECT_EQ("foo1 ", printToString(format("%-9s", "foo1")));
+ EXPECT_EQ("foo2 ", formatv("{0,-9}", "foo2").str());
+ EXPECT_EQ("foo3 ", formatv("{0, -9}", "foo3").str());
+ EXPECT_EQ("foo4_____", formatv("{0,_-9}", "foo4").str());
}
TEST(FormatAndFormatvTest, NonNegativePlusInteger) {
>From 71c7b2a9a62ff4475f3a456878b627b98f06facb Mon Sep 17 00:00:00 2001
From: Konrad Kleine <konrad.kleine at posteo.de>
Date: Tue, 14 Apr 2026 09:46:53 +0200
Subject: [PATCH 2/3] Update llvm/unittests/Support/FormatVariadicTest.cpp
Co-authored-by: Sergei Barannikov <barannikov88 at gmail.com>
---
llvm/unittests/Support/FormatVariadicTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index 22054e69676cd..693ca118a2b01 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -446,7 +446,7 @@ TEST(FormatAndFormatvTest, StringPadding) {
TEST(FormatAndFormatvTest, Alignment) {
EXPECT_EQ("250 ", printToString(format("%-15" PRIu32, 0xfa)));
- EXPECT_EQ("250 ", formatv("{0, -15}", 0xfa).str());
+ EXPECT_EQ("250 ", formatv("{0,-15}", 0xfa).str());
EXPECT_EQ("250 ",
formatv("{0}", fmt_align(0xfa, AlignStyle::Left, 15)).str());
>From ee8ae8471959da0142046c4132320cb39fb1a5a3 Mon Sep 17 00:00:00 2001
From: Konrad Kleine <kkleine at redhat.com>
Date: Tue, 14 Apr 2026 09:24:31 +0000
Subject: [PATCH 3/3] Padding example with fmt_align
---
llvm/unittests/Support/FormatVariadicTest.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index 693ca118a2b01..7ab66415f371b 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -442,6 +442,9 @@ TEST(FormatAndFormatvTest, StringPadding) {
// the format string and the padding needs to be one off.
EXPECT_EQ("[abcd ]",
formatv("[{0}]", fmt_pad("abcd", 0, 10 - 1)).str());
+ // We can also use the fmt_align function instead of fmt_pad but it makes it a bit more complex
+ EXPECT_EQ("[abcd ]",
+ formatv("[{0}]", fmt_align("abcd", AlignStyle::Left, 10 + strlen("abcd") - 1 )).str());
}
TEST(FormatAndFormatvTest, Alignment) {
More information about the llvm-commits
mailing list