clang-tools 8.0.0

LexerUtils.h
Go to the documentation of this file.
1 //===--- LexerUtils.h - clang-tidy-------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
12
13 #include "clang/AST/ASTContext.h"
14 #include "clang/Lex/Lexer.h"
15
16 namespace clang {
17 namespace tidy {
18 namespace utils {
19 namespace lexer {
20
21 /// Returns previous token or ``tok::unknown`` if not found.
22 Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
23 const LangOptions &LangOpts, bool SkipComments = true);
24
25 SourceLocation findPreviousTokenStart(SourceLocation Start,
26 const SourceManager &SM,
27 const LangOptions &LangOpts);
28
29 SourceLocation findPreviousTokenKind(SourceLocation Start,
30 const SourceManager &SM,
31 const LangOptions &LangOpts,
32 tok::TokenKind TK);
33
34 SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM,
35 const LangOptions &LangOpts);
36
37 template <typename TokenKind, typename... TokenKinds>
38 SourceLocation findPreviousAnyTokenKind(SourceLocation Start,
39 const SourceManager &SM,
40 const LangOptions &LangOpts,
41 TokenKind TK, TokenKinds... TKs) {
42 while (true) {
43 SourceLocation L = findPreviousTokenStart(Start, SM, LangOpts);
44 if (L.isInvalid() || L.isMacroID())
45 return SourceLocation();
46
47 Token T;
48 // Returning 'true' is used to signal failure to retrieve the token.
49 if (Lexer::getRawToken(L, T, SM, LangOpts))
50 return SourceLocation();
51
52 if (T.isOneOf(TK, TKs...))
53 return T.getLocation();
54
55 Start = L;
56 }
57 }
58
59 template <typename TokenKind, typename... TokenKinds>
60 SourceLocation findNextAnyTokenKind(SourceLocation Start,
61 const SourceManager &SM,
62 const LangOptions &LangOpts, TokenKind TK,
63 TokenKinds... TKs) {
64 while (true) {
65 Optional<Token> CurrentToken = Lexer::findNextToken(Start, SM, LangOpts);
66
67 if (!CurrentToken)
68 return SourceLocation();
69
70 Token PotentialMatch = *CurrentToken;
71 if (PotentialMatch.isOneOf(TK, TKs...))
72 return PotentialMatch.getLocation();
73
74 Start = PotentialMatch.getLastLoc();
75 }
76 }
77
78 /// Re-lex the provide \p Range and return \c false if either a macro spans
79 /// multiple tokens, a pre-processor directive or failure to retrieve the
80 /// next token is found, otherwise \c true.
81 bool rangeContainsExpansionsOrDirectives(SourceRange Range,
82 const SourceManager &SM,
83 const LangOptions &LangOpts);
84
85 /// Assuming that ``Range`` spans a const-qualified type, returns the ``const``
86 /// token in ``Range`` that is responsible for const qualification. ``Range``
87 /// must be valid with respect to ``SM``. Returns ``None`` if no ``const``
88 /// tokens are found.
89 llvm::Optional<Token> getConstQualifyingToken(CharSourceRange Range,
90 const ASTContext &Context,
91 const SourceManager &SM);
92
93 } // namespace lexer
94 } // namespace utils
95 } // namespace tidy
96 } // namespace clang
97
98 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_LEXER_UTILS_H
clang::tidy::utils::lexer::rangeContainsExpansionsOrDirectives
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, a pre-processor directive or failure to retrieve the next token is found, otherwise true.
Definition: LexerUtils.cpp:72
clang::tidy::utils::lexer::getPreviousToken
Token getPreviousToken(SourceLocation Location, const SourceManager &SM, const LangOptions &LangOpts, bool SkipComments)
Returns previous token or tok::unknown if not found.
Definition: LexerUtils.cpp:17
clang::tidy::utils::lexer::findNextTerminator
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:67
clang::tidy::utils::lexer::findNextAnyTokenKind
SourceLocation findNextAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition: LexerUtils.h:60
clang::tidy::utils::lexer::getConstQualifyingToken
llvm::Optional< Token > getConstQualifyingToken(CharSourceRange Range, const ASTContext &Context, const SourceManager &SM)
Assuming that Range spans a const-qualified type, returns the const token in Range that is responsibl...
Definition: LexerUtils.cpp:96
clang::tidy::utils::lexer::findPreviousTokenStart
SourceLocation findPreviousTokenStart(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:34
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::tidy::utils::lexer::findPreviousAnyTokenKind
SourceLocation findPreviousAnyTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, TokenKind TK, TokenKinds... TKs)
Definition: LexerUtils.h:38
clang::tidy::utils::lexer::findPreviousTokenKind
SourceLocation findPreviousTokenKind(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts, tok::TokenKind TK)
Definition: LexerUtils.cpp:47
Location
Definition: Modularize.cpp:384