Skip to content

Commit

Permalink
[clang-format] Reimplement InsertNewlineAtEOF (llvm#108513)
Browse files Browse the repository at this point in the history
  • Loading branch information
owenca authored Sep 18, 2024
1 parent ddbe6c4 commit 7153a4b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 7 additions & 0 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ ArrayRef<FormatToken *> FormatTokenLexer::lex() {
if (Tokens.back()->NewlinesBefore > 0 || Tokens.back()->IsMultiline)
FirstInLineIndex = Tokens.size() - 1;
} while (Tokens.back()->isNot(tok::eof));
if (Style.InsertNewlineAtEOF) {
auto &TokEOF = *Tokens.back();
if (TokEOF.NewlinesBefore == 0) {
TokEOF.NewlinesBefore = 1;
TokEOF.OriginalColumn = 0;
}
}
return Tokens;
}

Expand Down
5 changes: 0 additions & 5 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3704,11 +3704,6 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) {
auto *First = Line.First;
First->SpacesRequiredBefore = 1;
First->CanBreakBefore = First->MustBreakBefore;

if (First->is(tok::eof) && First->NewlinesBefore == 0 &&
Style.InsertNewlineAtEOF) {
First->NewlinesBefore = 1;
}
}

// This function heuristically determines whether 'Current' starts the name of a
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27577,6 +27577,12 @@ TEST_F(FormatTest, InsertNewlineAtEOF) {

verifyNoChange("int i;\n", Style);
verifyFormat("int i;\n", "int i;", Style);

constexpr StringRef Code{"namespace {\n"
"int i;\n"
"} // namespace"};
verifyFormat(Code.str() + '\n', Code, Style,
{tooling::Range(19, 13)}); // line 3
}

TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
Expand Down

0 comments on commit 7153a4b

Please sign in to comment.