Skip to content

Commit 6548c31

Browse files
author
Galen Elias
committed
Address additional PR comments
1 parent 22967da commit 6548c31

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

clang/include/clang/Format/Format.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ struct FormatStyle {
988988
/// \version 3.7
989989
bool AllowShortLoopsOnASingleLine;
990990

991-
/// If ``true``, ``namespace a { class b; }`` can be put on a single a single
992-
/// line.
991+
/// If ``true``, ``namespace a { class b; }`` can be put on a single line.
993992
/// \version 20
994993
bool AllowShortNamespacesOnASingleLine;
995994

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ class LineJoiner {
368368
if (TheLine->Last->is(tok::l_brace)) {
369369
if (Style.AllowShortNamespacesOnASingleLine &&
370370
TheLine->First->is(tok::kw_namespace)) {
371-
if (unsigned result = tryMergeNamespace(I, E, Limit))
371+
unsigned result = tryMergeNamespace(I, E, Limit);
372+
if (result > 0)
372373
return result;
373374
}
374375
}
@@ -643,26 +644,27 @@ class LineJoiner {
643644
if (!nextTwoLinesFitInto(I, Limit))
644645
return 0;
645646

646-
// Check if it's a namespace inside a namespace, and call recursively if so
647+
// Check if it's a namespace inside a namespace, and call recursively if so.
647648
// '3' is the sizes of the whitespace and closing brace for " _inner_ }".
648649
if (I[1]->First->is(tok::kw_namespace)) {
649-
if (I[1]->Last->is(TT_LineComment))
650+
if (I[1]->Last->is(tok::comment))
650651
return 0;
651652

652653
const unsigned InnerLimit = Limit - I[1]->Last->TotalLength - 3;
653654
const unsigned MergedLines = tryMergeNamespace(I + 1, E, InnerLimit);
654655
if (!MergedLines)
655656
return 0;
657+
const auto N = MergedLines + 2;
656658
// Check if there is even a line after the inner result.
657-
if (std::distance(I, E) <= MergedLines + 2)
659+
if (std::distance(I, E) <= N)
658660
return 0;
659661
// Check that the line after the inner result starts with a closing brace
660662
// which we are permitted to merge into one line.
661-
if (I[2 + MergedLines]->First->is(tok::r_brace) &&
662-
!I[2 + MergedLines]->First->MustBreakBefore &&
663-
!I[1 + MergedLines]->Last->is(TT_LineComment) &&
664-
nextNLinesFitInto(I, I + 2 + MergedLines + 1, Limit)) {
665-
return 2 + MergedLines;
663+
if (I[N]->First->is(tok::r_brace) &&
664+
!I[N]->First->MustBreakBefore &&
665+
!I[MergedLines + 1]->Last->is(tok::comment) &&
666+
nextNLinesFitInto(I, I + N + 1, Limit)) {
667+
return N;
666668
}
667669
return 0;
668670
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28407,7 +28407,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
2840728407

2840828408
// If we can't merge an outer nested namespaces, but can merge an inner
2840928409
// nested namespace, then CompactNamespaces will merge the outer namespace
28410-
// first, preventing the merging of the inner namespace
28410+
// first, preventing the merging of the inner namespace.
2841128411
verifyFormat("namespace foo { namespace baz {\n"
2841228412
"class qux;\n"
2841328413
"} // comment\n"

0 commit comments

Comments
 (0)