r186154 - clang-format: Fix string literal breaking.
Daniel Jasper
djasper at google.com
Fri Jul 12 04:37:05 PDT 2013
Author: djasper
Date: Fri Jul 12 06:37:05 2013
New Revision: 186154
URL: http://llvm.org/viewvc/llvm-project?rev=186154&view=rev
Log:
clang-format: Fix string literal breaking.
Before this patch, it did not cooperate with
Style::AlwaysBreakBeforeMultilineStrings. Thus, it would turn
aaaaaaaaaaaa(aaaaaaaaaaaaa, "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa");
into:
aaaaaaaaaaaa(aaaaaaaaaaaaa, "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
"aaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa");
and only a second format step would lead to the desired (with that
option):
aaaaaaaaaaaa(aaaaaaaaaaaaa,
"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
"aaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa");
This could even lead to clang-format breaking the string at a different
character and thus leading to a completely different end result.
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=186154&r1=186153&r2=186154&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Jul 12 06:37:05 2013
@@ -297,7 +297,7 @@ public:
State.IgnoreStackForComparison = false;
// The first token has already been indented and thus consumed.
- moveStateToNextToken(State, /*DryRun=*/false);
+ moveStateToNextToken(State, /*DryRun=*/false, /*Newline=*/false);
// If everything fits on a single line, just put it there.
unsigned ColumnLimit = Style.ColumnLimit;
@@ -729,12 +729,12 @@ private:
}
}
- return moveStateToNextToken(State, DryRun) + ExtraPenalty;
+ return moveStateToNextToken(State, DryRun, Newline) + ExtraPenalty;
}
/// \brief Mark the next token as consumed in \p State and modify its stacks
/// accordingly.
- unsigned moveStateToNextToken(LineState &State, bool DryRun) {
+ unsigned moveStateToNextToken(LineState &State, bool DryRun, bool Newline) {
const FormatToken &Current = *State.NextToken;
assert(State.Stack.size());
@@ -875,6 +875,10 @@ private:
State.NextToken = State.NextToken->Next;
+ if (!Newline && Style.AlwaysBreakBeforeMultilineStrings &&
+ Current.is(tok::string_literal))
+ return 0;
+
return breakProtrudingToken(Current, State, DryRun);
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=186154&r1=186153&r2=186154&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Jul 12 06:37:05 2013
@@ -4958,6 +4958,16 @@ TEST_F(FormatTest, BreakStringLiterals)
"rs\"",
getLLVMStyleWithColumns(20)));
+ // Verify that splitting the strings understands
+ // Style::AlwaysBreakBeforeMultilineStrings.
+ EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
+ " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
+ " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
+ format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
+ "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
+ "aaaaaaaaaaaaaaaaaaaaaa\");",
+ getGoogleStyle()));
+
FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
AlignLeft.AlignEscapedNewlinesLeft = true;
EXPECT_EQ(
More information about the cfe-commits
mailing list