r176349 - Implement fallback split point for string literals.
Manuel Klimek
klimek at google.com
Fri Mar 1 05:14:09 PST 2013
Author: klimek
Date: Fri Mar 1 07:14:08 2013
New Revision: 176349
URL: http://llvm.org/viewvc/llvm-project?rev=176349&view=rev
Log:
Implement fallback split point for string literals.
If we don't find a natural split point (currently space) in a string
literal protruding over the line, we just split at the last possible
point.
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=176349&r1=176348&r2=176349&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar 1 07:14:08 2013
@@ -720,6 +720,8 @@ private:
while (StartColumn + TailLength > getColumnLimit()) {
StringRef Text = StringRef(Current.FormatTok.Tok.getLiteralData() +
TailOffset, TailLength);
+ if (StartColumn + 1 > getColumnLimit())
+ break;
StringRef::size_type SplitPoint =
getSplitPoint(Text, getColumnLimit() - StartColumn - 1);
if (SplitPoint == StringRef::npos)
@@ -748,8 +750,11 @@ private:
StringRef::size_type
getSplitPoint(StringRef Text, StringRef::size_type Offset) {
- // FIXME: Implement more sophisticated splitting mechanism, and a fallback.
- return Text.rfind(' ', Offset);
+ StringRef::size_type SpaceOffset = Text.rfind(' ', Offset);
+ if (SpaceOffset == StringRef::npos && Offset > 0) {
+ return Offset - 1;
+ }
+ return SpaceOffset;
}
unsigned getColumnLimit() {
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176349&r1=176348&r2=176349&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 1 07:14:08 2013
@@ -3003,7 +3003,8 @@ TEST_F(FormatTest, BreakStringLiterals)
EXPECT_EQ("\"some \"\n"
"\"text\"",
format("\"some text\"", getLLVMStyleWithColumns(7)));
- EXPECT_EQ("\"some text\"",
+ EXPECT_EQ("\"some\"\n"
+ "\" text\"",
format("\"some text\"", getLLVMStyleWithColumns(6)));
EXPECT_EQ("variable =\n"
@@ -3041,6 +3042,12 @@ TEST_F(FormatTest, BreakStringLiterals)
"aaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaa,\n"
" aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
+
+ EXPECT_EQ(
+ "\"splitmea\"\n"
+ "\"trandompo\"\n"
+ "\"int\"",
+ format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
}
} // end namespace tooling
More information about the cfe-commits
mailing list