summaryrefslogtreecommitdiffstats
path: root/lib/Format/UnwrappedLineFormatter.cpp
diff options
context:
space:
mode:
authorFrancois Ferrand <thetypz@gmail.com>2017-06-30 20:25:55 +0000
committerFrancois Ferrand <thetypz@gmail.com>2017-06-30 20:25:55 +0000
commit0ce8b803dc6e34c9938aeb37cd2c6460e529f0e0 (patch)
tree3bcea0d2a498819324ecbc5c0d8789085b83867b /lib/Format/UnwrappedLineFormatter.cpp
parent1e786d52fd3187a3f27de9b1206d3a408e2c1864 (diff)
clang-format: add options to merge empty record body
Summary: This patch introduces a few extra BraceWrapping options, similar to `SplitEmptyFunction`, to allow merging empty 'record' bodies (e.g. class, struct, union and namespace): * SplitEmptyClass * SplitEmptyStruct * SplitEmptyUnion * SplitEmptyNamespace The `SplitEmptyFunction` option name has also been simplified/ shortened (from `SplitEmptyFunctionBody`). These options are helpful when the correspond AfterXXX option is enabled, to allow merging the empty record: class Foo {}; In addition, this fixes an unexpected merging of short records, when the AfterXXXX options are used, which caused to be formatted like this: class Foo { void Foo(); }; This is now properly formatted as: class Foo { void Foo(); }; Reviewers: djasper, krasimir Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D34395 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@306874 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/UnwrappedLineFormatter.cpp')
-rw-r--r--lib/Format/UnwrappedLineFormatter.cpp28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/Format/UnwrappedLineFormatter.cpp b/lib/Format/UnwrappedLineFormatter.cpp
index 8836c07cac..2005a28229 100644
--- a/lib/Format/UnwrappedLineFormatter.cpp
+++ b/lib/Format/UnwrappedLineFormatter.cpp
@@ -136,10 +136,7 @@ private:
bool isNamespaceDeclaration(const AnnotatedLine *Line) {
const FormatToken *NamespaceTok = Line->First;
- // Detect "(inline)? namespace" in the beginning of a line.
- if (NamespaceTok->is(tok::kw_inline))
- NamespaceTok = NamespaceTok->getNextNonComment();
- return NamespaceTok && NamespaceTok->is(tok::kw_namespace);
+ return NamespaceTok && NamespaceTok->getNamespaceToken();
}
bool isEndOfNamespace(const AnnotatedLine *Line,
@@ -216,10 +213,31 @@ private:
if (TheLine->Last->is(TT_FunctionLBrace) &&
TheLine->First == TheLine->Last &&
- !Style.BraceWrapping.SplitEmptyFunctionBody &&
+ !Style.BraceWrapping.SplitEmptyFunction &&
I[1]->First->is(tok::r_brace))
return tryMergeSimpleBlock(I, E, Limit);
+ // Handle empty record blocks where the brace has already been wrapped
+ if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last &&
+ I != AnnotatedLines.begin()) {
+ bool EmptyBlock = I[1]->First->is(tok::r_brace);
+
+ const FormatToken *Tok = I[-1]->First;
+ if (Tok && Tok->is(tok::comment))
+ Tok = Tok->getNextNonComment();
+
+ if (Tok && Tok->getNamespaceToken())
+ return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
+ ? tryMergeSimpleBlock(I, E, Limit) : 0;
+
+ if (Tok && Tok->is(tok::kw_typedef))
+ Tok = Tok->getNextNonComment();
+ if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
+ Keywords.kw_interface))
+ return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
+ ? tryMergeSimpleBlock(I, E, Limit) : 0;
+ }
+
// FIXME: TheLine->Level != 0 might or might not be the right check to do.
// If necessary, change to something smarter.
bool MergeShortFunctions =