clang-tools  5.0.0
LexerUtils.cpp
Go to the documentation of this file.
1 //===--- LexerUtils.cpp - clang-tidy---------------------------------------===//
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 #include "LexerUtils.h"
11 
12 namespace clang {
13 namespace tidy {
14 namespace utils {
15 namespace lexer {
16 
17 Token getPreviousToken(const ASTContext &Context, SourceLocation Location,
18  bool SkipComments) {
19  const auto &SourceManager = Context.getSourceManager();
20  Token Token;
21  Token.setKind(tok::unknown);
22  Location = Location.getLocWithOffset(-1);
23  auto StartOfFile =
24  SourceManager.getLocForStartOfFile(SourceManager.getFileID(Location));
25  while (Location != StartOfFile) {
26  Location = Lexer::GetBeginningOfToken(Location, SourceManager,
27  Context.getLangOpts());
28  if (!Lexer::getRawToken(Location, Token, SourceManager,
29  Context.getLangOpts()) &&
30  (!SkipComments || !Token.is(tok::comment))) {
31  break;
32  }
33  Location = Location.getLocWithOffset(-1);
34  }
35  return Token;
36 }
37 
38 } // namespace lexer
39 } // namespace utils
40 } // namespace tidy
41 } // namespace clang
Token getPreviousToken(const ASTContext &Context, SourceLocation Location, bool SkipComments)
Returns previous token or tok::unknown if not found.
Definition: LexerUtils.cpp:17
ClangTidyContext & Context
Definition: ClangTidy.cpp:87