Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 1 | //===--- LexerUtils.h - clang-tidy-------------------------------*- C++ -*-===// |
| 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_UTILS_LEXER_UTILS_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H |
| 11 | |
| 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/Basic/TokenKinds.h" |
| 14 | #include "clang/Lex/Lexer.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
| 18 | namespace utils { |
| 19 | namespace lexer { |
| 20 | |
| 21 | /// Returns previous token or ``tok::unknown`` if not found. |
| 22 | Token getPreviousToken(SourceLocation Location, const SourceManager &SM, |
| 23 | const LangOptions &LangOpts, bool SkipComments = true); |
| 24 | |
| 25 | SourceLocation findPreviousTokenStart(SourceLocation Start, |
| 26 | const SourceManager &SM, |
| 27 | const LangOptions &LangOpts); |
| 28 | |
| 29 | SourceLocation findPreviousTokenKind(SourceLocation Start, |
| 30 | const SourceManager &SM, |
| 31 | const LangOptions &LangOpts, |
| 32 | tok::TokenKind TK); |
| 33 | |
| 34 | SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, |
| 35 | const LangOptions &LangOpts); |
| 36 | |
| 37 | template <typename TokenKind, typename... TokenKinds> |
| 38 | SourceLocation findPreviousAnyTokenKind(SourceLocation Start, |
| 39 | const SourceManager &SM, |
| 40 | const LangOptions &LangOpts, |
| 41 | TokenKind TK, TokenKinds... TKs) { |
| 42 | if (Start.isInvalid() || Start.isMacroID()) |
| 43 | return SourceLocation(); |
| 44 | while (true) { |
| 45 | SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts); |
| 46 | if (L.isInvalid() || L.isMacroID()) |
| 47 | return SourceLocation(); |
| 48 | |
| 49 | Token T; |
| 50 | // Returning 'true' is used to signal failure to retrieve the token. |
| 51 | if (Lexer::getRawToken(L, T, SM, LangOpts, /*IgnoreWhiteSpace=*/true)) |
| 52 | return SourceLocation(); |
| 53 | |
| 54 | if (T.isOneOf(TK, TKs...)) |
| 55 | return T.getLocation(); |
| 56 | |
| 57 | Start = L; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | template <typename TokenKind, typename... TokenKinds> |
| 62 | SourceLocation findNextAnyTokenKind(SourceLocation Start, |
| 63 | const SourceManager &SM, |
| 64 | const LangOptions &LangOpts, TokenKind TK, |
| 65 | TokenKinds... TKs) { |
| 66 | while (true) { |
| 67 | Optional<Token> CurrentToken = Lexer::findNextToken(Start, SM, LangOpts); |
| 68 | |
| 69 | if (!CurrentToken) |
| 70 | return SourceLocation(); |
| 71 | |
| 72 | Token PotentialMatch = *CurrentToken; |
| 73 | if (PotentialMatch.isOneOf(TK, TKs...)) |
| 74 | return PotentialMatch.getLocation(); |
| 75 | |
| 76 | // If we reach the end of the file, and eof is not the target token, we stop |
| 77 | // the loop, otherwise we will get infinite loop (findNextToken will return |
| 78 | // eof on eof). |
| 79 | if (PotentialMatch.is(tok::eof)) |
| 80 | return SourceLocation(); |
| 81 | Start = PotentialMatch.getLastLoc(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // Finds next token that's not a comment. |
| 86 | Optional<Token> findNextTokenSkippingComments(SourceLocation Start, |
| 87 | const SourceManager &SM, |
| 88 | const LangOptions &LangOpts); |
| 89 | |
| 90 | /// Re-lex the provide \p Range and return \c false if either a macro spans |
| 91 | /// multiple tokens, a pre-processor directive or failure to retrieve the |
| 92 | /// next token is found, otherwise \c true. |
| 93 | bool rangeContainsExpansionsOrDirectives(SourceRange Range, |
| 94 | const SourceManager &SM, |
| 95 | const LangOptions &LangOpts); |
| 96 | |
| 97 | /// Assuming that ``Range`` spans a CVR-qualified type, returns the |
| 98 | /// token in ``Range`` that is responsible for the qualification. ``Range`` |
| 99 | /// must be valid with respect to ``SM``. Returns ``None`` if no qualifying |
| 100 | /// tokens are found. |
| 101 | /// \note: doesn't support member function qualifiers. |
| 102 | llvm::Optional<Token> getQualifyingToken(tok::TokenKind TK, |
| 103 | CharSourceRange Range, |
| 104 | const ASTContext &Context, |
| 105 | const SourceManager &SM); |
| 106 | |
| 107 | } // namespace lexer |
| 108 | } // namespace utils |
| 109 | } // namespace tidy |
| 110 | } // namespace clang |
| 111 | |
| 112 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H |