aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/modernize/IntegralLiteralExpressionMatcher.cpp
blob: 9b97b46d580eedac64d8b7b1ceb61f2292540087 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//===--- IntegralLiteralExpressionMatcher.cpp - clang-tidy ----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "IntegralLiteralExpressionMatcher.h"

#include <algorithm>
#include <cctype>
#include <stdexcept>

namespace clang::tidy::modernize {

// Validate that this literal token is a valid integer literal.  A literal token
// could be a floating-point token, which isn't acceptable as a value for an
// enumeration.  A floating-point token must either have a decimal point or an
// exponent ('E' or 'P').
static bool isIntegralConstant(const Token &Token) {
  const char *Begin = Token.getLiteralData();
  const char *End = Begin + Token.getLength();

  // Not a hexadecimal floating-point literal.
  if (Token.getLength() > 2 && Begin[0] == '0' && std::toupper(Begin[1]) == 'X')
    return std::none_of(Begin + 2, End, [](char C) {
      return C == '.' || std::toupper(C) == 'P';
    });

  // Not a decimal floating-point literal or complex literal.
  return std::none_of(Begin, End, [](char C) {
    return C == '.' || std::toupper(C) == 'E' || std::toupper(C) == 'I';
  });
}

bool IntegralLiteralExpressionMatcher::advance() {
  ++Current;
  return Current != End;
}

bool IntegralLiteralExpressionMatcher::consume(tok::TokenKind Kind) {
  if (Current->is(Kind)) {
    ++Current;
    return true;
  }

  return false;
}

bool IntegralLiteralExpressionMatcher::nonTerminalChainedExpr(
    bool (IntegralLiteralExpressionMatcher::*NonTerminal)(),
    const std::function<bool(Token)> &IsKind) {
  if (!(this->*NonTerminal)())
    return false;
  if (Current == End)
    return true;

  while (Current != End) {
    if (!IsKind(*Current))
      break;

    if (!advance())
      return false;

    if (!(this->*NonTerminal)())
      return false;
  }

  return true;
}

// Advance over unary operators.
bool IntegralLiteralExpressionMatcher::unaryOperator() {
  if (Current->isOneOf(tok::TokenKind::minus, tok::TokenKind::plus,
                       tok::TokenKind::tilde, tok::TokenKind::exclaim)) {
    return advance();
  }

  return true;
}

static LiteralSize literalTokenSize(const Token &Tok) {
  unsigned int Length = Tok.getLength();
  if (Length <= 1)
    return LiteralSize::Int;

  bool SeenUnsigned = false;
  bool SeenLong = false;
  bool SeenLongLong = false;
  const char *Text = Tok.getLiteralData();
  for (unsigned int End = Length - 1; End > 0; --End) {
    if (std::isdigit(Text[End]))
      break;

    if (std::toupper(Text[End]) == 'U')
      SeenUnsigned = true;
    else if (std::toupper(Text[End]) == 'L') {
      if (SeenLong)
        SeenLongLong = true;
      SeenLong = true;
    }
  }

  if (SeenLongLong) {
    if (SeenUnsigned)
      return LiteralSize::UnsignedLongLong;

    return LiteralSize::LongLong;
  }
  if (SeenLong) {
    if (SeenUnsigned)
      return LiteralSize::UnsignedLong;

    return LiteralSize::Long;
  }
  if (SeenUnsigned)
    return LiteralSize::UnsignedInt;

  return LiteralSize::Int;
}

static bool operator<(LiteralSize LHS, LiteralSize RHS) {
  return static_cast<int>(LHS) < static_cast<int>(RHS);
}

bool IntegralLiteralExpressionMatcher::unaryExpr() {
  if (!unaryOperator())
    return false;

  if (consume(tok::TokenKind::l_paren)) {
    if (Current == End)
      return false;

    if (!expr())
      return false;

    if (Current == End)
      return false;

    return consume(tok::TokenKind::r_paren);
  }

  if (!Current->isLiteral() || isStringLiteral(Current->getKind()) ||
      !isIntegralConstant(*Current)) {
    return false;
  }

  LargestSize = std::max(LargestSize, literalTokenSize(*Current));
  ++Current;

  return true;
}

bool IntegralLiteralExpressionMatcher::multiplicativeExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::star, tok::TokenKind::slash,
                                tok::TokenKind::percent>(
      &IntegralLiteralExpressionMatcher::unaryExpr);
}

bool IntegralLiteralExpressionMatcher::additiveExpr() {
  return nonTerminalChainedExpr<tok::plus, tok::minus>(
      &IntegralLiteralExpressionMatcher::multiplicativeExpr);
}

bool IntegralLiteralExpressionMatcher::shiftExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::lessless,
                                tok::TokenKind::greatergreater>(
      &IntegralLiteralExpressionMatcher::additiveExpr);
}

bool IntegralLiteralExpressionMatcher::compareExpr() {
  if (!shiftExpr())
    return false;
  if (Current == End)
    return true;

  if (Current->is(tok::TokenKind::spaceship)) {
    if (!advance())
      return false;

    if (!shiftExpr())
      return false;
  }

  return true;
}

bool IntegralLiteralExpressionMatcher::relationalExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::less, tok::TokenKind::greater,
                                tok::TokenKind::lessequal,
                                tok::TokenKind::greaterequal>(
      &IntegralLiteralExpressionMatcher::compareExpr);
}

bool IntegralLiteralExpressionMatcher::equalityExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::equalequal,
                                tok::TokenKind::exclaimequal>(
      &IntegralLiteralExpressionMatcher::relationalExpr);
}

bool IntegralLiteralExpressionMatcher::andExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::amp>(
      &IntegralLiteralExpressionMatcher::equalityExpr);
}

bool IntegralLiteralExpressionMatcher::exclusiveOrExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::caret>(
      &IntegralLiteralExpressionMatcher::andExpr);
}

bool IntegralLiteralExpressionMatcher::inclusiveOrExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::pipe>(
      &IntegralLiteralExpressionMatcher::exclusiveOrExpr);
}

bool IntegralLiteralExpressionMatcher::logicalAndExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::ampamp>(
      &IntegralLiteralExpressionMatcher::inclusiveOrExpr);
}

bool IntegralLiteralExpressionMatcher::logicalOrExpr() {
  return nonTerminalChainedExpr<tok::TokenKind::pipepipe>(
      &IntegralLiteralExpressionMatcher::logicalAndExpr);
}

bool IntegralLiteralExpressionMatcher::conditionalExpr() {
  if (!logicalOrExpr())
    return false;
  if (Current == End)
    return true;

  if (Current->is(tok::TokenKind::question)) {
    if (!advance())
      return false;

    // A gcc extension allows x ? : y as a synonym for x ? x : y.
    if (Current->is(tok::TokenKind::colon)) {
      if (!advance())
        return false;

      if (!expr())
        return false;

      return true;
    }

    if (!expr())
      return false;
    if (Current == End)
      return false;

    if (!Current->is(tok::TokenKind::colon))
      return false;

    if (!advance())
      return false;

    if (!expr())
      return false;
  }
  return true;
}

bool IntegralLiteralExpressionMatcher::commaExpr() {
  auto Pred = CommaAllowed
                  ? std::function<bool(Token)>(
                        [](Token Tok) { return Tok.is(tok::TokenKind::comma); })
                  : std::function<bool(Token)>([](Token) { return false; });
  return nonTerminalChainedExpr(
      &IntegralLiteralExpressionMatcher::conditionalExpr, Pred);
}

bool IntegralLiteralExpressionMatcher::expr() { return commaExpr(); }

bool IntegralLiteralExpressionMatcher::match() {
  // Top-level allowed expression is conditionalExpr(), not expr(), because
  // comma operators are only valid initializers when used inside parentheses.
  return conditionalExpr() && Current == End;
}

LiteralSize IntegralLiteralExpressionMatcher::largestLiteralSize() const {
  return LargestSize;
}

} // namespace clang::tidy::modernize