blob: f80661d68db2e021f99e814849ae521c22f8ce03 [file] [log] [blame]
Alexander Kornienkob959f4c2015-12-30 10:24:40 +00001//===--- 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
12namespace clang {
13namespace tidy {
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000014namespace utils {
15namespace lexer {
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000016
17Token getPreviousNonCommentToken(const ASTContext &Context,
18 SourceLocation Location) {
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 !Token.is(tok::comment)) {
31 break;
32 }
33 Location = Location.getLocWithOffset(-1);
34 }
35 return Token;
36}
37
Etienne Bergeron2a4c00f2016-05-03 02:54:05 +000038} // namespace lexer
39} // namespace utils
Alexander Kornienkob959f4c2015-12-30 10:24:40 +000040} // namespace tidy
41} // namespace clang