]> granicus.if.org Git - clang/commitdiff
clang-format: Fix assertion on incomplete string literals.
authorDaniel Jasper <djasper@google.com>
Sun, 29 Sep 2013 12:02:57 +0000 (12:02 +0000)
committerDaniel Jasper <djasper@google.com>
Sun, 29 Sep 2013 12:02:57 +0000 (12:02 +0000)
Before, this could would lead to an assert:
  llvm::errs() << "
               << a;

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@191639 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 992104fe4c235431acf20819174f26cf986131fd..6772c894dc0278b7f62867088f22ca9cc6ba6a32 100644 (file)
@@ -1161,7 +1161,11 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
   if (Right.is(tok::lessless)) {
     if (Left.is(tok::string_literal)) {
       StringRef Content = Left.TokenText;
-      Content = Content.drop_back(1).drop_front(1).trim();
+      if (Content.startswith("\""))
+        Content = Content.drop_front(1);
+      if (Content.endswith("\""))
+        Content = Content.drop_back(1);
+      Content = Content.trim();
       if (Content.size() > 1 &&
           (Content.back() == ':' || Content.back() == '='))
         return 25;
index 08b5643ac0870c396c769e222e094a24f255be9d..833a87c4fd03055187938f70098ff77f82c43d73 100644 (file)
@@ -3398,6 +3398,11 @@ TEST_F(FormatTest, AlignsPipes) {
   verifyFormat(
       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
+
+  // Incomplete string literal.
+  EXPECT_EQ("llvm::errs() << \"\n"
+            "             << a;",
+            format("llvm::errs() << \"\n<<a;"));
 }
 
 TEST_F(FormatTest, UnderstandsEquals) {