clang-tools 17.0.0git
IntegralLiteralExpressionMatcher.h
Go to the documentation of this file.
1//===--- IntegralLiteralExpressionMatcher.h - 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#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_INTEGRAL_LITERAL_EXPRESSION_MATCHER_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_INTEGRAL_LITERAL_EXPRESSION_MATCHER_H
11
12#include <clang/Lex/Token.h>
13#include <llvm/ADT/ArrayRef.h>
14
15namespace clang::tidy::modernize {
16
17enum class LiteralSize {
18 Unknown = 0,
19 Int,
21 Long,
25};
26
27// Parses an array of tokens and returns true if they conform to the rules of
28// C++ for whole expressions involving integral literals. Follows the operator
29// precedence rules of C++. Optionally exclude comma operator expressions.
31public:
32 IntegralLiteralExpressionMatcher(ArrayRef<Token> Tokens, bool CommaAllowed)
33 : Current(Tokens.begin()), End(Tokens.end()), CommaAllowed(CommaAllowed) {
34 }
35
36 bool match();
38
39private:
40 bool advance();
41 bool consume(tok::TokenKind Kind);
42 bool nonTerminalChainedExpr(
43 bool (IntegralLiteralExpressionMatcher::*NonTerminal)(),
44 const std::function<bool(Token)> &IsKind);
45 template <tok::TokenKind Kind>
46 bool nonTerminalChainedExpr(
47 bool (IntegralLiteralExpressionMatcher::*NonTerminal)()) {
48 return nonTerminalChainedExpr(NonTerminal,
49 [](Token Tok) { return Tok.is(Kind); });
50 }
51 template <tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind... Ks>
52 bool nonTerminalChainedExpr(
53 bool (IntegralLiteralExpressionMatcher::*NonTerminal)()) {
54 return nonTerminalChainedExpr(
55 NonTerminal, [](Token Tok) { return Tok.isOneOf(K1, K2, Ks...); });
56 }
57
58 bool unaryOperator();
59 bool unaryExpr();
60 bool multiplicativeExpr();
61 bool additiveExpr();
62 bool shiftExpr();
63 bool compareExpr();
64 bool relationalExpr();
65 bool equalityExpr();
66 bool andExpr();
67 bool exclusiveOrExpr();
68 bool inclusiveOrExpr();
69 bool logicalAndExpr();
70 bool logicalOrExpr();
71 bool conditionalExpr();
72 bool commaExpr();
73 bool expr();
74
75 ArrayRef<Token>::iterator Current;
76 ArrayRef<Token>::iterator End;
78 bool CommaAllowed;
79};
80
81} // namespace clang::tidy::modernize
82
83#endif
BindArgumentKind Kind
IntegralLiteralExpressionMatcher(ArrayRef< Token > Tokens, bool CommaAllowed)