[clang] 2c6ecc9 - [clang-format] Add an option to insert a newline at EOF if missing
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 5 15:25:59 PST 2023
Author: Owen Pan
Date: 2023-01-05T15:25:51-08:00
New Revision: 2c6ecc9db624442dcc4c80ab02d15e0833c7f8a3
URL: https://github.com/llvm/llvm-project/commit/2c6ecc9db624442dcc4c80ab02d15e0833c7f8a3
DIFF: https://github.com/llvm/llvm-project/commit/2c6ecc9db624442dcc4c80ab02d15e0833c7f8a3.diff
LOG: [clang-format] Add an option to insert a newline at EOF if missing
Closes #38042.
Differential Revision: https://reviews.llvm.org/D141035
Added:
Modified:
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
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 2fa0eef92c9d9..4134d5a387d2d 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3162,6 +3162,9 @@ the configuration (without a prefix: ``Auto``).
--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.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1e370acff3cc2..b69067139ffa5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -876,6 +876,7 @@ clang-format
in C++, C#, Java, and JavaScript.
- Add ``BreakAfterAttributes`` option for breaking after a group of C++11
attributes before a function declaration/definition name.
+- Add ``InsertNewlineAtEOF`` option for inserting a newline at EOF if missing.
clang-extdef-mapping
--------------------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 1762ff977aa49..6e5d3a163ec32 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2450,6 +2450,10 @@ struct FormatStyle {
/// \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.
@@ -4151,6 +4155,7 @@ struct FormatStyle {
IndentWidth == R.IndentWidth &&
IndentWrappedFunctionNames == R.IndentWrappedFunctionNames &&
InsertBraces == R.InsertBraces &&
+ InsertNewlineAtEOF == R.InsertNewlineAtEOF &&
IntegerLiteralSeparator.Binary == R.IntegerLiteralSeparator.Binary &&
IntegerLiteralSeparator.Decimal ==
R.IntegerLiteralSeparator.Decimal &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 223a86b8eba27..68f24fa8596a1 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -899,6 +899,7 @@ template <> struct MappingTraits<FormatStyle> {
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);
@@ -1355,6 +1356,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
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;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 05fd60e315235..3ed0b3d3c6612 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1289,6 +1289,10 @@ class AnnotatingParser {
Tok->setType(TT_TrailingReturnArrow);
}
break;
+ case tok::eof:
+ if (Style.InsertNewlineAtEOF && Tok->NewlinesBefore == 0)
+ Tok->NewlinesBefore = 1;
+ break;
default:
break;
}
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 75be2e2d8cfd0..ccdeaacdcf40f 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -167,6 +167,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
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);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4e4d23908a6e9..2265b03f0868a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25171,6 +25171,14 @@ TEST_F(FormatTest, BreakAfterAttributes) {
EXPECT_EQ(Code, format(Code, Style));
}
+TEST_F(FormatTest, InsertNewlineAtEOF) {
+ FormatStyle Style = getLLVMStyle();
+ Style.InsertNewlineAtEOF = true;
+
+ verifyFormat("int i;\n", Style);
+ verifyFormat("int i;\n", "int i;", Style);
+}
+
} // namespace
} // namespace format
} // namespace clang
More information about the cfe-commits
mailing list