summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2017-06-19 07:45:41 +0000
committerDaniel Jasper <djasper@google.com>2017-06-19 07:45:41 +0000
commit2d7e549a3c69b2f8d269137c13f142fc8ebdbfee (patch)
treeeaeba648f63435bc50920257629c3414171cd2fd
parent57fae474a57c5a09403fc1e8ee98a28afad260f5 (diff)
clang-format: Improve understanding of combined typedef+record declarations
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
-rw-r--r--lib/Format/UnwrappedLineFormatter.cpp7
-rw-r--r--unittests/Format/FormatTest.cpp8
2 files changed, 13 insertions, 2 deletions
diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp
index 7f644651a6..63a0b7df59 100644
--- a/lib/Format/UnwrappedLineFormatter.cpp
+++ b/lib/Format/UnwrappedLineFormatter.cpp
@@ -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.
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index e6cd617abd..bae7fba0a6 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -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"
"};",