Skip to content

Commit e3eca33

Browse files
committed
[clang-format] Replace DeriveLineEnding and UseCRLF with LineEnding
Below is the mapping: LineEnding DeriveLineEnding UseCRLF LF false false CRLF false true DeriveLF true false DeriveCRLF true true Differential Revision: https://reviews.llvm.org/D141654
1 parent df58935 commit e3eca33

File tree

7 files changed

+97
-35
lines changed

7 files changed

+97
-35
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,8 +2685,8 @@ the configuration (without a prefix: ``Auto``).
26852685
.. _DeriveLineEnding:
26862686

26872687
**DeriveLineEnding** (``Boolean``) :versionbadge:`clang-format 10` :ref:`<DeriveLineEnding>`
2688-
Analyze the formatted file for the most used line ending (``\r\n``
2689-
or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived.
2688+
This option is **deprecated**. See ``DeriveLF`` and ``DeriveCRLF`` of
2689+
``LineEnding``.
26902690

26912691
.. _DerivePointerAlignment:
26922692

@@ -3583,6 +3583,27 @@ the configuration (without a prefix: ``Auto``).
35833583

35843584

35853585

3586+
.. _LineEnding:
3587+
3588+
**LineEnding** (``LineEndingStyle``) :versionbadge:`clang-format 16` :ref:`<LineEnding>`
3589+
Line ending style (``\n`` or ``\r\n``) to use.
3590+
3591+
Possible values:
3592+
3593+
* ``LE_LF`` (in configuration: ``LF``)
3594+
Use ``\n``.
3595+
3596+
* ``LE_CRLF`` (in configuration: ``CRLF``)
3597+
Use ``\r\n``.
3598+
3599+
* ``LE_DeriveLF`` (in configuration: ``DeriveLF``)
3600+
Use ``\n`` unless the input has more lines ending in ``\r\n``.
3601+
3602+
* ``LE_DeriveCRLF`` (in configuration: ``DeriveCRLF``)
3603+
Use ``\r\n`` unless the input has more lines ending in ``\n``.
3604+
3605+
3606+
35863607
.. _MacroBlockBegin:
35873608

35883609
**MacroBlockBegin** (``String``) :versionbadge:`clang-format 3.7` :ref:`<MacroBlockBegin>`
@@ -5114,8 +5135,7 @@ the configuration (without a prefix: ``Auto``).
51145135
.. _UseCRLF:
51155136

51165137
**UseCRLF** (``Boolean``) :versionbadge:`clang-format 10` :ref:`<UseCRLF>`
5117-
Use ``\r\n`` instead of ``\n`` for line breaks.
5118-
Also used as fallback if ``DeriveLineEnding`` is true.
5138+
This option is **deprecated**. See ``LF`` and ``CRLF`` of ``LineEnding``.
51195139

51205140
.. _UseTab:
51215141

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ clang-format
912912
- Add ``BreakAfterAttributes`` option for breaking after a group of C++11
913913
attributes before a function declaration/definition name.
914914
- Add ``InsertNewlineAtEOF`` option for inserting a newline at EOF if missing.
915+
- Add ``LineEnding`` option to deprecate ``DeriveLineEnding`` and ``UseCRLF``.
915916

916917
clang-extdef-mapping
917918
--------------------

clang/include/clang/Format/Format.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,10 +2006,10 @@ struct FormatStyle {
20062006
/// \version 3.4
20072007
bool Cpp11BracedListStyle;
20082008

2009-
/// \brief Analyze the formatted file for the most used line ending (``\r\n``
2010-
/// or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived.
2009+
/// This option is **deprecated**. See ``DeriveLF`` and ``DeriveCRLF`` of
2010+
/// ``LineEnding``.
20112011
/// \version 10
2012-
bool DeriveLineEnding;
2012+
// bool DeriveLineEnding;
20132013

20142014
/// If ``true``, analyze the formatted file for the most common
20152015
/// alignment of ``&`` and ``*``.
@@ -2695,6 +2695,22 @@ struct FormatStyle {
26952695
/// \version 3.5
26962696
LanguageKind Language;
26972697

2698+
/// Line ending style.
2699+
enum LineEndingStyle : int8_t {
2700+
/// Use ``\n``.
2701+
LE_LF,
2702+
/// Use ``\r\n``.
2703+
LE_CRLF,
2704+
/// Use ``\n`` unless the input has more lines ending in ``\r\n``.
2705+
LE_DeriveLF,
2706+
/// Use ``\r\n`` unless the input has more lines ending in ``\n``.
2707+
LE_DeriveCRLF,
2708+
};
2709+
2710+
/// Line ending style (``\n`` or ``\r\n``) to use.
2711+
/// \version 16
2712+
LineEndingStyle LineEnding;
2713+
26982714
/// A regular expression matching macros that start a block.
26992715
/// \code
27002716
/// # With:
@@ -4051,10 +4067,9 @@ struct FormatStyle {
40514067
/// \version 9
40524068
std::vector<std::string> TypenameMacros;
40534069

4054-
/// \brief Use ``\r\n`` instead of ``\n`` for line breaks.
4055-
/// Also used as fallback if ``DeriveLineEnding`` is true.
4070+
/// This option is **deprecated**. See ``LF`` and ``CRLF`` of ``LineEnding``.
40564071
/// \version 10
4057-
bool UseCRLF;
4072+
// bool UseCRLF;
40584073

40594074
/// Different ways to use tab in formatting.
40604075
enum UseTabStyle : int8_t {
@@ -4144,7 +4159,6 @@ struct FormatStyle {
41444159
R.ConstructorInitializerIndentWidth &&
41454160
ContinuationIndentWidth == R.ContinuationIndentWidth &&
41464161
Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
4147-
DeriveLineEnding == R.DeriveLineEnding &&
41484162
DerivePointerAlignment == R.DerivePointerAlignment &&
41494163
DisableFormat == R.DisableFormat &&
41504164
EmptyLineAfterAccessModifier == R.EmptyLineAfterAccessModifier &&
@@ -4181,7 +4195,7 @@ struct FormatStyle {
41814195
R.KeepEmptyLinesAtTheStartOfBlocks &&
41824196
Language == R.Language &&
41834197
LambdaBodyIndentation == R.LambdaBodyIndentation &&
4184-
MacroBlockBegin == R.MacroBlockBegin &&
4198+
LineEnding == R.LineEnding && MacroBlockBegin == R.MacroBlockBegin &&
41854199
MacroBlockEnd == R.MacroBlockEnd &&
41864200
MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep &&
41874201
NamespaceIndentation == R.NamespaceIndentation &&
@@ -4248,8 +4262,7 @@ struct FormatStyle {
42484262
Standard == R.Standard &&
42494263
StatementAttributeLikeMacros == R.StatementAttributeLikeMacros &&
42504264
StatementMacros == R.StatementMacros && TabWidth == R.TabWidth &&
4251-
TypenameMacros == R.TypenameMacros && UseCRLF == R.UseCRLF &&
4252-
UseTab == R.UseTab &&
4265+
TypenameMacros == R.TypenameMacros && UseTab == R.UseTab &&
42534266
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros;
42544267
}
42554268

clang/lib/Format/DefinitionBlockSeparator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ void DefinitionBlockSeparator::separateBlocks(
6969
(Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always ? 1 : 0) + 1;
7070
WhitespaceManager Whitespaces(
7171
Env.getSourceManager(), Style,
72-
Style.DeriveLineEnding
72+
Style.LineEnding > FormatStyle::LE_CRLF
7373
? WhitespaceManager::inputUsesCRLF(
7474
Env.getSourceManager().getBufferData(Env.getFileID()),
75-
Style.UseCRLF)
76-
: Style.UseCRLF);
75+
Style.LineEnding == FormatStyle::LE_DeriveCRLF)
76+
: Style.LineEnding == FormatStyle::LE_CRLF);
7777
for (unsigned I = 0; I < Lines.size(); ++I) {
7878
const auto &CurrentLine = Lines[I];
7979
if (CurrentLine->InPPDirective)

clang/lib/Format/Format.cpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ struct ScalarEnumerationTraits<FormatStyle::LambdaBodyIndentationKind> {
403403
}
404404
};
405405

406+
template <> struct ScalarEnumerationTraits<FormatStyle::LineEndingStyle> {
407+
static void enumeration(IO &IO, FormatStyle::LineEndingStyle &Value) {
408+
IO.enumCase(Value, "LF", FormatStyle::LE_LF);
409+
IO.enumCase(Value, "CRLF", FormatStyle::LE_CRLF);
410+
IO.enumCase(Value, "DeriveLF", FormatStyle::LE_DeriveLF);
411+
IO.enumCase(Value, "DeriveCRLF", FormatStyle::LE_DeriveCRLF);
412+
}
413+
};
414+
406415
template <>
407416
struct ScalarEnumerationTraits<FormatStyle::NamespaceIndentationKind> {
408417
static void enumeration(IO &IO,
@@ -784,6 +793,9 @@ template <> struct MappingTraits<FormatStyle> {
784793
bool BreakBeforeInheritanceComma = false;
785794
bool BreakConstructorInitializersBeforeComma = false;
786795

796+
bool DeriveLineEnding = true;
797+
bool UseCRLF = false;
798+
787799
// For backward compatibility.
788800
if (!IO.outputting()) {
789801
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
@@ -794,13 +806,15 @@ template <> struct MappingTraits<FormatStyle> {
794806
BreakConstructorInitializersBeforeComma);
795807
IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
796808
OnCurrentLine);
809+
IO.mapOptional("DeriveLineEnding", DeriveLineEnding);
797810
IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
798811
IO.mapOptional("IndentFunctionDeclarationAfterType",
799812
Style.IndentWrappedFunctionNames);
800813
IO.mapOptional("IndentRequires", Style.IndentRequiresClause);
801814
IO.mapOptional("PointerBindsToType", Style.PointerAlignment);
802815
IO.mapOptional("SpaceAfterControlStatementKeyword",
803816
Style.SpaceBeforeParens);
817+
IO.mapOptional("UseCRLF", UseCRLF);
804818
}
805819

806820
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
@@ -871,7 +885,6 @@ template <> struct MappingTraits<FormatStyle> {
871885
Style.ConstructorInitializerIndentWidth);
872886
IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
873887
IO.mapOptional("Cpp11BracedListStyle", Style.Cpp11BracedListStyle);
874-
IO.mapOptional("DeriveLineEnding", Style.DeriveLineEnding);
875888
IO.mapOptional("DerivePointerAlignment", Style.DerivePointerAlignment);
876889
IO.mapOptional("DisableFormat", Style.DisableFormat);
877890
IO.mapOptional("EmptyLineAfterAccessModifier",
@@ -908,6 +921,7 @@ template <> struct MappingTraits<FormatStyle> {
908921
IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks",
909922
Style.KeepEmptyLinesAtTheStartOfBlocks);
910923
IO.mapOptional("LambdaBodyIndentation", Style.LambdaBodyIndentation);
924+
IO.mapOptional("LineEnding", Style.LineEnding);
911925
IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin);
912926
IO.mapOptional("MacroBlockEnd", Style.MacroBlockEnd);
913927
IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep);
@@ -1003,7 +1017,6 @@ template <> struct MappingTraits<FormatStyle> {
10031017
IO.mapOptional("StatementMacros", Style.StatementMacros);
10041018
IO.mapOptional("TabWidth", Style.TabWidth);
10051019
IO.mapOptional("TypenameMacros", Style.TypenameMacros);
1006-
IO.mapOptional("UseCRLF", Style.UseCRLF);
10071020
IO.mapOptional("UseTab", Style.UseTab);
10081021
IO.mapOptional("WhitespaceSensitiveMacros",
10091022
Style.WhitespaceSensitiveMacros);
@@ -1052,6 +1065,13 @@ template <> struct MappingTraits<FormatStyle> {
10521065
else if (!OnNextLine)
10531066
Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
10541067
}
1068+
1069+
if (Style.LineEnding == FormatStyle::LE_DeriveLF) {
1070+
if (!DeriveLineEnding)
1071+
Style.LineEnding = UseCRLF ? FormatStyle::LE_CRLF : FormatStyle::LE_LF;
1072+
else if (UseCRLF)
1073+
Style.LineEnding = FormatStyle::LE_DeriveCRLF;
1074+
}
10551075
}
10561076
};
10571077

@@ -1329,7 +1349,6 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
13291349
LLVMStyle.ConstructorInitializerIndentWidth = 4;
13301350
LLVMStyle.ContinuationIndentWidth = 4;
13311351
LLVMStyle.Cpp11BracedListStyle = true;
1332-
LLVMStyle.DeriveLineEnding = true;
13331352
LLVMStyle.DerivePointerAlignment = false;
13341353
LLVMStyle.DisableFormat = false;
13351354
LLVMStyle.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never;
@@ -1363,6 +1382,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
13631382
LLVMStyle.JavaScriptWrapImports = true;
13641383
LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true;
13651384
LLVMStyle.LambdaBodyIndentation = FormatStyle::LBI_Signature;
1385+
LLVMStyle.LineEnding = FormatStyle::LE_DeriveLF;
13661386
LLVMStyle.MaxEmptyLinesToKeep = 1;
13671387
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
13681388
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
@@ -1416,7 +1436,6 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14161436
LLVMStyle.StatementMacros.push_back("Q_UNUSED");
14171437
LLVMStyle.StatementMacros.push_back("QT_REQUIRE_VERSION");
14181438
LLVMStyle.TabWidth = 8;
1419-
LLVMStyle.UseCRLF = false;
14201439
LLVMStyle.UseTab = FormatStyle::UT_Never;
14211440
LLVMStyle.WhitespaceSensitiveMacros.push_back("BOOST_PP_STRINGIZE");
14221441
LLVMStyle.WhitespaceSensitiveMacros.push_back("CF_SWIFT_NAME");
@@ -2190,11 +2209,11 @@ class Formatter : public TokenAnalyzer {
21902209

21912210
WhitespaceManager Whitespaces(
21922211
Env.getSourceManager(), Style,
2193-
Style.DeriveLineEnding
2212+
Style.LineEnding > FormatStyle::LE_CRLF
21942213
? WhitespaceManager::inputUsesCRLF(
21952214
Env.getSourceManager().getBufferData(Env.getFileID()),
2196-
Style.UseCRLF)
2197-
: Style.UseCRLF);
2215+
Style.LineEnding == FormatStyle::LE_DeriveCRLF)
2216+
: Style.LineEnding == FormatStyle::LE_CRLF);
21982217
ContinuationIndenter Indenter(Style, Tokens.getKeywords(),
21992218
Env.getSourceManager(), Whitespaces, Encoding,
22002219
BinPackInconclusiveFunctions);

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
155155
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
156156
CHECK_PARSE_BOOL(BreakStringLiterals);
157157
CHECK_PARSE_BOOL(CompactNamespaces);
158-
CHECK_PARSE_BOOL(DeriveLineEnding);
159158
CHECK_PARSE_BOOL(DerivePointerAlignment);
160159
CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
161160
CHECK_PARSE_BOOL(DisableFormat);
@@ -193,7 +192,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
193192
CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon);
194193
CHECK_PARSE_BOOL(SpaceBeforeRangeBasedForLoopColon);
195194
CHECK_PARSE_BOOL(SpaceBeforeSquareBrackets);
196-
CHECK_PARSE_BOOL(UseCRLF);
197195

198196
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterCaseLabel);
199197
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
@@ -886,6 +884,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {
886884
FormatStyle::ABS_Leave);
887885
CHECK_PARSE("BreakAfterAttributes: Never", BreakAfterAttributes,
888886
FormatStyle::ABS_Never);
887+
888+
const auto DefaultLineEnding = FormatStyle::LE_DeriveLF;
889+
CHECK_PARSE("LineEnding: LF", LineEnding, FormatStyle::LE_LF);
890+
CHECK_PARSE("LineEnding: CRLF", LineEnding, FormatStyle::LE_CRLF);
891+
CHECK_PARSE("LineEnding: DeriveCRLF", LineEnding, FormatStyle::LE_DeriveCRLF);
892+
CHECK_PARSE("LineEnding: DeriveLF", LineEnding, DefaultLineEnding);
893+
// For backward compatibility:
894+
CHECK_PARSE("DeriveLineEnding: false", LineEnding, FormatStyle::LE_LF);
895+
Style.LineEnding = DefaultLineEnding;
896+
CHECK_PARSE("DeriveLineEnding: false\n"
897+
"UseCRLF: true",
898+
LineEnding, FormatStyle::LE_CRLF);
899+
Style.LineEnding = DefaultLineEnding;
900+
CHECK_PARSE("UseCRLF: true", LineEnding, FormatStyle::LE_DeriveCRLF);
889901
}
890902

891903
TEST(ConfigParseTest, ParsesConfigurationWithLanguages) {

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22218,8 +22218,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2221822218

2221922219
FormatStyle style = getLLVMStyle();
2222022220

22221-
style.DeriveLineEnding = true;
22222-
style.UseCRLF = false;
22221+
EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF);
2222322222
EXPECT_EQ("union FooBarBazQux {\n"
2222422223
" int foo;\n"
2222522224
" int bar;\n"
@@ -22231,7 +22230,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2223122230
" int baz;\n"
2223222231
"};",
2223322232
style));
22234-
style.UseCRLF = true;
22233+
style.LineEnding = FormatStyle::LE_DeriveCRLF;
2223522234
EXPECT_EQ("union FooBarBazQux {\r\n"
2223622235
" int foo;\r\n"
2223722236
" int bar;\r\n"
@@ -22244,8 +22243,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2224422243
"};",
2224522244
style));
2224622245

22247-
style.DeriveLineEnding = false;
22248-
style.UseCRLF = false;
22246+
style.LineEnding = FormatStyle::LE_LF;
2224922247
EXPECT_EQ("union FooBarBazQux {\n"
2225022248
" int foo;\n"
2225122249
" int bar;\n"
@@ -22259,7 +22257,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2225922257
" int qux;\r\n"
2226022258
"};",
2226122259
style));
22262-
style.UseCRLF = true;
22260+
style.LineEnding = FormatStyle::LE_CRLF;
2226322261
EXPECT_EQ("union FooBarBazQux {\r\n"
2226422262
" int foo;\r\n"
2226522263
" int bar;\r\n"
@@ -22274,8 +22272,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2227422272
"};",
2227522273
style));
2227622274

22277-
style.DeriveLineEnding = true;
22278-
style.UseCRLF = false;
22275+
style.LineEnding = FormatStyle::LE_DeriveLF;
2227922276
EXPECT_EQ("union FooBarBazQux {\r\n"
2228022277
" int foo;\r\n"
2228122278
" int bar;\r\n"
@@ -22289,7 +22286,7 @@ TEST_F(FormatTest, SupportsCRLF) {
2228922286
" int qux;\r\n"
2229022287
"};",
2229122288
style));
22292-
style.UseCRLF = true;
22289+
style.LineEnding = FormatStyle::LE_DeriveCRLF;
2229322290
EXPECT_EQ("union FooBarBazQux {\n"
2229422291
" int foo;\n"
2229522292
" int bar;\n"

0 commit comments

Comments
 (0)