From bc30c71d20f05bc39b5dd73f06ee4dede9c55710 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 1 Mar 2013 13:29:19 +0000 Subject: [PATCH] Implements breaking string literals at slashes. We now break at a slash if we do not find a space to break on. Also fixes a bug where we would go over the limit when breaking the second line. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@176350 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 17 +++++++++++------ unittests/Format/FormatTest.cpp | 10 ++++++++-- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index d2b12f6814..01813ef6c8 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -720,10 +720,10 @@ private: while (StartColumn + TailLength > getColumnLimit()) { StringRef Text = StringRef(Current.FormatTok.Tok.getLiteralData() + TailOffset, TailLength); - if (StartColumn + 1 > getColumnLimit()) + if (StartColumn + OffsetFromStart + 1 > getColumnLimit()) break; - StringRef::size_type SplitPoint = - getSplitPoint(Text, getColumnLimit() - StartColumn - 1); + StringRef::size_type SplitPoint = getSplitPoint( + Text, getColumnLimit() - StartColumn - OffsetFromStart - 1); if (SplitPoint == StringRef::npos) break; assert(SplitPoint != 0); @@ -751,10 +751,15 @@ private: StringRef::size_type getSplitPoint(StringRef Text, StringRef::size_type Offset) { StringRef::size_type SpaceOffset = Text.rfind(' ', Offset); - if (SpaceOffset == StringRef::npos && Offset > 0) { + if (SpaceOffset != StringRef::npos) + return SpaceOffset; + StringRef::size_type SlashOffset = Text.rfind('/', Offset); + if (SlashOffset != StringRef::npos) + return SlashOffset; + if (Offset > 1) + // Do not split at 0. return Offset - 1; - } - return SpaceOffset; + return StringRef::npos; } unsigned getColumnLimit() { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 8cc81684cb..8b1f69a76e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3045,9 +3045,15 @@ TEST_F(FormatTest, BreakStringLiterals) { EXPECT_EQ( "\"splitmea\"\n" - "\"trandompo\"\n" - "\"int\"", + "\"trandomp\"\n" + "\"oint\"", format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); + + EXPECT_EQ( + "\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); } } // end namespace tooling