Skip to content

Commit e16f668

Browse files
owencatstellar
authored andcommitted
[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 4d21868)
1 parent 42d1b27 commit e16f668

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
106106
(IsBase16 && SkipHex) || B == Base::Other) {
107107
continue;
108108
}
109+
if (Style.isCpp()) {
110+
if (const auto Pos = Text.find_first_of("_i"); Pos != StringRef::npos) {
111+
Text = Text.substr(0, Pos);
112+
Length = Pos;
113+
}
114+
}
109115
if ((IsBase10 && Text.find_last_of(".eEfFdDmM") != StringRef::npos) ||
110116
(IsBase16 && Text.find_last_of(".pP") != StringRef::npos)) {
111117
continue;
@@ -116,7 +122,7 @@ IntegerLiteralSeparatorFixer::process(const Environment &Env,
116122
continue;
117123
}
118124
const auto Start = Text[0] == '0' ? 2 : 0;
119-
auto End = Text.find_first_of("uUlLzZn");
125+
auto End = Text.find_first_of("uUlLzZn", Start);
120126
if (End == StringRef::npos)
121127
End = Length;
122128
if (Start > 0 || End < Length) {

clang/unittests/Format/IntegerLiteralSeparatorTest.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,21 @@ TEST_F(IntegerLiteralSeparatorTest, SingleQuoteAsSeparator) {
9999

100100
verifyFormat("o0 = 0;\n"
101101
"o1 = 07;\n"
102-
"o5 = 012345",
102+
"o5 = 012345;",
103+
Style);
104+
105+
verifyFormat("bi = 0b1'0000i;\n"
106+
"dif = 1'234if;\n"
107+
"hil = 0xA'BCil;",
108+
"bi = 0b10000i;\n"
109+
"dif = 1234if;\n"
110+
"hil = 0xABCil;",
111+
Style);
112+
113+
verifyFormat("d = 5'678_km;\n"
114+
"h = 0xD'EF_u16;",
115+
"d = 5678_km;\n"
116+
"h = 0xDEF_u16;",
103117
Style);
104118
}
105119

0 commit comments

Comments
 (0)