46 const FormatStyle &Style) {
49 switch (Style.Language) {
50 case FormatStyle::LK_CSharp:
51 case FormatStyle::LK_Java:
52 case FormatStyle::LK_JavaScript:
55 case FormatStyle::LK_C:
56 case FormatStyle::LK_Cpp:
57 case FormatStyle::LK_ObjC:
58 if (!LangOpts.AllowLiteralDigitSeparator)
66 const auto &Option = Style.IntegerLiteralSeparator;
67 const auto Binary = Option.Binary;
68 const auto Decimal = Option.Decimal;
69 const auto Hex = Option.Hex;
70 const bool SkipBinary =
Binary == 0;
71 const bool SkipDecimal =
Decimal == 0;
72 const bool SkipHex =
Hex == 0;
74 if (SkipBinary && SkipDecimal && SkipHex)
77 auto CalcMinAndMax = [](
int DigitsPerGroup,
int MinDigitsInsert,
78 int MaxDigitsRemove) {
79 MinDigitsInsert = std::max(MinDigitsInsert, DigitsPerGroup + 1);
80 if (MinDigitsInsert < 1)
82 else if (MaxDigitsRemove < 1 || MaxDigitsRemove >= MinDigitsInsert)
83 MaxDigitsRemove = MinDigitsInsert - 1;
84 return std::pair(MinDigitsInsert, MaxDigitsRemove);
87 const auto [BinaryMinDigitsInsert, BinaryMaxDigitsRemove] = CalcMinAndMax(
88 Binary, Option.BinaryMinDigitsInsert, Option.BinaryMaxDigitsRemove);
89 const auto [DecimalMinDigitsInsert, DecimalMaxDigitsRemove] = CalcMinAndMax(
90 Decimal, Option.DecimalMinDigitsInsert, Option.DecimalMaxDigitsRemove);
91 const auto [HexMinDigitsInsert, HexMaxDigitsRemove] =
92 CalcMinAndMax(
Hex, Option.HexMinDigitsInsert, Option.HexMaxDigitsRemove);
98 Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
105 auto Length =
Tok.getLength();
108 auto Location =
Tok.getLocation();
109 auto Text = StringRef(SourceMgr.getCharacterData(Location), Length);
110 if (
Tok.is(tok::comment)) {
117 if (
Skip ||
Tok.isNot(tok::numeric_constant) ||
Text[0] ==
'.' ||
126 if ((IsBase2 && SkipBinary) || (IsBase10 && SkipDecimal) ||
132 static constexpr StringRef
Suffixes(
"_himnsuyd");
135 Pos != StringRef::npos) {
140 if ((IsBase10 &&
Text.find_last_of(
".eEfFdDmM") != StringRef::npos) ||
141 (IsBase16 &&
Text.find_last_of(
".pP") != StringRef::npos)) {
144 const auto Start =
Text[0] ==
'0' ? 2 : 0;
145 auto End =
Text.find_first_of(
"uUlLzZn", Start);
146 if (End == StringRef::npos)
148 if (Start > 0 || End < Length) {
149 Length = End - Start;
153 auto MinDigitsInsert = DecimalMinDigitsInsert;
154 auto MaxDigitsRemove = DecimalMaxDigitsRemove;
157 MinDigitsInsert = BinaryMinDigitsInsert;
158 MaxDigitsRemove = BinaryMaxDigitsRemove;
159 }
else if (IsBase16) {
160 DigitsPerGroup =
Hex;
161 MinDigitsInsert = HexMinDigitsInsert;
162 MaxDigitsRemove = HexMaxDigitsRemove;
164 const auto SeparatorCount =
Text.count(Separator);
165 const int DigitCount = Length - SeparatorCount;
166 if (DigitCount > MaxDigitsRemove && DigitCount < MinDigitsInsert)
168 const bool RemoveSeparator =
169 DigitsPerGroup < 0 || DigitCount <= MaxDigitsRemove;
170 if (RemoveSeparator && SeparatorCount == 0)
172 if (!RemoveSeparator && SeparatorCount > 0 &&
173 checkSeparator(
Text, DigitsPerGroup)) {
176 const auto &Formatted =
177 format(
Text, DigitsPerGroup, DigitCount, RemoveSeparator);
178 assert(Formatted !=
Text);
180 Location = Location.getLocWithOffset(Start);