clang-tools 17.0.0git
LexerUtils.h
Go to the documentation of this file.
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
17namespace clang {
18
19class Stmt;
20
21namespace tidy::utils::lexer {
22
23/// Returns previous token or ``tok::unknown`` if not found.
24Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
25 const LangOptions &LangOpts, bool SkipComments = true);
26
27SourceLocation findPreviousTokenStart(SourceLocation Start,
28 const SourceManager &SM,
29 const LangOptions &LangOpts);
30
31SourceLocation findPreviousTokenKind(SourceLocation Start,
32 const SourceManager &SM,
33 const LangOptions &LangOpts,
34 tok::TokenKind TK);
35
36SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
37 const LangOptions &LangOpts);
38
39template <typename TokenKind, typename... TokenKinds>
40SourceLocation 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
63template <typename TokenKind, typename... TokenKinds>
64SourceLocation 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.
89std::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.
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.
105std::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.
112SourceLocation 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
CharSourceRange Range
SourceRange for the file name.
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
Stmt->getEndLoc does not always behave the same way depending on Token type.
Definition: LexerUtils.cpp:198
bool rangeContainsExpansionsOrDirectives(SourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Re-lex the provide Range and return false if either a macro spans multiple tokens,...
Definition: LexerUtils.cpp:88
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:73
SourceLocation findNextAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition: LexerUtils.h:64
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
Definition: LexerUtils.cpp:16
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:79
SourceLocation findPreviousTokenStart(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:37
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)
Definition: LexerUtils.cpp:50
std::optional< Token > getQualifyingToken(tok::TokenKind TK, CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a CVR-qualified type, returns the token in Range that is responsible for th...
Definition: LexerUtils.cpp:112
SourceLocation findPreviousAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition: LexerUtils.h:40
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//