[clangd][tests] Fix handling of EOF in delimited input
authorJan Korous <jkorous@apple.com>
Mon, 23 Apr 2018 15:55:07 +0000 (15:55 +0000)
committerJan Korous <jkorous@apple.com>
Mon, 23 Apr 2018 15:55:07 +0000 (15:55 +0000)
Request in delimited input ended by EOF shouldn't be an error state.
Comments at the end of test file shouldn't be logged as an error state.
Input mirroring should work for the last request in delimited test file.

llvm-svn: 330608

clang-tools-extra/clangd/JSONRPCDispatcher.cpp
clang-tools-extra/test/clangd/delimited-input-comment-at-the-end.test [new file with mode: 0644]

index 2cf4da3..1e0f5bc 100644 (file)
@@ -277,21 +277,19 @@ static llvm::Optional<std::string> readDelimitedMessage(std::istream &In,
     if (LineRef.startswith("#")) // comment
       continue;
 
-    bool IsDelim = LineRef.find_first_not_of('-') == llvm::StringRef::npos;
-    if (!IsDelim) // Line is part of a JSON message.
-      JSON += Line;
-    if (IsDelim) {
-      Out.mirrorInput(
-          llvm::formatv("Content-Length: {0}\r\n\r\n{1}", JSON.size(), JSON));
-      return std::move(JSON);
-    }
+    // found a delimiter
+    if (LineRef.find_first_not_of('-') == llvm::StringRef::npos)
+      break;
+
+    JSON += Line;
   }
 
   if (In.bad()) {
     log("Input error while reading message!");
     return llvm::None;
   } else {
-    log("Input message terminated by EOF");
+    Out.mirrorInput(
+        llvm::formatv("Content-Length: {0}\r\n\r\n{1}", JSON.size(), JSON));
     return std::move(JSON);
   }
 }
diff --git a/clang-tools-extra/test/clangd/delimited-input-comment-at-the-end.test b/clang-tools-extra/test/clangd/delimited-input-comment-at-the-end.test
new file mode 100644 (file)
index 0000000..9c10b92
--- /dev/null
@@ -0,0 +1,12 @@
+# RUN: clangd -input-style=delimited -run-synchronously -input-mirror-file %t < %s\r
+# RUN: grep '{"jsonrpc":"2.0","id":3,"method":"exit"}' %t\r
+#\r
+# RUN: clangd -lit-test -input-mirror-file %t < %s\r
+# RUN: grep '{"jsonrpc":"2.0","id":3,"method":"exit"}' %t\r
+#\r
+{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":123,"rootPath":"clangd","capabilities":{},"trace":"off"}}\r
+---\r
+{"jsonrpc":"2.0","id":3,"method":"shutdown"}\r
+---\r
+{"jsonrpc":"2.0","id":3,"method":"exit"}\r
+# comment at the end\r