Skip to content

Commit 36b0516

Browse files
committed
Merge from 'master' to 'sycl-web' (#1)
2 parents 7003df1 + 465dca7 commit 36b0516

File tree

296 files changed

+12554
-2921
lines changed

Some content is hidden

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

296 files changed

+12554
-2921
lines changed

clang-tools-extra/clangd/ClangdLSPServer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ void ClangdLSPServer::onWorkspaceSymbol(
761761
void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
762762
Callback<llvm::Optional<Range>> Reply) {
763763
Server->prepareRename(Params.textDocument.uri.file(), Params.position,
764-
std::move(Reply));
764+
RenameOpts, std::move(Reply));
765765
}
766766

767767
void ClangdLSPServer::onRename(const RenameParams &Params,
@@ -772,8 +772,7 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
772772
return Reply(llvm::make_error<LSPError>(
773773
"onRename called for non-added file", ErrorCode::InvalidParams));
774774
Server->rename(
775-
File, Params.position, Params.newName,
776-
/*WantFormat=*/true,
775+
File, Params.position, Params.newName, RenameOpts,
777776
[File, Params, Reply = std::move(Reply),
778777
this](llvm::Expected<FileEdits> Edits) mutable {
779778
if (!Edits)
@@ -1230,12 +1229,14 @@ void ClangdLSPServer::onDocumentLink(
12301229
ClangdLSPServer::ClangdLSPServer(
12311230
class Transport &Transp, const FileSystemProvider &FSProvider,
12321231
const clangd::CodeCompleteOptions &CCOpts,
1232+
const clangd::RenameOptions &RenameOpts,
12331233
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
12341234
llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
12351235
const ClangdServer::Options &Opts)
12361236
: BackgroundContext(Context::current().clone()), Transp(Transp),
12371237
MsgHandler(new MessageHandler(*this)), FSProvider(FSProvider),
1238-
CCOpts(CCOpts), SupportedSymbolKinds(defaultSymbolKinds()),
1238+
CCOpts(CCOpts), RenameOpts(RenameOpts),
1239+
SupportedSymbolKinds(defaultSymbolKinds()),
12391240
SupportedCompletionItemKinds(defaultCompletionItemKinds()),
12401241
UseDirBasedCDB(UseDirBasedCDB),
12411242
CompileCommandsDir(std::move(CompileCommandsDir)), ClangdServerOpts(Opts),

clang-tools-extra/clangd/ClangdLSPServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
4242
// FIXME: Clean up signature around CDBs.
4343
ClangdLSPServer(Transport &Transp, const FileSystemProvider &FSProvider,
4444
const clangd::CodeCompleteOptions &CCOpts,
45+
const clangd::RenameOptions &RenameOpts,
4546
llvm::Optional<Path> CompileCommandsDir, bool UseDirBasedCDB,
4647
llvm::Optional<OffsetEncoding> ForcedOffsetEncoding,
4748
const ClangdServer::Options &Opts);
@@ -197,6 +198,8 @@ class ClangdLSPServer : private ClangdServer::Callbacks {
197198
const FileSystemProvider &FSProvider;
198199
/// Options used for code completion
199200
clangd::CodeCompleteOptions CCOpts;
201+
/// Options used for rename.
202+
clangd::RenameOptions RenameOpts;
200203
/// Options used for diagnostics.
201204
ClangdDiagnosticOptions DiagOpts;
202205
/// The supported kinds of the client.

clang-tools-extra/clangd/ClangdServer.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ ClangdServer::ClangdServer(const GlobalCompilationDatabase &CDB,
131131
: nullptr),
132132
GetClangTidyOptions(Opts.GetClangTidyOptions),
133133
SuggestMissingIncludes(Opts.SuggestMissingIncludes),
134-
CrossFileRename(Opts.CrossFileRename), TweakFilter(Opts.TweakFilter),
135-
WorkspaceRoot(Opts.WorkspaceRoot),
134+
TweakFilter(Opts.TweakFilter), WorkspaceRoot(Opts.WorkspaceRoot),
136135
// Pass a callback into `WorkScheduler` to extract symbols from a newly
137136
// parsed file and rebuild the file index synchronously each time an AST
138137
// is parsed.
@@ -319,8 +318,9 @@ ClangdServer::formatOnType(llvm::StringRef Code, PathRef File, Position Pos,
319318
}
320319

321320
void ClangdServer::prepareRename(PathRef File, Position Pos,
321+
const RenameOptions &RenameOpts,
322322
Callback<llvm::Optional<Range>> CB) {
323-
auto Action = [Pos, File = File.str(), CB = std::move(CB),
323+
auto Action = [Pos, File = File.str(), CB = std::move(CB), RenameOpts,
324324
this](llvm::Expected<InputsAndAST> InpAST) mutable {
325325
if (!InpAST)
326326
return CB(InpAST.takeError());
@@ -338,14 +338,13 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
338338
SM, CharSourceRange::getCharRange(TouchingIdentifier->location(),
339339
TouchingIdentifier->endLocation()));
340340

341-
if (CrossFileRename)
341+
if (RenameOpts.AllowCrossFile)
342342
// FIXME: we now assume cross-file rename always succeeds, revisit this.
343343
return CB(Range);
344344

345345
// Performing the local rename isn't substantially more expensive than
346346
// doing an AST-based check, so we just rename and throw away the results.
347-
auto Changes = clangd::rename({Pos, "dummy", AST, File, Index,
348-
/*AllowCrossFile=*/false,
347+
auto Changes = clangd::rename({Pos, "dummy", AST, File, Index, RenameOpts,
349348
/*GetDirtyBuffer=*/nullptr});
350349
if (!Changes) {
351350
// LSP says to return null on failure, but that will result in a generic
@@ -359,10 +358,10 @@ void ClangdServer::prepareRename(PathRef File, Position Pos,
359358
}
360359

361360
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
362-
bool WantFormat, Callback<FileEdits> CB) {
361+
const RenameOptions &Opts, Callback<FileEdits> CB) {
363362
// A snapshot of all file dirty buffers.
364363
llvm::StringMap<std::string> Snapshot = WorkScheduler.getAllFileContents();
365-
auto Action = [File = File.str(), NewName = NewName.str(), Pos, WantFormat,
364+
auto Action = [File = File.str(), NewName = NewName.str(), Pos, Opts,
366365
CB = std::move(CB), Snapshot = std::move(Snapshot),
367366
this](llvm::Expected<InputsAndAST> InpAST) mutable {
368367
if (!InpAST)
@@ -374,12 +373,12 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
374373
return llvm::None;
375374
return It->second;
376375
};
377-
auto Edits = clangd::rename({Pos, NewName, InpAST->AST, File, Index,
378-
CrossFileRename, GetDirtyBuffer});
376+
auto Edits = clangd::rename(
377+
{Pos, NewName, InpAST->AST, File, Index, Opts, GetDirtyBuffer});
379378
if (!Edits)
380379
return CB(Edits.takeError());
381380

382-
if (WantFormat) {
381+
if (Opts.WantFormat) {
383382
auto Style = getFormatStyleForFile(File, InpAST->Inputs.Contents,
384383
InpAST->Inputs.FS.get());
385384
llvm::Error Err = llvm::Error::success();
@@ -395,6 +394,14 @@ void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
395394
WorkScheduler.runWithAST("Rename", File, std::move(Action));
396395
}
397396

397+
void ClangdServer::rename(PathRef File, Position Pos, llvm::StringRef NewName,
398+
bool WantFormat, Callback<FileEdits> CB) {
399+
RenameOptions Opts;
400+
Opts.WantFormat = WantFormat;
401+
Opts.AllowCrossFile = false;
402+
rename(File, Pos, NewName, Opts, std::move(CB));
403+
}
404+
398405
// May generate several candidate selections, due to SelectionTree ambiguity.
399406
// vector of pointers because GCC doesn't like non-copyable Selection.
400407
static llvm::Expected<std::vector<std::unique_ptr<Tweak::Selection>>>

clang-tools-extra/clangd/ClangdServer.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ class ClangdServer {
145145
/// Enable semantic highlighting features.
146146
bool SemanticHighlighting = false;
147147

148-
/// Enable cross-file rename feature.
149-
bool CrossFileRename = false;
150-
151148
/// Returns true if the tweak should be enabled.
152149
std::function<bool(const Tweak &)> TweakFilter = [](const Tweak &T) {
153150
return !T.hidden(); // only enable non-hidden tweaks.
@@ -257,13 +254,17 @@ class ClangdServer {
257254

258255
/// Test the validity of a rename operation.
259256
void prepareRename(PathRef File, Position Pos,
257+
const RenameOptions &RenameOpts,
260258
Callback<llvm::Optional<Range>> CB);
261259

262260
/// Rename all occurrences of the symbol at the \p Pos in \p File to
263261
/// \p NewName.
264262
/// If WantFormat is false, the final TextEdit will be not formatted,
265263
/// embedders could use this method to get all occurrences of the symbol (e.g.
266264
/// highlighting them in prepare stage).
265+
void rename(PathRef File, Position Pos, llvm::StringRef NewName,
266+
const RenameOptions &Opts, Callback<FileEdits> CB);
267+
// FIXME: remove this compatibility method in favor above.
267268
void rename(PathRef File, Position Pos, llvm::StringRef NewName,
268269
bool WantFormat, Callback<FileEdits> CB);
269270

@@ -343,8 +344,6 @@ class ClangdServer {
343344
// can be caused by missing includes (e.g. member access in incomplete type).
344345
bool SuggestMissingIncludes = false;
345346

346-
bool CrossFileRename = false;
347-
348347
std::function<bool(const Tweak &)> TweakFilter;
349348

350349
// GUARDED_BY(CachedCompletionFuzzyFindRequestMutex)

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 71 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/ADT/StringExtras.h"
4444
#include "llvm/ADT/StringRef.h"
4545
#include "llvm/Support/Casting.h"
46+
#include "llvm/Support/Error.h"
4647
#include "llvm/Support/Path.h"
4748
#include "llvm/Support/raw_ostream.h"
4849

@@ -214,24 +215,34 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
214215
}
215216
}
216217

218+
auto CurLoc = sourceLocationInMainFile(SM, Pos);
219+
if (!CurLoc) {
220+
elog("locateSymbolAt failed to convert position to source location: {0}",
221+
CurLoc.takeError());
222+
return {};
223+
}
224+
217225
// Macros are simple: there's no declaration/definition distinction.
218226
// As a consequence, there's no need to look them up in the index either.
219-
SourceLocation IdentStartLoc = SM.getMacroArgExpandedLocation(
220-
getBeginningOfIdentifier(Pos, AST.getSourceManager(), AST.getLangOpts()));
221227
std::vector<LocatedSymbol> Result;
222-
if (auto M = locateMacroAt(IdentStartLoc, AST.getPreprocessor())) {
223-
if (auto Loc = makeLocation(AST.getASTContext(),
224-
M->Info->getDefinitionLoc(), *MainFilePath)) {
225-
LocatedSymbol Macro;
226-
Macro.Name = std::string(M->Name);
227-
Macro.PreferredDeclaration = *Loc;
228-
Macro.Definition = Loc;
229-
Result.push_back(std::move(Macro));
230-
231-
// Don't look at the AST or index if we have a macro result.
232-
// (We'd just return declarations referenced from the macro's
233-
// expansion.)
234-
return Result;
228+
const auto *TouchedIdentifier =
229+
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
230+
if (TouchedIdentifier) {
231+
if (auto M = locateMacroAt(TouchedIdentifier->location(),
232+
AST.getPreprocessor())) {
233+
if (auto Loc = makeLocation(AST.getASTContext(),
234+
M->Info->getDefinitionLoc(), *MainFilePath)) {
235+
LocatedSymbol Macro;
236+
Macro.Name = std::string(M->Name);
237+
Macro.PreferredDeclaration = *Loc;
238+
Macro.Definition = Loc;
239+
Result.push_back(std::move(Macro));
240+
241+
// Don't look at the AST or index if we have a macro result.
242+
// (We'd just return declarations referenced from the macro's
243+
// expansion.)
244+
return Result;
245+
}
235246
}
236247
}
237248

@@ -244,15 +255,6 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
244255
// Keep track of SymbolID -> index mapping, to fill in index data later.
245256
llvm::DenseMap<SymbolID, size_t> ResultIndex;
246257

247-
SourceLocation SourceLoc;
248-
if (auto L = sourceLocationInMainFile(SM, Pos)) {
249-
SourceLoc = *L;
250-
} else {
251-
elog("locateSymbolAt failed to convert position to source location: {0}",
252-
L.takeError());
253-
return Result;
254-
}
255-
256258
auto AddResultDecl = [&](const NamedDecl *D) {
257259
const NamedDecl *Def = getDefinition(D);
258260
const NamedDecl *Preferred = Def ? Def : D;
@@ -277,16 +279,15 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
277279
// Emit all symbol locations (declaration or definition) from AST.
278280
DeclRelationSet Relations =
279281
DeclRelation::TemplatePattern | DeclRelation::Alias;
280-
for (const NamedDecl *D : getDeclAtPosition(AST, SourceLoc, Relations)) {
282+
for (const NamedDecl *D : getDeclAtPosition(AST, *CurLoc, Relations)) {
281283
// Special case: void foo() ^override: jump to the overridden method.
282284
if (const auto *CMD = llvm::dyn_cast<CXXMethodDecl>(D)) {
283-
const InheritableAttr* Attr = D->getAttr<OverrideAttr>();
285+
const InheritableAttr *Attr = D->getAttr<OverrideAttr>();
284286
if (!Attr)
285287
Attr = D->getAttr<FinalAttr>();
286-
const syntax::Token *Tok =
287-
spelledIdentifierTouching(SourceLoc, AST.getTokens());
288-
if (Attr && Tok &&
289-
SM.getSpellingLoc(Attr->getLocation()) == Tok->location()) {
288+
if (Attr && TouchedIdentifier &&
289+
SM.getSpellingLoc(Attr->getLocation()) ==
290+
TouchedIdentifier->location()) {
290291
// We may be overridding multiple methods - offer them all.
291292
for (const NamedDecl *ND : CMD->overridden_methods())
292293
AddResultDecl(ND);
@@ -296,8 +297,9 @@ std::vector<LocatedSymbol> locateSymbolAt(ParsedAST &AST, Position Pos,
296297

297298
// Special case: the point of declaration of a template specialization,
298299
// it's more useful to navigate to the template declaration.
299-
if (SM.getMacroArgExpandedLocation(D->getLocation()) == IdentStartLoc) {
300-
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
300+
if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
301+
if (TouchedIdentifier &&
302+
D->getLocation() == TouchedIdentifier->location()) {
301303
AddResultDecl(CTSD->getSpecializedTemplate());
302304
continue;
303305
}
@@ -416,12 +418,12 @@ std::vector<DocumentHighlight> findDocumentHighlights(ParsedAST &AST,
416418
// FIXME: show references to macro within file?
417419
DeclRelationSet Relations =
418420
DeclRelation::TemplatePattern | DeclRelation::Alias;
419-
auto References = findRefs(
420-
getDeclAtPosition(AST,
421-
SM.getMacroArgExpandedLocation(getBeginningOfIdentifier(
422-
Pos, SM, AST.getLangOpts())),
423-
Relations),
424-
AST);
421+
auto CurLoc = sourceLocationInMainFile(SM, Pos);
422+
if (!CurLoc) {
423+
llvm::consumeError(CurLoc.takeError());
424+
return {};
425+
}
426+
auto References = findRefs(getDeclAtPosition(AST, *CurLoc, Relations), AST);
425427

426428
// FIXME: we may get multiple DocumentHighlights with the same location and
427429
// different kinds, deduplicate them.
@@ -456,11 +458,18 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
456458
return Results;
457459
}
458460
auto URIMainFile = URIForFile::canonicalize(*MainFilePath, *MainFilePath);
459-
auto Loc = SM.getMacroArgExpandedLocation(
460-
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
461+
auto CurLoc = sourceLocationInMainFile(SM, Pos);
462+
if (!CurLoc) {
463+
llvm::consumeError(CurLoc.takeError());
464+
return {};
465+
}
466+
SourceLocation SLocId;
467+
if (const auto *IdentifierAtCursor =
468+
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens()))
469+
SLocId = IdentifierAtCursor->location();
461470
RefsRequest Req;
462471

463-
if (auto Macro = locateMacroAt(Loc, AST.getPreprocessor())) {
472+
if (auto Macro = locateMacroAt(SLocId, AST.getPreprocessor())) {
464473
// Handle references to macro.
465474
if (auto MacroSID = getSymbolID(Macro->Name, Macro->Info, SM)) {
466475
// Collect macro references from main file.
@@ -483,7 +492,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
483492
// DeclRelation::Underlying.
484493
DeclRelationSet Relations = DeclRelation::TemplatePattern |
485494
DeclRelation::Alias | DeclRelation::Underlying;
486-
auto Decls = getDeclAtPosition(AST, Loc, Relations);
495+
auto Decls = getDeclAtPosition(AST, *CurLoc, Relations);
487496

488497
// We traverse the AST to find references in the main file.
489498
auto MainFileRefs = findRefs(Decls, AST);
@@ -541,16 +550,19 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
541550

542551
std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
543552
const SourceManager &SM = AST.getSourceManager();
544-
auto Loc = SM.getMacroArgExpandedLocation(
545-
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
553+
auto CurLoc = sourceLocationInMainFile(SM, Pos);
554+
if (!CurLoc) {
555+
llvm::consumeError(CurLoc.takeError());
556+
return {};
557+
}
546558

547559
std::vector<SymbolDetails> Results;
548560

549561
// We also want the targets of using-decls, so we include
550562
// DeclRelation::Underlying.
551563
DeclRelationSet Relations = DeclRelation::TemplatePattern |
552564
DeclRelation::Alias | DeclRelation::Underlying;
553-
for (const NamedDecl *D : getDeclAtPosition(AST, Loc, Relations)) {
565+
for (const NamedDecl *D : getDeclAtPosition(AST, *CurLoc, Relations)) {
554566
SymbolDetails NewSymbol;
555567
std::string QName = printQualifiedName(*D);
556568
auto SplitQName = splitQualifiedName(QName);
@@ -570,7 +582,13 @@ std::vector<SymbolDetails> getSymbolInfo(ParsedAST &AST, Position Pos) {
570582
Results.push_back(std::move(NewSymbol));
571583
}
572584

573-
if (auto M = locateMacroAt(Loc, AST.getPreprocessor())) {
585+
const auto *IdentifierAtCursor =
586+
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
587+
if (!IdentifierAtCursor)
588+
return Results;
589+
590+
if (auto M = locateMacroAt(IdentifierAtCursor->location(),
591+
AST.getPreprocessor())) {
574592
SymbolDetails NewMacro;
575593
NewMacro.name = std::string(M->Name);
576594
llvm::SmallString<32> USR;
@@ -747,17 +765,18 @@ const CXXRecordDecl *findRecordTypeAt(ParsedAST &AST, Position Pos) {
747765
};
748766

749767
const SourceManager &SM = AST.getSourceManager();
750-
SourceLocation SourceLocationBeg = SM.getMacroArgExpandedLocation(
751-
getBeginningOfIdentifier(Pos, SM, AST.getLangOpts()));
752-
unsigned Offset = SM.getDecomposedSpellingLoc(SourceLocationBeg).second;
753768
const CXXRecordDecl *Result = nullptr;
754-
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), Offset,
755-
Offset, [&](SelectionTree ST) {
769+
auto Offset = positionToOffset(SM.getBufferData(SM.getMainFileID()), Pos);
770+
if (!Offset) {
771+
llvm::consumeError(Offset.takeError());
772+
return Result;
773+
}
774+
SelectionTree::createEach(AST.getASTContext(), AST.getTokens(), *Offset,
775+
*Offset, [&](SelectionTree ST) {
756776
Result = RecordFromNode(ST.commonAncestor());
757777
return Result != nullptr;
758778
});
759779
return Result;
760-
761780
}
762781

763782
std::vector<const CXXRecordDecl *> typeParents(const CXXRecordDecl *CXXRD) {

0 commit comments

Comments
 (0)