1 //===--- LexerUtils.h - clang-tidy-------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
12 #include "clang/AST/ASTContext.h"
13 #include "clang/Basic/TokenKinds.h"
14 #include "clang/Lex/Lexer.h"
21 namespace tidy::utils::lexer
{
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);
27 SourceLocation
findPreviousTokenStart(SourceLocation Start
,
28 const SourceManager
&SM
,
29 const LangOptions
&LangOpts
);
31 SourceLocation
findPreviousTokenKind(SourceLocation Start
,
32 const SourceManager
&SM
,
33 const LangOptions
&LangOpts
,
36 SourceLocation
findNextTerminator(SourceLocation Start
, const SourceManager
&SM
,
37 const LangOptions
&LangOpts
);
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();
47 SourceLocation L
= findPreviousTokenStart(Start
, SM
, LangOpts
);
48 if (L
.isInvalid() || L
.isMacroID())
49 return SourceLocation();
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();
56 if (T
.isOneOf(TK
, TKs
...))
57 return T
.getLocation();
63 template <typename TokenKind
, typename
... TokenKinds
>
64 SourceLocation
findNextAnyTokenKind(SourceLocation Start
,
65 const SourceManager
&SM
,
66 const LangOptions
&LangOpts
, TokenKind TK
,
69 std::optional
<Token
> CurrentToken
=
70 Lexer::findNextToken(Start
, SM
, LangOpts
);
73 return SourceLocation();
75 Token PotentialMatch
= *CurrentToken
;
76 if (PotentialMatch
.isOneOf(TK
, TKs
...))
77 return PotentialMatch
.getLocation();
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
82 if (PotentialMatch
.is(tok::eof
))
83 return SourceLocation();
84 Start
= PotentialMatch
.getLastLoc();
89 findNextTokenIncludingComments(SourceLocation Start
, const SourceManager
&SM
,
90 const LangOptions
&LangOpts
);
92 // Finds next token that's not a comment.
93 std::optional
<Token
> findNextTokenSkippingComments(SourceLocation Start
,
94 const SourceManager
&SM
,
95 const LangOptions
&LangOpts
);
97 /// Re-lex the provide \p Range and return \c false if either a macro spans
98 /// multiple tokens, a pre-processor directive or failure to retrieve the
99 /// next token is found, otherwise \c true.
100 bool rangeContainsExpansionsOrDirectives(SourceRange Range
,
101 const SourceManager
&SM
,
102 const LangOptions
&LangOpts
);
104 /// Assuming that ``Range`` spans a CVR-qualified type, returns the
105 /// token in ``Range`` that is responsible for the qualification. ``Range``
106 /// must be valid with respect to ``SM``. Returns ``std::nullopt`` if no
107 /// qualifying tokens are found.
108 /// \note: doesn't support member function qualifiers.
109 std::optional
<Token
> getQualifyingToken(tok::TokenKind TK
,
110 CharSourceRange Range
,
111 const ASTContext
&Context
,
112 const SourceManager
&SM
);
114 /// Stmt->getEndLoc does not always behave the same way depending on Token type.
115 /// See implementation for exceptions.
116 SourceLocation
getUnifiedEndLoc(const Stmt
&S
, const SourceManager
&SM
,
117 const LangOptions
&LangOpts
);
119 /// For a given FunctionDecl returns the location where you would need to place
120 /// the noexcept specifier.
121 SourceLocation
getLocationForNoexceptSpecifier(const FunctionDecl
*FuncDecl
,
122 const SourceManager
&SM
);
124 } // namespace tidy::utils::lexer
127 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H