[llvm] 7a894a9 - [llvm][DebugInfo] Add more format/formatv equivalence tests (#191980)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 05:39:27 PDT 2026


Author: Konrad Kleine
Date: 2026-04-15T14:39:22+02:00
New Revision: 7a894a96014ebc319bc225a8e8107ee78794bff3

URL: https://github.com/llvm/llvm-project/commit/7a894a96014ebc319bc225a8e8107ee78794bff3
DIFF: https://github.com/llvm/llvm-project/commit/7a894a96014ebc319bc225a8e8107ee78794bff3.diff

LOG: [llvm][DebugInfo] Add more format/formatv equivalence tests (#191980)

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.

Co-authored-by: Sergei Barannikov <barannikov88 at gmail.com>

Added: 
    

Modified: 
    llvm/unittests/Support/FormatVariadicTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/FormatVariadicTest.cpp b/llvm/unittests/Support/FormatVariadicTest.cpp
index a18c20c76eb7a..ea549e7553b0c 100644
--- a/llvm/unittests/Support/FormatVariadicTest.cpp
+++ b/llvm/unittests/Support/FormatVariadicTest.cpp
@@ -407,13 +407,65 @@ TEST(FormatAndFormatvTest, EquivalentHexFormatting) {
   EXPECT_EQ("0x00000000ff",
             printToString(format("0x%*.*" PRIx64, HexDigits, HexDigits, N)));
 
-  // Now, do the same with formatv()
+  // Now, do the same with formatv().
   EXPECT_EQ("0x00000000ff",
             formatv("0x{0:x-}", fmt_align(N, AlignStyle::Right, HexDigits, '0'))
                 .str());
 
+  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());
+  // 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) {
+  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) {


        


More information about the llvm-commits mailing list