Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 1 | //===--- LexerUtils.h - clang-tidy-------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H |
| 11 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H |
| 12 | |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/Lex/Lexer.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace tidy { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 18 | namespace utils { |
| 19 | namespace lexer { |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 20 | |
Alexander Kornienko | 2d73022 | 2017-02-06 15:46:33 +0000 | [diff] [blame] | 21 | /// Returns previous token or ``tok::unknown`` if not found. |
Jonas Toth | a78c249 | 2018-10-05 14:15:19 +0000 | [diff] [blame] | 22 | Token getPreviousToken(SourceLocation Location, const SourceManager &SM, |
| 23 | const LangOptions &LangOpts, bool SkipComments = true); |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 24 | |
Jonas Toth | 0ea5af7 | 2018-10-31 16:50:44 +0000 | [diff] [blame] | 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 | while (true) { |
| 43 | SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts); |
| 44 | if (L.isInvalid() || L.isMacroID()) |
| 45 | return SourceLocation(); |
| 46 | |
| 47 | Token T; |
| 48 | // Returning 'true' is used to signal failure to retrieve the token. |
| 49 | if (Lexer::getRawToken(L, T, SM, LangOpts)) |
| 50 | return SourceLocation(); |
| 51 | |
| 52 | if (T.isOneOf(TK, TKs...)) |
| 53 | return T.getLocation(); |
| 54 | |
| 55 | Start = L; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | template <typename TokenKind, typename... TokenKinds> |
| 60 | SourceLocation findNextAnyTokenKind(SourceLocation Start, |
| 61 | const SourceManager &SM, |
| 62 | const LangOptions &LangOpts, TokenKind TK, |
| 63 | TokenKinds... TKs) { |
| 64 | while (true) { |
| 65 | Optional<Token> CurrentToken = Lexer::findNextToken(Start, SM, LangOpts); |
| 66 | |
| 67 | if (!CurrentToken) |
| 68 | return SourceLocation(); |
| 69 | |
| 70 | Token PotentialMatch = *CurrentToken; |
| 71 | if (PotentialMatch.isOneOf(TK, TKs...)) |
| 72 | return PotentialMatch.getLocation(); |
| 73 | |
| 74 | Start = PotentialMatch.getLastLoc(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Re-lex the provide \p Range and return \c false if either a macro spans |
| 79 | /// multiple tokens, a pre-processor directive or failure to retrieve the |
| 80 | /// next token is found, otherwise \c true. |
| 81 | bool rangeContainsExpansionsOrDirectives(SourceRange Range, |
| 82 | const SourceManager &SM, |
| 83 | const LangOptions &LangOpts); |
| 84 | |
Aaron Ballman | 3a02722 | 2018-10-31 19:11:38 +0000 | [diff] [blame] | 85 | /// Assuming that ``Range`` spans a const-qualified type, returns the ``const`` |
| 86 | /// token in ``Range`` that is responsible for const qualification. ``Range`` |
| 87 | /// must be valid with respect to ``SM``. Returns ``None`` if no ``const`` |
| 88 | /// tokens are found. |
| 89 | llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range, |
| 90 | const ASTContext &Context, |
| 91 | const SourceManager &SM); |
| 92 | |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 93 | } // namespace lexer |
| 94 | } // namespace utils |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 95 | } // namespace tidy |
| 96 | } // namespace clang |
| 97 | |
| 98 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H |