Skip to content

Commit 46977c4

Browse files
Merge from 'master' to 'sycl-web' (#2)
CONFLICT (content): Merge conflict in clang/include/clang/Basic/DiagnosticDriverKinds.td
2 parents 23ecc0b + 9d2378b commit 46977c4

File tree

737 files changed

+26242
-15271
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

737 files changed

+26242
-15271
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class ClangdLSPServer::MessageHandler : public Transport::MessageHandler {
395395
Context handlerContext() const {
396396
return Context::current().derive(
397397
kCurrentOffsetEncoding,
398-
Server.NegotiatedOffsetEncoding.getValueOr(OffsetEncoding::UTF16));
398+
Server.Opts.Encoding.getValueOr(OffsetEncoding::UTF16));
399399
}
400400

401401
// We run cancelable requests in a context that does two things:
@@ -465,43 +465,42 @@ static std::vector<llvm::StringRef> semanticTokenTypes() {
465465
void ClangdLSPServer::onInitialize(const InitializeParams &Params,
466466
Callback<llvm::json::Value> Reply) {
467467
// Determine character encoding first as it affects constructed ClangdServer.
468-
if (Params.capabilities.offsetEncoding && !NegotiatedOffsetEncoding) {
469-
NegotiatedOffsetEncoding = OffsetEncoding::UTF16; // fallback
468+
if (Params.capabilities.offsetEncoding && !Opts.Encoding) {
469+
Opts.Encoding = OffsetEncoding::UTF16; // fallback
470470
for (OffsetEncoding Supported : *Params.capabilities.offsetEncoding)
471471
if (Supported != OffsetEncoding::UnsupportedEncoding) {
472-
NegotiatedOffsetEncoding = Supported;
472+
Opts.Encoding = Supported;
473473
break;
474474
}
475475
}
476476

477-
ClangdServerOpts.TheiaSemanticHighlighting =
477+
Opts.TheiaSemanticHighlighting =
478478
Params.capabilities.TheiaSemanticHighlighting;
479479
if (Params.capabilities.TheiaSemanticHighlighting &&
480480
Params.capabilities.SemanticTokens) {
481481
log("Client supports legacy semanticHighlights notification and standard "
482482
"semanticTokens request, choosing the latter (no notifications).");
483-
ClangdServerOpts.TheiaSemanticHighlighting = false;
483+
Opts.TheiaSemanticHighlighting = false;
484484
}
485485

486486
if (Params.rootUri && *Params.rootUri)
487-
ClangdServerOpts.WorkspaceRoot = std::string(Params.rootUri->file());
487+
Opts.WorkspaceRoot = std::string(Params.rootUri->file());
488488
else if (Params.rootPath && !Params.rootPath->empty())
489-
ClangdServerOpts.WorkspaceRoot = *Params.rootPath;
489+
Opts.WorkspaceRoot = *Params.rootPath;
490490
if (Server)
491491
return Reply(llvm::make_error<LSPError>("server already initialized",
492492
ErrorCode::InvalidRequest));
493493
if (const auto &Dir = Params.initializationOptions.compilationDatabasePath)
494-
CompileCommandsDir = Dir;
495-
if (UseDirBasedCDB) {
494+
Opts.CompileCommandsDir = Dir;
495+
if (Opts.UseDirBasedCDB) {
496496
BaseCDB = std::make_unique<DirectoryBasedGlobalCompilationDatabase>(
497-
CompileCommandsDir);
498-
BaseCDB = getQueryDriverDatabase(
499-
llvm::makeArrayRef(ClangdServerOpts.QueryDriverGlobs),
500-
std::move(BaseCDB));
497+
Opts.CompileCommandsDir);
498+
BaseCDB = getQueryDriverDatabase(llvm::makeArrayRef(Opts.QueryDriverGlobs),
499+
std::move(BaseCDB));
501500
}
502501
auto Mangler = CommandMangler::detect();
503-
if (ClangdServerOpts.ResourceDir)
504-
Mangler.ResourceDir = *ClangdServerOpts.ResourceDir;
502+
if (Opts.ResourceDir)
503+
Mangler.ResourceDir = *Opts.ResourceDir;
505504
CDB.emplace(BaseCDB.get(), Params.initializationOptions.fallbackFlags,
506505
tooling::ArgumentsAdjuster(std::move(Mangler)));
507506
{
@@ -510,19 +509,18 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
510509
// Server, CDB, etc.
511510
WithContext MainContext(BackgroundContext.clone());
512511
llvm::Optional<WithContextValue> WithOffsetEncoding;
513-
if (NegotiatedOffsetEncoding)
514-
WithOffsetEncoding.emplace(kCurrentOffsetEncoding,
515-
*NegotiatedOffsetEncoding);
516-
Server.emplace(*CDB, TFS, ClangdServerOpts,
512+
if (Opts.Encoding)
513+
WithOffsetEncoding.emplace(kCurrentOffsetEncoding, *Opts.Encoding);
514+
Server.emplace(*CDB, TFS, Opts,
517515
static_cast<ClangdServer::Callbacks *>(this));
518516
}
519517
applyConfiguration(Params.initializationOptions.ConfigSettings);
520518

521-
CCOpts.EnableSnippets = Params.capabilities.CompletionSnippets;
522-
CCOpts.IncludeFixIts = Params.capabilities.CompletionFixes;
523-
if (!CCOpts.BundleOverloads.hasValue())
524-
CCOpts.BundleOverloads = Params.capabilities.HasSignatureHelp;
525-
CCOpts.DocumentationFormat =
519+
Opts.CodeComplete.EnableSnippets = Params.capabilities.CompletionSnippets;
520+
Opts.CodeComplete.IncludeFixIts = Params.capabilities.CompletionFixes;
521+
if (!Opts.CodeComplete.BundleOverloads.hasValue())
522+
Opts.CodeComplete.BundleOverloads = Params.capabilities.HasSignatureHelp;
523+
Opts.CodeComplete.DocumentationFormat =
526524
Params.capabilities.CompletionDocumentationFormat;
527525
DiagOpts.EmbedFixesInDiagnostics = Params.capabilities.DiagnosticFixes;
528526
DiagOpts.SendDiagnosticCategory = Params.capabilities.DiagnosticCategory;
@@ -622,14 +620,14 @@ void ClangdLSPServer::onInitialize(const InitializeParams &Params,
622620
}},
623621
{"typeHierarchyProvider", true},
624622
}}}};
625-
if (NegotiatedOffsetEncoding)
626-
Result["offsetEncoding"] = *NegotiatedOffsetEncoding;
627-
if (ClangdServerOpts.TheiaSemanticHighlighting)
623+
if (Opts.Encoding)
624+
Result["offsetEncoding"] = *Opts.Encoding;
625+
if (Opts.TheiaSemanticHighlighting)
628626
Result.getObject("capabilities")
629627
->insert(
630628
{"semanticHighlighting",
631629
llvm::json::Object{{"scopes", buildHighlightScopeLookupTable()}}});
632-
if (ClangdServerOpts.FoldingRanges)
630+
if (Opts.FoldingRanges)
633631
Result.getObject("capabilities")->insert({"foldingRangeProvider", true});
634632
Reply(std::move(Result));
635633
}
@@ -788,7 +786,7 @@ void ClangdLSPServer::onWorkspaceSymbol(
788786
const WorkspaceSymbolParams &Params,
789787
Callback<std::vector<SymbolInformation>> Reply) {
790788
Server->workspaceSymbols(
791-
Params.query, CCOpts.Limit,
789+
Params.query, Opts.CodeComplete.Limit,
792790
[Reply = std::move(Reply),
793791
this](llvm::Expected<std::vector<SymbolInformation>> Items) mutable {
794792
if (!Items)
@@ -803,7 +801,7 @@ void ClangdLSPServer::onWorkspaceSymbol(
803801
void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
804802
Callback<llvm::Optional<Range>> Reply) {
805803
Server->prepareRename(Params.textDocument.uri.file(), Params.position,
806-
RenameOpts, std::move(Reply));
804+
Opts.Rename, std::move(Reply));
807805
}
808806

809807
void ClangdLSPServer::onRename(const RenameParams &Params,
@@ -813,7 +811,7 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
813811
return Reply(llvm::make_error<LSPError>(
814812
"onRename called for non-added file", ErrorCode::InvalidParams));
815813
Server->rename(
816-
File, Params.position, Params.newName, RenameOpts,
814+
File, Params.position, Params.newName, Opts.Rename,
817815
[File, Params, Reply = std::move(Reply),
818816
this](llvm::Expected<FileEdits> Edits) mutable {
819817
if (!Edits)
@@ -1009,6 +1007,20 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
10091007
for (const auto &T : *Tweaks)
10101008
Actions.push_back(toCodeAction(T, File, Selection));
10111009

1010+
// If there's exactly one quick-fix, call it "preferred".
1011+
// We never consider refactorings etc as preferred.
1012+
CodeAction *OnlyFix = nullptr;
1013+
for (auto &Action : Actions) {
1014+
if (Action.kind && *Action.kind == CodeAction::QUICKFIX_KIND) {
1015+
if (OnlyFix) {
1016+
OnlyFix->isPreferred = false;
1017+
break;
1018+
}
1019+
Action.isPreferred = true;
1020+
OnlyFix = &Action;
1021+
}
1022+
}
1023+
10121024
if (SupportsCodeAction)
10131025
return Reply(llvm::json::Array(Actions));
10141026
std::vector<Command> Commands;
@@ -1030,15 +1042,16 @@ void ClangdLSPServer::onCompletion(const CompletionParams &Params,
10301042
vlog("ignored auto-triggered completion, preceding char did not match");
10311043
return Reply(CompletionList());
10321044
}
1033-
Server->codeComplete(Params.textDocument.uri.file(), Params.position, CCOpts,
1045+
Server->codeComplete(Params.textDocument.uri.file(), Params.position,
1046+
Opts.CodeComplete,
10341047
[Reply = std::move(Reply),
10351048
this](llvm::Expected<CodeCompleteResult> List) mutable {
10361049
if (!List)
10371050
return Reply(List.takeError());
10381051
CompletionList LSPList;
10391052
LSPList.isIncomplete = List->HasMore;
10401053
for (const auto &R : List->Completions) {
1041-
CompletionItem C = R.render(CCOpts);
1054+
CompletionItem C = R.render(Opts.CodeComplete);
10421055
C.kind = adjustKindToCapability(
10431056
C.kind, SupportedCompletionItemKinds);
10441057
LSPList.items.push_back(std::move(C));
@@ -1224,7 +1237,7 @@ void ClangdLSPServer::onChangeConfiguration(
12241237
void ClangdLSPServer::onReference(const ReferenceParams &Params,
12251238
Callback<std::vector<Location>> Reply) {
12261239
Server->findReferences(Params.textDocument.uri.file(), Params.position,
1227-
CCOpts.Limit,
1240+
Opts.CodeComplete.Limit,
12281241
[Reply = std::move(Reply)](
12291242
llvm::Expected<ReferencesResult> Refs) mutable {
12301243
if (!Refs)
@@ -1340,20 +1353,13 @@ void ClangdLSPServer::onSemanticTokensDelta(
13401353
});
13411354
}
13421355

1343-
ClangdLSPServer::ClangdLSPServer(
1344-
class Transport &Transp, const ThreadsafeFS &TFS,
1345-
const clangd::CodeCompleteOptions &CCOpts,
1346-
const clangd::RenameOptions &RenameOpts,
1347-
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
1348-
llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
1349-
const ClangdServer::Options &Opts)
1356+
ClangdLSPServer::ClangdLSPServer(class Transport &Transp,
1357+
const ThreadsafeFS &TFS,
1358+
const ClangdLSPServer::Options &Opts)
13501359
: BackgroundContext(Context::current().clone()), Transp(Transp),
1351-
MsgHandler(new MessageHandler(*this)), TFS(TFS), CCOpts(CCOpts),
1352-
RenameOpts(RenameOpts), SupportedSymbolKinds(defaultSymbolKinds()),
1353-
SupportedCompletionItemKinds(defaultCompletionItemKinds()),
1354-
UseDirBasedCDB(UseDirBasedCDB),
1355-
CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts),
1356-
NegotiatedOffsetEncoding(ForcedOffsetEncoding) {
1360+
MsgHandler(new MessageHandler(*this)), TFS(TFS),
1361+
SupportedSymbolKinds(defaultSymbolKinds()),
1362+
SupportedCompletionItemKinds(defaultCompletionItemKinds()), Opts(Opts) {
13571363
// clang-format off
13581364
MsgHandler->bind("initialize", &ClangdLSPServer::onInitialize);
13591365
MsgHandler->bind("initialized", &ClangdLSPServer::onInitialized);

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ class SymbolIndex;
3636
/// The server also supports $/cancelRequest (MessageHandler provides this).
3737
class ClangdLSPServer : private ClangdServer::Callbacks {
3838
public:
39-
/// If \p CompileCommandsDir has a value, compile_commands.json will be
40-
/// loaded only from \p CompileCommandsDir. Otherwise, clangd will look
41-
/// for compile_commands.json in all parent directories of each file.
42-
/// If UseDirBasedCDB is false, compile commands are not read from disk.
43-
// FIXME: Clean up signature around CDBs.
39+
struct Options : ClangdServer::Options {
40+
/// Look for compilation databases, rather than using compile commands
41+
/// set via LSP (extensions) only.
42+
bool UseDirBasedCDB = true;
43+
/// A fixed directory to search for a compilation database in.
44+
/// If not set, we search upward from the source file.
45+
llvm::Optional<Path> CompileCommandsDir;
46+
/// The offset-encoding to use, or None to negotiate it over LSP.
47+
llvm::Optional<OffsetEncoding> Encoding;
48+
49+
/// Per-feature options. Generally ClangdServer lets these vary
50+
/// per-request, but LSP allows limited/no customizations.
51+
clangd::CodeCompleteOptions CodeComplete;
52+
clangd::RenameOptions Rename;
53+
};
54+
4455
ClangdLSPServer(Transport &Transp, const ThreadsafeFS &TFS,
45-
const clangd::CodeCompleteOptions &CCOpts,
46-
const clangd::RenameOptions &RenameOpts,
47-
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
48-
llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
49-
const ClangdServer::Options &Opts);
56+
const ClangdLSPServer::Options &Opts);
5057
/// The destructor blocks on any outstanding background tasks.
5158
~ClangdLSPServer();
5259

@@ -227,10 +234,6 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
227234
}
228235

229236
const ThreadsafeFS &TFS;
230-
/// Options used for code completion
231-
clangd::CodeCompleteOptions CCOpts;
232-
/// Options used for rename.
233-
clangd::RenameOptions RenameOpts;
234237
/// Options used for diagnostics.
235238
ClangdDiagnosticOptions DiagOpts;
236239
/// The supported kinds of the client.
@@ -268,14 +271,11 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
268271
// Store of the current versions of the open documents.
269272
DraftStore DraftMgr;
270273

274+
Options Opts;
271275
// The CDB is created by the "initialize" LSP method.
272-
bool UseDirBasedCDB; // FIXME: make this a capability.
273-
llvm::Optional<Path> CompileCommandsDir; // FIXME: merge with capability?
274276
std::unique_ptr<GlobalCompilationDatabase> BaseCDB;
275277
// CDB is BaseCDB plus any commands overridden via LSP extensions.
276278
llvm::Optional<OverlayCDB> CDB;
277-
ClangdServer::Options ClangdServerOpts;
278-
llvm::Optional<OffsetEncoding> NegotiatedOffsetEncoding;
279279
// The ClangdServer is created by the "initialize" LSP method.
280280
llvm::Optional<ClangdServer> Server;
281281
};

clang-tools-extra/clangd/CodeCompletionStrings.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/AST/RawCommentList.h"
1313
#include "clang/Basic/SourceManager.h"
1414
#include "clang/Sema/CodeCompleteConsumer.h"
15+
#include "llvm/Support/JSON.h"
1516
#include <limits>
1617
#include <utility>
1718

@@ -86,7 +87,12 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) {
8687
assert(!Ctx.getSourceManager().isLoadedSourceLocation(RC->getBeginLoc()));
8788
std::string Doc =
8889
RC->getFormattedText(Ctx.getSourceManager(), Ctx.getDiagnostics());
89-
return looksLikeDocComment(Doc) ? Doc : "";
90+
if (!looksLikeDocComment(Doc))
91+
return "";
92+
// Clang requires source to be UTF-8, but doesn't enforce this in comments.
93+
if (!llvm::json::isUTF8(Doc))
94+
Doc = llvm::json::fixUTF8(Doc);
95+
return Doc;
9096
}
9197

9298
void getSignature(const CodeCompletionString &CCS, std::string *Signature,

clang-tools-extra/clangd/Diagnostics.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ void toLSPDiags(
411411
Main.codeActions.emplace();
412412
for (const auto &Fix : D.Fixes)
413413
Main.codeActions->push_back(toCodeAction(Fix, File));
414+
if (Main.codeActions->size() == 1)
415+
Main.codeActions->front().isPreferred = true;
414416
}
415417
if (Opts.SendDiagnosticCategory && !D.Category.empty())
416418
Main.category = D.Category;

clang-tools-extra/clangd/Hover.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ llvm::Optional<StringRef> getterVariableName(const CXXMethodDecl *CMD) {
413413
// If CMD is one of the forms:
414414
// void foo(T arg) { FieldName = arg; }
415415
// R foo(T arg) { FieldName = arg; return *this; }
416+
// void foo(T arg) { FieldName = std::move(arg); }
417+
// R foo(T arg) { FieldName = std::move(arg); return *this; }
416418
// then returns "FieldName"
417419
llvm::Optional<StringRef> setterVariableName(const CXXMethodDecl *CMD) {
418420
assert(CMD->hasBody());
@@ -455,6 +457,18 @@ llvm::Optional<StringRef> setterVariableName(const CXXMethodDecl *CMD) {
455457
} else {
456458
return llvm::None;
457459
}
460+
461+
// Detect the case when the item is moved into the field.
462+
if (auto *CE = llvm::dyn_cast<CallExpr>(RHS->IgnoreCasts())) {
463+
if (CE->getNumArgs() != 1)
464+
return llvm::None;
465+
auto *ND = llvm::dyn_cast<NamedDecl>(CE->getCalleeDecl());
466+
if (!ND || !ND->getIdentifier() || ND->getName() != "move" ||
467+
!ND->isInStdNamespace())
468+
return llvm::None;
469+
RHS = CE->getArg(0);
470+
}
471+
458472
auto *DRE = llvm::dyn_cast<DeclRefExpr>(RHS->IgnoreCasts());
459473
if (!DRE || DRE->getDecl() != Arg)
460474
return llvm::None;

clang-tools-extra/clangd/Protocol.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ llvm::json::Value toJSON(const CodeAction &CA) {
740740
CodeAction["kind"] = *CA.kind;
741741
if (CA.diagnostics)
742742
CodeAction["diagnostics"] = llvm::json::Array(*CA.diagnostics);
743+
if (CA.isPreferred)
744+
CodeAction["isPreferred"] = true;
743745
if (CA.edit)
744746
CodeAction["edit"] = *CA.edit;
745747
if (CA.command)

clang-tools-extra/clangd/Protocol.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,13 @@ struct CodeAction {
952952
/// The diagnostics that this code action resolves.
953953
llvm::Optional<std::vector<Diagnostic>> diagnostics;
954954

955+
/// Marks this as a preferred action. Preferred actions are used by the
956+
/// `auto fix` command and can be targeted by keybindings.
957+
/// A quick fix should be marked preferred if it properly addresses the
958+
/// underlying error. A refactoring should be marked preferred if it is the
959+
/// most reasonable choice of actions to take.
960+
bool isPreferred = false;
961+
955962
/// The workspace edit this code action performs.
956963
llvm::Optional<WorkspaceEdit> edit;
957964

0 commit comments

Comments
 (0)