[clang] e5a8f23 - [clang-format] Fix the issue that empty lines being removed at the beginning of namespace
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 27 08:02:11 PDT 2021
Author: Darwin Xu
Date: 2021-06-27T15:59:21+01:00
New Revision: e5a8f230c765aebff34221cb3e084316496fc08e
URL: https://github.com/llvm/llvm-project/commit/e5a8f230c765aebff34221cb3e084316496fc08e
DIFF: https://github.com/llvm/llvm-project/commit/e5a8f230c765aebff34221cb3e084316496fc08e.diff
LOG: [clang-format] Fix the issue that empty lines being removed at the beginning of namespace
This is a bug fix of https://bugs.llvm.org/show_bug.cgi?id=50116
Reviewed By: MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D104044
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/lib/Format/UnwrappedLineFormatter.h
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 3255c71d3407..cca85c1074de 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1134,6 +1134,7 @@ unsigned UnwrappedLineFormatter::format(
unsigned Penalty = 0;
LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
AdditionalIndent);
+ const AnnotatedLine *PrevPrevLine = nullptr;
const AnnotatedLine *PreviousLine = nullptr;
const AnnotatedLine *NextLine = nullptr;
@@ -1172,7 +1173,7 @@ unsigned UnwrappedLineFormatter::format(
if (ShouldFormat && TheLine.Type != LT_Invalid) {
if (!DryRun) {
bool LastLine = Line->First->is(tok::eof);
- formatFirstToken(TheLine, PreviousLine, Lines, Indent,
+ formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
LastLine ? LastStartColumn : NextStartColumn + Indent);
}
@@ -1218,7 +1219,7 @@ unsigned UnwrappedLineFormatter::format(
TheLine.LeadingEmptyLinesAffected);
// Format the first token.
if (ReformatLeadingWhitespace)
- formatFirstToken(TheLine, PreviousLine, Lines,
+ formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
TheLine.First->OriginalColumn,
TheLine.First->OriginalColumn);
else
@@ -1234,6 +1235,7 @@ unsigned UnwrappedLineFormatter::format(
}
if (!DryRun)
markFinalized(TheLine.First);
+ PrevPrevLine = PreviousLine;
PreviousLine = &TheLine;
}
PenaltyCache[CacheKey] = Penalty;
@@ -1242,6 +1244,7 @@ unsigned UnwrappedLineFormatter::format(
void UnwrappedLineFormatter::formatFirstToken(
const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
+ const AnnotatedLine *PrevPrevLine,
const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
unsigned NewlineIndent) {
FormatToken &RootToken = *Line.First;
@@ -1273,6 +1276,8 @@ void UnwrappedLineFormatter::formatFirstToken(
if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
PreviousLine->Last->is(tok::l_brace) &&
!PreviousLine->startsWithNamespace() &&
+ !(PrevPrevLine && PrevPrevLine->startsWithNamespace() &&
+ PreviousLine->startsWith(tok::l_brace)) &&
!startsExternCBlock(*PreviousLine))
Newlines = 1;
diff --git a/clang/lib/Format/UnwrappedLineFormatter.h b/clang/lib/Format/UnwrappedLineFormatter.h
index a1ff16999589..3e33de07fa12 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.h
+++ b/clang/lib/Format/UnwrappedLineFormatter.h
@@ -47,6 +47,7 @@ class UnwrappedLineFormatter {
/// of the \c UnwrappedLine if there was no structural parsing error.
void formatFirstToken(const AnnotatedLine &Line,
const AnnotatedLine *PreviousLine,
+ const AnnotatedLine *PrevPrevLine,
const SmallVectorImpl<AnnotatedLine *> &Lines,
unsigned Indent, unsigned NewlineIndent);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e5fa17270cdf..6747fe749a2f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -262,6 +262,128 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"}",
getGoogleStyle()));
+ auto CustomStyle = clang::format::getLLVMStyle();
+ CustomStyle.BreakBeforeBraces = clang::format::FormatStyle::BS_Custom;
+ CustomStyle.BraceWrapping.AfterNamespace = true;
+ CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false;
+ EXPECT_EQ("namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("namespace N\n"
+ "{\n"
+ "\n"
+ "\n"
+ "int i;\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("/* something */ namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("/* something */ namespace N {\n"
+ "\n"
+ "\n"
+ "int i;\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("inline namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("inline namespace N\n"
+ "{\n"
+ "\n"
+ "\n"
+ "int i;\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("/* something */ inline namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("/* something */ inline namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("export namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ format("export namespace N\n"
+ "{\n"
+ "\n"
+ "int i;\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("namespace a\n"
+ "{\n"
+ "namespace b\n"
+ "{\n"
+ "\n"
+ "class AA {};\n"
+ "\n"
+ "} // namespace b\n"
+ "} // namespace a\n",
+ format("namespace a\n"
+ "{\n"
+ "namespace b\n"
+ "{\n"
+ "\n"
+ "\n"
+ "class AA {};\n"
+ "\n"
+ "\n"
+ "}\n"
+ "}\n",
+ CustomStyle));
+ EXPECT_EQ("namespace A /* comment */\n"
+ "{\n"
+ "class B {}\n"
+ "} // namespace A",
+ format("namespace A /* comment */ { class B {} }", CustomStyle));
+ EXPECT_EQ("namespace A\n"
+ "{ /* comment */\n"
+ "class B {}\n"
+ "} // namespace A",
+ format("namespace A {/* comment */ class B {} }", CustomStyle));
+ EXPECT_EQ("namespace A\n"
+ "{ /* comment */\n"
+ "\n"
+ "class B {}\n"
+ "\n"
+ ""
+ "} // namespace A",
+ format("namespace A { /* comment */\n"
+ "\n"
+ "\n"
+ "class B {}\n"
+ "\n"
+ "\n"
+ "}",
+ CustomStyle));
+ EXPECT_EQ("namespace A /* comment */\n"
+ "{\n"
+ "\n"
+ "class B {}\n"
+ "\n"
+ "} // namespace A",
+ format("namespace A/* comment */ {\n"
+ "\n"
+ "\n"
+ "class B {}\n"
+ "\n"
+ "\n"
+ "}",
+ CustomStyle));
+
// ...but do keep inlining and removing empty lines for non-block extern "C"
// functions.
verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
More information about the cfe-commits
mailing list