Commit Graph

335 Commits

Author SHA1 Message Date
Sam McCall 4d006520b8 [clangd] Clean up unused includes. NFCI
Add includes where needed to fix build.
Haven't systematically added used headers, so there is still accidental
dependency on transitive includes.
2022-02-26 12:00:16 +01:00
Kadir Cetinkaya 825a3cd6b6
[clangd] Fail inlayHints requests on content changes
This should improve the overall UX by making the labels less jumpy.

Differential Revision: https://reviews.llvm.org/D117776
2022-01-21 12:40:49 +01:00
Nathan James 8b88ff0803
[clangd] Add option to use dirty file contents when building preambles.
Adds a option `use-dirty-preambles` to enable using unsaved in editor contents when building pre-ambles.
This enables a more seamless user experience when switching between header and implementation files and forgetting to save inbetween.
It's also in line with the LSP spec that states open files in the editor should be used instead of on the contents on disk - https://microsoft.github.io/language-server-protocol/overviews/lsp/overview/
For now the option is defaulted to off and hidden, Though I have a feeling it should be moved into the `.clangd` config and possibly defaulted to true.

Addresses https://github.com/clangd/clangd/issues/488

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D95046
2022-01-17 10:55:35 +00:00
Haojian Wu 884832407e [clangd] Avoid a code completion crash
This is a workaround (adding a newline to the eof) in clangd to avoid the code
completion crash, see https://github.com/clangd/clangd/issues/332.

In principle, this is a clang bug, we should fix it in clang, but it is not
trivial.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D117456
2022-01-17 10:35:55 +01:00
Sam McCall 71a082f726 [clangd] Implement textDocument/typeDefinition
This reuses the type=>decl mapping from go-to-definition on auto.
(Which could stand some improvement, but that can happen later).

Fixes https://github.com/clangd/clangd/issues/367

Differential Revision: https://reviews.llvm.org/D116443
2022-01-13 22:15:10 +01:00
Sam McCall 7c19fdd599 [clangd] Polish clangd/inlayHints and expose them by default.
This means it's a "real feature" in clangd 14, albeit one that requires special
client support.

- remove "preview" from the flag description
- expose the `clangdInlayHints` capability by default
- provide `position` as well as `range`
- support `InlayHintsParams.range` to restrict the range retrieved
- inlay hint list is in document order (sorted by position)

Still to come: control feature via config rather than flag.

Fixes https://github.com/clangd/clangd/issues/313
Protocol doc is in https://github.com/llvm/clangd-www/pull/56/files

Differential Revision: https://reviews.llvm.org/D116699
2022-01-07 15:12:43 +01:00
Sam McCall 6917f87b3c [clangd] Cleanup unneeded use of shared_ptr. NFC 2021-12-15 02:13:50 +01:00
Kadir Cetinkaya d3606a3375
[clangd] Provide documentation as MarkupContent in signaturehelp
This unifies the behaviour we have in code completion item
documentations and signaturehelp. Providing better line wrapping and detection
of inline code blocks in comments to be renedered appropriately in markdown.

Differential Revision: https://reviews.llvm.org/D115442
2021-12-10 12:58:08 +01:00
Kadir Cetinkaya 614b46e4dc
[clangd] Add a flag to disable formatting of tweak edits
Some tweaks might edit file types not supported by clang-format. This
patch gives them a way to signal that they do not require formatting.

Differential Revision: https://reviews.llvm.org/D105039
2021-06-28 20:52:47 +02:00
Kadir Cetinkaya 8f79203a22
[clangd] New ParsingCallback for semantics changes
Previously notification of the Server about semantic happened strictly
before notification of the AST thread.
Hence a racy Server could make a request (like semantic tokens) after
the notification, with the assumption that it'll be served fresh
content. But it wasn't true if AST thread wasn't notified about the
change yet.

This change reverses the order of those notifications to prevent racy
interactions.

Differential Revision: https://reviews.llvm.org/D102761
2021-05-26 16:57:30 +02:00
Nathan James 6815037085
[clangd][NFC] Remove unnecessary string captures in lambdas.
Due to a somewhat annoying, but necessary, shortfall in -Wunused-lambda-capture, These unused captures aren't warned about.

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D101611
2021-04-30 13:27:24 +01:00
Christian Kandeler 81dae18dff [clangd] Allow AST request without range
If no range is given, return the translation unit AST.
This is useful for tooling operations that require e.g. the full path to
a node.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D101057
2021-04-23 21:35:42 +02:00
Nathan James 6b4e8f82a3
[clangd] Use dirty filesystem when performing cross file tweaks
Cross file tweaks can now use the dirty buffer contents easily when performing cross file effects.
This can be noted on the DefineOutline tweak, now working when the target file is unsaved

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D93978
2021-04-20 17:13:44 +01:00
Nathan Ridge cbc9c4ea90 [clangd] Add support for inline parameter hints
Differential Revision: https://reviews.llvm.org/D98748
2021-04-14 02:31:20 -04:00
Kadir Cetinkaya bce3ac4f22
[clangd] Introduce ASTHooks to FeatureModules
These can be invoked at different stages while building an AST to let
FeatureModules implement features on top of it. The patch also
introduces a sawDiagnostic hook, which can mutate the final clangd::Diag
while reading a clang::Diagnostic.

Differential Revision: https://reviews.llvm.org/D98499
2021-04-13 17:45:09 +02:00
Kadir Cetinkaya bb6d96ced8
[clangd] Enable modules to contribute tweaks.
First patch to enable diagnostic fix generation through modules. The
workflow will look like:
- ASTWorker letting modules know about diagnostics while building AST,
modules can read clang::Diagnostic and mutate clangd::Diagnostic through
that hook.
- Modules can implement and expose tweaks to fix diagnostics or act as
general refactorings.
- Tweak::Selection will contain information about the diagnostic
associated with the codeAction request to enable modules to fail their
diagnostic fixing tweakson prepare if need be.

Differential Revision: https://reviews.llvm.org/D98498
2021-04-13 17:45:08 +02:00
Kadir Cetinkaya f71404c37c
[clangd] Replace usages of dummy with more descriptive words
Dummy is a word with inappropriate associations. This patch updates the
references to it in clangd code base with more precise ones.

The only user-visible change is the default variable name used when extracting a
variable. It will be named as `placeholder` from now on.

Differential Revision: https://reviews.llvm.org/D99065
2021-03-22 12:49:24 +01:00
Kadir Cetinkaya 2772c3a975
[clangd] Introduce pullDiags endpoint
Implement initial support for pull-based diagnostics in ClangdServer.
This is planned for LSP 3.17, and initial proposal is in
d15eb0671e/protocol/src/common/proposed.diagnostic.ts (L111).

We chose to serve the requests only when clangd has a fresh preamble
available. In case of a stale preamble we just drop the request on the
floor.

This patch doesn't plumb this to LSP layer yet, as pullDiags is still a
proposal with only an implementation in vscode.

Differential Revision: https://reviews.llvm.org/D98623
2021-03-16 12:52:15 +01:00
Sam McCall 3b99731c4e [clangd] Turn off implicit cancellation based on client capabilities
Capability is in upcoming 3.17: https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/

(This is also useful for C++ embedders)

Differential Revision: https://reviews.llvm.org/D98414
2021-03-16 12:27:40 +01:00
Kadir Cetinkaya ac292dafa7
[clangd] Add config block for Completion and option for AllScopes
Depends on D98029

Differential Revision: https://reviews.llvm.org/D98037
2021-03-11 20:30:35 +01:00
Nathan James 7044f1d875
[clangd] Use Dirty Filesystem for cross file rename.
Refactor cross file rename to use a Filesystem instead of a function for getting buffer contents of open files.

Depends on D94554

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D95043
2021-03-10 13:41:29 +00:00
Nathan James 0250b053b5
[clangd] Add a Filesystem that overlays Dirty files.
Create a `ThreadsafeFS` in the `DraftStore` that overlays the dirty file contents over another `ThreadsafeFS`.
This provides a nice thread-safe interface for using dirty file contents throughout the codebase, for example cross file refactoring.
Creating a Filesystem view will overlay a snapshot of the current contents, so if the draft store is updated while the view is being used, it will contain stale contents.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D94554
2021-03-09 14:35:21 +00:00
Sam McCall a60d06d8b7 [clangd] Rename Module -> FeatureModule to avoid confusion. NFC
As pointed out in D96244, "Module" is already pretty overloaded to refer
to clang and llvm modules. (And clangd deals directly with the former).

FeatureModule is a bit of a mouthful but it's pretty self-descriptive.
I think it might be better than "Component" which doesn't really capture
the "common interface" aspect - it's IMO confusing to refer to
"components" but exclude CDB for example.

Differential Revision: https://reviews.llvm.org/D97950
2021-03-05 10:04:00 +01:00
Kadir Cetinkaya 1d7b328198
[clangd] Introduce client state invalidation
Clangd can invalidate client state of features like semantic higlighting
without client explicitly triggering, for example after a preamble build
caused by an onSave notification on a different file.

This patch introduces a mechanism to let client know of such actions,
and also calls the workspace/semanticTokens/refresh request to
demonstrate the situation after each preamble build.

Fixes https://github.com/clangd/clangd/issues/699.

Differential Revision: https://reviews.llvm.org/D97548
2021-03-04 11:15:10 +01:00
Sam McCall bca3e24139 [clangd] Move DraftStore from ClangdLSPServer into ClangdServer.
ClangdServer already gets notified of every change, so it makes sense for it to
be the source of truth.
This is a step towards having ClangdServer expose a FS that includes dirty
buffers: D94554

Related changes:
 - version is now optional for ClangdServer, to preserve our existing fuzziness
   in this area (missing version ==> autoincrement)
 - ClangdServer::format{File,Range} are now more regular ClangdServer functions
   that don't need the code passed in. While here, combine into one function.
 - incremental content update logic is moved from DraftStore to
   ClangdLSPServer, with most of the implementation in SourceCode.cpp.
   DraftStore is now fairly trivial, and will probably ultimately be
   *replaced* by the dirty FS stuff.

Differential Revision: https://reviews.llvm.org/D97738
2021-03-02 22:58:50 +01:00
Sam McCall f0e69272c6 [clangd] Shutdown sequence for modules, and doc threading requirements
This allows modules to do work on non-TUScheduler background threads.

Differential Revision: https://reviews.llvm.org/D96755
2021-02-22 23:14:47 +01:00
Sam McCall 2d9cfcfef0 [clangd] Narrow and document a loophole in blockUntilIdle
blockUntilIdle of a parent can't always be correctly implemented as
  return ChildA.blockUntilIdle() && ChildB.blockUntilIdle()
The problem is that B can schedule work on A while we're waiting on it.

I believe this is theoretically possible today between CDB and background index.
Modules open more possibilities and it's hard to reason about all of them.

I don't have a perfect fix, and the abstraction is too good to lose. this patch:
 - calls out why we block on workscheduler first, and asserts correctness
 - documents the issue
 - reduces the practical possibility of spuriously returning true significantly

This function is ultimately only for testing, so we're driving down flake rate.

Differential Revision: https://reviews.llvm.org/D96856
2021-02-22 23:08:52 +01:00
Sam McCall b6e52d8fa7 [clangd] Give modules access to filesystem, scheduler, and index.
This finally makes it possible to implement useful modules.

Differential Revision: https://reviews.llvm.org/D96726
2021-02-16 15:30:08 +01:00
Sam McCall 40cc63ea6e [clangd] Modules can have a public API. NFC
Differential Revision: https://reviews.llvm.org/D96730
2021-02-16 15:22:57 +01:00
Kadir Cetinkaya d25fbaa4a4
[clangd] Fix unsued private field warning 2021-02-13 13:20:15 +01:00
Kadir Cetinkaya 2423a3863e
[clangd] Introduce Modules
Modules can be used to augment clangd's behaviours in various features.

Differential Revision: https://reviews.llvm.org/D96244
2021-02-12 18:37:16 +01:00
Haojian Wu ee4dd0f876 [clangd] Remove the cross-file-rename option.
and simplify the code.

Differential Revision: https://reviews.llvm.org/D96578
2021-02-12 15:38:55 +01:00
Sam McCall 4dc8365f80 [clangd] Remove support for pre-standard semanticHighlighting notification
This is obsoleted by the standard semanticTokens request family.
As well as the protocol details, this allows us to remove a bunch of plumbing
around pushing highlights to clients.

This should not land until the new protocol has feature parity, see D77702.

Differential Revision: https://reviews.llvm.org/D95576
2021-02-10 22:09:03 +01:00
Sam McCall 1eb7fd089e [clangd] Remove some obsolete options that are now always on
- always collect main-file refs when indexing
 - always build preambles asynchronously
 - always use dex for fast preamble index

Retire associated flags

Differential Revision: https://reviews.llvm.org/D95571
2021-02-01 17:24:03 +01:00
Quentin Chateau 3680cb99a7 [clangd] ignore parallelism level for quick tasks
This allows quick tasks without dependencies that
need to run fast to run ASAP. This is mostly useful
for code formatting.

----------------------------

This fixes something that's been annoying me:
- Open your IDE workspace and its 20 open files
- Clangd spends 5 minutes parsing it all
- In the meantime you start to work
- Save a file, trigger format-on-save, which hangs because clangd is busy
- You're stuck waiting until clangd is done parsing your files before the formatting and save takes place

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D94875
2021-01-25 23:03:43 +01:00
Sam McCall 60cd75a098 [clangd] Inject context provider rather than config into ClangdServer. NFC
This is a step towards allowing CDB behavior to being configurable.

Previously ClangdServer itself created the configs and installed them into
contexts. This was natural as it knows how to deal with resulting diagnostics.

However this prevents config being used in CDB, which must be created before
ClangdServer. So we extract the context provider (config loader) as a separate
object, which publishes diagnostics to a ClangdServer::Callbacks itself.

Now initialization looks like:
 - First create the config::Provider
 - Then create the ClangdLSPServer, passing config provider
 - Next, create the context provider, passing config provider + diagnostic callbacks
 - now create the CDB, passing context provider
 - finally create ClangdServer, passing CDB, context provider, and diagnostic callbacks

Differential Revision: https://reviews.llvm.org/D95087
2021-01-22 14:34:30 +01:00
Sam McCall 2ab5fd2c85 [clangd] Retire some flags for uncontroversial, stable features.
And mark a couple to be retired afther the next release branch.

Differential Revision: https://reviews.llvm.org/D94727
2021-01-20 11:47:12 +01:00
Sam McCall e6be5c7cd6 [clangd] Remove the recovery-ast options.
These force a couple of flags or that are now on by default.
So the flags don't currently do anything unless the compile command has
-fno-recovery-ast explicitly.

(For turning recovery *off* for debugging we can inject the flag with config)

This leaves the command-line flags around with no effect, I'm planning to add
a "retired flag" mechanism shortly in a separate patch.

Differential Revision: https://reviews.llvm.org/D94724
2021-01-20 11:23:57 +01:00
Sam McCall 536a1b0ea2 [clangd] Allow CDBs to have background work to block on.
In preparation for moving DirectoryBasedCompilationDatabase broadcasting off
the main thread.

Differential Revision: https://reviews.llvm.org/D94603
2021-01-20 11:11:01 +01:00
Utkarsh Saxena 275716d6db [clangd] Derive new signals in CC from ASTSignals.
This patch only introduces new signals but does not use their value
in scoring a CC candidate. Usage of these signals in CC ranking in both
heiristics and ML model will be introduced in later patches.

Differential Revision: https://reviews.llvm.org/D94473
2021-01-18 17:37:27 +01:00
Sam McCall 17fb21f875 [clangd] Remove another option that was effectively always true. NFC 2021-01-14 17:19:47 +01:00
Nathan James 3505d8dc07 [clangd][NFC] Use PathRef for getCorrespondingHeaderOrSource 2021-01-07 02:41:19 +00:00
Quentin Chateau 3fa2d37eb3 [clangd][NFC] Improve clangd status messages
clangd actions have various naming schemes, the most
common being PascalCase. This commit applies PascalCase
to all clangd actions, and fix the status rendering
in `renderTUAction` to look more consistent.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D93546
2020-12-21 20:19:25 +01:00
Sam McCall fed9af29c2 [clangd] Publish config file errors over LSP
We don't act as a language server for these files (e.g. don't get open/close
notifications for them), but just blindly publish diagnostics for them.

This works reasonably well in coc.nvim and vscode: they show up in the
workspace diagnostic list and when you open the file.
The only update after the file is reparsed, not as you type which is a bit
janky, but seems a lot better than nothing.

Fixes https://github.com/clangd/clangd/issues/614

Differential Revision: https://reviews.llvm.org/D92704
2020-12-07 11:07:32 +01:00
Nathan James 73fdd99870
[clangd] Implement clang-tidy options from config
Added some new ClangTidyOptionsProvider like classes designed for clangd work flow.
These providers are designed to source the options on the worker thread but in a thread safe manner.
This is done through making the options getter take a pointer to the filesystem used by the worker thread which natuarally is from a ThreadsafeFS.
Internal caching in the providers is also guarded.

The providers don't inherit from `ClangTidyOptionsProvider` instead they share a base class which is able to create a provider for the `ClangTidyContext` using a specific FileSystem.
This approach means one provider can be used for multiple contexts even though `ClangTidyContext` owns its provider.

Depends on D90531

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D91029
2020-11-25 18:35:35 +00:00
Nathan Ridge dced150375 [clangd] Use WorkScheduler.run() in ClangdServer::resolveTypeHierarchy()
Differential Revision: https://reviews.llvm.org/D91941
2020-11-23 20:44:14 -05:00
Nathan Ridge 4cb976e014 [clangd] Call hierarchy (ClangdServer layer)
Differential Revision: https://reviews.llvm.org/D91123
2020-11-23 20:43:41 -05:00
Utkarsh Saxena b31486ad97 [clangd] textDocument/implementation (LSP layer)
Differential Revision: https://reviews.llvm.org/D91721
2020-11-23 13:50:44 +01:00
Sam McCall 8adc4d1ec7 [clangd] Add textDocument/ast extension method to dump the AST
This is a mass-market version of the "dump AST" tweak we have behind
-hidden-features.
I think in this friendlier form it'll be useful for people outside clang
developers, which would justify making it a real feature.
It could be useful as a step towards lightweight clang-AST tooling in clangd
itself (like matcher-based search).

Advantages over the tweak:
 - simplified information makes it more accessible, likely somewhat useful
   without learning too much clang internals
 - can be shown in a tree view
 - structured information gives some options for presentation (e.g.
   icon + two text colors + tooltip in vscode)
 - clickable nodes jump to the corresponding code
Disadvantages:
 - a bunch of code to handle different node types
 - likely missing some important info vs dump-ast due to brevity/oversight
 - may end up chasing/maintaining support for the long tail of nodes

Demo with VSCode support: https://imgur.com/a/6gKfyIV

Differential Revision: https://reviews.llvm.org/D89571
2020-11-20 01:13:28 +01:00
Nathan Ridge cb3c13fab6 [clangd] Propagate CollectMainFileRefs to BackgroundIndex
This appears to have been an omission in D83536.

Differential Revision: https://reviews.llvm.org/D89284
2020-10-13 09:20:18 -04:00