From 5be59ba8c9fb291f83970f0ebddceda70088bb28 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 15 May 2013 14:09:55 +0000 Subject: [PATCH] Don't put short namespace on a single line. Before: namespace abc { class SomeClass; } namespace def { void someFunction() {} } After: namespace abc { class Def; } namespace def { void someFunction() {} } Rationale: a) Having anything other than forward declaration on the same line as a namespace looks confusing. b) Formatting namespace-forward-declaration-combinations different from other stuff is inconsistent. c) Wasting vertical space close to such forward declarations really does not affect readability. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181887 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 9 ++++++--- unittests/Format/FormatTest.cpp | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 9e882157aa..9fd8ac6916 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1348,8 +1348,7 @@ private: if (I + 1 == E || (I + 1)->Type == LT_Invalid) return; - if (I->Last->is(tok::l_brace) && - Style.BreakBeforeBraces == FormatStyle::BS_Attach) { + if (I->Last->is(tok::l_brace)) { tryMergeSimpleBlock(I, E, Limit); } else if (I->First.is(tok::kw_if)) { tryMergeSimpleIf(I, E, Limit); @@ -1402,13 +1401,17 @@ private: void tryMergeSimpleBlock(std::vector::iterator &I, std::vector::iterator E, unsigned Limit) { + // No merging if the brace already is on the next line. + if (Style.BreakBeforeBraces != FormatStyle::BS_Attach) + return; + // First, check that the current line allows merging. This is the case if // we're not in a control flow statement and the last token is an opening // brace. 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_for, tok::kw_namespace, // This gets rid of all ObjC @ keywords and methods. tok::at, tok::minus, tok::plus)) return; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 499388e6ba..58dbffd863 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -745,11 +745,11 @@ TEST_F(FormatTest, SplitsLongCxxComments) { } TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) { - EXPECT_EQ("namespace {}\n// Test\n#define A", + EXPECT_EQ("namespace {\n}\n// Test\n#define A", format("namespace {}\n // Test\n#define A")); - EXPECT_EQ("namespace {}\n/* Test */\n#define A", + EXPECT_EQ("namespace {\n}\n/* Test */\n#define A", format("namespace {}\n /* Test */\n#define A")); - EXPECT_EQ("namespace {}\n/* Test */ #define A", + EXPECT_EQ("namespace {\n}\n/* Test */ #define A", format("namespace {}\n /* Test */ #define A")); } @@ -2921,7 +2921,10 @@ TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { " return\n" "}", format("void f ( ) { if ( a ) return }")); - EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); + EXPECT_EQ("namespace N {\n" + "void f()\n" + "}", + format("namespace N { void f() }")); EXPECT_EQ("namespace N {\n" "void f() {}\n" "void g()\n" -- 2.40.0