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