Skip to content

[clang-format][NFC] Minor efficiency cleanup#140835

Merged
owenca merged 1 commit intollvm:mainfrom
owenca:nfc
May 22, 2025
Merged

[clang-format][NFC] Minor efficiency cleanup#140835
owenca merged 1 commit intollvm:mainfrom
owenca:nfc

Conversation

@owenca
Copy link
Copy Markdown
Contributor

@owenca owenca commented May 21, 2025

No description provided.

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented May 21, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/140835.diff

2 Files Affected:

  • (modified) clang/lib/Format/FormatToken.h (+3-1)
  • (modified) clang/lib/Format/TokenAnnotator.cpp (+7-17)
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index e9d41183b5ebe..94014aee3221f 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -712,8 +712,10 @@ struct FormatToken {
   }
 
   bool isObjCLifetimeQualifier(const FormatStyle &Style) const {
-    if (Style.Language != FormatStyle::LK_ObjC || !TokenText.starts_with("__"))
+    if (Style.Language != FormatStyle::LK_ObjC || isNot(tok::identifier) ||
+        !TokenText.starts_with("__")) {
       return false;
+    }
     const auto Qualifier = TokenText.substr(2);
     return Qualifier == "autoreleasing" || Qualifier == "strong" ||
            Qualifier == "weak" || Qualifier == "unsafe_unretained";
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 542c362ccacae..23c26c821bec9 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -263,8 +263,7 @@ class AnnotatingParser {
             Previous->setType(TT_SelectorName);
           }
         }
-      }
-      if (Style.isTableGen()) {
+      } else if (Style.isTableGen()) {
         if (CurrentToken->isOneOf(tok::comma, tok::equal)) {
           // They appear as separators. Unless they are not in class definition.
           next();
@@ -3016,12 +3015,13 @@ class AnnotatingParser {
       return TT_BinaryOperator;
 
     const FormatToken *NextToken = Tok.getNextNonComment();
+    if (!NextToken)
+      return TT_PointerOrReference;
 
-    if (InTemplateArgument && NextToken && NextToken->is(tok::kw_noexcept))
+    if (InTemplateArgument && NextToken->is(tok::kw_noexcept))
       return TT_BinaryOperator;
 
-    if (!NextToken ||
-        NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
+    if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
                            TT_RequiresClause) ||
         (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
         NextToken->canBePointerOrReferenceQualifier() ||
@@ -6519,18 +6519,8 @@ TokenAnnotator::getTokenReferenceAlignment(const FormatToken &Reference) const {
 FormatStyle::PointerAlignmentStyle
 TokenAnnotator::getTokenPointerOrReferenceAlignment(
     const FormatToken &PointerOrReference) const {
-  if (PointerOrReference.isOneOf(tok::amp, tok::ampamp)) {
-    switch (Style.ReferenceAlignment) {
-    case FormatStyle::RAS_Pointer:
-      return Style.PointerAlignment;
-    case FormatStyle::RAS_Left:
-      return FormatStyle::PAS_Left;
-    case FormatStyle::RAS_Right:
-      return FormatStyle::PAS_Right;
-    case FormatStyle::RAS_Middle:
-      return FormatStyle::PAS_Middle;
-    }
-  }
+  if (PointerOrReference.isOneOf(tok::amp, tok::ampamp))
+    return getTokenReferenceAlignment(PointerOrReference);
   assert(PointerOrReference.is(tok::star));
   return Style.PointerAlignment;
 }

@owenca owenca merged commit d5d8d8a into llvm:main May 22, 2025
13 checks passed
@owenca owenca deleted the nfc branch May 22, 2025 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants