[PATCH] D136635: [clang-format] Don't misannotate in CTor init list

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 24 13:31:56 PDT 2022


HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: owenpan, curdeius, MyDeveloperDay, rymiel.
HazardyKnusperkeks added a project: clang-format.
Herald added a project: All.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

They were annotated with `TrailingAnnotation`, which they are not. And that resulted in some quirky formatting in some cases.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136635

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1059,6 +1059,28 @@
   EXPECT_TOKEN(Tokens[9], tok::colon, TT_GotoLabelColon);
 }
 
+TEST_F(TokenAnnotatorTest, UnderstandConstructors) {
+  auto Tokens = annotate("Class::Class() : BaseClass(), Member() {}");
+
+  // The TT_Unknown is clearly not binding for the future, please adapt if those
+  // tokens get annotated.
+  ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon);
+  EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown);
+  EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown);
+  EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma);
+  EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
+
+  Tokens = annotate("Class::Class() : BaseClass{}, Member{} {}");
+  EXPECT_TOKEN(Tokens[5], tok::colon, TT_CtorInitializerColon);
+  EXPECT_TOKEN(Tokens[6], tok::identifier, TT_Unknown);
+  EXPECT_TOKEN(Tokens[7], tok::l_brace, TT_Unknown);
+  EXPECT_TOKEN(Tokens[8], tok::r_brace, TT_Unknown);
+  EXPECT_TOKEN(Tokens[9], tok::comma, TT_CtorInitializerComma);
+  EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace);
+}
+
 } // namespace
 } // namespace format
 } // namespace clang
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1983,7 +1983,9 @@
     } else if (Current.isOneOf(tok::identifier, tok::kw_const, tok::kw_noexcept,
                                tok::kw_requires) &&
                Current.Previous &&
-               !Current.Previous->isOneOf(tok::equal, tok::at) &&
+               !Current.Previous->isOneOf(tok::equal, tok::at,
+                                          TT_CtorInitializerComma,
+                                          TT_CtorInitializerColon) &&
                Line.MightBeFunctionDecl && Contexts.size() == 1) {
       // Line.MightBeFunctionDecl can only be true after the parentheses of a
       // function declaration have been found.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136635.470276.patch
Type: text/x-patch
Size: 2288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221024/7a37edd4/attachment.bin>


More information about the cfe-commits mailing list