clang-tools 24.0.0git
LexerUtils.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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_LEXERUTILS_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXERUTILS_H
11
12#include "clang/AST/ASTContext.h"
13#include "clang/Basic/TokenKinds.h"
14#include "clang/Lex/Lexer.h"
15#include "llvm/ADT/STLFunctionalExtras.h"
16#include <optional>
17#include <utility>
18#include <vector>
19
20namespace clang {
21
22class Stmt;
23
24namespace tidy::utils::lexer {
25
26/// Returns the spelling of an identifier token.
27StringRef getTokenName(const Token &Tok);
28
29/// Returns the raw tokens in \p Range. The returned tokens refer to the
30/// source manager's backing buffer.
31std::vector<Token> getRawTokens(CharSourceRange Range, const SourceManager &SM,
32 const LangOptions &LangOpts);
33
34/// Returns previous token or ``std::nullopt`` if not found.
35std::optional<Token> getPreviousToken(SourceLocation Location,
36 const SourceManager &SM,
37 const LangOptions &LangOpts,
38 bool SkipComments = true);
39
40std::pair<std::optional<Token>, SourceLocation>
41getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM,
42 const LangOptions &LangOpts, bool SkipComments = true);
43
44SourceLocation findPreviousTokenStart(SourceLocation Start,
45 const SourceManager &SM,
46 const LangOptions &LangOpts);
47
48SourceLocation findPreviousTokenKind(SourceLocation Start,
49 const SourceManager &SM,
50 const LangOptions &LangOpts,
51 tok::TokenKind TK);
52
53SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
54 const LangOptions &LangOpts);
55
56template <typename TokenKind, typename... TokenKinds>
57SourceLocation findPreviousAnyTokenKind(SourceLocation Start,
58 const SourceManager &SM,
59 const LangOptions &LangOpts,
60 TokenKind TK, TokenKinds... TKs) {
61 if (Start.isInvalid() || Start.isMacroID())
62 return {};
63 while (true) {
64 const SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts);
65 if (L.isInvalid() || L.isMacroID())
66 return {};
67
68 Token T;
69 // Returning 'true' is used to signal failure to retrieve the token.
70 if (Lexer::getRawToken(L, T, SM, LangOpts, /*IgnoreWhiteSpace=*/true))
71 return {};
72
73 if (T.isOneOf(TK, TKs...))
74 return T.getLocation();
75
76 Start = L;
77 }
78}
79
80template <typename TokenKind, typename... TokenKinds>
81SourceLocation findNextAnyTokenKind(SourceLocation Start,
82 const SourceManager &SM,
83 const LangOptions &LangOpts, TokenKind TK,
84 TokenKinds... TKs) {
85 while (true) {
86 std::optional<Token> CurrentToken =
87 Lexer::findNextToken(Start, SM, LangOpts);
88
89 if (!CurrentToken)
90 return {};
91
92 const Token PotentialMatch = *CurrentToken;
93 if (PotentialMatch.isOneOf(TK, TKs...))
94 return PotentialMatch.getLocation();
95
96 // If we reach the end of the file, and eof is not the target token, we stop
97 // the loop, otherwise we will get infinite loop (findNextToken will return
98 // eof on eof).
99 if (PotentialMatch.is(tok::eof))
100 return {};
101 Start = PotentialMatch.getLastLoc();
102 }
103}
104
105// Finds next token, possibly a comment.
106inline std::optional<Token>
107findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM,
108 const LangOptions &LangOpts) {
109 return Lexer::findNextToken(Start, SM, LangOpts, true);
110}
111
112// Finds next token that's not a comment.
113inline std::optional<Token>
114findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM,
115 const LangOptions &LangOpts) {
116 return Lexer::findNextToken(Start, SM, LangOpts, false);
117}
118
119/// Re-lex the provide \p Range and return \c false if either a macro spans
120/// multiple tokens, a pre-processor directive or failure to retrieve the
121/// next token is found, otherwise \c true.
122bool rangeContainsExpansionsOrDirectives(SourceRange Range,
123 const SourceManager &SM,
124 const LangOptions &LangOpts);
125
126// Represents a comment token and its source location in the original file.
128 SourceLocation Loc;
129 StringRef Text;
130};
131
132/// Returns all comment tokens found in the given range.
133std::vector<CommentToken> getCommentsInRange(CharSourceRange Range,
134 const SourceManager &SM,
135 const LangOptions &LangOpts);
136
137/// Returns comment tokens found in the given range. If a non-comment token is
138/// encountered, clears previously collected comments and continues.
139std::vector<CommentToken>
140getTrailingCommentsInRange(CharSourceRange Range, const SourceManager &SM,
141 const LangOptions &LangOpts);
142
143/// Returns source range of the first token in \p Range matching \p Pred.
144/// The returned char range starts at the matched token and ends at the start
145/// of the next token. Returns invalid range if no token matches.
146CharSourceRange
147findTokenTextInRange(CharSourceRange Range, const SourceManager &SM,
148 const LangOptions &LangOpts,
149 llvm::function_ref<bool(const Token &)> Pred);
150
151/// Assuming that ``Range`` spans a CVR-qualified type, returns the
152/// token in ``Range`` that is responsible for the qualification. ``Range``
153/// must be valid with respect to ``SM``. Returns ``std::nullopt`` if no
154/// qualifying tokens are found.
155/// \note: doesn't support member function qualifiers.
156std::optional<Token> getQualifyingToken(tok::TokenKind TK,
157 CharSourceRange Range,
158 const ASTContext &Context,
159 const SourceManager &SM);
160
161/// Stmt->getEndLoc does not always behave the same way depending on Token type.
162/// See implementation for exceptions.
163SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM,
164 const LangOptions &LangOpts);
165
166/// For a given FunctionDecl returns the location where you would need to place
167/// the noexcept specifier.
168SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl,
169 const SourceManager &SM);
170
171} // namespace tidy::utils::lexer
172} // namespace clang
173
174#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXERUTILS_H
std::pair< std::optional< Token >, SourceLocation > getPreviousTokenAndStart(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
SourceLocation getLocationForNoexceptSpecifier(const FunctionDecl *FuncDecl, const SourceManager &SM)
For a given FunctionDecl returns the location where you would need to place the noexcept specifier.
SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM, const LangOptions &LangOpts)
Stmt->getEndLoc does not always behave the same way depending on Token type.
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,...
std::vector< Token > getRawTokens(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns the raw tokens in Range.
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
StringRef getTokenName(const Token &Tok)
Returns the spelling of an identifier token.
std::optional< Token > getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or std::nullopt if not found.
std::vector< CommentToken > getTrailingCommentsInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns comment tokens found in the given range. If a non-comment token is encountered,...
SourceLocation findNextAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition LexerUtils.h:81
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition LexerUtils.h:114
SourceLocation findPreviousTokenStart(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
CharSourceRange findTokenTextInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts, llvm::function_ref< bool(const Token &)> Pred)
Returns source range of the first token in Range matching Pred. The returned char range starts at the...
std::vector< CommentToken > getCommentsInRange(CharSourceRange Range, const SourceManager &SM, const LangOptions &LangOpts)
Returns all comment tokens found in the given range.
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)
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...
std::optional< Token > findNextTokenIncludingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition LexerUtils.h:107
SourceLocation findPreviousAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition LexerUtils.h:57
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static constexpr const char FuncDecl[]