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

Jonas Toth via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 5 07:15:19 PDT 2018


Author: jonastoth
Date: Fri Oct  5 07:15:19 2018
New Revision: 343850

URL: http://llvm.org/viewvc/llvm-project?rev=343850&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clang-tidy/bugprone/ArgumentCommentCheck.cpp
    clang-tools-extra/trunk/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
    clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
    clang-tools-extra/trunk/clang-tidy/utils/FixItHintUtils.cpp
    clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.cpp
    clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.h

Modified: clang-tools-extra/trunk/clang-tidy/bugprone/ArgumentCommentCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/ArgumentCommentCheck.cpp?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/ArgumentCommentCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/bugprone/ArgumentCommentCheck.cpp Fri Oct  5 07:15:19 2018
@@ -91,8 +91,9 @@ static std::vector<std::pair<SourceLocat
 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();

Modified: clang-tools-extra/trunk/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp Fri Oct  5 07:15:19 2018
@@ -40,7 +40,8 @@ void SuspiciousSemicolonCheck::check(con
     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);
 

Modified: clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp Fri Oct  5 07:15:19 2018
@@ -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:

Modified: clang-tools-extra/trunk/clang-tidy/utils/FixItHintUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/FixItHintUtils.cpp?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/FixItHintUtils.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/FixItHintUtils.cpp Fri Oct  5 07:15:19 2018
@@ -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(),

Modified: clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.cpp?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.cpp Fri Oct  5 07:15:19 2018
@@ -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;
     }

Modified: clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.h?rev=343850&r1=343849&r2=343850&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.h (original)
+++ clang-tools-extra/trunk/clang-tidy/utils/LexerUtils.h Fri Oct  5 07:15:19 2018
@@ -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




More information about the cfe-commits mailing list