diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 6f879006465ecf..543c119620bf28 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1414,10 +1414,6 @@ class AnnotatingParser { Tok->setType(TT_TrailingReturnArrow); } break; - case tok::eof: - if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0) - Tok->NewlinesBefore = 1; - break; default: break; } @@ -3244,8 +3240,14 @@ void TokenAnnotator::annotate(AnnotatedLine &Line) { else if (Line.startsWith(TT_ObjCProperty)) Line.Type = LT_ObjCProperty; - Line.First->SpacesRequiredBefore = 1; - Line.First->CanBreakBefore = Line.First->MustBreakBefore; + 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