]> granicus.if.org Git - clang/commitdiff
clang-format: [Proto] Don't do bad things if imports are missing ;.
authorDaniel Jasper <djasper@google.com>
Mon, 20 Jun 2016 18:20:38 +0000 (18:20 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 20 Jun 2016 18:20:38 +0000 (18:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@273179 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestProto.cpp

index 18df7576ca22ce808bef8c98ce3de5e2a84d5e41..53d8c15d48d92acfc77b8522bf79276116060ff1 100644 (file)
@@ -882,10 +882,21 @@ void UnwrappedLineParser::parseStructuralElement() {
                  /*MunchSemi=*/false);
       return;
     }
-    if (Style.Language == FormatStyle::LK_JavaScript &&
-        FormatTok->is(Keywords.kw_import)) {
-      parseJavaScriptEs6ImportExport();
-      return;
+    if (FormatTok->is(Keywords.kw_import)) {
+      if (Style.Language == FormatStyle::LK_JavaScript) {
+        parseJavaScriptEs6ImportExport();
+        return;
+      }
+      if (Style.Language == FormatStyle::LK_Proto) {
+        nextToken();
+        if (!FormatTok->is(tok::string_literal))
+          return;
+        nextToken();
+        if (FormatTok->is(tok::semi))
+          nextToken();
+        addUnwrappedLine();
+        return;
+      }
     }
     if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
                            Keywords.kw_slots, Keywords.kw_qslots)) {
index d3d3d42aa84a6c8f02a34d564308737d5254d11f..68d724981ced14a4135afe0de33526626b6f1877 100644 (file)
@@ -189,5 +189,20 @@ TEST_F(FormatTestProto, ExtendingMessage) {
                "}");
 }
 
+TEST_F(FormatTestProto, FormatsImports) {
+  verifyFormat("import \"a.proto\";\n"
+               "import \"b.proto\";\n"
+               "// comment\n"
+               "message A {\n"
+               "}");
+
+  // Missing semicolons should not confuse clang-format.
+  verifyFormat("import \"a.proto\"\n"
+               "import \"b.proto\"\n"
+               "// comment\n"
+               "message A {\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang