From a9eb2aafa120d3e2848e4a50da086a6f4430439d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 31 May 2013 14:56:20 +0000 Subject: 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 --- clang/lib/Format/Format.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'clang/lib/Format/Format.cpp') 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)) -- cgit v1.2.3