]> granicus.if.org Git - clang/commitdiff
clang-format: Improve understanding of combined typedef+record declarations
authorDaniel Jasper <djasper@google.com>
Mon, 19 Jun 2017 07:45:41 +0000 (07:45 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 Jun 2017 07:45:41 +0000 (07:45 +0000)
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.

Patch by Jacob Bandes-Storch. Thank you.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@305667 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 7f644651a6ab860d12882cfedaaa20e7619adc67..63a0b7df59fcfaf6a2edd9f6840ecbc5bde3721d 100644 (file)
@@ -434,8 +434,11 @@ private:
     } 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 *RecordTok =
+          Line.First->is(tok::kw_typedef) ? Line.First->Next : Line.First;
+      if (RecordTok &&
+          RecordTok->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.
index e6cd617abd8aaeb890d82eec6bc0f460e8bb8490..bae7fba0a607fd0dd5ef5399e98337175a008305 100644 (file)
@@ -458,6 +458,14 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
                "}",
                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"
                "};",