llvm-project/clang-tools-extra/clang-tidy/utils/LexerUtils.cpp

40 lines
1.2 KiB
C++
Raw Normal View History

//===--- LexerUtils.cpp - clang-tidy---------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "LexerUtils.h"
namespace clang {
namespace tidy {
namespace lexer_utils {
Token getPreviousNonCommentToken(const ASTContext &Context,
SourceLocation Location) {
const auto &SourceManager = Context.getSourceManager();
Token Token;
Token.setKind(tok::unknown);
Location = Location.getLocWithOffset(-1);
auto StartOfFile =
SourceManager.getLocForStartOfFile(SourceManager.getFileID(Location));
while (Location != StartOfFile) {
Location = Lexer::GetBeginningOfToken(Location, SourceManager,
Context.getLangOpts());
if (!Lexer::getRawToken(Location, Token, SourceManager,
Context.getLangOpts()) &&
!Token.is(tok::comment)) {
break;
}
Location = Location.getLocWithOffset(-1);
}
return Token;
}
} // namespace lexer_utils
} // namespace tidy
} // namespace clang