float b = 23;
std::string ccc = 23;
-**AlignEscapedNewlinesLeft** (``bool``)
- If ``true``, aligns escaped newlines as far left as possible.
- Otherwise puts them into the right-most column.
+**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``)
+ Options for aligning backslashes in escaped newlines.
- .. code-block:: c++
+ Possible values:
+
+ * ``ENAS_DontAlign`` (in configuration: ``DontAlign``)
+ Don't align escaped newlines.
+
+ .. code-block:: c++
+
+ #define A \
+ int aaaa; \
+ int b; \
+ int dddddddddd;
+
+ * ``ENAS_Left`` (in configuration: ``Left``)
+ Align escaped newlines as far left as possible.
+
+ .. code-block:: c++
+
+ true:
+ #define A \
+ int aaaa; \
+ int b; \
+ int dddddddddd;
+
+ false:
+
+ * ``ENAS_Right`` (in configuration: ``Right``)
+ Align escaped newlines in the right-most column.
+
+ .. code-block:: c++
+
+ #define A \
+ int aaaa; \
+ int b; \
+ int dddddddddd;
- true:
- #define A \
- int aaaa; \
- int b; \
- int dddddddddd;
- false:
- #define A \
- int aaaa; \
- int b; \
- int dddddddddd;
**AlignOperands** (``bool``)
If ``true``, horizontally align operands of binary and ternary
Use C++03-compatible syntax.
* ``LS_Cpp11`` (in configuration: ``Cpp11``)
- Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of
+ Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of
``A<A<int> >``).
* ``LS_Auto`` (in configuration: ``Auto``)
/// \endcode
bool AlignConsecutiveDeclarations;
- /// \brief If ``true``, aligns escaped newlines as far left as possible.
- /// Otherwise puts them into the right-most column.
- /// \code
- /// true:
- /// #define A \
- /// int aaaa; \
- /// int b; \
- /// int dddddddddd;
- ///
- /// false:
- /// #define A \
- /// int aaaa; \
- /// int b; \
- /// int dddddddddd;
- /// \endcode
- bool AlignEscapedNewlinesLeft;
+ /// \brief Different styles for aligning escaped newlines.
+ enum EscapedNewlineAlignmentStyle {
+ /// \brief Don't align escaped newlines.
+ /// \code
+ /// #define A \
+ /// int aaaa; \
+ /// int b; \
+ /// int dddddddddd;
+ /// \endcode
+ ENAS_DontAlign,
+ /// \brief Align escaped newlines as far left as possible.
+ /// \code
+ /// true:
+ /// #define A \
+ /// int aaaa; \
+ /// int b; \
+ /// int dddddddddd;
+ ///
+ /// false:
+ /// \endcode
+ ENAS_Left,
+ /// \brief Align escaped newlines in the right-most column.
+ /// \code
+ /// #define A \
+ /// int aaaa; \
+ /// int b; \
+ /// int dddddddddd;
+ /// \endcode
+ ENAS_Right,
+ };
+
+ /// \brief Options for aligning backslashes in escaped newlines.
+ EscapedNewlineAlignmentStyle AlignEscapedNewlines;
/// \brief If ``true``, horizontally align operands of binary and ternary
/// expressions.
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
AlignConsecutiveAssignments == R.AlignConsecutiveAssignments &&
AlignConsecutiveDeclarations == R.AlignConsecutiveDeclarations &&
- AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
+ AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
AllowAllParametersOfDeclarationOnNextLine ==
}
};
+template <> struct ScalarEnumerationTraits<FormatStyle::EscapedNewlineAlignmentStyle> {
+ static void enumeration(IO &IO, FormatStyle::EscapedNewlineAlignmentStyle &Value) {
+ IO.enumCase(Value, "DontAlign", FormatStyle::ENAS_DontAlign);
+ IO.enumCase(Value, "Left", FormatStyle::ENAS_Left);
+ IO.enumCase(Value, "Right", FormatStyle::ENAS_Right);
+
+ // For backward compatibility.
+ IO.enumCase(Value, "true", FormatStyle::ENAS_Left);
+ IO.enumCase(Value, "false", FormatStyle::ENAS_Right);
+ }
+};
+
template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
// For backward compatibility.
if (!IO.outputting()) {
+ IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlines);
IO.mapOptional("DerivePointerBinding", Style.DerivePointerAlignment);
IO.mapOptional("IndentFunctionDeclarationAfterType",
Style.IndentWrappedFunctionNames);
Style.AlignConsecutiveAssignments);
IO.mapOptional("AlignConsecutiveDeclarations",
Style.AlignConsecutiveDeclarations);
- IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
+ IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
FormatStyle LLVMStyle;
LLVMStyle.Language = FormatStyle::LK_Cpp;
LLVMStyle.AccessModifierOffset = -2;
- LLVMStyle.AlignEscapedNewlinesLeft = false;
+ LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align;
LLVMStyle.AlignOperands = true;
LLVMStyle.AlignTrailingComments = true;
GoogleStyle.Language = Language;
GoogleStyle.AccessModifierOffset = -1;
- GoogleStyle.AlignEscapedNewlinesLeft = true;
+ GoogleStyle.AlignEscapedNewlines = FormatStyle::ENAS_Left;
GoogleStyle.AllowShortIfStatementsOnASingleLine = true;
GoogleStyle.AllowShortLoopsOnASingleLine = true;
GoogleStyle.AlwaysBreakBeforeMultilineStrings = true;
}
void WhitespaceManager::alignEscapedNewlines() {
- unsigned MaxEndOfLine =
- Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
+ if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign)
+ return;
+
+ bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left;
+ unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
unsigned StartOfMacro = 0;
for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
Change &C = Changes[i];
MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
} else {
alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
- MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
+ MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit;
StartOfMacro = i;
}
}
unsigned EscapedNewlineColumn) {
if (Newlines > 0) {
unsigned Offset =
- std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
+ std::min<int>(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn);
for (unsigned i = 0; i < Newlines; ++i) {
Text.append(EscapedNewlineColumn - Offset - 1, ' ');
Text.append(UseCRLF ? "\\\r\n" : "\\\n");
verifyFormat("if (a)\n if (b) {\n f();\n }\ng();");
FormatStyle AllowsMergedIf = getLLVMStyle();
- AllowsMergedIf.AlignEscapedNewlinesLeft = true;
+ AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
verifyFormat("if (a)\n"
" // comment\n"
EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
- AlignLeft.AlignEscapedNewlinesLeft = true;
+ AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
EXPECT_EQ("#define A \\\n"
" \"some \" \\\n"
" \"text \" \\\n"
FormatStyle Tab = getLLVMStyleWithColumns(42);
Tab.IndentWidth = 8;
Tab.UseTab = FormatStyle::UT_Always;
- Tab.AlignEscapedNewlinesLeft = true;
+ Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
EXPECT_EQ("if (aaaaaaaa && // q\n"
" bb)\t\t// w\n"
"int oneTwoThree = 123;\n"
"int oneTwo = 12;",
Alignment));
- Alignment.AlignEscapedNewlinesLeft = true;
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+ verifyFormat("#define A \\\n"
+ " int aaaa = 12; \\\n"
+ " int b = 23; \\\n"
+ " int ccc = 234; \\\n"
+ " int dddddddddd = 2345;",
+ Alignment);
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
verifyFormat("#define A \\\n"
" int aaaa = 12; \\\n"
" int b = 23; \\\n"
" int ccc = 234; \\\n"
" int dddddddddd = 2345;",
Alignment);
- Alignment.AlignEscapedNewlinesLeft = false;
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
verifyFormat("#define A "
" \\\n"
" int aaaa = 12; "
"}",
Alignment));
Alignment.AlignConsecutiveAssignments = false;
- Alignment.AlignEscapedNewlinesLeft = true;
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
+ verifyFormat("#define A \\\n"
+ " int aaaa = 12; \\\n"
+ " float b = 23; \\\n"
+ " const int ccc = 234; \\\n"
+ " unsigned dddddddddd = 2345;",
+ Alignment);
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
verifyFormat("#define A \\\n"
" int aaaa = 12; \\\n"
" float b = 23; \\\n"
" const int ccc = 234; \\\n"
" unsigned dddddddddd = 2345;",
Alignment);
- Alignment.AlignEscapedNewlinesLeft = false;
+ Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
Alignment.ColumnLimit = 30;
verifyFormat("#define A \\\n"
" int aaaa = 12; \\\n"
TEST_F(FormatTest, ParsesConfigurationBools) {
FormatStyle Style = {};
Style.Language = FormatStyle::LK_Cpp;
- CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
CHECK_PARSE_BOOL(AlignOperands);
CHECK_PARSE_BOOL(AlignTrailingComments);
CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
FormatStyle::BAS_Align);
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
+ CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
+ FormatStyle::ENAS_DontAlign);
+ CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
+ FormatStyle::ENAS_Left);
+ CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
+ FormatStyle::ENAS_Right);
+ // For backward compatibility:
+ CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
+ FormatStyle::ENAS_Left);
+ CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
+ FormatStyle::ENAS_Right);
+
Style.UseTab = FormatStyle::UT_ForIndentation;
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
}
TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
- Style.AlignEscapedNewlinesLeft = true;
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
EXPECT_EQ("int i;\n"
"#define A \\\n"
" int i; \\\n"
TEST_F(FormatTestSelective, UnderstandsTabs) {
Style.IndentWidth = 8;
Style.UseTab = FormatStyle::UT_Always;
- Style.AlignEscapedNewlinesLeft = true;
+ Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
EXPECT_EQ("void f() {\n"
"\tf();\n"
"\tg();\n"