[clang] 07181e2 - [clang-format][NFC] Simplify getFirstNonComment() in the annotator

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 18 13:23:54 PDT 2023


Author: Owen Pan
Date: 2023-08-18T13:23:47-07:00
New Revision: 07181e289e3c6296ed5e982dca0764b91acbb8e6

URL: https://github.com/llvm/llvm-project/commit/07181e289e3c6296ed5e982dca0764b91acbb8e6
DIFF: https://github.com/llvm/llvm-project/commit/07181e289e3c6296ed5e982dca0764b91acbb8e6.diff

LOG: [clang-format][NFC] Simplify getFirstNonComment() in the annotator

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

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/lib/Format/TokenAnnotator.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 73840332e22c2b..6972008a27fb1b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4763,16 +4763,6 @@ static bool isAllmanLambdaBrace(const FormatToken &Tok) {
          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral);
 }
 
-// Returns the first token on the line that is not a comment.
-static const FormatToken *getFirstNonComment(const AnnotatedLine &Line) {
-  const FormatToken *Next = Line.First;
-  if (!Next)
-    return Next;
-  if (Next->is(tok::comment))
-    Next = Next->getNextNonComment();
-  return Next;
-}
-
 bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
                                      const FormatToken &Right) const {
   const FormatToken &Left = *Right.Previous;
@@ -5044,7 +5034,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     return Right.HasUnescapedNewline;
 
   if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
-    auto FirstNonComment = getFirstNonComment(Line);
+    auto *FirstNonComment = Line.getFirstNonComment();
     bool AccessSpecifier =
         FirstNonComment &&
         FirstNonComment->isOneOf(Keywords.kw_internal, tok::kw_public,

diff  --git a/clang/lib/Format/TokenAnnotator.h b/clang/lib/Format/TokenAnnotator.h
index f3e5b397aa78b3..58e2cf79f488fa 100644
--- a/clang/lib/Format/TokenAnnotator.h
+++ b/clang/lib/Format/TokenAnnotator.h
@@ -151,6 +151,11 @@ class AnnotatedLine {
            startsWith(tok::kw_export, tok::kw_namespace);
   }
 
+  FormatToken *getFirstNonComment() const {
+    assert(First);
+    return First->is(tok::comment) ? First->getNextNonComment() : First;
+  }
+
   FormatToken *First;
   FormatToken *Last;
 


        


More information about the cfe-commits mailing list