Skip to content

Commit 061f409

Browse files
committed
Remove reduntant tests and simplify code
1 parent f361664 commit 061f409

File tree

2 files changed

+16
-146
lines changed

2 files changed

+16
-146
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,6 @@ bool isRecordLBrace(const FormatToken &Tok) {
3232
TT_StructLBrace, TT_UnionLBrace);
3333
}
3434

35-
bool LineStartsNamespaceScope(const AnnotatedLine *Line,
36-
const AnnotatedLine *PreviousLine,
37-
const AnnotatedLine *PrevPrevLine) {
38-
return PreviousLine &&
39-
((PreviousLine->Last->is(tok::l_brace) &&
40-
PreviousLine->startsWithNamespace()) ||
41-
(PrevPrevLine && PrevPrevLine->startsWithNamespace() &&
42-
PreviousLine->startsWith(tok::l_brace)));
43-
}
44-
45-
bool LineEndsNamespaceScope(const AnnotatedLine *Line,
46-
const SmallVectorImpl<AnnotatedLine *> &Lines) {
47-
if (!Line)
48-
return false;
49-
const FormatToken *Tok = Line->First;
50-
if (!Tok || Tok->isNot(tok::r_brace))
51-
return false;
52-
return getNamespaceToken(Line, Lines) != nullptr;
53-
}
54-
5535
/// Tracks the indent level of \c AnnotatedLines across levels.
5636
///
5737
/// \c nextLine must be called for each \c AnnotatedLine, after which \c
@@ -1604,27 +1584,21 @@ static auto computeNewlines(const AnnotatedLine &Line,
16041584
Newlines = 1;
16051585
}
16061586

1607-
// Modify empty lines after "{" that opens namespace scope.
1608-
if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave &&
1609-
LineStartsNamespaceScope(&Line, PreviousLine, PrevPrevLine)) {
1610-
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1611-
Newlines = std::min(Newlines, 1u);
1612-
else if (!Line.startsWithNamespace())
1613-
Newlines = std::max(Newlines, 2u);
1614-
else
1615-
Newlines = std::min(Newlines, 1u);
1616-
}
1617-
1618-
// Modify empty lines before "}" that closes namespace scope.
1619-
if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave &&
1620-
LineEndsNamespaceScope(&Line, Lines)) {
1621-
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1622-
Newlines = std::min(Newlines, 1u);
1623-
else if (!LineEndsNamespaceScope(PreviousLine, Lines))
1624-
Newlines = std::max(Newlines, 2u);
1625-
else
1626-
Newlines = std::min(Newlines, 1u);
1627-
}
1587+
if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave) {
1588+
// Modify empty lines after TT_NamespaceLBrace.
1589+
if (PreviousLine && PreviousLine->endsWith(TT_NamespaceLBrace)) {
1590+
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1591+
Newlines = 1;
1592+
else if (!Line.startsWithNamespace())
1593+
Newlines = std::max(Newlines, 2u);
1594+
}
1595+
// Modify empty lines before TT_NamespaceRBrace.
1596+
if (Line.startsWith(TT_NamespaceRBrace)) {
1597+
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
1598+
Newlines = 1;
1599+
else if (!PreviousLine->startsWith(TT_NamespaceRBrace))
1600+
Newlines = std::max(Newlines, 2u);
1601+
}
16281602

16291603
// Insert or remove empty line before access specifiers.
16301604
if (PreviousLine && RootToken.isAccessSpecifier()) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -28430,6 +28430,7 @@ TEST_F(FormatTest, ShortNamespacesOption) {
2843028430
TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
2843128431
auto Style = getLLVMStyle();
2843228432
Style.FixNamespaceComments = false;
28433+
Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;
2843328434

2843428435
// Empty namespace.
2843528436
verifyFormat("namespace N {}", Style);
@@ -28511,21 +28512,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
2851128512
"}",
2851228513
Style);
2851328514

28514-
// Removing empty lines.
28515-
verifyFormat("namespace N {\n"
28516-
"\n"
28517-
"int a = 1;\n"
28518-
"\n"
28519-
"}",
28520-
"namespace N {"
28521-
"\n"
28522-
"\n"
28523-
"int a = 1;\n"
28524-
"\n"
28525-
"\n"
28526-
"}",
28527-
Style);
28528-
2852928515
Style.CompactNamespaces = true;
2853028516

2853128517
// Nested namespace.
@@ -28550,9 +28536,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
2855028536

2855128537
Style.CompactNamespaces = false;
2855228538

28553-
// Empty namespace.
28554-
verifyFormat("namespace N {}", Style);
28555-
2855628539
// Single namespace.
2855728540
verifyNoChange("namespace N {\n"
2855828541
"\n"
@@ -28576,93 +28559,6 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
2857628559
Style);
2857728560
}
2857828561

28579-
TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) {
28580-
auto Style = getLLVMStyle();
28581-
Style.FixNamespaceComments = false;
28582-
28583-
// Empty namespace.
28584-
verifyFormat("namespace N {}", Style);
28585-
28586-
// Single namespace.
28587-
verifyFormat("namespace N {\n"
28588-
"int f1(int a) { return 2 * a; }\n"
28589-
"}",
28590-
Style);
28591-
28592-
// Nested namespace.
28593-
verifyFormat("namespace N1 {\n"
28594-
"namespace N2 {\n"
28595-
"int a = 1;\n"
28596-
"}\n"
28597-
"}",
28598-
Style);
28599-
28600-
// Removing empty lines.
28601-
verifyFormat("namespace N {\n"
28602-
"\n"
28603-
"int a = 1;\n"
28604-
"\n"
28605-
"}",
28606-
"namespace N {\n"
28607-
"\n"
28608-
"\n"
28609-
"int a = 1;\n"
28610-
"\n"
28611-
"\n"
28612-
"}",
28613-
Style);
28614-
28615-
Style.MaxEmptyLinesToKeep = 0;
28616-
28617-
verifyFormat("namespace N {\n"
28618-
"int a = 1;\n"
28619-
"}",
28620-
"namespace N {\n"
28621-
"\n"
28622-
"\n"
28623-
"int a = 1;\n"
28624-
"\n"
28625-
"\n"
28626-
"}",
28627-
Style);
28628-
28629-
Style.MaxEmptyLinesToKeep = 2;
28630-
28631-
// Single namespace.
28632-
verifyNoChange("namespace N {\n"
28633-
"\n"
28634-
"\n"
28635-
"int f1(int a) { return 2 * a; }\n"
28636-
"\n"
28637-
"\n"
28638-
"}",
28639-
Style);
28640-
28641-
// Nested namespace.
28642-
verifyNoChange("namespace N1 {\n"
28643-
"namespace N2 {\n"
28644-
"\n"
28645-
"\n"
28646-
"int a = 1;\n"
28647-
"\n"
28648-
"\n"
28649-
"}\n"
28650-
"}",
28651-
Style);
28652-
28653-
Style.CompactNamespaces = true;
28654-
28655-
// Nested namespace.
28656-
verifyNoChange("namespace N1 { namespace N2 {\n"
28657-
"\n"
28658-
"\n"
28659-
"int a = 1;\n"
28660-
"\n"
28661-
"\n"
28662-
"}}",
28663-
Style);
28664-
}
28665-
2866628562
} // namespace
2866728563
} // namespace test
2866828564
} // namespace format

0 commit comments

Comments
 (0)