|
| 1 | +//===--- IntegralLiteralExpressionMatcher.cpp - clang-tidy ----------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "IntegralLiteralExpressionMatcher.h" |
| 10 | + |
| 11 | +#include <cctype> |
| 12 | +#include <stdexcept> |
| 13 | + |
| 14 | +namespace clang { |
| 15 | +namespace tidy { |
| 16 | +namespace modernize { |
| 17 | + |
| 18 | +// Validate that this literal token is a valid integer literal. A literal token |
| 19 | +// could be a floating-point token, which isn't acceptable as a value for an |
| 20 | +// enumeration. A floating-point token must either have a decimal point or an |
| 21 | +// exponent ('E' or 'P'). |
| 22 | +static bool isIntegralConstant(const Token &Token) { |
| 23 | + const char *Begin = Token.getLiteralData(); |
| 24 | + const char *End = Begin + Token.getLength(); |
| 25 | + |
| 26 | + // Not a hexadecimal floating-point literal. |
| 27 | + if (Token.getLength() > 2 && Begin[0] == '0' && std::toupper(Begin[1]) == 'X') |
| 28 | + return std::none_of(Begin + 2, End, [](char C) { |
| 29 | + return C == '.' || std::toupper(C) == 'P'; |
| 30 | + }); |
| 31 | + |
| 32 | + // Not a decimal floating-point literal or complex literal. |
| 33 | + return std::none_of(Begin, End, [](char C) { |
| 34 | + return C == '.' || std::toupper(C) == 'E' || std::toupper(C) == 'I'; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +bool IntegralLiteralExpressionMatcher::advance() { |
| 39 | + ++Current; |
| 40 | + return Current != End; |
| 41 | +} |
| 42 | + |
| 43 | +bool IntegralLiteralExpressionMatcher::consume(tok::TokenKind Kind) { |
| 44 | + if (Current->is(Kind)) { |
| 45 | + ++Current; |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + return false; |
| 50 | +} |
| 51 | + |
| 52 | +bool IntegralLiteralExpressionMatcher::nonTerminalChainedExpr( |
| 53 | + bool (IntegralLiteralExpressionMatcher::*NonTerminal)(), |
| 54 | + const std::function<bool(Token)> &IsKind) { |
| 55 | + if (!(this->*NonTerminal)()) |
| 56 | + return false; |
| 57 | + if (Current == End) |
| 58 | + return true; |
| 59 | + |
| 60 | + while (Current != End) { |
| 61 | + if (!IsKind(*Current)) |
| 62 | + break; |
| 63 | + |
| 64 | + if (!advance()) |
| 65 | + return false; |
| 66 | + |
| 67 | + if (!(this->*NonTerminal)()) |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +// Advance over unary operators. |
| 75 | +bool IntegralLiteralExpressionMatcher::unaryOperator() { |
| 76 | + if (Current->isOneOf(tok::TokenKind::minus, tok::TokenKind::plus, |
| 77 | + tok::TokenKind::tilde, tok::TokenKind::exclaim)) { |
| 78 | + return advance(); |
| 79 | + } |
| 80 | + |
| 81 | + return true; |
| 82 | +} |
| 83 | + |
| 84 | +bool IntegralLiteralExpressionMatcher::unaryExpr() { |
| 85 | + if (!unaryOperator()) |
| 86 | + return false; |
| 87 | + |
| 88 | + if (consume(tok::TokenKind::l_paren)) { |
| 89 | + if (Current == End) |
| 90 | + return false; |
| 91 | + |
| 92 | + if (!expr()) |
| 93 | + return false; |
| 94 | + |
| 95 | + if (Current == End) |
| 96 | + return false; |
| 97 | + |
| 98 | + return consume(tok::TokenKind::r_paren); |
| 99 | + } |
| 100 | + |
| 101 | + if (!Current->isLiteral() || isStringLiteral(Current->getKind()) || |
| 102 | + !isIntegralConstant(*Current)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + ++Current; |
| 106 | + return true; |
| 107 | +} |
| 108 | + |
| 109 | +bool IntegralLiteralExpressionMatcher::multiplicativeExpr() { |
| 110 | + return nonTerminalChainedExpr<tok::TokenKind::star, tok::TokenKind::slash, |
| 111 | + tok::TokenKind::percent>( |
| 112 | + &IntegralLiteralExpressionMatcher::unaryExpr); |
| 113 | +} |
| 114 | + |
| 115 | +bool IntegralLiteralExpressionMatcher::additiveExpr() { |
| 116 | + return nonTerminalChainedExpr<tok::plus, tok::minus>( |
| 117 | + &IntegralLiteralExpressionMatcher::multiplicativeExpr); |
| 118 | +} |
| 119 | + |
| 120 | +bool IntegralLiteralExpressionMatcher::shiftExpr() { |
| 121 | + return nonTerminalChainedExpr<tok::TokenKind::lessless, |
| 122 | + tok::TokenKind::greatergreater>( |
| 123 | + &IntegralLiteralExpressionMatcher::additiveExpr); |
| 124 | +} |
| 125 | + |
| 126 | +bool IntegralLiteralExpressionMatcher::compareExpr() { |
| 127 | + if (!shiftExpr()) |
| 128 | + return false; |
| 129 | + if (Current == End) |
| 130 | + return true; |
| 131 | + |
| 132 | + if (Current->is(tok::TokenKind::spaceship)) { |
| 133 | + if (!advance()) |
| 134 | + return false; |
| 135 | + |
| 136 | + if (!shiftExpr()) |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + return true; |
| 141 | +} |
| 142 | + |
| 143 | +bool IntegralLiteralExpressionMatcher::relationalExpr() { |
| 144 | + return nonTerminalChainedExpr<tok::TokenKind::less, tok::TokenKind::greater, |
| 145 | + tok::TokenKind::lessequal, |
| 146 | + tok::TokenKind::greaterequal>( |
| 147 | + &IntegralLiteralExpressionMatcher::compareExpr); |
| 148 | +} |
| 149 | + |
| 150 | +bool IntegralLiteralExpressionMatcher::equalityExpr() { |
| 151 | + return nonTerminalChainedExpr<tok::TokenKind::equalequal, |
| 152 | + tok::TokenKind::exclaimequal>( |
| 153 | + &IntegralLiteralExpressionMatcher::relationalExpr); |
| 154 | +} |
| 155 | + |
| 156 | +bool IntegralLiteralExpressionMatcher::andExpr() { |
| 157 | + return nonTerminalChainedExpr<tok::TokenKind::amp>( |
| 158 | + &IntegralLiteralExpressionMatcher::equalityExpr); |
| 159 | +} |
| 160 | + |
| 161 | +bool IntegralLiteralExpressionMatcher::exclusiveOrExpr() { |
| 162 | + return nonTerminalChainedExpr<tok::TokenKind::caret>( |
| 163 | + &IntegralLiteralExpressionMatcher::andExpr); |
| 164 | +} |
| 165 | + |
| 166 | +bool IntegralLiteralExpressionMatcher::inclusiveOrExpr() { |
| 167 | + return nonTerminalChainedExpr<tok::TokenKind::pipe>( |
| 168 | + &IntegralLiteralExpressionMatcher::exclusiveOrExpr); |
| 169 | +} |
| 170 | + |
| 171 | +bool IntegralLiteralExpressionMatcher::logicalAndExpr() { |
| 172 | + return nonTerminalChainedExpr<tok::TokenKind::ampamp>( |
| 173 | + &IntegralLiteralExpressionMatcher::inclusiveOrExpr); |
| 174 | +} |
| 175 | + |
| 176 | +bool IntegralLiteralExpressionMatcher::logicalOrExpr() { |
| 177 | + return nonTerminalChainedExpr<tok::TokenKind::pipepipe>( |
| 178 | + &IntegralLiteralExpressionMatcher::logicalAndExpr); |
| 179 | +} |
| 180 | + |
| 181 | +bool IntegralLiteralExpressionMatcher::conditionalExpr() { |
| 182 | + if (!logicalOrExpr()) |
| 183 | + return false; |
| 184 | + if (Current == End) |
| 185 | + return true; |
| 186 | + |
| 187 | + if (Current->is(tok::TokenKind::question)) { |
| 188 | + if (!advance()) |
| 189 | + return false; |
| 190 | + |
| 191 | + // A gcc extension allows x ? : y as a synonym for x ? x : y. |
| 192 | + if (Current->is(tok::TokenKind::colon)) { |
| 193 | + if (!advance()) |
| 194 | + return false; |
| 195 | + |
| 196 | + if (!expr()) |
| 197 | + return false; |
| 198 | + |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | + if (!expr()) |
| 203 | + return false; |
| 204 | + if (Current == End) |
| 205 | + return false; |
| 206 | + |
| 207 | + if (!Current->is(tok::TokenKind::colon)) |
| 208 | + return false; |
| 209 | + |
| 210 | + if (!advance()) |
| 211 | + return false; |
| 212 | + |
| 213 | + if (!expr()) |
| 214 | + return false; |
| 215 | + } |
| 216 | + return true; |
| 217 | +} |
| 218 | + |
| 219 | +bool IntegralLiteralExpressionMatcher::commaExpr() { |
| 220 | + return nonTerminalChainedExpr<tok::TokenKind::comma>( |
| 221 | + &IntegralLiteralExpressionMatcher::conditionalExpr); |
| 222 | +} |
| 223 | + |
| 224 | +bool IntegralLiteralExpressionMatcher::expr() { return commaExpr(); } |
| 225 | + |
| 226 | +bool IntegralLiteralExpressionMatcher::match() { |
| 227 | + return expr() && Current == End; |
| 228 | +} |
| 229 | + |
| 230 | +} // namespace modernize |
| 231 | +} // namespace tidy |
| 232 | +} // namespace clang |
0 commit comments