[llvm] [llvm][DebugInfo] formatv - Tests (PR #191980)

Konrad Kleine via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 02:26:31 PDT 2026


================
@@ -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());
----------------
kwk wrote:

Here's an example of how things would need to look when using `fmt_align` in this case: [ee8ae84](https://github.com/llvm/llvm-project/pull/191980/commits/ee8ae8471959da0142046c4132320cb39fb1a5a3). I think it is fair to say that the calculation for fmt_pad is a bit easier, right?

```c++
  // 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());
```

https://github.com/llvm/llvm-project/pull/191980


More information about the llvm-commits mailing list