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

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 25 03:48:22 PDT 2023


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

Don't format imaginary numbers.


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
@@ -51,6 +51,18 @@
                "o1 = 07;\n"
                "o5 = 012345",
                Style);
+
+  verifyFormat("bi = 0b10000i;\n"
+               "dif = 1234if;\n"
+               "hil = 0xABCil",
+               Style);
+
+  const StringRef UDL("u = 0xDEAD'BEEF'DE'AD'BEE'F_u64;");
+  verifyFormat("u = 0xDE'AD'BE'EF'DE'AD'BE'EF_u64;", UDL, Style);
+  Style.IntegerLiteralSeparator.Hex = -1;
+  verifyFormat("u = 0xDEADBEEFDEADBEEF_u64;", UDL, Style);
+  Style.IntegerLiteralSeparator.Hex = 0;
+  verifyFormat(UDL, Style);
 }
 
 TEST_F(IntegerLiteralSeparatorTest, UnderscoreAsSeparator) {
Index: clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
===================================================================
--- clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
+++ clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
@@ -44,10 +44,13 @@
 std::pair<tooling::Replacements, unsigned>
 IntegerLiteralSeparatorFixer::process(const Environment &Env,
                                       const FormatStyle &Style) {
+  std::string Suffix("uUlLzZn");
+
   switch (Style.Language) {
   case FormatStyle::LK_Cpp:
   case FormatStyle::LK_ObjC:
     Separator = '\'';
+    Suffix.push_back('_');
     break;
   case FormatStyle::LK_CSharp:
   case FormatStyle::LK_Java:
@@ -107,7 +110,8 @@
       continue;
     }
     if ((IsBase10 && Text.find_last_of(".eEfFdDmM") != StringRef::npos) ||
-        (IsBase16 && Text.find_last_of(".pP") != StringRef::npos)) {
+        (IsBase16 && Text.find_last_of(".pP") != StringRef::npos) ||
+        Text.find_last_of('i') != StringRef::npos) {
       continue;
     }
     if (((IsBase2 && Binary < 0) || (IsBase10 && Decimal < 0) ||
@@ -116,7 +120,7 @@
       continue;
     }
     const auto Start = Text[0] == '0' ? 2 : 0;
-    auto End = Text.find_first_of("uUlLzZn");
+    auto End = Text.find_first_of(Suffix, Start);
     if (End == StringRef::npos)
       End = Length;
     if (Start > 0 || End < Length) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146844.508293.patch
Type: text/x-patch
Size: 2211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230325/70c7b2d5/attachment.bin>


More information about the cfe-commits mailing list