]> granicus.if.org Git - clang/commitdiff
clang-format: (JavaScript) Don't crash on empty string literals.
authorDaniel Jasper <djasper@google.com>
Fri, 31 Jan 2014 12:49:42 +0000 (12:49 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 31 Jan 2014 12:49:42 +0000 (12:49 +0000)
Before, this would lead to a crash:
  f('', true);

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

lib/Format/Format.cpp
unittests/Format/FormatTestJS.cpp

index 1eca6cf79cf95ca321a27cf5f077e908d0dc562e..eb74fc8f1d7b092547ac8b5ca786af6bebb57b99 100644 (file)
@@ -1358,10 +1358,14 @@ private:
                               Tok.Tok.getLength());
     // For formatting, treat unterminated string literals like normal string
     // literals.
-    if (Tok.is(tok::unknown) && !Tok.TokenText.empty() &&
-        Tok.TokenText[0] == '"') {
-      Tok.Tok.setKind(tok::string_literal);
-      Tok.IsUnterminatedLiteral = true;
+    if (Tok.is(tok::unknown)) {
+      if (!Tok.TokenText.empty() && Tok.TokenText[0] == '"') {
+        Tok.Tok.setKind(tok::string_literal);
+        Tok.IsUnterminatedLiteral = true;
+      } else if (Style.Language == FormatStyle::LK_JavaScript &&
+                 Tok.TokenText == "''") {
+        Tok.Tok.setKind(tok::char_constant);
+      }
     }
   }
 };
index 08969ff8e0715d40b590ee085f82f16abf459c27..8a9ac9e20b855d92e273c831d01d39f93926377b 100644 (file)
@@ -84,5 +84,9 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) {
   verifyFormat("var obj = {a: 1, b: 2, c: 3};");
 }
 
+TEST_F(FormatTestJS, SingleQuoteStrings) {
+  verifyFormat("this.function('', true);");
+}
+
 } // end namespace tooling
 } // end namespace clang