summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/JSONRPCDispatcher.h
Commit message (Collapse)AuthorAgeFilesLines
* [clangd] Lay JSONRPCDispatcher to rest.Sam McCall2018-10-181-95/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Most of its functionality is moved into ClangdLSPServer. The decoupling between JSONRPCDispatcher, ProtocolCallbacks, ClangdLSPServer was never real, and only served to obfuscate. Some previous implicit/magic stuff is now explicit: - the return type of LSP method calls are now in the signature - no more reply() that gets the ID using global context magic - arg tracing no longer relies on RequestArgs::stash context magic either This is mostly refactoring, but some deliberate fixes while here: - LSP method params are now by const reference - notifications and calls are now distinct namespaces. (some tests had protocol errors and needed updating) - we now reply to calls we failed to decode - outgoing calls use distinct IDs A few error codes and message IDs changed in unimportant ways (see tests). Reviewers: ioeric Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D53387 llvm-svn: 344737
* [clangd] Hide unused function. NFCSam McCall2018-10-171-3/+0
| | | | llvm-svn: 344676
* [clangd] Rename and move trivial logger to Logger.cpp. NFCSam McCall2018-10-171-17/+0
| | | | llvm-svn: 344675
* [clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction. ↵Sam McCall2018-10-171-45/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | (re-land r344620) Summary: This paves the way for alternative transports (mac XPC, maybe messagepack?), and also generally improves layering: testing ClangdLSPServer becomes less of a pipe dream, we split up the JSONOutput monolith, etc. This isn't a final state, much of what remains in JSONRPCDispatcher can go away, handlers can call reply() on the transport directly, JSONOutput can be renamed to StreamLogger and removed, etc. But this patch is sprawling already. The main observable change (see tests) is that hitting EOF on input is now an error: the client should send the 'exit' notification. This is defensible: the protocol doesn't spell this case out. Reproducing the current behavior for all combinations of shutdown/exit/EOF clutters interfaces. We can iterate on this if desired. Reviewers: jkorous, ioeric, hokein Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D53286 llvm-svn: 344672
* Revert "[clangd] Refactor JSON-over-stdin/stdout code into Transport ↵Krasimir Georgiev2018-10-161-22/+45
| | | | | | | | | abstraction." This reverts commit r344620. Breaks upstream bots. llvm-svn: 344637
* [clangd] Refactor JSON-over-stdin/stdout code into Transport abstraction.Sam McCall2018-10-161-45/+22
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: This paves the way for alternative transports (mac XPC, maybe messagepack?), and also generally improves layering: testing ClangdLSPServer becomes less of a pipe dream, we split up the JSONOutput monolith, etc. This isn't a final state, much of what remains in JSONRPCDispatcher can go away, handlers can call reply() on the transport directly, JSONOutput can be renamed to StreamLogger and removed, etc. But this patch is sprawling already. The main observable change (see tests) is that hitting EOF on input is now an error: the client should send the 'exit' notification. This is defensible: the protocol doesn't spell this case out. Reproducing the current behavior for all combinations of shutdown/exit/EOF clutters interfaces. We can iterate on this if desired. Reviewers: jkorous, ioeric, hokein Subscribers: mgorny, ilya-biryukov, MaskRay, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D53286 llvm-svn: 344620
* [clangd] Allow all LSP methods to signal cancellation via $/cancelRequestSam McCall2018-09-131-4/+19
| | | | | | | | | | | | | | | | | | | | | | | | | Summary: The cancelable scopes are managed by JSONRPCDispatcher so that all Handlers run in cancelable contexts. (Previously ClangdServer did this, for code completion only). Cancellation request processing is therefore also in JSONRPCDispatcher. (Previously it was in ClangdLSPServer). This doesn't actually make any new commands *respect* cancellation - they'd need to check isCancelled() and bail out. But it opens the door to doing this incrementally, and putting such logic in common machinery like TUScheduler. I also rewrote the ClangdServer class/threading comments because I wanted to add to it and I got carried away. Reviewers: ilya-biryukov, kadircet Subscribers: ioeric, MaskRay, jkorous, arphaman, cfe-commits Differential Revision: https://reviews.llvm.org/D52004 llvm-svn: 342135
* [clangd] Initial cancellation mechanism for LSP requests.Kadir Cetinkaya2018-08-241-1/+7
| | | | | | | | | | | | Reviewers: ilya-biryukov, ioeric, hokein Reviewed By: ilya-biryukov Subscribers: mgorny, ioeric, MaskRay, jkorous, arphaman, jfb, cfe-commits Differential Revision: https://reviews.llvm.org/D50502 llvm-svn: 340607
* [clangd] Fix (most) naming warnings from clang-tidy. NFCIlya Biryukov2018-07-261-1/+1
| | | | llvm-svn: 338021
* [clangd] Upgrade logging facilities with levels and formatv.Sam McCall2018-07-111-3/+6
| | | | | | | | | | | | | | | | | | | | | | Summary: log() is split into four functions: - elog()/log()/vlog() have different severity levels, allowing filtering - dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but conditionally based on -debug-only flag and is omitted in release builds All logging functions use formatv-style format strings now, e.g: log("Could not resolve URI {0}: {1}", URI, Result.takeError()); Existing log sites have been split between elog/log/vlog by best guess. This includes a workaround for passing Error to formatv that can be simplified when D49170 or similar lands. Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits Differential Revision: https://reviews.llvm.org/D49008 llvm-svn: 336785
* [clangd] Remove JSON library in favor of llvm/Support/JSONSam McCall2018-07-091-6/+6
| | | | | | | | | | | | | | | | | | | | Summary: The library has graduated from clangd to llvm/Support. This is a mechanical change to move to the new API and remove the old one. Main API changes: - namespace clang::clangd::json --> llvm::json - json::Expr --> json::Value - Expr::asString() etc --> Value::getAsString() etc - unsigned longs need a cast (due to r336541 adding lossless integer support) Reviewers: ilya-biryukov Subscribers: mgorny, ioeric, MaskRay, jkorous, omtcyfz, cfe-commits Differential Revision: https://reviews.llvm.org/D49077 llvm-svn: 336549
* [clangd] Rewrite JSON dispatcher loop using C IO (FILE*) instead of ↵Sam McCall2018-06-051-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | std::istream. Summary: The EINTR loop around getline was added to fix an issue with mac gdb, but seems to loop infinitely in rare cases on linux where the parent editor exits (most reports with VSCode). I can't work out how to fix this in a portable way with std::istream, but the C APIs have clearer contracts and LLVM has a RetryAfterSignal function for use with them which seems battle-tested. While here, clean up some inconsistency around \n in log messages (now add it only after JSON payloads), and reduce the scope of the long-message handling which was only really added to fight fuzzers. Reviewers: malaperle, ilya-biryukov Subscribers: klimek, ioeric, jkorous, cfe-commits Differential Revision: https://reviews.llvm.org/D47643 llvm-svn: 333993
* [clangd] Support simpler JSON-RPC stream parsing for lit tests.Sam McCall2018-02-061-0/+9
| | | | | | | | | | | | | | Summary: Instead of content-length, we delimit messages with ---. This also removes the need for (most) dos-formatted test files. Reviewers: ioeric Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits Differential Revision: https://reviews.llvm.org/D42919 llvm-svn: 324333
* [clangd] Pass Context implicitly using TLS.Sam McCall2018-01-311-13/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Instead of passing Context explicitly around, we now have a thread-local Context object `Context::current()` which is an implicit argument to every function. Most manipulation of this should use the WithContextValue helper, which augments the current Context to add a single KV pair, and restores the old context on destruction. Advantages are: - less boilerplate in functions that just propagate contexts - reading most code doesn't require understanding context at all, and using context as values in fewer places still - fewer options to pass the "wrong" context when it changes within a scope (e.g. when using Span) - contexts pass through interfaces we can't modify, such as VFS - propagating contexts across threads was slightly tricky (e.g. copy vs move, no move-init in lambdas), and is now encapsulated in the threadpool Disadvantages are all the usual TLS stuff - hidden magic, and potential for higher memory usage on threads that don't use the context. (In practice, it's just one pointer) Reviewers: ilya-biryukov Subscribers: klimek, jkorous-apple, ioeric, cfe-commits Differential Revision: https://reviews.llvm.org/D42517 llvm-svn: 323872
* [clangd] Implemented logging using ContextIlya Biryukov2017-12-131-27/+15
| | | | | | | | | | | | Reviewers: sammccall, ioeric, hokein Reviewed By: sammccall Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D40486 llvm-svn: 320576
* [clangd] Logger implicitly adds newlineSam McCall2017-11-301-2/+1
| | | | llvm-svn: 319497
* [clangd] Switch from YAMLParser to JSONExprSam McCall2017-11-281-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: - Converted Protocol.h parse() functions to take JSON::Expr. These no longer detect and log unknown fields, as this is not that useful and no longer free. I haven't changed the error handling too much: fields that were treated as optional before are still optional, even when it's wrong. Exception: object properties with the wrong type are now ignored. - Made JSONRPCDispatcher parse using json::parse - The bug where 'method' must come before 'params' in the stream is fixed as a side-effect. (And the same bug in executeCommand). - Some parser crashers fixed as a side effect. e.g. https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=3890 - The debug stream now prettyprints the input messages with --pretty. - Request params are attached to traces when tracing is enabled. - Fixed some bugs in tests (errors tolerated by YAMLParser, and off-by-ones in Content-Length that our null-termination was masking) - Fixed a random double-escape bug in ClangdLSPServer (it was our last use of YAMLParser!) Reviewers: ilya-biryukov Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D40406 llvm-svn: 319159
* [clangd] Tracing improvementsSam McCall2017-11-231-2/+11
| | | | | | | | | | | | | | | | | | | | Summary: [clangd] Tracing improvements Compose JSON using JSONExpr Allow attaching metadata to spans (and avoid it if tracing is off) Attach IDs and responses of JSON RPCs to their spans The downside is that large responses make the trace viewer sluggish. We should make our responses less huge :-) Or fix trace viewer. Reviewers: ilya-biryukov Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D40132 llvm-svn: 318928
* [clangd] Add ErrorCode enum class.Haojian Wu2017-11-071-1/+2
| | | | | | | | | | | | | | Summary: Avoid using magic number in the code everywhere. Reviewers: sammccall Reviewed By: sammccall Subscribers: ilya-biryukov, cfe-commits Differential Revision: https://reviews.llvm.org/D39718 llvm-svn: 317559
* Adds a json::Expr type to represent intermediate JSON expressions.Sam McCall2017-11-061-8/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Summary: This form can be created with a nice clang-format-friendly literal syntax, and gets escaping right. It knows how to call unparse() on our Protocol types. All the places where we pass around JSON internally now use this type. Object properties are sorted (stored as std::map) and so serialization is canonicalized, with optional prettyprinting (triggered by a -pretty flag). This makes the lit tests much nicer to read and somewhat nicer to debug. (Unfortunately the completion tests use CHECK-DAG, which only has line-granularity, so pretty-printing is disabled there. In future we could make completion ordering deterministic, or switch to unittests). Compared to the current approach, it has some efficiencies like avoiding copies of string literals used as object keys, but is probably slower overall. I think the code/test quality benefits are worth it. This patch doesn't attempt to do anything about JSON *parsing*. It takes direction from the proposal in this doc[1], but is limited in scope and visibility, for now. I am of half a mind just to use Expr as the target of a parser, and maybe do a little string deduplication, but not bother with clever memory allocation. That would be simple, and fast enough for clangd... [1] https://docs.google.com/document/d/1OEF9IauWwNuSigZzvvbjc1cVS1uGHRyGTXaoy3DjqM4/edit +cc d0k so he can tell me not to use std::map. Reviewers: ioeric, malaperle Subscribers: bkramer, ilya-biryukov, mgorny, klimek Differential Revision: https://reviews.llvm.org/D39435 llvm-svn: 317486
* [clangd] Handle clangd.applyFix server-sideMarc-Andre Laperle2017-11-031-0/+2
| | | | | | | | | | | | | | | | | | | | | | | Summary: When the user selects a fix-it (or any code action with commands), it is possible to let the client forward the selected command to the server. When the clangd.applyFix command is handled on the server, it can send a workspace/applyEdit request to the client. This has the advantage that the client doesn't explicitly have to know how to handle clangd.applyFix. Therefore, the code to handle clangd.applyFix in the VS Code extension (and any other Clangd client) is not required anymore. Reviewers: ilya-biryukov, sammccall, Nebiroth, hokein Reviewed By: hokein Subscribers: ioeric, hokein, rwols, puremourning, bkramer, ilya-biryukov Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D39276 llvm-svn: 317322
* [clangd] less boilerplate in RPC dispatchSam McCall2017-10-121-23/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | Summary: Make the ProtocolHandlers glue between JSONRPCDispatcher and ClangdLSPServer generic. Eliminate small differences between methods, de-emphasize the unimportant distinction between notifications and methods. ClangdLSPServer is no longer responsible for producing a complete JSON-RPC response, just the JSON of the result object. (In future, we should move that JSON serialization out, too). Handler methods now take a context object that we may hang more functionality off in the future. Added documentation to ProtocolHandlers. Reviewers: ilya-biryukov, bkramer Reviewed By: ilya-biryukov Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D38464 llvm-svn: 315577
* Revert "Revert r315214 since diff -Z isn't portable, this is breaking:"Ilya Biryukov2017-10-101-2/+9
| | | | | | | | | This reverts commit r315242 and restores r315214. To fix original failure, replaced non-portable `diff -Z` with portable alternative: `diff -b`. llvm-svn: 315287
* Revert r315214 since diff -Z isn't portable, this is breaking:Bruno Cardoso Lopes2017-10-091-9/+2
| | | | | | | http://green.lab.llvm.org/green/job/clang-stage1-cmake-RA-expensive http://green.lab.llvm.org/green/job/clang-stage1-configure-RA llvm-svn: 315242
* [clangd] Added a command-line arg to mirror clangd input into a file.Ilya Biryukov2017-10-091-2/+9
| | | | | | | | | | | | | | Summary: The arg is useful for debugging and creating test cases. Reviewers: bkramer, krasimir Reviewed By: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D37970 llvm-svn: 315214
* [clangd] Introduced Logger interface.Ilya Biryukov2017-09-201-2/+3
| | | | | | | | | | | | | | Summary: This fixes a bunch of logging-related FIXMEs. Reviewers: bkramer, krasimir, malaperle Reviewed By: malaperle Subscribers: malaperle, klimek, cfe-commits, mgorny Differential Revision: https://reviews.llvm.org/D37972 llvm-svn: 313730
* Added missing includes in clangd to fix the build.Ilya Biryukov2017-05-161-0/+1
| | | | | | This commit should fix buildbot failures. llvm-svn: 303177
* [clangd] Refactor ProtocolHandlers to decouple them from ClangdLSPServerIlya Biryukov2017-05-161-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | Summary: A refactoring to decouple ProtocolHandlers and Language Server input parsing loop from the ClangdLSPServer. The input parsing was extracted from `main` to a function(runLanguageServerLoop). ProtocolHandlers now provide an interface to handle various LSP methods, this interface is used by ClangdLSPServer. Methods for code formatting were moved from ProtocolHandlers to ClangdServer. ClangdLSPServer now provides a cleaner interface that only runs Language Server input loop. Reviewers: bkramer, krasimir Reviewed By: krasimir Subscribers: cfe-commits, klimek Tags: #clang-tools-extra Differential Revision: https://reviews.llvm.org/D33201 llvm-svn: 303173
* [clangd] Synchronize logs access.Benjamin Kramer2017-02-151-2/+2
| | | | | | | I don't think that this is necessary for correctness, but makes tsan much more useful. llvm-svn: 295194
* [clangd] Move isDone from the JSONOutput to ShutdownHandler.Benjamin Kramer2017-02-101-7/+0
| | | | | | | This is just as easy to check from main but prevents random code from shutting down the server. llvm-svn: 294760
* [clangd] Refactor stream output into a single thread-safe output object.Benjamin Kramer2017-02-101-6/+32
| | | | | | | This abstracts away the passing of raw_ostreams everywhere, thread safety will be used soon. llvm-svn: 294747
* Add a prototype for clangdBenjamin Kramer2017-02-071-0/+66
clangd is a language server protocol implementation based on clang. It's supposed to provide editor integration while not suffering from the confined ABI of libclang. This implementation is limited to the bare minimum functionality of doing (whole-document) formatting and rangeFormatting. The JSON parsing is based on LLVM's YAMLParser but yet most of the code of clangd is currently dealing with JSON serialization and deserialization. This was only tested with VS Code so far, mileage with other LSP clients may vary. Differential Revision: https://reviews.llvm.org/D29451 llvm-svn: 294291
OpenPOWER on IntegriCloud