[PATCH] D146844: [clang-format] Handle '_' in ud-suffix for IntegerLiteralSeparator

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 27 22:04:24 PDT 2023


owenpan updated this revision to Diff 508889.
owenpan added a comment.

Treats imaginary number suffixes (starting with an `i`) just like any user-defined suffixes (starting with an underscore). Also handles ud-suffixes (e.g. `_km` and `_Pa`) containing letters that can be part of a floating-point number.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D146844/new/

https://reviews.llvm.org/D146844

Files:
  clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
  clang/unittests/Format/IntegerLiteralSeparatorTest.cpp


Index: clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
===================================================================
--- clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
+++ clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
@@ -49,7 +49,21 @@
 
   verifyFormat("o0 = 0;\n"
                "o1 = 07;\n"
-               "o5 = 012345",
+               "o5 = 012345;",
+               Style);
+
+  verifyFormat("bi = 0b1'0000i;\n"
+               "dif = 1'234if;\n"
+               "hil = 0xA'BCil;",
+               "bi = 0b10000i;\n"
+               "dif = 1234if;\n"
+               "hil = 0xABCil;",
+               Style);
+
+  verifyFormat("d = 5'678_km;\n"
+               "h = 0xD'EF_u16;",
+               "d = 5678_km;\n"
+               "h = 0xDEF_u16;",
                Style);
 }
 
Index: clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
===================================================================
--- clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
+++ clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
@@ -106,6 +106,12 @@
         (IsBase16 && SkipHex) || B == Base::Other) {
       continue;
     }
+    if (Style.isCpp()) {
+      if (const auto Pos = Text.find_first_of("_i"); Pos != StringRef::npos) {
+        Text = Text.substr(0, Pos);
+        Length = Pos;
+      }
+    }
     if ((IsBase10 && Text.find_last_of(".eEfFdDmM") != StringRef::npos) ||
         (IsBase16 && Text.find_last_of(".pP") != StringRef::npos)) {
       continue;
@@ -116,7 +122,7 @@
       continue;
     }
     const auto Start = Text[0] == '0' ? 2 : 0;
-    auto End = Text.find_first_of("uUlLzZn");
+    auto End = Text.find_first_of("uUlLzZn", Start);
     if (End == StringRef::npos)
       End = Length;
     if (Start > 0 || End < Length) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146844.508889.patch
Type: text/x-patch
Size: 1777 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230328/b8da461c/attachment.bin>


More information about the cfe-commits mailing list