[PATCH] D52684: [clang-tidy] NFC refactor lexer-utils slightly to be easier to use

Jonas Toth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 29 04:50:48 PDT 2018


JonasToth created this revision.
JonasToth added reviewers: alexfh, aaron.ballman, hokein.
Herald added subscribers: cfe-commits, kbarton, xazax.hun, nemanjai.
JonasToth added a project: clang-tools-extra.

This patch is a small refactoring necessary for
'readability-isolate-declaration' and does not introduce functional changes.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D52684

Files:
  clang-tidy/bugprone/ArgumentCommentCheck.cpp
  clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
  clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
  clang-tidy/utils/LexerUtils.cpp
  clang-tidy/utils/LexerUtils.h


Index: clang-tidy/utils/LexerUtils.h
===================================================================
--- clang-tidy/utils/LexerUtils.h
+++ clang-tidy/utils/LexerUtils.h
@@ -19,8 +19,8 @@
 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
Index: clang-tidy/utils/LexerUtils.cpp
===================================================================
--- clang-tidy/utils/LexerUtils.cpp
+++ clang-tidy/utils/LexerUtils.cpp
@@ -14,19 +14,15 @@
 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;
     }
Index: clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
===================================================================
--- clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
+++ clang-tidy/cppcoreguidelines/ProTypeMemberInitCheck.cpp
@@ -120,12 +120,14 @@
     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:
Index: clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
===================================================================
--- clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
+++ clang-tidy/bugprone/SuspiciousSemicolonCheck.cpp
@@ -40,7 +40,8 @@
     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);
 
Index: clang-tidy/bugprone/ArgumentCommentCheck.cpp
===================================================================
--- clang-tidy/bugprone/ArgumentCommentCheck.cpp
+++ clang-tidy/bugprone/ArgumentCommentCheck.cpp
@@ -91,8 +91,9 @@
 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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52684.167590.patch
Type: text/x-patch
Size: 4279 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180929/c856a41a/attachment.bin>


More information about the cfe-commits mailing list