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,
20 UnsignedInt,
21 Long,
22 UnsignedLong,
23 LongLong,
24 UnsignedLongLong
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.
30class IntegralLiteralExpressionMatcher {
31public:
32 IntegralLiteralExpressionMatcher(ArrayRef<Token> Tokens, bool CommaAllowed)
33 : Current(Tokens.begin()), End(Tokens.end()), CommaAllowed(CommaAllowed) {
34 }
35
36 bool match();
37 LiteralSize largestLiteralSize() const;
38
39private:
40 bool advance();
41 bool consume(tok::TokenKind Kind);
42 template <typename NonTerminalFunctor, typename IsKindFunctor>
43 bool nonTerminalChainedExpr(const NonTerminalFunctor &NonTerminal,
44 const IsKindFunctor &IsKind);
45 template <tok::TokenKind Kind, typename NonTerminalFunctor>
46 bool nonTerminalChainedExpr(const NonTerminalFunctor &NonTerminal);
47 template <tok::TokenKind K1, tok::TokenKind K2, tok::TokenKind... Ks,
48 typename NonTerminalFunctor>
49 bool nonTerminalChainedExpr(const NonTerminalFunctor &NonTerminal);
50
51 bool unaryOperator();
52 bool unaryExpr();
53 bool multiplicativeExpr();
54 bool additiveExpr();
55 bool shiftExpr();
56 bool compareExpr();
57 bool relationalExpr();
58 bool equalityExpr();
59 bool andExpr();
60 bool exclusiveOrExpr();
61 bool inclusiveOrExpr();
62 bool logicalAndExpr();
63 bool logicalOrExpr();
64 bool conditionalExpr();
65 bool commaExpr();
66 bool expr();
67
68 ArrayRef<Token>::iterator Current;
69 ArrayRef<Token>::iterator End;
70 LiteralSize LargestSize{LiteralSize::Unknown};
71 bool CommaAllowed;
72};
73
74} // namespace clang::tidy::modernize
75
76#endif
77

source code of clang-tools-extra/clang-tidy/modernize/IntegralLiteralExpressionMatcher.h