r197785 - clang-format: Better support for multi-line wide string literals.

Daniel Jasper djasper at google.com
Thu Dec 19 22:22:01 PST 2013


Author: djasper
Date: Fri Dec 20 00:22:01 2013
New Revision: 197785

URL: http://llvm.org/viewvc/llvm-project?rev=197785&view=rev
Log:
clang-format: Better support for multi-line wide string literals.

Before:
  SomeFunction(L"A" L"B");

After:
  SomeFunction(L"A"
               L"B");

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=197785&r1=197784&r2=197785&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Fri Dec 20 00:22:01 2013
@@ -352,8 +352,7 @@ unsigned ContinuationIndenter::addTokenO
       State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
     else
       State.Column = State.FirstIndent;
-  } else if (Current.is(tok::string_literal) &&
-             State.StartOfStringLiteral != 0) {
+  } else if (Current.isStringLiteral() && State.StartOfStringLiteral != 0) {
     State.Column = State.StartOfStringLiteral;
     State.Stack.back().BreakBeforeParameter = true;
   } else if (Current.is(tok::lessless) &&
@@ -664,10 +663,10 @@ unsigned ContinuationIndenter::moveState
     }
   }
 
-  if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
+  if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
     State.StartOfStringLiteral = State.Column;
-  } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
-                              tok::string_literal)) {
+  } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
+             !Current.isStringLiteral()) {
     State.StartOfStringLiteral = 0;
   }
 
@@ -737,19 +736,14 @@ unsigned ContinuationIndenter::breakProt
   if (Current.Type == TT_ImplicitStringLiteral)
     return 0;
 
-  if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
-                       tok::utf8_string_literal, tok::utf16_string_literal,
-                       tok::utf32_string_literal, tok::comment))
+  if (!Current.isStringLiteral() && !Current.is(tok::comment))
     return 0;
 
   llvm::OwningPtr<BreakableToken> Token;
   unsigned StartColumn = State.Column - Current.ColumnWidth;
   unsigned ColumnLimit = getColumnLimit(State);
 
-  if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
-                      tok::utf8_string_literal, tok::utf16_string_literal,
-                      tok::utf32_string_literal) &&
-      Current.Type != TT_ImplicitStringLiteral) {
+  if (Current.isStringLiteral()) {
     // Don't break string literals inside preprocessor directives (except for
     // #define directives, as their contents are stored in separate lines and
     // are not affected by this check).
@@ -858,8 +852,8 @@ unsigned ContinuationIndenter::breakProt
         State.Stack[i].BreakBeforeParameter = true;
     }
 
-    Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
-                                               : Style.PenaltyBreakComment;
+    Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString
+                                         : Style.PenaltyBreakComment;
 
     State.Stack.back().LastSpace = StartColumn;
   }
@@ -873,7 +867,7 @@ unsigned ContinuationIndenter::getColumn
 
 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
   const FormatToken &Current = *State.NextToken;
-  if (!Current.is(tok::string_literal))
+  if (!Current.isStringLiteral())
     return false;
   // We never consider raw string literals "multiline" for the purpose of
   // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
@@ -883,7 +877,7 @@ bool ContinuationIndenter::nextIsMultili
   if (Current.IsMultiline)
     return true;
   if (Current.getNextNonComment() &&
-      Current.getNextNonComment()->is(tok::string_literal))
+      Current.getNextNonComment()->isStringLiteral())
     return true; // Implicit concatenation.
   if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
       Style.ColumnLimit)

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=197785&r1=197784&r2=197785&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Fri Dec 20 00:22:01 2013
@@ -266,6 +266,7 @@ struct FormatToken {
   }
 
   bool isNot(tok::TokenKind Kind) const { return Tok.isNot(Kind); }
+  bool isStringLiteral() const { return tok::isStringLiteral(Tok.getKind()); }
 
   bool isObjCAtKeyword(tok::ObjCKeywordKind Kind) const {
     return Tok.isObjCAtKeyword(Kind);

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=197785&r1=197784&r2=197785&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Dec 20 00:22:01 2013
@@ -1405,8 +1405,7 @@ bool TokenAnnotator::mustBreakBefore(con
     return Right.Previous->BlockKind != BK_BracedInit &&
            Right.NewlinesBefore > 0;
   } else if (Right.Previous->isTrailingComment() ||
-             (Right.is(tok::string_literal) &&
-              Right.Previous->is(tok::string_literal))) {
+             (Right.isStringLiteral() && Right.Previous->isStringLiteral())) {
     return true;
   } else if (Right.Previous->IsUnterminatedLiteral) {
     return true;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=197785&r1=197784&r2=197785&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Dec 20 00:22:01 2013
@@ -3680,6 +3680,13 @@ TEST_F(FormatTest, AlignsStringLiterals)
   verifyFormat("#define A \"def\"\n"
                "f(\"abc\" A \"ghi\"\n"
                "  \"jkl\");");
+
+  verifyFormat("f(L\"a\"\n"
+               "  L\"b\")");
+  verifyFormat("#define A(X)            \\\n"
+               "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
+               "  L\"ccccc\"",
+               getLLVMStyleWithColumns(25));
 }
 
 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
@@ -3708,6 +3715,10 @@ TEST_F(FormatTest, AlwaysBreakBeforeMult
                "     \"bbbb\"\n"
                "     \"cccc\");",
                Break);
+  verifyFormat("aaaa(qqq,\n"
+               "     L\"bbbb\"\n"
+               "     L\"cccc\");",
+               Break);
 
   // Don't break if there is no column gain.
   verifyFormat("f(\"aaaa\"\n"





More information about the cfe-commits mailing list