Fixes bug in string literal alignment.
We now indent the following correctly:
1. some + "literal" /* comment */
"literal";
2. breaking string literals after which we have another string literal.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175628 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 625f931..aca5d36 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -284,6 +284,7 @@
State.VariablePos = 0;
State.LineContainsContinuedForLoopSection = false;
State.ParenLevel = 0;
+ State.StartOfStringLiteral = 0;
State.StartOfLineLevel = State.ParenLevel;
DEBUG({
@@ -421,6 +422,10 @@
/// \brief The \c ParenLevel at the start of this line.
unsigned StartOfLineLevel;
+ /// \brief The start column of the string literal, if we're in a string
+ /// literal sequence, 0 otherwise.
+ unsigned StartOfStringLiteral;
+
/// \brief A stack keeping track of properties applying to parenthesis
/// levels.
std::vector<ParenState> Stack;
@@ -440,6 +445,8 @@
return ParenLevel < Other.ParenLevel;
if (StartOfLineLevel != Other.StartOfLineLevel)
return StartOfLineLevel < Other.StartOfLineLevel;
+ if (StartOfStringLiteral != Other.StartOfStringLiteral)
+ return StartOfStringLiteral < Other.StartOfStringLiteral;
return Stack < Other.Stack;
}
};
@@ -472,8 +479,8 @@
if (Current.is(tok::r_brace)) {
State.Column = Line.Level * 2;
} else if (Current.is(tok::string_literal) &&
- Previous.is(tok::string_literal)) {
- State.Column = State.Column - Previous.FormatTok.TokenLength;
+ State.StartOfStringLiteral != 0) {
+ State.Column = State.StartOfStringLiteral;
State.Stack.back().BreakBeforeParameter = true;
} else if (Current.is(tok::lessless) &&
State.Stack.back().FirstLessLess != 0) {
@@ -685,6 +692,12 @@
State.Stack.pop_back();
}
+ if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
+ State.StartOfStringLiteral = State.Column;
+ } else if (Current.isNot(tok::comment)) {
+ State.StartOfStringLiteral = 0;
+ }
+
State.Column += Current.FormatTok.TokenLength;
if (State.NextToken->Children.empty())
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index dba6183..ab4825b 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -1439,6 +1439,19 @@
verifyFormat("someFunction(\"Always break between multi-line\"\n"
" \" string literals\",\n"
" and, other, parameters);");
+ EXPECT_EQ("fun + \"1243\" /* comment */\n"
+ " \"5678\";",
+ format("fun + \"1243\" /* comment */\n"
+ " \"5678\";",
+ getLLVMStyleWithColumns(28)));
+ EXPECT_EQ(
+ "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
+ " \"aaaaaaaaaaaaaaaaaaaaa\"\n"
+ " \"aaaaaaaaaaaaaaaa\";",
+ format("aaaaaa ="
+ "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
+ "aaaaaaaaaaaaaaaaaaaaa\" "
+ "\"aaaaaaaaaaaaaaaa\";"));
}
TEST_F(FormatTest, AlignsPipes) {