Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 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 { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 14 | namespace utils { |
| 15 | namespace lexer { |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 16 | |
| 17 | Token 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 Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 38 | } // namespace lexer |
| 39 | } // namespace utils |
Alexander Kornienko | b959f4c | 2015-12-30 10:24:40 +0000 | [diff] [blame] | 40 | } // namespace tidy |
| 41 | } // namespace clang |