318 Commits

Author SHA1 Message Date
Matheus Izvekov
dc17429ae6
[clang] improved preservation of template keyword (#133610) 2025-04-01 17:15:18 -03:00
Mythreya
9cdbc47144
[clangd] Add HeaderInsertion config option (#128503)
This is the config file equivalent of the `--header-insertion` command line option

Fixes https://github.com/clangd/clangd/issues/2032
2025-03-20 02:22:29 -04:00
Bevin Hansson
030d48b7db
[clangd] Augment code completion results with documentation from the index. (#120099)
When looking up code completions from Sema, there is no associated
documentation. This is due to crash issues with stale preambles.
However, this also means that code completion results from other
than the main file do not have documentation in certain cases,
which is a bad user experience.

This patch performs a lookup into the index using the code
completion result declarations to find documentation, and
attaches it to the results.

Fixes clangd/clangd#2252
Fixes clangd/clangd#564
2025-01-15 01:14:20 -05:00
kleines Filmröllchen
1f90797f6a
[clangd] Allow specifying what headers are always included via "" or <> (#67749)
Projects can now add config fragments like this to their .clangd:

```yaml
Style:
  QuotedHeaders: "src/.*"
  AngledHeaders: ["path/sdk/.*", "third-party/.*"]
```

to force headers inserted via the --header-insertion=iwyu mode matching
at least one of the regexes to have <> (AngledHeaders) or ""
(QuotedHeaders) around them, respectively. For other headers (and in
conflicting cases where both styles have a matching regex), the current
system header detection remains.

Fixes https://github.com/clangd/clangd/issues/1247
2024-12-27 14:14:29 -05:00
Nathan Ridge
18ca7ad339
[clangd] Add ArgumentLists config option under Completion (#111322)
The new config option is a more flexible version of
--function-arg-placeholders, allowing users more detailed control of
what is inserted in argument list position when clangd completes the
name of a function in a function call context.

Fixes https://github.com/llvm/llvm-project/issues/63565

Co-authored-by: MK-Alias <ImNotReadingThis@maininator.com>
2024-10-06 20:32:54 -04:00
Chuanqi Xu
af47038fb1
[clangd] [C++20] [Modules] Support code complete for C++20 modules (#110083)
According to https://github.com/ChuanqiXu9/clangd-for-modules/issues/9,
I surprisingly found the support for C++20 modules doesn't support code
completion well.

After debugging, I found there are problems:
(1) We forgot to call `adjustHeaderSearchOptions` in code complete. This
may be an easy oversight.
(2) In `CodeCompleteOptions::getClangCompleteOpts`, we may set
`LoadExternal` as false when index is available. But we have support
modules with index. So it is conflicting. Given modules are opt in now,
I think it makes sense to to set LoadExternal as true when modules are
enabled.

This is a small fix and I wish it can land faster.
2024-09-30 13:07:41 +08:00
Nathan Ridge
7aa371687a
[clangd] Avoid using CompletionItemKind.Text for macro completions from the index (#88236)
This was fixed in https://github.com/clangd/clangd/issues/1484 for Sema
completions but the fix did not apply to index completions.

Fixes https://github.com/clangd/clangd/issues/2002
2024-04-11 19:34:53 -04:00
Nathan Ridge
807fd07646
[clangd] Handle variable templates consistently with class templates in code completion (#85740)
The option --function-arg-placeholders=0 results in placeholders being
omitted for class template argument lists. This patch extends the same
treatment to variable template argument lists.

Fixes https://github.com/clangd/clangd/issues/1976
2024-03-20 02:44:04 -04:00
Nathan Ridge
3093d731df
[clangd] Avoid libFormat's objective-c guessing heuristic where possible (#84133)
This avoids a known libFormat bug where the heuristic can OOM on certain
large files (particularly single-header libraries such as miniaudio.h).

The OOM will still happen on affected files if you actually try to
format them (this is harder to avoid since the underlyting issue affects
the actual formatting logic, not just the language-guessing heuristic),
but at least it's avoided during non-modifying operations like hover,
and modifying operations that do local formatting like code completion.

Fixes https://github.com/clangd/clangd/issues/719
Fixes https://github.com/clangd/clangd/issues/1384
Fixes https://github.com/llvm/llvm-project/issues/70945
2024-03-11 04:16:45 -04:00
Kazu Hirata
d5953e3e30 [clangd] Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 23:26:09 -08:00
Nathan Ridge
cee598720f
[clangd] Correctly identify the next token after the completion point (#69153)
The code was previously using Lexer::findNextToken() which does not
handle being passed the completion point as input.

Fixes https://github.com/clangd/clangd/issues/1785
2023-11-12 02:13:22 -05:00
Younan Zhang
23ef8bf9c0 [clangd][CodeComplete] Improve FunctionCanBeCall
From two aspects:

- For function templates, emit additional template argument
placeholders in the context where it can't be a call in order
to specify an instantiation explicitly.

- Consider expressions with base type specifier such as
'Derived().Base::foo^' a function call.

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D156605
2023-09-28 21:42:02 +08:00
Jan Svoboda
5746002ebb [clang] NFCI: Change returned LanguageOptions pointer to reference 2023-09-05 13:23:53 -07:00
Fred Fu
79af92bb99 Reland "[clang-repl] support code completion at a REPL."
Original commit message:
"
This patch enabled code completion for ClangREPL. The feature was built upon
three existing Clang components: a list completer for LineEditor, a
CompletionConsumer from SemaCodeCompletion, and the ASTUnit::codeComplete method.
The first component serves as the main entry point of handling interactive inputs.

Because a completion point for a compiler instance has to be unchanged once it
is set, an incremental compiler instance is created for each code
completion. Such a compiler instance carries over AST context source from the
main interpreter compiler in order to obtain declarations or bindings from
previous input in the same REPL session.

The most important API codeComplete in Interpreter/CodeCompletion is a thin
wrapper that calls with ASTUnit::codeComplete with necessary arguments, such as
a code completion point and a ReplCompletionConsumer, which communicates
completion results from SemaCodeCompletion back to the list completer for the
REPL.

In addition, PCC_TopLevelOrExpression and CCC_TopLevelOrExpression` top levels
were added so that SemaCodeCompletion can treat top level statements like
expression statements at the REPL. For example,

clang-repl> int foo = 42;
clang-repl> f<tab>

From a parser's persective, the cursor is at a top level. If we used code
completion without any changes, PCC_Namespace would be supplied to
Sema::CodeCompleteOrdinaryName, and thus the completion results would not
include foo.

Currently, the way we use PCC_TopLevelOrExpression and
CCC_TopLevelOrExpression is no different from the way we use PCC_Statement
and CCC_Statement respectively.

Differential revision: https://reviews.llvm.org/D154382
"

The new patch also fixes clangd and several memory issues that the bots reported
and upload the missing files.
2023-08-28 20:09:03 +00:00
Vassil Vassilev
752f87cd6a Revert "Reland "[clang-repl] support code completion at a REPL.""
This reverts commit 5ab25a42ba70c4b50214b0e78eaaccd30696fa09 due to forgotten
files.
2023-08-28 20:05:14 +00:00
Fred Fu
5ab25a42ba Reland "[clang-repl] support code completion at a REPL."
Original commit message:
"
This patch enabled code completion for ClangREPL. The feature was built upon
three existing Clang components: a list completer for LineEditor, a
CompletionConsumer from SemaCodeCompletion, and the ASTUnit::codeComplete method.
The first component serves as the main entry point of handling interactive inputs.

Because a completion point for a compiler instance has to be unchanged once it
is set, an incremental compiler instance is created for each code
completion. Such a compiler instance carries over AST context source from the
main interpreter compiler in order to obtain declarations or bindings from
previous input in the same REPL session.

The most important API codeComplete in Interpreter/CodeCompletion is a thin
wrapper that calls with ASTUnit::codeComplete with necessary arguments, such as
a code completion point and a ReplCompletionConsumer, which communicates
completion results from SemaCodeCompletion back to the list completer for the
REPL.

In addition, PCC_TopLevelOrExpression and CCC_TopLevelOrExpression` top levels
were added so that SemaCodeCompletion can treat top level statements like
expression statements at the REPL. For example,

clang-repl> int foo = 42;
clang-repl> f<tab>

From a parser's persective, the cursor is at a top level. If we used code
completion without any changes, PCC_Namespace would be supplied to
Sema::CodeCompleteOrdinaryName, and thus the completion results would not
include foo.

Currently, the way we use PCC_TopLevelOrExpression and
CCC_TopLevelOrExpression is no different from the way we use PCC_Statement
and CCC_Statement respectively.

Differential revision: https://reviews.llvm.org/D154382
"

The new patch also fixes clangd and several memory issues that the bots reported.
2023-08-28 19:59:56 +00:00
Jens Massberg
1af0e34477 [clangd][c++20] Drop first template argument in code completion in some contexts.
In case of a top level context the first template argument of a concept
should be dropped. Currently the indexer doesn't support different
signatures for different contexts (for an index entry always the default
`Symbol` context is used). Thus we add a hack which checks if we are in
a top level context and have a concept and in that case removes the
first argment of the signature and snippet suffix. If there is only a
single argument, the signature and snippet suffix are completly
removed. The check for the first argument is done by simply looking for
the first comma which should be sufficient in most cases.

Additionally extend test environment to support adding artificial index
entries with signature and completion snippet suffix.

Differential Revision: https://reviews.llvm.org/D154450
2023-07-05 12:04:24 +02:00
David Goldman
a42ce094d9 [clang][Sema] Add CodeCompletionContext::CCC_ObjCClassForwardDecl
- Use this new context in Sema to limit completions to seen ObjC class
  names

- Use this new context in clangd to disable include insertions when
  completing ObjC forward decls

Reviewed By: kadircet

Differential Revision: https://reviews.llvm.org/D150978
2023-06-27 16:25:40 -04:00
Sam McCall
3f9f146371 [clangd] Move completion signatures and labelDetails
(When clients support it, otherwise keep the existing rendering).

In VSCode this makes the signature darker.

Differential Revision: https://reviews.llvm.org/D151253
2023-06-05 18:05:18 +02:00
Kazu Hirata
ed1539c6ad Migrate {starts,ends}with_insensitive to {starts,ends}_with_insensitive (NFC)
This patch migrates uses of StringRef::{starts,ends}with_insensitive
to StringRef::{starts,ends}_with_insensitive so that we can use names
similar to those used in std::string_view.

Note that the llvm/ directory has migrated in commit
6c3ea866e93003e16fc55d3b5cedd3bc371d1fde.

I'll post a separate patch to deprecate
StringRef::{starts,ends}with_insensitive.

Differential Revision: https://reviews.llvm.org/D150506
2023-05-16 10:12:42 -07:00
Haojian Wu
4b1cec0652 [clangd] Add batch fixes for include-cleaner diagnostics
For each unused-include/missing-include diagnostic, we provide fix-all
alternative to them.

This patch also adds LSP ChangeAnnotation support.

Differential Revision: https://reviews.llvm.org/D147684
2023-04-27 12:08:59 +02:00
Younan Zhang
138adb0980 [clangd] Refine logic on $0 in completion snippets
We have a workaround from D128621 that makes $0 no longer being
a placeholder to conform a vscode feature. However, we have to
refine the logic as it may suppress the last parameter placeholder
for constructor of base class because not all patterns of completion
are compound statements.

This fixes clangd/clangd#1479

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D145319
2023-03-17 17:57:24 +08:00
Nathan Ridge
c4972d3729 [clangd] Avoid using CompletionItemKind.Text for macro completions
Fixes https://github.com/clangd/clangd/issues/1484

Differential Revision: https://reviews.llvm.org/D144703
2023-02-24 04:41:27 -05:00
Tom Praschan
ce87b03143 [clangd] Fix getQueryScopes for using-directive with inline namespace
For example, in the following code

```

using namespace std::string_literals;

int main() {
    strin^ // Completes `string` instead of `std::string`
}
```

The using declaration would make completion drop the std namespace, even though it shouldn't.

printNamespaceScope() skips inline namespaces, so to fix this use
printQualifiedName() instead

See https://github.com/clangd/clangd/issues/1451

Differential Revision: https://reviews.llvm.org/D140915
2023-02-09 20:53:33 +01:00
David Goldman
042dd99484 [clangd] Full support for #import insertions
These are still disabled by default, but will work in ObjC code if you
enable the `-import-insertions` flag.

Completion requires ASTSignals to be available; before ASTSignals are
available, we will always use #include. Once they are available, the
behavior varies as follows:

- For source files, use #import if the ObjC language flag is enabled
- For header files:
  - If the ObjC language flag is disabled, use #include
  - If the header file contains any #imports, use #import
  - If the header file references any ObjC decls, use #import
  - Otherwise, use #include

IncludeFixer support is similar, but it does not rely upon ASTSignals,
instead it does the above checks excluding the scan for ObjC symbols.

Differential Revision: https://reviews.llvm.org/D139458
2023-01-09 09:48:30 -05:00
Kazu Hirata
29ffafb575 [clang-tools-extra] Remove remaining uses of llvm::Optional (NFC)
This patch removes the unused "using" declaration and removes #include
"llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:34:53 -08:00
Kazu Hirata
f71ffd3b73 [clang-tools-extra] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to clean up the "using" declarations, #include
"llvm/ADT/Optional.h", etc.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:19:42 -08:00
Kazu Hirata
71f557355d [clang-tools-extra] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07 20:02:20 -08:00
Ilya Biryukov
c9b325088d [clangd] Allow to build Clangd without decision forest
Make it possible to disable building the decision forest ranking
model for clangd.  To unbreak build of Clangd on PPC32 in gentoo, see
https://bugs.gentoo.org/829602

Based on D138520.

Reviewed By: sammccall

Differential Revision: https://reviews.llvm.org/D139107
2022-12-07 13:52:22 +01:00
Kazu Hirata
b70fbd0fd9 [clangd] Use std::nullopt instead of None (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-06 23:50:19 -08:00
David Goldman
fc46d6e67f [clang][Tooling] Add support for generating #import edits
And make use of this from clangd's CodeComplete and IncludeFixer, although currently they are both restricted only to #include symbols.

Differential Revision: https://reviews.llvm.org/D128677
2022-12-06 13:47:07 -05:00
David Goldman
51f1ae52b0 [clangd] Add new IncludeDirective to IncludeHeaderWithReferences
The IncludeDirective contains both Include (the current behavior) and Import,
which we can use in the future to provide #import suggestions for
Objective-C files/symbols.

Differential Revision: https://reviews.llvm.org/D128457
2022-12-06 13:47:07 -05:00
Kazu Hirata
059a23c0f0 [clang-tools-extra] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:54:50 -08:00
Kazu Hirata
1f914944b6 Don't use Optional::getPointer (NFC)
Since std::optional does not offer getPointer(), this patch replaces
X.getPointer() with &*X to make the migration from llvm::Optional to
std::optional easier.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Differential Revision: https://reviews.llvm.org/D138466
2022-11-21 19:03:40 -08:00
Tom Praschan
0e00611cbc [clangd] Add heuristic for dropping snippet when completing member function pointer
This implements the 1st heuristic mentioned in https://github.com/clangd/clangd/issues/968#issuecomment-1002242704:

When completing a function that names a non-static member of a class, and we are not inside that class's scope, assume the reference will not be a call (and thus don't add the snippetSuffix)

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D137040
2022-11-17 00:37:16 +01:00
Tom Praschan
990c189379 [clangd] Add scoped enum constants to all-scopes-completion
This was originally part of https://reviews.llvm.org/D136925, but we decided to move it to a separate patch.
In case it turns out to be controversial, it can be reverted more easily.

Differential Revision: https://reviews.llvm.org/D137104
2022-11-02 20:38:01 +01:00
Tom Praschan
a68bcd81dc [clangd] Index unscoped enums in class scope for code completion
Fixes https://github.com/clangd/clangd/issues/1082

Differential Revision: https://reviews.llvm.org/D136925
2022-11-02 12:50:50 +01:00
Kadir Cetinkaya
da5ded4fc9
[clangd] Populate ranges and symbol origin for paramname completions
Differential Revision: https://reviews.llvm.org/D136951
2022-10-31 11:34:06 +01:00
Tom Praschan
60528c690a [clangd] Return earlier when snippet is empty
Fixes github.com/clangd/clangd/issues/1216

If the Snippet string is empty, Snippet.front() would trigger a crash.
Move the Snippet->empty() check up a few lines to avoid this. Should not
break any existing behavior.

Differential Revision: https://reviews.llvm.org/D134137
2022-09-21 00:45:26 +02:00
Kadir Cetinkaya
71c4fb1d64
[clangd] Set Incompleteness for spec fuzzyfind requests
Differential Revision: https://reviews.llvm.org/D133479
2022-09-08 18:34:25 +02:00
David Goldman
e09c750498 [clangd][ObjC] Improve completions for protocols + category names
- Render protocols as interfaces to differentiate them from classes
  since a protocol and class can have the same name. Take this one step
  further though, and only recommend protocols in ObjC protocol completions.

- Properly call `includeSymbolFromIndex` even with a cached
  speculative fuzzy find request

- Don't use the index to provide completions for category names,
  symbols there don't make sense

Differential Revision: https://reviews.llvm.org/D132962
2022-09-08 11:30:26 -04:00
Fangrui Song
0972a390b9 LLVM_FALLTHROUGH => [[fallthrough]]. NFC 2022-08-09 04:06:52 +00:00
Kazu Hirata
95a932fb15 Remove redundaunt override specifiers (NFC)
Identified with modernize-use-override.
2022-07-24 22:28:11 -07:00
Qwinci
3f73c57935 Argument name support for function pointer signature hints
Fixes https://github.com/clangd/clangd/issues/1068

Reviewed By: nridge

Differential Revision: https://reviews.llvm.org/D125120
2022-07-19 20:02:26 -04:00
Kazu Hirata
53daa177f8 [clang, clang-tools-extra] Use has_value instead of hasValue (NFC) 2022-07-12 22:47:41 -07:00
David Goldman
dc6c1f181b [clangd][ObjC] Fix ObjC method definition completion
D124637 improved filtering of method expressions, but not method
definitions. With this change, clangd will now filter ObjC method
definition completions based on their entire selector instead of
only the first selector fragment.

Differential Revision: https://reviews.llvm.org/D128821
2022-07-01 10:02:47 -04:00
Kazu Hirata
3b7c3a654c Revert "Don't use Optional::hasValue (NFC)"
This reverts commit aa8feeefd3ac6c78ee8f67bf033976fc7d68bc6d.
2022-06-25 11:56:50 -07:00
Kazu Hirata
aa8feeefd3 Don't use Optional::hasValue (NFC) 2022-06-25 11:55:57 -07:00
Kazu Hirata
0916d96d12 Don't use Optional::hasValue (NFC) 2022-06-20 20:17:57 -07:00
Kazu Hirata
5413bf1bac Don't use Optional::hasValue (NFC) 2022-06-20 11:33:56 -07:00