Skip to content

Commit 2cc6c7b

Browse files
owencallvmbot
authored andcommitted
[clang-format] Reimplement InsertNewlineAtEOF (#108513)
Fixes #108333. (cherry picked from commit 7153a4b)
1 parent 6407583 commit 2cc6c7b

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

clang/lib/Format/FormatTokenLexer.cpp

+7
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

-5
Original file line numberDiff line numberDiff line change
@@ -3680,11 +3680,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
36803680
auto *First = Line.First;
36813681
First->SpacesRequiredBefore = 1;
36823682
First->CanBreakBefore = First->MustBreakBefore;
3683-
3684-
if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
3685-
Style.InsertNewlineAtEOF) {
3686-
First->NewlinesBefore = 1;
3687-
}
36883683
}
36893684

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

clang/unittests/Format/FormatTest.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -27364,6 +27364,12 @@ TEST_F(FormatTest, InsertNewlineAtEOF) {
2736427364

2736527365
verifyNoChange("int i;\n", Style);
2736627366
verifyFormat("int i;\n", "int i;", Style);
27367+
27368+
constexpr StringRef Code{"namespace {\n"
27369+
"int i;\n"
27370+
"} // namespace"};
27371+
verifyFormat(Code.str() + '\n', Code, Style,
27372+
{tooling::Range(19, 13)}); // line 3
2736727373
}
2736827374

2736927375
TEST_F(FormatTest, KeepEmptyLinesAtEOF) {

0 commit comments

Comments
 (0)