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