r175628 - Fixes bug in string literal alignment.
Manuel Klimek
klimek at google.com
Wed Feb 20 07:25:48 PST 2013
Author: klimek
Date: Wed Feb 20 09:25:48 2013
New Revision: 175628
URL: http://llvm.org/viewvc/llvm-project?rev=175628&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=175628&r1=175627&r2=175628&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Feb 20 09:25:48 2013
@@ -284,6 +284,7 @@ public:
State.VariablePos = 0;
State.LineContainsContinuedForLoopSection = false;
State.ParenLevel = 0;
+ State.StartOfStringLiteral = 0;
State.StartOfLineLevel = State.ParenLevel;
DEBUG({
@@ -421,6 +422,10 @@ private:
/// \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 @@ private:
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 @@ private:
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 @@ private:
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())
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=175628&r1=175627&r2=175628&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb 20 09:25:48 2013
@@ -1439,6 +1439,19 @@ TEST_F(FormatTest, AlignsStringLiterals)
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) {
More information about the cfe-commits
mailing list