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