[clang-tidy] NFC refactor lexer-utils to be usable without ASTContext

Summary:
This patch is a small refactoring necessary for
'readability-isolate-declaration' and does not introduce functional changes.
It allows to use the utility functions without a full `ASTContext` and requires only the `SourceManager` and the `LangOpts`.

Reviewers: alexfh, aaron.ballman, hokein

Reviewed By: alexfh

Subscribers: nemanjai, xazax.hun, kbarton, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D52684

llvm-svn: 343850
This commit is contained in:
Jonas Toth 2018-10-05 14:15:19 +00:00
parent 5fb9746c49
commit a78c249af6
6 changed files with 18 additions and 17 deletions

View File

@ -91,8 +91,9 @@ static std::vector<std::pair<SourceLocation, StringRef>>
getCommentsBeforeLoc(ASTContext *Ctx, SourceLocation Loc) {
std::vector<std::pair<SourceLocation, StringRef>> Comments;
while (Loc.isValid()) {
clang::Token Tok =
utils::lexer::getPreviousToken(*Ctx, Loc, /*SkipComments=*/false);
clang::Token Tok = utils::lexer::getPreviousToken(
Loc, Ctx->getSourceManager(), Ctx->getLangOpts(),
/*SkipComments=*/false);
if (Tok.isNot(tok::comment))
break;
Loc = Tok.getLocation();

View File

@ -40,7 +40,8 @@ void SuspiciousSemicolonCheck::check(const MatchFinder::MatchResult &Result) {
return;
ASTContext &Ctxt = *Result.Context;
auto Token = utils::lexer::getPreviousToken(Ctxt, LocStart);
auto Token = utils::lexer::getPreviousToken(LocStart, Ctxt.getSourceManager(),
Ctxt.getLangOpts());
auto &SM = *Result.SourceManager;
unsigned SemicolonLine = SM.getSpellingLineNumber(LocStart);

View File

@ -120,12 +120,14 @@ struct IntializerInsertion {
switch (Placement) {
case InitializerPlacement::New:
Location = utils::lexer::getPreviousToken(
Context, Constructor.getBody()->getBeginLoc())
Constructor.getBody()->getBeginLoc(),
Context.getSourceManager(), Context.getLangOpts())
.getLocation();
break;
case InitializerPlacement::Before:
Location = utils::lexer::getPreviousToken(
Context, Where->getSourceRange().getBegin())
Where->getSourceRange().getBegin(),
Context.getSourceManager(), Context.getLangOpts())
.getLocation();
break;
case InitializerPlacement::After:

View File

@ -18,7 +18,8 @@ namespace fixit {
FixItHint changeVarDeclToReference(const VarDecl &Var, ASTContext &Context) {
SourceLocation AmpLocation = Var.getLocation();
auto Token = utils::lexer::getPreviousToken(Context, AmpLocation);
auto Token = utils::lexer::getPreviousToken(
AmpLocation, Context.getSourceManager(), Context.getLangOpts());
if (!Token.is(tok::unknown))
AmpLocation = Lexer::getLocForEndOfToken(Token.getLocation(), 0,
Context.getSourceManager(),

View File

@ -14,19 +14,15 @@ namespace tidy {
namespace utils {
namespace lexer {
Token getPreviousToken(const ASTContext &Context, SourceLocation Location,
bool SkipComments) {
const auto &SourceManager = Context.getSourceManager();
Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
const LangOptions &LangOpts, bool SkipComments) {
Token Token;
Token.setKind(tok::unknown);
Location = Location.getLocWithOffset(-1);
auto StartOfFile =
SourceManager.getLocForStartOfFile(SourceManager.getFileID(Location));
auto StartOfFile = SM.getLocForStartOfFile(SM.getFileID(Location));
while (Location != StartOfFile) {
Location = Lexer::GetBeginningOfToken(Location, SourceManager,
Context.getLangOpts());
if (!Lexer::getRawToken(Location, Token, SourceManager,
Context.getLangOpts()) &&
Location = Lexer::GetBeginningOfToken(Location, SM, LangOpts);
if (!Lexer::getRawToken(Location, Token, SM, LangOpts) &&
(!SkipComments || !Token.is(tok::comment))) {
break;
}

View File

@ -19,8 +19,8 @@ namespace utils {
namespace lexer {
/// Returns previous token or ``tok::unknown`` if not found.
Token getPreviousToken(const ASTContext &Context, SourceLocation Location,
bool SkipComments = true);
Token getPreviousToken(SourceLocation Location, const SourceManager &SM,
const LangOptions &LangOpts, bool SkipComments = true);
} // namespace lexer
} // namespace utils