diff options
author | Daniel Jasper <djasper@google.com> | 2013-05-31 14:56:20 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-05-31 14:56:20 +0000 |
commit | a9eb2aafa120d3e2848e4a50da086a6f4430439d (patch) | |
tree | cb99a103106bb386b56f2acf981aa228fb3df45a /clang/lib | |
parent | 2c611c034163b5211c141b7964c4d2e4d9b7bcdb (diff) | |
download | bcm5719-llvm-a9eb2aafa120d3e2848e4a50da086a6f4430439d.tar.gz bcm5719-llvm-a9eb2aafa120d3e2848e4a50da086a6f4430439d.zip |
Make formatting of empty blocks more consistent.
With this patch, the simplified rule is:
If the block is part of a declaration (class, namespace, function,
enum, ..), merge an empty block onto a single line. Otherwise
(specifically for the compound statements of if, for, while, ...),
keep the braces on two separate lines.
The reasons are:
- Mostly the formatting of empty blocks does not matter much.
- Empty compound statements are really rare and are usually just
inserted while still working on the code. If they are on two lines,
inserting code is easier. Also, overlooking the "{}" of an
"if (...) {}" can be really bad.
- Empty declarations are not uncommon, e.g. empty constructors. Putting
them on one line saves vertical space at no loss of readability.
llvm-svn: 183008
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Format/Format.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index b6e8079b31a..3ae279216f8 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1479,20 +1479,21 @@ private: AnnotatedLine &Line = *I; if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::r_brace, tok::kw_else, tok::kw_try, tok::kw_catch, - tok::kw_for, tok::kw_namespace, + tok::kw_for, // This gets rid of all ObjC @ keywords and methods. tok::at, tok::minus, tok::plus)) return; FormatToken *Tok = (I + 1)->First; - if (Tok->getNextNoneComment() == NULL && Tok->is(tok::r_brace) && - !Tok->MustBreakBefore) { + if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && + (Tok->getNextNoneComment() == NULL || + Tok->getNextNoneComment()->is(tok::semi))) { // We merge empty blocks even if the line exceeds the column limit. Tok->SpacesRequiredBefore = 0; Tok->CanBreakBefore = true; join(Line, *(I + 1)); I += 1; - } else if (Limit != 0) { + } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace)) { // Check that we still have three lines and they fit into the limit. if (I + 2 == E || (I + 2)->Type == LT_Invalid || !nextTwoLinesFitInto(I, Limit)) |