| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 53 |
if (Lexer::getRawToken(L, T, SM, LangOpts, 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 |
|
| 80 |
|
| 81 |
|
| 82 |
if (PotentialMatch.is(tok::eof)) |
| 83 |
return SourceLocation(); |
| 84 |
Start = PotentialMatch.getLastLoc(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
std::optional<Token> findNextTokenSkippingComments(SourceLocation Start, |
| 90 |
const SourceManager &SM, |
| 91 |
const LangOptions &LangOpts); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
bool rangeContainsExpansionsOrDirectives(SourceRange Range, |
| 97 |
const SourceManager &SM, |
| 98 |
const LangOptions &LangOpts); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
std::optional<Token> getQualifyingToken(tok::TokenKind TK, |
| 106 |
CharSourceRange Range, |
| 107 |
const ASTContext &Context, |
| 108 |
const SourceManager &SM); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM, |
| 113 |
const LangOptions &LangOpts); |
| 114 |
|
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#endif |