[clang] 006bf8d - [clang-format][NFC] Handle language specific stuff at the top...

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 24 12:28:23 PDT 2022


Author: Björn Schäpers
Date: 2022-10-24T21:28:13+02:00
New Revision: 006bf8d817704eb8addd95d894152a6199dc0718

URL: https://github.com/llvm/llvm-project/commit/006bf8d817704eb8addd95d894152a6199dc0718
DIFF: https://github.com/llvm/llvm-project/commit/006bf8d817704eb8addd95d894152a6199dc0718.diff

LOG: [clang-format][NFC] Handle language specific stuff at the top...

... of TokenAnnotator::splitPenalty. That is in my eyes a bit clearer
in the workflow.

As a drive-by introduce (but not adapt anywhere else) isProto().

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 3205b502ac195..b90de05a162c0 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2617,6 +2617,7 @@ struct FormatStyle {
   bool isJson() const { return Language == LK_Json; }
   bool isJavaScript() const { return Language == LK_JavaScript; }
   bool isVerilog() const { return Language == LK_Verilog; }
+  bool isProto() const { return Language == LK_Proto; }
 
   /// Language, this format style is targeted at.
   /// \version 3.5

diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 944bea5f1a7fc..f226e8ffa61c1 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3097,6 +3097,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
   if (Left.is(tok::semi))
     return 0;
 
+  // Language specific handling.
   if (Style.Language == FormatStyle::LK_Java) {
     if (Right.isOneOf(Keywords.kw_extends, Keywords.kw_throws))
       return 1;
@@ -3116,13 +3117,16 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     // Prefer breaking call chains (".foo") over empty "{}", "[]" or "()".
     if (Left.opensScope() && Right.closesScope())
       return 200;
+  } else if (Style.isProto()) {
+    if (Right.is(tok::l_square))
+      return 1;
+    if (Right.is(tok::period))
+      return 500;
   }
 
   if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
     return 1;
   if (Right.is(tok::l_square)) {
-    if (Style.Language == FormatStyle::LK_Proto)
-      return 1;
     if (Left.is(tok::r_square))
       return 200;
     // Slightly prefer formatting local lambda definitions like functions.
@@ -3135,10 +3139,8 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     }
   }
 
-  if (Left.is(tok::coloncolon) ||
-      (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) {
+  if (Left.is(tok::coloncolon))
     return 500;
-  }
   if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
       Right.is(tok::kw_operator)) {
     if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)


        


More information about the cfe-commits mailing list