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
16 namespace clang {
17
18 class Stmt;
19
20 namespace tidy {
21 namespace utils {
22 namespace lexer {
23
24 /// Returns previous token or ``tok::unknown`` if not found.
25 Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
26 const LangOptions &LangOpts, bool SkipComments = true);
27
28 SourceLocation findPreviousTokenStart(SourceLocation Start,
29 const SourceManager &SM,
30 const LangOptions &LangOpts);
31
32 SourceLocation findPreviousTokenKind(SourceLocation Start,
33 const SourceManager &SM,
34 const LangOptions &LangOpts,
35 tok::TokenKind TK);
36
37 SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
38 const LangOptions &LangOpts);
39
40 template <typename TokenKind, typename... TokenKinds>
findPreviousAnyTokenKind(SourceLocation Start,const SourceManager & SM,const LangOptions & LangOpts,TokenKind TK,TokenKinds...TKs)41 SourceLocation findPreviousAnyTokenKind(SourceLocation Start,
42 const SourceManager &SM,
43 const LangOptions &LangOpts,
44 TokenKind TK, TokenKinds... TKs) {
45 if (Start.isInvalid() || Start.isMacroID())
46 return SourceLocation();
47 while (true) {
48 SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts);
49 if (L.isInvalid() || L.isMacroID())
50 return SourceLocation();
51
52 Token T;
53 // Returning 'true' is used to signal failure to retrieve the token.
54 if (Lexer::getRawToken(L, T, SM, LangOpts, /*IgnoreWhiteSpace=*/true))
55 return SourceLocation();
56
57 if (T.isOneOf(TK, TKs...))
58 return T.getLocation();
59
60 Start = L;
61 }
62 }
63
64 template <typename TokenKind, typename... TokenKinds>
findNextAnyTokenKind(SourceLocation Start,const SourceManager & SM,const LangOptions & LangOpts,TokenKind TK,TokenKinds...TKs)65 SourceLocation findNextAnyTokenKind(SourceLocation Start,
66 const SourceManager &SM,
67 const LangOptions &LangOpts, TokenKind TK,
68 TokenKinds... TKs) {
69 while (true) {
70 Optional<Token> CurrentToken = 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.
89 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.
96 bool rangeContainsExpansionsOrDirectives(SourceRange Range,
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 ``None`` if no qualifying
103 /// tokens are found.
104 /// \note: doesn't support member function qualifiers.
105 llvm::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.
112 SourceLocation getUnifiedEndLoc(const Stmt &S, const SourceManager &SM,
113 const LangOptions &LangOpts);
114
115 } // namespace lexer
116 } // namespace utils
117 } // namespace tidy
118 } // namespace clang
119
120 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
121