owenpan created this revision. owenpan added reviewers: MyDeveloperDay, HazardyKnusperkeks, rymiel. owenpan added a project: clang-format. Herald added a project: All. owenpan requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fixes #https://github.com/llvm/llvm-project/issues/38042. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D141035 Files: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/ConfigParseTest.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -25121,6 +25121,17 @@ #endif } +TEST_F(FormatTest, InsertNewlineAtEOF) { + verifyFormat("int i;\n"); + verifyFormat("int i;"); + + FormatStyle Style = getLLVMStyle(); + Style.InsertNewlineAtEOF = true; + + verifyFormat("int i;\n", Style); + verifyFormat("int i;\n", "int i;", Style); +} + } // namespace } // namespace format } // namespace clang Index: clang/unittests/Format/ConfigParseTest.cpp =================================================================== --- clang/unittests/Format/ConfigParseTest.cpp +++ clang/unittests/Format/ConfigParseTest.cpp @@ -167,6 +167,7 @@ CHECK_PARSE_BOOL(IndentRequiresClause); CHECK_PARSE_BOOL(IndentWrappedFunctionNames); CHECK_PARSE_BOOL(InsertBraces); + CHECK_PARSE_BOOL(InsertNewlineAtEOF); CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -1283,6 +1283,10 @@ Tok->setType(TT_TrailingReturnArrow); } break; + case tok::eof: + if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0) + Tok->NewlinesBefore = 1; + break; default: break; } Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -889,6 +889,7 @@ IO.mapOptional("IndentWrappedFunctionNames", Style.IndentWrappedFunctionNames); IO.mapOptional("InsertBraces", Style.InsertBraces); + IO.mapOptional("InsertNewlineAtEOF", Style.InsertNewlineAtEOF); IO.mapOptional("InsertTrailingCommas", Style.InsertTrailingCommas); IO.mapOptional("IntegerLiteralSeparator", Style.IntegerLiteralSeparator); IO.mapOptional("JavaImportGroups", Style.JavaImportGroups); @@ -1344,6 +1345,7 @@ LLVMStyle.IndentWidth = 2; LLVMStyle.IndentWrappedFunctionNames = false; LLVMStyle.InsertBraces = false; + LLVMStyle.InsertNewlineAtEOF = false; LLVMStyle.InsertTrailingCommas = FormatStyle::TCS_None; LLVMStyle.IntegerLiteralSeparator = {/*Binary=*/0, /*Decimal=*/0, /*Hex=*/0}; LLVMStyle.JavaScriptQuotes = FormatStyle::JSQS_Leave; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -2420,6 +2420,10 @@ /// \version 15 bool InsertBraces; + /// Insert a newline at end of file if missing. + /// \version 16 + bool InsertNewlineAtEOF; + /// The style of inserting trailing commas into container literals. enum TrailingCommaStyle : int8_t { /// Do not insert trailing commas. @@ -4120,6 +4124,7 @@ IndentWidth == R.IndentWidth && IndentWrappedFunctionNames == R.IndentWrappedFunctionNames && InsertBraces == R.InsertBraces && + InsertNewlineAtEOF == R.InsertNewlineAtEOF && IntegerLiteralSeparator.Binary == R.IntegerLiteralSeparator.Binary && IntegerLiteralSeparator.Decimal == R.IntegerLiteralSeparator.Decimal && Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -867,6 +867,7 @@ To match the default behavior of clang-format 15, use the ``Keyword`` value. - Add ``IntegerLiteralSeparator`` option for fixing integer literal separators in C++, C#, Java, and JavaScript. +- Add ``InsertNewlineAtEOF`` option for inserting a newline at EOF if missing. clang-extdef-mapping -------------------- Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -3127,6 +3127,9 @@ --i; --i; while (i); } while (i); +**InsertNewlineAtEOF** (``Boolean``) :versionbadge:`clang-format 16` + Insert a newline at end of file if missing. + **InsertTrailingCommas** (``TrailingCommaStyle``) :versionbadge:`clang-format 11` If set to ``TCS_Wrapped`` will insert trailing commas in container literals (arrays and objects) that wrap across multiple lines.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits