[PATCH] D32825: [clang-format] Improve understanding of combined typedef+record declarations

Jacob Bandes-Storch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 3 12:49:04 PDT 2017


jtbandes created this revision.
Herald added a subscriber: klimek.

Fixes an issue where `struct A { int X; };` would be broken onto multiple lines, but `typedef struct A { int X; } A2;` was collapsed onto a single line.


https://reviews.llvm.org/D32825

Files:
  lib/Format/UnwrappedLineFormatter.cpp
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -443,6 +443,14 @@
                "}",
                AllowSimpleBracedStatements);
 
+  verifyFormat("struct A2 {\n"
+               "  int X;\n"
+               "};",
+               AllowSimpleBracedStatements);
+  verifyFormat("typedef struct A2 {\n"
+               "  int X;\n"
+               "} A2_t;",
+               AllowSimpleBracedStatements);
   verifyFormat("template <int> struct A2 {\n"
                "  struct B {};\n"
                "};",
Index: lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- lib/Format/UnwrappedLineFormatter.cpp
+++ lib/Format/UnwrappedLineFormatter.cpp
@@ -365,8 +365,11 @@
     } else if (Limit != 0 && !Line.startsWith(tok::kw_namespace) &&
                !startsExternCBlock(Line)) {
       // We don't merge short records.
-      if (Line.First->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
-                              Keywords.kw_interface))
+      FormatToken *T =
+          Line.First->is(tok::kw_typedef) ? Line.First->Next : Line.First;
+      if (T &&
+          T->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
+                     Keywords.kw_interface))
         return 0;
 
       // Check that we still have three lines and they fit into the limit.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32825.97710.patch
Type: text/x-patch
Size: 1475 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170503/cce071b9/attachment.bin>


More information about the cfe-commits mailing list