Skip to content

Commit 7153a4b

Browse files
authored
[clang-format] Reimplement InsertNewlineAtEOF (#108513)
Fixes #108333.
1 parent ddbe6c4 commit 7153a4b

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
100100
if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
101101
FirstInLineIndex = Tokens.size() - 1;
102102
} while (Tokens.back()->isNot(tok::eof));
103+
if (Style.InsertNewlineAtEOF) {
104+
auto &TokEOF = *Tokens.back();
105+
if (TokEOF.NewlinesBefore == 0) {
106+
TokEOF.NewlinesBefore = 1;
107+
TokEOF.OriginalColumn = 0;
108+
}
109+
}
103110
return Tokens;
104111
}
105112

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,11 +3704,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
37043704
auto *First = Line.First;
37053705
First->SpacesRequiredBefore = 1;
37063706
First->CanBreakBefore = First->MustBreakBefore;
3707-
3708-
if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
3709-
Style.InsertNewlineAtEOF) {
3710-
First->NewlinesBefore = 1;
3711-
}
37123707
}
37133708

37143709
// This function heuristically determines whether 'Current' starts the name of a

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27577,6 +27577,12 @@ TEST_F(FormatTest, InsertNewlineAtEOF) {
2757727577

2757827578
verifyNoChange("int i;\n", Style);
2757927579
verifyFormat("int i;\n", "int i;", Style);
27580+
27581+
constexpr StringRef Code{"namespace {\n"
27582+
"int i;\n"
27583+
"} // namespace"};
27584+
verifyFormat(Code.str() + '\n', Code, Style,
27585+
{tooling::Range(19, 13)}); // line 3
2758027586
}
2758127587

2758227588
TEST_F(FormatTest, KeepEmptyLinesAtEOF) {

0 commit comments

Comments
 (0)