[PATCH] Don't break string literals inside the _T() macro. A workaround for http://llvm.org/PR17122

Alexander Kornienko alexfh at google.com
Tue Sep 10 07:05:46 PDT 2013


Hi djasper,

http://llvm-reviews.chandlerc.com/D1640

Files:
  lib/Format/ContinuationIndenter.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -651,6 +651,17 @@
     if (Current.IsUnterminatedLiteral)
       return 0;
 
+    // FIXME: Split the string and add _T() around each segment to work around a
+    // Visual C++ bug, which doesn't handle L"xxx" "yyy" as per [lex.string]p13:
+    // "If one string literal has no encoding-prefix, it is treated as a string
+    // literal of the same encoding-prefix as the other operand."
+    // More context here: http://llvm.org/PR17122
+    if (Current.Previous && Current.Previous->is(tok::l_paren) &&
+        Current.Previous->Previous &&
+        Current.Previous->Previous->TokenText == "_T" && Current.Next &&
+        Current.Next->is(tok::r_paren))
+      return 0;
+
     Token.reset(new BreakableStringLiteral(
         Current, StartColumn, State.Line->InPPDirective, Encoding, Style));
   } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5450,6 +5450,20 @@
       format("#define A \"some text other\";", AlignLeft));
 }
 
+TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
+  // FIXME: Implement breaking strings withing _T() macro like this:
+  //EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
+  //          "_T(\"aaaaaaaaaaaaa\")\n"
+  //          "_T(\"aaaaaaaaaaaaa\")\n"
+  //          "_T(\"a\")",
+  //          format("_T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")",
+  //                 getLLVMStyleWithColumns(20)));
+
+  EXPECT_EQ("_T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")",
+            format("_T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")",
+                   getLLVMStyleWithColumns(20)));
+}
+
 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
   EXPECT_EQ(
       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1640.1.patch
Type: text/x-patch
Size: 2093 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130910/d28eea61/attachment.bin>


More information about the cfe-commits mailing list