summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOwen Pan <owenpiano@gmail.com>2023-03-24 13:16:44 -0700
committerTom Stellard <tstellar@redhat.com>2023-04-03 21:20:41 -0700
commite16f668f5ed0699498a987be21d32f04e8bdcfbb (patch)
treedaaf602457704cc107a4197870585d90a9b9921f
parent42d1b276f7793999be3f9b6a99efbb143254c729 (diff)
downloadllvm-e16f668f5ed0699498a987be21d32f04e8bdcfbb.tar.gz
[clang-format] Handle '_' in ud-suffix for IntegerLiteralSeparator
Also, handle imaginary numbers, i.e., those with suffixes starting with an 'i'. Fixes #61676. Differential Revision: https://reviews.llvm.org/D146844 (cherry picked from commit 4d21868b09681dffb9997f1a8d0c0dac12a226d3)
-rw-r--r--clang/lib/Format/IntegerLiteralSeparatorFixer.cpp8
-rw-r--r--clang/unittests/Format/IntegerLiteralSeparatorTest.cpp16
2 files changed, 22 insertions, 2 deletions
diff --git a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
index 05e37c34a8a0..508dc82cabeb 100644
--- a/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
+++ b/clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
@@ -106,6 +106,12 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
(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 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
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) {
diff --git a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
index 8698947818e5..d16c321badc3 100644
--- a/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
+++ b/clang/unittests/Format/IntegerLiteralSeparatorTest.cpp
@@ -99,7 +99,21 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
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);
}