aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/lib/Format/IntegerLiteralSeparatorFixer.cpp
blob: 05e37c34a8a047cf58b94b0201f0c98af77019ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===--- IntegerLiteralSeparatorFixer.cpp -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements IntegerLiteralSeparatorFixer that fixes C++ integer
/// literal separators.
///
//===----------------------------------------------------------------------===//

#include "IntegerLiteralSeparatorFixer.h"

namespace clang {
namespace format {

enum class Base { Binary, Decimal, Hex, Other };

static Base getBase(const StringRef IntegerLiteral) {
  assert(IntegerLiteral.size() > 1);

  if (IntegerLiteral[0] > '0') {
    assert(IntegerLiteral[0] <= '9');
    return Base::Decimal;
  }

  assert(IntegerLiteral[0] == '0');

  switch (IntegerLiteral[1]) {
  case 'b':
  case 'B':
    return Base::Binary;
  case 'x':
  case 'X':
    return Base::Hex;
  default:
    return Base::Other;
  }
}

std::pair<tooling::Replacements, unsigned>
IntegerLiteralSeparatorFixer::process(const Environment &Env,
                                      const FormatStyle &Style) {
  switch (Style.Language) {
  case FormatStyle::LK_Cpp:
  case FormatStyle::LK_ObjC:
    Separator = '\'';
    break;
  case FormatStyle::LK_CSharp:
  case FormatStyle::LK_Java:
  case FormatStyle::LK_JavaScript:
    Separator = '_';
    break;
  default:
    return {};
  }

  const auto &Option = Style.IntegerLiteralSeparator;
  const auto Binary = Option.Binary;
  const auto Decimal = Option.Decimal;
  const auto Hex = Option.Hex;
  const bool SkipBinary = Binary == 0;
  const bool SkipDecimal = Decimal == 0;
  const bool SkipHex = Hex == 0;

  if (SkipBinary && SkipDecimal && SkipHex)
    return {};

  const auto &SourceMgr = Env.getSourceManager();
  AffectedRangeManager AffectedRangeMgr(SourceMgr, Env.getCharRanges());

  const auto ID = Env.getFileID();
  const auto LangOpts = getFormattingLangOpts(Style);
  Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
  Lex.SetCommentRetentionState(true);

  Token Tok;
  tooling::Replacements Result;

  for (bool Skip = false; !Lex.LexFromRawLexer(Tok);) {
    auto Length = Tok.getLength();
    if (Length < 2)
      continue;
    auto Location = Tok.getLocation();
    auto Text = StringRef(SourceMgr.getCharacterData(Location), Length);
    if (Tok.is(tok::comment)) {
      if (Text == "// clang-format off" || Text == "/* clang-format off */")
        Skip = true;
      else if (Text == "// clang-format on" || Text == "/* clang-format on */")
        Skip = false;
      continue;
    }
    if (Skip || Tok.isNot(tok::numeric_constant) || Text[0] == '.' ||
        !AffectedRangeMgr.affectsCharSourceRange(
            CharSourceRange::getCharRange(Location, Tok.getEndLoc()))) {
      continue;
    }
    const auto B = getBase(Text);
    const bool IsBase2 = B == Base::Binary;
    const bool IsBase10 = B == Base::Decimal;
    const bool IsBase16 = B == Base::Hex;
    if ((IsBase2 && SkipBinary) || (IsBase10 && SkipDecimal) ||
        (IsBase16 && SkipHex) || B == Base::Other) {
      continue;
    }
    if ((IsBase10 && Text.find_last_of(".eEfFdDmM") != StringRef::npos) ||
        (IsBase16 && Text.find_last_of(".pP") != StringRef::npos)) {
      continue;
    }
    if (((IsBase2 && Binary < 0) || (IsBase10 && Decimal < 0) ||
         (IsBase16 && Hex < 0)) &&
        Text.find(Separator) == StringRef::npos) {
      continue;
    }
    const auto Start = Text[0] == '0' ? 2 : 0;
    auto End = Text.find_first_of("uUlLzZn");
    if (End == StringRef::npos)
      End = Length;
    if (Start > 0 || End < Length) {
      Length = End - Start;
      Text = Text.substr(Start, Length);
    }
    auto DigitsPerGroup = Decimal;
    if (IsBase2)
      DigitsPerGroup = Binary;
    else if (IsBase16)
      DigitsPerGroup = Hex;
    if (DigitsPerGroup > 0 && checkSeparator(Text, DigitsPerGroup))
      continue;
    if (Start > 0)
      Location = Location.getLocWithOffset(Start);
    if (const auto &Formatted = format(Text, DigitsPerGroup);
        Formatted != Text) {
      cantFail(Result.add(
          tooling::Replacement(SourceMgr, Location, Length, Formatted)));
    }
  }

  return {Result, 0};
}

bool IntegerLiteralSeparatorFixer::checkSeparator(
    const StringRef IntegerLiteral, int DigitsPerGroup) const {
  assert(DigitsPerGroup > 0);

  int I = 0;
  for (auto C : llvm::reverse(IntegerLiteral)) {
    if (C == Separator) {
      if (I < DigitsPerGroup)
        return false;
      I = 0;
    } else {
      ++I;
      if (I == DigitsPerGroup)
        return false;
    }
  }

  return true;
}

std::string IntegerLiteralSeparatorFixer::format(const StringRef IntegerLiteral,
                                                 int DigitsPerGroup) const {
  assert(DigitsPerGroup != 0);

  std::string Formatted;

  if (DigitsPerGroup < 0) {
    for (auto C : IntegerLiteral)
      if (C != Separator)
        Formatted.push_back(C);
    return Formatted;
  }

  int DigitCount = 0;
  for (auto C : IntegerLiteral)
    if (C != Separator)
      ++DigitCount;

  int Remainder = DigitCount % DigitsPerGroup;

  int I = 0;
  for (auto C : IntegerLiteral) {
    if (C == Separator)
      continue;
    if (I == (Remainder > 0 ? Remainder : DigitsPerGroup)) {
      Formatted.push_back(Separator);
      I = 0;
      Remainder = 0;
    }
    Formatted.push_back(C);
    ++I;
  }

  return Formatted;
}

} // namespace format
} // namespace clang