RequestResult class and Pass*InfoForDecl fixes

This commit is contained in:
David Goldman
2019-05-03 15:17:40 -04:00
parent a3a4d2d22b
commit 00d771ca6b
10 changed files with 352 additions and 285 deletions

View File

@@ -302,6 +302,55 @@ public:
struct Statistic;
typedef std::function<void(ArrayRef<Statistic *> stats)> StatisticsReceiver;
// Used to wrap the result of a request. There are three possibilities:
// - The request succeeded (`value` is valid)
// - The request was cancelled
// - The request failed (with an `error`)
template <typename T>
class RequestResult {
private:
enum Type {
Value,
Error,
Cancelled
};
union {
const T *data;
StringRef error;
};
RequestResult::Type type;
RequestResult(const T &V): data(&V), type(Value) {}
RequestResult(StringRef E): error(E), type(Error) {}
RequestResult(): type(Cancelled) {}
public:
static RequestResult fromResult(const T &value) {
return RequestResult(value);
}
static RequestResult fromError(StringRef error) {
return RequestResult(error);
}
static RequestResult cancelled() {
return RequestResult();
}
const T &value() const {
assert(type == Value);
return *data;
}
bool isError() const {
return type == Error;
}
StringRef getError() const {
assert(type == Error);
return error;
}
bool isCancelled() const {
return type == Cancelled;
}
};
struct RefactoringInfo {
UIdent Kind;
StringRef KindName;
@@ -309,7 +358,6 @@ struct RefactoringInfo {
};
struct CursorInfoData {
bool IsCancelled = false;
// If nonempty, a proper Info could not be resolved (and the rest of the Info
// will be empty). Clients can potentially use this to show a diagnostic
// message to the user in lieu of using the empty response.
@@ -355,14 +403,12 @@ struct CursorInfoData {
};
struct RangeInfo {
bool IsCancelled = false;
UIdent RangeKind;
StringRef ExprType;
StringRef RangeContent;
};
struct NameTranslatingInfo {
bool IsCancelled = false;
// If nonempty, a proper Info could not be resolved (and the rest of the Info
// will be empty). Clients can potentially use this to show a diagnostic
// message to the user in lieu of using the empty response.
@@ -389,7 +435,6 @@ struct SemanticRefactoringInfo {
};
struct RelatedIdentsInfo {
bool IsCancelled = false;
/// (Offset,Length) pairs.
ArrayRef<std::pair<unsigned, unsigned>> Ranges;
};
@@ -500,10 +545,9 @@ struct RenameLocations {
std::vector<RenameLocation> LineColumnLocs;
};
typedef std::function<void(ArrayRef<CategorizedEdits> Edits,
StringRef Error)> CategorizedEditsReceiver;
typedef std::function<void(ArrayRef<CategorizedRenameRanges> Edits,
StringRef Error)>
typedef std::function<void(RequestResult<ArrayRef<CategorizedEdits>> Result)>
CategorizedEditsReceiver;
typedef std::function<void(RequestResult<ArrayRef<CategorizedRenameRanges>> Result)>
CategorizedRenameRangesReceiver;
class DocInfoConsumer {
@@ -674,48 +718,41 @@ public:
unsigned Length, bool Actionables,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const CursorInfoData &,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) = 0;
virtual void getNameInfo(StringRef Filename, unsigned Offset,
NameTranslatingInfo &Input,
ArrayRef<const char *> Args,
std::function<void(const NameTranslatingInfo &,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) = 0;
virtual void getRangeInfo(StringRef Filename, unsigned Offset, unsigned Length,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const RangeInfo&,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<RangeInfo> &)> Receiver) = 0;
virtual void
getCursorInfoFromUSR(StringRef Filename, StringRef USR,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const CursorInfoData &,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) = 0;
virtual void findRelatedIdentifiersInFile(StringRef Filename,
unsigned Offset,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const RelatedIdentsInfo &,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver) = 0;
virtual llvm::Optional<std::pair<unsigned, unsigned>>
findUSRRange(StringRef DocumentName, StringRef USR) = 0;
virtual void findInterfaceDocument(StringRef ModuleName,
ArrayRef<const char *> Args,
std::function<void(const InterfaceDocInfo &,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<InterfaceDocInfo> &)> Receiver) = 0;
virtual void findModuleGroups(StringRef ModuleName,
ArrayRef<const char *> Args,
std::function<void(ArrayRef<StringRef>,
StringRef Error)> Receiver) = 0;
std::function<void(const RequestResult<ArrayRef<StringRef>> &)> Receiver) = 0;
virtual void syntacticRename(llvm::MemoryBuffer *InputBuf,
ArrayRef<RenameLocations> RenameLocations,
@@ -738,8 +775,8 @@ public:
virtual void collectExpressionTypes(StringRef FileName,
ArrayRef<const char *> Args,
ArrayRef<const char *> ExpectedProtocols,
std::function<void(const ExpressionTypesInFile&,
StringRef Error)> Receiver) = 0;
std::function<void(const
RequestResult<ExpressionTypesInFile> &)> Receiver) = 0;
virtual void getDocInfo(llvm::MemoryBuffer *InputBuf,
StringRef ModuleName,

View File

@@ -1127,7 +1127,7 @@ public:
Receiver(std::move(Receiver)), OS(ErrBuffer), DiagConsumer(OS) {}
~Implementation() {
if (DiagConsumer.didErrorOccur()) {
Receiver({}, OS.str());
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(OS.str()));
return;
}
assert(UIds.size() == StartEnds.size());
@@ -1138,7 +1138,7 @@ public:
llvm::makeArrayRef(AllEdits.data() + Pair.first,
Pair.second - Pair.first)});
}
Receiver(Results, "");
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromResult(Results));
}
void accept(SourceManager &SM, RegionType RegionType,
ArrayRef<Replacement> Replacements) {
@@ -1205,10 +1205,10 @@ public:
~Implementation() {
if (DiagConsumer.didErrorOccur()) {
Receiver({}, OS.str());
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromError(OS.str()));
return;
}
Receiver(CategorizedRanges, "");
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromResult(CategorizedRanges));
}
void accept(SourceManager &SM, RegionType RegionType,
@@ -1277,7 +1277,7 @@ syntacticRename(llvm::MemoryBuffer *InputBuf,
ParseCI.addDiagnosticConsumer(&PrintDiags);
SourceFile *SF = getSyntacticSourceFile(InputBuf, Args, ParseCI, Error);
if (!SF) {
Receiver({}, Error);
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(Error));
return;
}
@@ -1295,7 +1295,7 @@ void SwiftLangSupport::findRenameRanges(
ParseCI.addDiagnosticConsumer(&PrintDiags);
SourceFile *SF = getSyntacticSourceFile(InputBuf, Args, ParseCI, Error);
if (!SF) {
Receiver({}, Error);
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromError(Error));
return;
}
@@ -1310,9 +1310,8 @@ void SwiftLangSupport::findLocalRenameRanges(
std::string Error;
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, Filename, Error);
if (!Invok) {
// FIXME: Report it as failed request.
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver({}, Error);
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromError(Error));
return;
}
@@ -1332,9 +1331,13 @@ void SwiftLangSupport::findLocalRenameRanges(
swift::ide::findLocalRenameRanges(&SF, Range, Consumer, Consumer);
}
void cancelled() override { Receiver({}, "The refactoring is canceled."); }
void cancelled() override {
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::cancelled());
}
void failed(StringRef Error) override { Receiver({}, Error); }
void failed(StringRef Error) override {
Receiver(RequestResult<ArrayRef<CategorizedRenameRanges>>::fromError(Error));
}
};
auto ASTConsumer = std::make_shared<LocalRenameRangeASTConsumer>(
@@ -1429,8 +1432,7 @@ void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf,
void SwiftLangSupport::
findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
std::function<void(ArrayRef<StringRef>,
StringRef Error)> Receiver) {
std::function<void(const RequestResult<ArrayRef<StringRef>> &)> Receiver) {
CompilerInvocation Invocation;
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
Invocation.getFrontendOptions().InputsAndOutputs.clearInputs();
@@ -1443,12 +1445,12 @@ findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
std::string Error;
if (getASTManager()->initCompilerInvocationNoInputs(Invocation, Args,
CI.getDiags(), Error)) {
Receiver(Groups, Error);
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
return;
}
if (CI.setup(Invocation)) {
Error = "Compiler invocation set up fails.";
Receiver(Groups, Error);
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
return;
}
@@ -1460,15 +1462,16 @@ findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
if (!Stdlib) {
Error = "Cannot load stdlib.";
Receiver(Groups, Error);
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
return;
}
auto *M = getModuleByFullName(Ctx, ModuleName);
if (!M) {
Error = "Cannot find the module.";
Receiver(Groups, Error);
Receiver(RequestResult<ArrayRef<StringRef>>::fromError(Error));
return;
}
std::vector<StringRef> Scratch;
Receiver(collectModuleGroups(M, Scratch), Error);
Receiver(RequestResult<ArrayRef<StringRef>>::fromResult(
collectModuleGroups(M, Scratch)));
}

View File

@@ -834,8 +834,7 @@ void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
void SwiftLangSupport::findInterfaceDocument(StringRef ModuleName,
ArrayRef<const char *> Args,
std::function<void(const InterfaceDocInfo &,
StringRef Error)> Receiver) {
std::function<void(const RequestResult<InterfaceDocInfo> &)> Receiver) {
InterfaceDocInfo Info;
CompilerInstance CI;
@@ -847,7 +846,7 @@ void SwiftLangSupport::findInterfaceDocument(StringRef ModuleName,
std::string Error;
if (getASTManager()->initCompilerInvocation(Invocation, Args, CI.getDiags(),
StringRef(), Error)) {
return Receiver({}, Error);
return Receiver(RequestResult<InterfaceDocInfo>::fromError(Error));
}
if (auto IFaceGenRef = IFaceGenContexts.find(ModuleName, Invocation))
@@ -905,5 +904,5 @@ void SwiftLangSupport::findInterfaceDocument(StringRef ModuleName,
}
Info.CompilerArgs = NewArgs;
return Receiver(Info, "");
return Receiver(RequestResult<InterfaceDocInfo>::fromResult(Info));
}

View File

@@ -490,31 +490,26 @@ public:
unsigned Length, bool Actionables,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const CursorInfoData &,
StringRef Error)> Receiver) override;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) override;
void getNameInfo(StringRef Filename, unsigned Offset,
NameTranslatingInfo &Input,
ArrayRef<const char *> Args,
std::function<void(const NameTranslatingInfo &,
StringRef Error)> Receiver) override;
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) override;
void getRangeInfo(StringRef Filename, unsigned Offset, unsigned Length,
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
std::function<void(const RangeInfo&,
StringRef Error)> Receiver) override;
std::function<void(const RequestResult<RangeInfo> &)> Receiver) override;
void getCursorInfoFromUSR(
StringRef Filename, StringRef USR, bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const CursorInfoData &,
StringRef Errro)> Receiver) override;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) override;
void findRelatedIdentifiersInFile(StringRef Filename, unsigned Offset,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const RelatedIdentsInfo &,
StringRef Errror)> Receiver) override;
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver) override;
void syntacticRename(llvm::MemoryBuffer *InputBuf,
ArrayRef<RenameLocations> RenameLocations,
@@ -532,8 +527,7 @@ public:
void collectExpressionTypes(StringRef FileName, ArrayRef<const char *> Args,
ArrayRef<const char *> ExpectedProtocols,
std::function<void(const ExpressionTypesInFile&,
StringRef Errror)> Receiver) override;
std::function<void(const RequestResult<ExpressionTypesInFile> &)> Receiver) override;
void semanticRefactoring(StringRef Filename, SemanticRefactoringInfo Info,
ArrayRef<const char*> Args,
@@ -548,12 +542,10 @@ public:
findUSRRange(StringRef DocumentName, StringRef USR) override;
void findInterfaceDocument(StringRef ModuleName, ArrayRef<const char *> Args,
std::function<void(const InterfaceDocInfo &,
StringRef Error)> Receiver) override;
std::function<void(const RequestResult<InterfaceDocInfo> &)> Receiver) override;
void findModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args,
std::function<void(ArrayRef<StringRef>,
StringRef Error)> Receiver) override;
std::function<void(const RequestResult<ArrayRef<StringRef>> &)> Receiver) override;
void getExpressionContextInfo(llvm::MemoryBuffer *inputBuf, unsigned Offset,
ArrayRef<const char *> Args,

View File

@@ -604,7 +604,7 @@ tryRemappingLocToLatestSnapshot(SwiftLangSupport &Lang,
static bool passCursorInfoForModule(ModuleEntity Mod,
SwiftInterfaceGenMap &IFaceGenContexts,
const CompilerInvocation &Invok,
std::function<void(const CursorInfoData &, StringRef error)> Receiver) {
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
std::string Name = Mod.getName();
std::string FullName = Mod.getFullName();
CursorInfoData Info;
@@ -619,7 +619,7 @@ static bool passCursorInfoForModule(ModuleEntity Mod,
Info.ModuleGroupArray = ide::collectModuleGroups(const_cast<ModuleDecl*>(MD),
Groups);
}
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return false;
}
@@ -697,8 +697,8 @@ getParamParentNameOffset(const ValueDecl *VD, SourceLoc Cursor) {
return SM.getLocOffsetInBuffer(Loc, SM.findBufferContainingLoc(Loc));
}
/// Returns a non-empty StringRef on error, representing an internal diagnostic.
static StringRef passCursorInfoForDecl(SourceFile* SF,
/// Returns true on success, false on error (and sets `Diagnostic` accordingly).
static bool passCursorInfoForDecl(SourceFile* SF,
const ValueDecl *VD,
const ModuleDecl *MainModule,
const Type ContainerTy,
@@ -710,11 +710,13 @@ static StringRef passCursorInfoForDecl(SourceFile* SF,
ArrayRef<RefactoringInfo> KownRefactoringInfoFromRange,
SwiftLangSupport &Lang,
const CompilerInvocation &Invok,
std::string &Diagnostic,
ArrayRef<ImmutableTextSnapshotRef> PreviousASTSnaps,
std::function<void(const CursorInfoData &,
StringRef Error)> Receiver) {
if (AvailableAttr::isUnavailable(VD))
return "Unavailable in the current compilation context.";
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
if (AvailableAttr::isUnavailable(VD)) {
Diagnostic = "Unavailable in the current compilation context.";
return false;
}
SmallString<64> SS;
auto BaseType = findBaseTypeForReplacingArchetype(VD, ContainerTy);
@@ -920,8 +922,10 @@ static StringRef passCursorInfoForDecl(SourceFile* SF,
*DeclarationLoc,
Filename,
PreviousASTSnaps);
if (!DeclarationLoc.hasValue())
return "Failed to remap declaration to latest snapshot.";
if (!DeclarationLoc.hasValue()) {
Diagnostic = "Failed to remap declaration to latest snapshot.";
return false;
}
}
SmallVector<StringRef, 4> OverUSRs;
@@ -966,8 +970,8 @@ static StringRef passCursorInfoForDecl(SourceFile* SF,
Info.TypeInterface = StringRef();
Info.AvailableActions = llvm::makeArrayRef(RefactoringInfoBuffer);
Info.ParentNameOffset = getParamParentNameOffset(VD, CursorLoc);
Receiver(Info, "");
return "";
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return true;
}
static clang::DeclarationName
@@ -1035,11 +1039,11 @@ static DeclName getSwiftDeclName(const ValueDecl *VD,
return DeclName(Ctx, BaseName, llvm::makeArrayRef(Args));
}
/// Returns a non-empty StringRef on error, representing an internal diagnostic.
static StringRef passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
/// Returns true on success, false on error (and sets `Diagnostic` accordingly).
static bool passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
NameTranslatingInfo &Info,
std::function<void(const NameTranslatingInfo &,
StringRef Error)> Receiver) {
std::string &Diagnostic,
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
auto *VD = CursorInfo.ValueD;
// If the given name is not a function name, and the cursor points to
@@ -1054,15 +1058,17 @@ static StringRef passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
case NameKind::Swift: {
NameTranslatingInfo Result;
auto DeclName = getSwiftDeclName(VD, Info);
if (!DeclName)
return "Unable to resolve Swift declaration name.";
if (!DeclName) {
Diagnostic = "Unable to resolve Swift declaration name.";
return false;
}
auto ResultPair =
swift::objc_translation::getObjCNameForSwiftDecl(VD, DeclName);
Identifier Name = ResultPair.first;
if (!Name.empty()) {
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::ObjC);
Result.BaseName = Name.str();
Receiver(Result, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
} else if (ObjCSelector Selector = ResultPair.second) {
Result.NameKind = SwiftLangSupport::getUIDForNameKind(NameKind::ObjC);
SmallString<64> Buffer;
@@ -1076,11 +1082,12 @@ static StringRef passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
Result.IsZeroArgSelector = true;
}
Result.ArgNames.insert(Result.ArgNames.begin(), Pieces.begin(), Pieces.end());
Receiver(Result, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
} else {
return "Unable to resolve name info.";
Diagnostic = "Unable to resolve name info.";
return false;
}
return "";
return true;
}
case NameKind::ObjC: {
ClangImporter *Importer = static_cast<ClangImporter *>(VD->getDeclContext()->
@@ -1093,12 +1100,16 @@ static StringRef passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
Named = dyn_cast_or_null<clang::NamedDecl>(BaseDecl->getClangDecl());
BaseDecl = BaseDecl->getOverriddenDecl();
}
if (!Named)
return "Unable to resolve a named declaration.";
if (!Named) {
Diagnostic = "Unable to resolve a named declaration.";
return false;
}
auto ObjCName = getClangDeclarationName(Named, Info);
if (!ObjCName)
return "Unable to resolve ObjC declaration name.";
if (!ObjCName) {
Diagnostic = "Unable to resolve ObjC declaration name.";
return false;
}
DeclName Name = Importer->importName(Named, ObjCName);
NameTranslatingInfo Result;
@@ -1108,8 +1119,8 @@ static StringRef passNameInfoForDecl(ResolvedCursorInfo CursorInfo,
Name.getArgumentNames().end(),
std::back_inserter(Result.ArgNames),
[](Identifier Id) { return Id.str(); });
Receiver(Result, "");
return "";
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Result));
return true;
}
}
}
@@ -1201,12 +1212,12 @@ static void resolveCursor(SwiftLangSupport &Lang,
SwiftInvocationRef Invok,
bool TryExistingAST,
bool CancelOnSubsequentRequest,
std::function<void(const CursorInfoData &, StringRef)> Receiver) {
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
assert(Invok);
class CursorInfoConsumer : public CursorRangeInfoConsumer {
bool Actionables;
std::function<void(const CursorInfoData &, StringRef)> Receiver;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver;
public:
CursorInfoConsumer(StringRef InputFile, unsigned Offset,
@@ -1215,7 +1226,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
SwiftInvocationRef ASTInvok,
bool TryExistingAST,
bool CancelOnSubsequentRequest,
std::function<void(const CursorInfoData &, StringRef)> Receiver)
std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
: CursorRangeInfoConsumer(InputFile, Offset, Length, Lang, ASTInvok,
TryExistingAST, CancelOnSubsequentRequest),
Actionables(Actionables),
@@ -1229,7 +1240,8 @@ static void resolveCursor(SwiftLangSupport &Lang,
SourceLoc Loc =
Lexer::getLocForStartOfToken(SM, BufferID, Offset);
if (Loc.isInvalid()) {
Receiver(CursorInfoData(), "Unable to resolve the start of the token.");
Receiver(RequestResult<CursorInfoData>::fromError(
"Unable to resolve the start of the token."));
return;
}
@@ -1273,7 +1285,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
// If Length is given, then the cursor-info request should only about
// collecting available refactorings for the range.
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
// If the range start may need rename, we fall back to a regular cursor
@@ -1285,7 +1297,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
if (CursorInfo.isInvalid()) {
CursorInfoData Info;
Info.InternalDiagnostic = "Unable to resolve cursor info.";
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
CompilerInvocation CompInvok;
@@ -1305,7 +1317,8 @@ static void resolveCursor(SwiftLangSupport &Lang,
VD = CursorInfo.CtorTyRef;
ContainerType = Type();
}
StringRef Diag = passCursorInfoForDecl(&AstUnit->getPrimarySourceFile(),
std::string Diagnostic;
bool Success = passCursorInfoForDecl(&AstUnit->getPrimarySourceFile(),
VD, MainModule,
ContainerType,
CursorInfo.IsRef,
@@ -1313,10 +1326,10 @@ static void resolveCursor(SwiftLangSupport &Lang,
CursorInfo,
BufferID, Loc,
AvailableRefactorings,
Lang, CompInvok,
Lang, CompInvok, Diagnostic,
getPreviousASTSnaps(),
Receiver);
if (!Diag.empty()) {
if (!Success) {
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
resolveCursor(Lang, InputFile, Offset, Length, Actionables, ASTInvok,
@@ -1324,8 +1337,8 @@ static void resolveCursor(SwiftLangSupport &Lang,
Receiver);
} else {
CursorInfoData Info;
Info.InternalDiagnostic = Diag;
Receiver(Info, "");
Info.InternalDiagnostic = Diagnostic;
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
}
return;
@@ -1350,7 +1363,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
NameRetriever[I], ReasonRetriever[I]});
}
Info.AvailableActions = llvm::makeArrayRef(AvailableRefactorings);
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
}
@@ -1358,7 +1371,7 @@ static void resolveCursor(SwiftLangSupport &Lang,
CursorInfoData Info;
Info.InternalDiagnostic =
"Resolved to incomplete expression or statement.";
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
case CursorInfoKind::Invalid: {
@@ -1368,14 +1381,12 @@ static void resolveCursor(SwiftLangSupport &Lang,
}
void cancelled() override {
CursorInfoData Info;
Info.IsCancelled = true;
Receiver(Info, "Cancelled.");
Receiver(RequestResult<CursorInfoData>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("cursor info failed: " << Error);
Receiver(CursorInfoData(), Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
}
};
@@ -1397,20 +1408,18 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
unsigned Offset, SwiftInvocationRef Invok,
bool TryExistingAST,
NameTranslatingInfo &Input,
std::function<void(const NameTranslatingInfo &,
StringRef Error)> Receiver) {
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
assert(Invok);
class NameInfoConsumer : public CursorRangeInfoConsumer {
NameTranslatingInfo Input;
std::function<void(const NameTranslatingInfo &, StringRef Error)> Receiver;
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver;
public:
NameInfoConsumer(StringRef InputFile, unsigned Offset,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, NameTranslatingInfo Input,
std::function<void(const NameTranslatingInfo &,
StringRef Error)> Receiver)
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver)
: CursorRangeInfoConsumer(InputFile, Offset, 0, Lang, ASTInvok,
TryExistingAST,
/*CancelOnSubsequentRequest=*/false),
@@ -1423,7 +1432,8 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
SourceLoc Loc =
Lexer::getLocForStartOfToken(CompIns.getSourceMgr(), BufferID, Offset);
if (Loc.isInvalid()) {
Receiver({}, "Unable to resolve the start of the token.");
Receiver(RequestResult<NameTranslatingInfo>::fromError(
"Unable to resolve the start of the token."));
return;
}
@@ -1432,7 +1442,7 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
if (CursorInfo.isInvalid()) {
NameTranslatingInfo Info;
Info.InternalDiagnostic = "Unable to resolve cursor info.";
Receiver(Info, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
return;
}
@@ -1444,8 +1454,10 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
return;
case CursorInfoKind::ValueRef: {
StringRef Diagnostic = passNameInfoForDecl(CursorInfo, Input, Receiver);
if (!Diagnostic.empty()) {
std::string Diagnostic;
bool Success = passNameInfoForDecl(CursorInfo, Input, Diagnostic,
Receiver);
if (!Success) {
if (!getPreviousASTSnaps().empty()) {
// Attempt again using the up-to-date AST.
resolveName(Lang, InputFile, Offset, ASTInvok,
@@ -1453,7 +1465,7 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
} else {
NameTranslatingInfo Info;
Info.InternalDiagnostic = Diagnostic;
Receiver(Info, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
}
}
return;
@@ -1463,7 +1475,7 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
NameTranslatingInfo Info;
Info.InternalDiagnostic =
"Resolved to incomplete expression or statement.";
Receiver(Info, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
return;
}
case CursorInfoKind::Invalid:
@@ -1472,14 +1484,12 @@ static void resolveName(SwiftLangSupport &Lang, StringRef InputFile,
}
void cancelled() override {
NameTranslatingInfo Info;
Info.IsCancelled = true;
Receiver(Info, "Cancelled.");
Receiver(RequestResult<NameTranslatingInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("name info failed: " << Error);
Receiver({}, Error);
Receiver(RequestResult<NameTranslatingInfo>::fromError(Error));
}
};
@@ -1493,19 +1503,17 @@ static void resolveRange(SwiftLangSupport &Lang,
StringRef InputFile, unsigned Offset, unsigned Length,
SwiftInvocationRef Invok,
bool TryExistingAST, bool CancelOnSubsequentRequest,
std::function<void(const RangeInfo&,
StringRef Error)> Receiver) {
std::function<void(const RequestResult<RangeInfo> &)> Receiver) {
assert(Invok);
class RangeInfoConsumer : public CursorRangeInfoConsumer {
std::function<void(const RangeInfo&, StringRef Error)> Receiver;
std::function<void(const RequestResult<RangeInfo> &)> Receiver;
public:
RangeInfoConsumer(StringRef InputFile, unsigned Offset, unsigned Length,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, bool CancelOnSubsequentRequest,
std::function<void(const RangeInfo&,
StringRef Error)> Receiver)
std::function<void(const RequestResult<RangeInfo> &)> Receiver)
: CursorRangeInfoConsumer(InputFile, Offset, Length, Lang, ASTInvok,
TryExistingAST, CancelOnSubsequentRequest),
Receiver(std::move(Receiver)){ }
@@ -1533,14 +1541,14 @@ static void resolveRange(SwiftLangSupport &Lang,
llvm::raw_svector_ostream OS(SS);
Info.ExitInfo.ReturnType->print(OS);
Result.ExprType = OS.str();
Receiver(Result, "");
Receiver(RequestResult<RangeInfo>::fromResult(Result));
return;
}
case RangeKind::SingleDecl:
case RangeKind::MultiTypeMemberDecl:
case RangeKind::MultiStatement:
case RangeKind::SingleStatement: {
Receiver(Result, "");
Receiver(RequestResult<RangeInfo>::fromResult(Result));
return;
}
case RangeKind::PartOfExpression:
@@ -1551,21 +1559,19 @@ static void resolveRange(SwiftLangSupport &Lang,
/*TryExistingAST=*/false, CancelOnSubsequentRequest,
Receiver);
} else {
Receiver(Result, "");
Receiver(RequestResult<RangeInfo>::fromResult(Result));
}
return;
}
}
void cancelled() override {
RangeInfo Info;
Info.IsCancelled = true;
Receiver(Info, "Cancelled.");
Receiver(RequestResult<RangeInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("range info failed: " << Error);
Receiver({}, Error);
Receiver(RequestResult<RangeInfo>::fromError(Error));
}
};
@@ -1582,7 +1588,7 @@ static void resolveRange(SwiftLangSupport &Lang,
void SwiftLangSupport::getCursorInfo(
StringRef InputFile, unsigned Offset, unsigned Length, bool Actionables,
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
std::function<void(const CursorInfoData &, StringRef error)> Receiver) {
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(InputFile)) {
IFaceGenRef->accessASTAsync([this, IFaceGenRef, Offset, Actionables, Receiver] {
@@ -1595,19 +1601,20 @@ void SwiftLangSupport::getCursorInfo(
passCursorInfoForModule(Entity.Mod, IFaceGenContexts, Invok,
Receiver);
} else {
std::string Diagnostic; // Unused.
// FIXME: Should pass the main module for the interface but currently
// it's not necessary.
passCursorInfoForDecl(
/*SourceFile*/nullptr, Entity.Dcl, /*MainModule*/ nullptr,
Type(), Entity.IsRef, Actionables, ResolvedCursorInfo(),
/*OrigBufferID=*/None, SourceLoc(),
{}, *this, Invok, {}, Receiver);
{}, *this, Invok, Diagnostic, {}, Receiver);
}
} else {
CursorInfoData Info;
Info.InternalDiagnostic =
"Unable to resolve entity from generated interface.";
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
});
return;
@@ -1617,7 +1624,7 @@ void SwiftLangSupport::getCursorInfo(
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, InputFile, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(CursorInfoData(), Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
return;
}
@@ -1628,22 +1635,22 @@ void SwiftLangSupport::getCursorInfo(
void SwiftLangSupport::
getRangeInfo(StringRef InputFile, unsigned Offset, unsigned Length,
bool CancelOnSubsequentRequest, ArrayRef<const char *> Args,
std::function<void(const RangeInfo&, StringRef error)> Receiver) {
std::function<void(const RequestResult<RangeInfo> &)> Receiver) {
if (IFaceGenContexts.get(InputFile)) {
// FIXME: return range info for generated interfaces.
Receiver(RangeInfo(),
"Range info for generated interfaces is not implemented.");
Receiver(RequestResult<RangeInfo>::fromError(
"Range info for generated interfaces is not implemented."));
return;
}
std::string Error;
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, InputFile, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver(RangeInfo(), Error);
Receiver(RequestResult<RangeInfo>::fromError(Error));
return;
}
if (Length == 0) {
Receiver(RangeInfo(), "Invalid range length.");
Receiver(RequestResult<RangeInfo>::fromError("Invalid range length."));
return;
}
resolveRange(*this, InputFile, Offset, Length, Invok, /*TryExistingAST=*/true,
@@ -1653,7 +1660,7 @@ getRangeInfo(StringRef InputFile, unsigned Offset, unsigned Length,
void SwiftLangSupport::
getNameInfo(StringRef InputFile, unsigned Offset, NameTranslatingInfo &Input,
ArrayRef<const char *> Args,
std::function<void(const NameTranslatingInfo &, StringRef error)> Receiver) {
std::function<void(const RequestResult<NameTranslatingInfo> &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(InputFile)) {
IFaceGenRef->accessASTAsync([IFaceGenRef, Offset, Input, Receiver] {
@@ -1672,7 +1679,7 @@ getNameInfo(StringRef InputFile, unsigned Offset, NameTranslatingInfo &Input,
NameTranslatingInfo Info;
Info.InternalDiagnostic =
"Unable to resolve entity from generated interface.";
Receiver(Info, "");
Receiver(RequestResult<NameTranslatingInfo>::fromResult(Info));
}
});
return;
@@ -1682,7 +1689,7 @@ getNameInfo(StringRef InputFile, unsigned Offset, NameTranslatingInfo &Input,
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, InputFile, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver({}, Error);
Receiver(RequestResult<NameTranslatingInfo>::fromError(Error));
return;
}
@@ -1694,7 +1701,7 @@ static void
resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
SwiftInvocationRef Invok, bool TryExistingAST,
bool CancelOnSubsequentRequest,
std::function<void(const CursorInfoData &, StringRef error)> Receiver) {
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
assert(Invok);
class CursorInfoConsumer : public SwiftASTConsumer {
@@ -1704,15 +1711,14 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
SwiftInvocationRef ASTInvok;
const bool TryExistingAST;
bool CancelOnSubsequentRequest;
std::function<void(const CursorInfoData &, StringRef Error)> Receiver;
std::function<void(const RequestResult<CursorInfoData> &)> Receiver;
SmallVector<ImmutableTextSnapshotRef, 4> PreviousASTSnaps;
public:
CursorInfoConsumer(StringRef InputFile, StringRef USR,
SwiftLangSupport &Lang, SwiftInvocationRef ASTInvok,
bool TryExistingAST, bool CancelOnSubsequentRequest,
std::function<void(const CursorInfoData &,
StringRef Error)> Receiver)
std::function<void(const RequestResult<CursorInfoData> &)> Receiver)
: InputFile(InputFile), USR(USR), Lang(Lang),
ASTInvok(std::move(ASTInvok)), TryExistingAST(TryExistingAST),
CancelOnSubsequentRequest(CancelOnSubsequentRequest),
@@ -1746,7 +1752,7 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
LOG_WARN_FUNC("lookup for C/C++/ObjC USRs not implemented");
CursorInfoData Info;
Info.InternalDiagnostic = "Lookup for C/C++/ObjC USRs not implemented.";
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
@@ -1756,7 +1762,7 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
if (!D) {
CursorInfoData Info;
Info.InternalDiagnostic = "Unable to resolve type from USR.";
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
@@ -1773,12 +1779,13 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
selfTy = DC->getSelfInterfaceType();
selfTy = D->getInnermostDeclContext()->mapTypeIntoContext(selfTy);
}
StringRef Diagnostic =
std::string Diagnostic;
bool Success =
passCursorInfoForDecl(/*SourceFile*/nullptr, D, MainModule, selfTy,
/*IsRef=*/false, false, ResolvedCursorInfo(),
BufferID, SourceLoc(), {}, Lang, CompInvok,
PreviousASTSnaps, Receiver);
if (!Diagnostic.empty()) {
Diagnostic, PreviousASTSnaps, Receiver);
if (!Success) {
if (!PreviousASTSnaps.empty()) {
// Attempt again using the up-to-date AST.
resolveCursorFromUSR(Lang, InputFile, USR, ASTInvok,
@@ -1787,21 +1794,19 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
} else {
CursorInfoData Info;
Info.InternalDiagnostic = Diagnostic;
Receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
}
}
}
}
void cancelled() override {
CursorInfoData Info;
Info.IsCancelled = true;
Receiver(Info, "Cancelled.");
Receiver(RequestResult<CursorInfoData>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("cursor info failed: " << Error);
Receiver(CursorInfoData(), Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
}
};
@@ -1817,26 +1822,26 @@ resolveCursorFromUSR(SwiftLangSupport &Lang, StringRef InputFile, StringRef USR,
void SwiftLangSupport::getCursorInfoFromUSR(
StringRef filename, StringRef USR, bool CancelOnSubsequentRequest,
ArrayRef<const char *> args,
std::function<void(const CursorInfoData &, StringRef Error)> receiver) {
ArrayRef<const char *> Args,
std::function<void(const RequestResult<CursorInfoData> &)> Receiver) {
if (auto IFaceGenRef = IFaceGenContexts.get(filename)) {
LOG_WARN_FUNC("Info from usr for generated interface not implemented yet.");
CursorInfoData Info;
Info.InternalDiagnostic = "Info for generated interfaces not implemented.";
receiver(Info, "");
Receiver(RequestResult<CursorInfoData>::fromResult(Info));
return;
}
std::string Error;
SwiftInvocationRef Invok = ASTMgr->getInvocation(args, filename, Error);
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, filename, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
receiver(CursorInfoData(), Error);
Receiver(RequestResult<CursorInfoData>::fromError(Error));
return;
}
resolveCursorFromUSR(*this, filename, USR, Invok, /*TryExistingAST=*/true,
CancelOnSubsequentRequest, receiver);
CancelOnSubsequentRequest, Receiver);
}
//===----------------------------------------------------------------------===//
@@ -1923,25 +1928,24 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
StringRef InputFile, unsigned Offset,
bool CancelOnSubsequentRequest,
ArrayRef<const char *> Args,
std::function<void(const RelatedIdentsInfo &, StringRef error)> Receiver) {
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver) {
std::string Error;
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, InputFile, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver({}, Error);
Receiver(RequestResult<RelatedIdentsInfo>::fromError(Error));
return;
}
class RelatedIdConsumer : public SwiftASTConsumer {
unsigned Offset;
std::function<void(const RelatedIdentsInfo &, StringRef Error)> Receiver;
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver;
SwiftInvocationRef Invok;
public:
RelatedIdConsumer(unsigned Offset,
std::function<void(const RelatedIdentsInfo &,
StringRef Error)> Receiver,
std::function<void(const RequestResult<RelatedIdentsInfo> &)> Receiver,
SwiftInvocationRef Invok)
: Offset(Offset), Receiver(std::move(Receiver)), Invok(Invok) { }
@@ -1997,18 +2001,16 @@ void SwiftLangSupport::findRelatedIdentifiersInFile(
RelatedIdentsInfo Info;
Info.Ranges = Ranges;
Receiver(Info, "");
Receiver(RequestResult<RelatedIdentsInfo>::fromResult(Info));
}
void cancelled() override {
RelatedIdentsInfo Info;
Info.IsCancelled = true;
Receiver(Info, "Cancelled.");
Receiver(RequestResult<RelatedIdentsInfo>::cancelled());
}
void failed(StringRef Error) override {
LOG_WARN_FUNC("related idents failed: " << Error);
Receiver({}, Error);
Receiver(RequestResult<RelatedIdentsInfo>::fromError(Error));
}
static CaseStmt *getCaseStmtOfCanonicalVar(Decl *D) {
@@ -2051,7 +2053,7 @@ semanticRefactoring(StringRef Filename, SemanticRefactoringInfo Info,
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, Filename, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver({}, Error);
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(Error));
return;
}
assert(Invok);
@@ -2081,12 +2083,11 @@ semanticRefactoring(StringRef Filename, SemanticRefactoringInfo Info,
}
void cancelled() override {
// FIXME: inform the receiver of cancellation instead of an error.
Receiver({}, "Cancelled.");
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::cancelled());
}
void failed(StringRef Error) override {
Receiver({}, Error);
Receiver(RequestResult<ArrayRef<CategorizedEdits>>::fromError(Error));
}
};
@@ -2100,23 +2101,21 @@ semanticRefactoring(StringRef Filename, SemanticRefactoringInfo Info,
void SwiftLangSupport::collectExpressionTypes(StringRef FileName,
ArrayRef<const char *> Args,
ArrayRef<const char *> ExpectedProtocols,
std::function<void(const ExpressionTypesInFile&,
StringRef Error)> Receiver) {
std::function<void(const RequestResult<ExpressionTypesInFile> &)> Receiver) {
std::string Error;
SwiftInvocationRef Invok = ASTMgr->getInvocation(Args, FileName, Error);
if (!Invok) {
LOG_WARN_FUNC("failed to create an ASTInvocation: " << Error);
Receiver({}, Error);
Receiver(RequestResult<ExpressionTypesInFile>::fromError(Error));
return;
}
assert(Invok);
class ExpressionTypeCollector: public SwiftASTConsumer {
std::function<void(const ExpressionTypesInFile&, StringRef Error)> Receiver;
std::function<void(const RequestResult<ExpressionTypesInFile> &)> Receiver;
std::vector<const char *> ExpectedProtocols;
public:
ExpressionTypeCollector(
std::function<void(const ExpressionTypesInFile&,
StringRef Error)> Receiver,
std::function<void(const RequestResult<ExpressionTypesInFile> &)> Receiver,
ArrayRef<const char *> ExpectedProtocols): Receiver(std::move(Receiver)),
ExpectedProtocols(ExpectedProtocols.vec()) {}
void handlePrimaryAST(ASTUnitRef AstUnit) override {
@@ -2132,16 +2131,15 @@ void SwiftLangSupport::collectExpressionTypes(StringRef FileName,
}
}
Result.TypeBuffer = OS.str();
Receiver(Result, "");
Receiver(RequestResult<ExpressionTypesInFile>::fromResult(Result));
}
void cancelled() override {
// FIXME: inform the receiver of cancellation instead of an error.
Receiver({}, "Cancelled.");
Receiver(RequestResult<ExpressionTypesInFile>::cancelled());
}
void failed(StringRef Error) override {
Receiver({}, Error);
Receiver(RequestResult<ExpressionTypesInFile>::fromError(Error));
}
};
auto Collector = std::make_shared<ExpressionTypeCollector>(Receiver,

View File

@@ -161,7 +161,7 @@ void printRequestObject(sourcekitd_object_t Obj, llvm::raw_ostream &OS);
void printResponse(sourcekitd_response_t Resp, llvm::raw_ostream &OS);
sourcekitd_response_t createErrorRequestInvalid(const char *Description);
sourcekitd_response_t createErrorRequestFailed(const char *Description);
sourcekitd_response_t createErrorRequestFailed(llvm::StringRef Description);
sourcekitd_response_t createErrorRequestInterrupted(const char *Description);
sourcekitd_response_t createErrorRequestCancelled();

View File

@@ -140,14 +140,14 @@ static sourcekitd_response_t reportDocInfo(llvm::MemoryBuffer *InputBuf,
StringRef ModuleName,
ArrayRef<const char *> Args);
static void reportCursorInfo(const CursorInfoData &Info, ResponseReceiver Rec, StringRef Error);
static void reportCursorInfo(const RequestResult<CursorInfoData> &Result, ResponseReceiver Rec);
static void reportExpressionTypeInfo(const ExpressionTypesInFile &Info, ResponseReceiver Rec,
StringRef Error);
static void reportExpressionTypeInfo(const RequestResult<ExpressionTypesInFile> &Result,
ResponseReceiver Rec);
static void reportRangeInfo(const RangeInfo &Info, ResponseReceiver Rec, StringRef Error);
static void reportRangeInfo(const RequestResult<RangeInfo> &Result, ResponseReceiver Rec);
static void reportNameInfo(const NameTranslatingInfo &Info, ResponseReceiver Rec, StringRef Error);
static void reportNameInfo(const RequestResult<NameTranslatingInfo> &Result, ResponseReceiver Rec);
static void findRelatedIdents(StringRef Filename,
int64_t Offset,
@@ -241,8 +241,8 @@ buildRenameLocationsFromDict(RequestDict &Req, bool UseNewName,
llvm::SmallString<64> &Error);
static sourcekitd_response_t
createCategorizedEditsResponse(ArrayRef<CategorizedEdits> AllEdits,
StringRef Error);
createCategorizedEditsResponse(
const RequestResult<ArrayRef<CategorizedEdits>> &Result);
static sourcekitd_response_t
syntacticRename(llvm::MemoryBuffer *InputBuf,
@@ -250,8 +250,8 @@ syntacticRename(llvm::MemoryBuffer *InputBuf,
ArrayRef<const char*> Args);
static sourcekitd_response_t
createCategorizedRenameRangesResponse(ArrayRef<CategorizedRenameRanges> Ranges,
StringRef Error);
createCategorizedRenameRangesResponse(
const RequestResult<ArrayRef<CategorizedRenameRanges>> &Result);
static sourcekitd_response_t
findRenameRanges(llvm::MemoryBuffer *InputBuf,
@@ -936,15 +936,15 @@ handleSemanticRequest(RequestDict Req,
Req.getInt64(KeyRetrieveRefactorActions, Actionables, /*isOptional=*/true);
return Lang.getCursorInfo(
*SourceFile, Offset, Length, Actionables, CancelOnSubsequentRequest,
Args, [Rec](const CursorInfoData &Info, StringRef Error) {
reportCursorInfo(Info, Rec, Error);
Args, [Rec](const RequestResult<CursorInfoData> &Result) {
reportCursorInfo(Result, Rec);
});
}
if (auto USR = Req.getString(KeyUSR)) {
return Lang.getCursorInfoFromUSR(
*SourceFile, *USR, CancelOnSubsequentRequest, Args,
[Rec](const CursorInfoData &Info, StringRef Error) {
reportCursorInfo(Info, Rec, Error);
[Rec](const RequestResult<CursorInfoData> &Result) {
reportCursorInfo(Result, Rec);
});
}
@@ -964,8 +964,8 @@ handleSemanticRequest(RequestDict Req,
if (!Req.getInt64(KeyLength, Length, /*isOptional=*/false)) {
return Lang.getRangeInfo(*SourceFile, Offset, Length,
CancelOnSubsequentRequest, Args,
[Rec](const RangeInfo &Info, StringRef Error) {
reportRangeInfo(Info, Rec, Error);
[Rec](const RequestResult<RangeInfo> &Result) {
reportRangeInfo(Result, Rec);
});
}
}
@@ -1002,8 +1002,8 @@ handleSemanticRequest(RequestDict Req,
Info.Column = Column;
Info.Length = Length;
return Lang.semanticRefactoring(*SourceFile, Info, Args,
[Rec](ArrayRef<CategorizedEdits> CategorizedEdits, StringRef Error) {
Rec(createCategorizedEditsResponse(CategorizedEdits, Error));
[Rec](const RequestResult<ArrayRef<CategorizedEdits>> &Result) {
Rec(createCategorizedEditsResponse(Result));
});
}
}
@@ -1017,8 +1017,8 @@ handleSemanticRequest(RequestDict Req,
if (Req.getStringArray(KeyExpectedTypes, ExpectedProtocols, true))
return Rec(createErrorRequestInvalid("invalid 'key.interested_protocols'"));
return Lang.collectExpressionTypes(*SourceFile, Args, ExpectedProtocols,
[Rec](const ExpressionTypesInFile &Info, StringRef Error) {
reportExpressionTypeInfo(Info, Rec, Error);
[Rec](const RequestResult<ExpressionTypesInFile> &Result) {
reportExpressionTypeInfo(Result, Rec);
});
}
@@ -1033,8 +1033,8 @@ handleSemanticRequest(RequestDict Req,
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
return Lang.findLocalRenameRanges(
*SourceFile, Line, Column, Length, Args,
[Rec](ArrayRef<CategorizedRenameRanges> Ranges, StringRef Error) {
Rec(createCategorizedRenameRangesResponse(Ranges, Error));
[Rec](const RequestResult<ArrayRef<CategorizedRenameRanges>> &Result) {
Rec(createCategorizedRenameRangesResponse(Result));
});
}
@@ -1077,8 +1077,8 @@ handleSemanticRequest(RequestDict Req,
std::back_inserter(Input.ArgNames),
[](const char *C) { return StringRef(C); });
return Lang.getNameInfo(*SourceFile, Offset, Input, Args,
[Rec](const NameTranslatingInfo &Info, StringRef Error) {
reportNameInfo(Info, Rec, Error);
[Rec](const RequestResult<NameTranslatingInfo> &Result) {
reportNameInfo(Result, Rec);
});
}
@@ -1617,12 +1617,14 @@ bool SKDocConsumer::handleDiagnostic(const DiagnosticEntryInfo &Info) {
// ReportCursorInfo
//===----------------------------------------------------------------------===//
static void reportCursorInfo(const CursorInfoData &Info, ResponseReceiver Rec,
StringRef Error) {
if (Info.IsCancelled)
static void reportCursorInfo(const RequestResult<CursorInfoData> &Result,
ResponseReceiver Rec) {
if (Result.isCancelled())
return Rec(createErrorRequestCancelled());
if (!Error.empty())
return Rec(createErrorRequestFailed(Error.str().c_str()));
if (Result.isError())
return Rec(createErrorRequestFailed(Result.getError()));
const CursorInfoData &Info = Result.value();
ResponseBuilder RespBuilder;
if (!Info.InternalDiagnostic.empty()) {
@@ -1710,12 +1712,14 @@ static void reportCursorInfo(const CursorInfoData &Info, ResponseReceiver Rec,
// ReportRangeInfo
//===----------------------------------------------------------------------===//
static void reportRangeInfo(const RangeInfo &Info, ResponseReceiver Rec,
StringRef Error) {
if (Info.IsCancelled)
static void reportRangeInfo(const RequestResult<RangeInfo> &Result,
ResponseReceiver Rec) {
if (Result.isCancelled())
return Rec(createErrorRequestCancelled());
if (!Error.empty())
return Rec(createErrorRequestFailed(Error.str().c_str()));
if (Result.isError())
return Rec(createErrorRequestFailed(Result.getError()));
const RangeInfo &Info = Result.value();
ResponseBuilder RespBuilder;
auto Elem = RespBuilder.getDictionary();
@@ -1729,12 +1733,14 @@ static void reportRangeInfo(const RangeInfo &Info, ResponseReceiver Rec,
// ReportNameInfo
//===----------------------------------------------------------------------===//
static void reportNameInfo(const NameTranslatingInfo &Info,
ResponseReceiver Rec, StringRef Error) {
if (Info.IsCancelled)
static void reportNameInfo(const RequestResult<NameTranslatingInfo> &Result,
ResponseReceiver Rec) {
if (Result.isCancelled())
return Rec(createErrorRequestCancelled());
if (!Error.empty())
return Rec(createErrorRequestFailed(Error.str().c_str()));
if (Result.isError())
return Rec(createErrorRequestFailed(Result.getError()));
const NameTranslatingInfo &Info = Result.value();
ResponseBuilder RespBuilder;
if (!Info.InternalDiagnostic.empty()) {
@@ -1771,10 +1777,14 @@ static void reportNameInfo(const NameTranslatingInfo &Info,
//===----------------------------------------------------------------------===//
// ReportExpressionTypeInfo
//===----------------------------------------------------------------------===//
static void reportExpressionTypeInfo(const ExpressionTypesInFile &Info,
ResponseReceiver Rec, StringRef Error) {
if (!Error.empty())
return Rec(createErrorRequestFailed(Error.str().c_str()));
static void reportExpressionTypeInfo(const RequestResult<ExpressionTypesInFile> &Result,
ResponseReceiver Rec) {
if (Result.isCancelled())
return Rec(createErrorRequestCancelled());
if (Result.isError())
return Rec(createErrorRequestFailed(Result.getError()));
const ExpressionTypesInFile &Info = Result.value();
ResponseBuilder Builder;
auto Dict = Builder.getDictionary();
@@ -1798,12 +1808,14 @@ static void findRelatedIdents(StringRef Filename,
ResponseReceiver Rec) {
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
Lang.findRelatedIdentifiersInFile(Filename, Offset, CancelOnSubsequentRequest,
Args, [Rec](const RelatedIdentsInfo &Info,
StringRef Error) {
if (Info.IsCancelled)
Args,
[Rec](const RequestResult<RelatedIdentsInfo> &Result) {
if (Result.isCancelled())
return Rec(createErrorRequestCancelled());
if (!Error.empty())
return Rec(createErrorRequestFailed(Error.str().c_str()));
if (Result.isError())
return Rec(createErrorRequestFailed(Result.getError()));
const RelatedIdentsInfo &Info = Result.value();
ResponseBuilder RespBuilder;
auto Arr = RespBuilder.getDictionary().setArray(KeyResults);
@@ -2773,11 +2785,17 @@ editorFindInterfaceDoc(StringRef ModuleName, ArrayRef<const char *> Args) {
sourcekitd_response_t Resp;
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
Lang.findInterfaceDocument(ModuleName, Args,
[&](const InterfaceDocInfo &Info, StringRef Error) {
if (!Error.empty()) {
Resp = createErrorRequestFailed(Error.str().c_str());
[&](const RequestResult<InterfaceDocInfo> &Result) {
if (Result.isCancelled()) {
Resp = createErrorRequestCancelled();
return;
}
if (Result.isError()) {
Resp = createErrorRequestFailed(Result.getError());
return;
}
const InterfaceDocInfo &Info = Result.value();
auto Elem = RespBuilder.getDictionary();
if (!Info.ModuleInterfaceName.empty())
@@ -2796,11 +2814,18 @@ editorFindModuleGroups(StringRef ModuleName, ArrayRef<const char *> Args) {
sourcekitd_response_t Resp;
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
Lang.findModuleGroups(ModuleName, Args,
[&](ArrayRef<StringRef> Groups, StringRef Error) {
if (!Error.empty()) {
Resp = createErrorRequestFailed(Error.str().c_str());
[&](const RequestResult<ArrayRef<StringRef>> &Result) {
if (Result.isCancelled()) {
Resp = createErrorRequestCancelled();
return;
}
if (Result.isError()) {
Resp = createErrorRequestFailed(Result.getError());
return;
}
ArrayRef<StringRef> Groups = Result.value();
auto Dict = RespBuilder.getDictionary();
auto Arr = Dict.setArray(KeyModuleGroups);
for (auto G : Groups) {
@@ -2897,11 +2922,14 @@ buildRenameLocationsFromDict(RequestDict &Req, bool UseNewName,
}
static sourcekitd_response_t
createCategorizedEditsResponse(ArrayRef<CategorizedEdits> AllEdits,
StringRef Error) {
if (!Error.empty()) {
return createErrorRequestFailed(Error.str().c_str());
}
createCategorizedEditsResponse(const RequestResult<ArrayRef<CategorizedEdits>> &Result) {
if (Result.isCancelled())
return createErrorRequestCancelled();
if (Result.isError())
return createErrorRequestFailed(Result.getError());
const ArrayRef<CategorizedEdits> &AllEdits = Result.value();
ResponseBuilder RespBuilder;
auto Dict = RespBuilder.getDictionary();
auto Arr = Dict.setArray(KeyCategorizedEdits);
@@ -2941,18 +2969,21 @@ syntacticRename(llvm::MemoryBuffer *InputBuf,
LangSupport &Lang = getGlobalContext().getSwiftLangSupport();
sourcekitd_response_t Result;
Lang.syntacticRename(InputBuf, RenameLocations, Args,
[&](ArrayRef<CategorizedEdits> AllEdits, StringRef Error) {
Result = createCategorizedEditsResponse(AllEdits, Error);
[&](const RequestResult<ArrayRef<CategorizedEdits>> &ReqResult) {
Result = createCategorizedEditsResponse(ReqResult);
});
return Result;
}
static sourcekitd_response_t
createCategorizedRenameRangesResponse(ArrayRef<CategorizedRenameRanges> Ranges,
StringRef Error) {
if (!Error.empty()) {
return createErrorRequestFailed(Error.str().c_str());
}
createCategorizedRenameRangesResponse(const RequestResult<ArrayRef<CategorizedRenameRanges>> &Result) {
if (Result.isCancelled())
return createErrorRequestCancelled();
if (Result.isError())
return createErrorRequestFailed(Result.getError());
const ArrayRef<CategorizedRenameRanges> &Ranges = Result.value();
ResponseBuilder RespBuilder;
auto Dict = RespBuilder.getDictionary();
auto Arr = Dict.setArray(KeyCategorizedRanges);
@@ -2983,8 +3014,8 @@ findRenameRanges(llvm::MemoryBuffer *InputBuf,
sourcekitd_response_t Result;
Lang.findRenameRanges(
InputBuf, RenameLocations, Args,
[&](ArrayRef<CategorizedRenameRanges> Ranges, StringRef Error) {
Result = createCategorizedRenameRangesResponse(Ranges, Error);
[&](const RequestResult<ArrayRef<CategorizedRenameRanges>> &ReqResult) {
Result = createCategorizedRenameRangesResponse(ReqResult);
});
return Result;
}

View File

@@ -798,9 +798,9 @@ sourcekitd::createErrorRequestInvalid(const char *Description) {
}
sourcekitd_response_t
sourcekitd::createErrorRequestFailed(const char *Description) {
sourcekitd::createErrorRequestFailed(StringRef Description) {
return retained(new SKDError(SOURCEKITD_ERROR_REQUEST_FAILED,
StringRef(Description)));
Description));
}
sourcekitd_response_t

View File

@@ -379,7 +379,8 @@ sourcekitd::createErrorRequestInvalid(const char *Description) {
return CustomXPCData::createErrorRequestInvalid(Description).getXObj();
}
sourcekitd_response_t
sourcekitd::createErrorRequestFailed(const char *Description) {
sourcekitd::createErrorRequestFailed(StringRef DescRef) {
const char *Description = DescRef.str().c_str();
return CustomXPCData::createErrorRequestFailed(Description).getXObj();
}
sourcekitd_response_t

View File

@@ -148,8 +148,14 @@ public:
TestCursorInfo TestInfo;
getLang().getCursorInfo(DocName, Offset, 0, false, false, Args,
[&](const CursorInfoData &Info, StringRef Error) {
TestInfo.Error = Error;
[&](const RequestResult<CursorInfoData> &Result) {
assert(!Result.isCancelled());
if (Result.isError()) {
TestInfo.Error = Result.getError();
sema.signal();
return;
}
const CursorInfoData &Info = Result.value();
TestInfo.Name = Info.Name;
TestInfo.Typename = Info.TypeName;
TestInfo.Filename = Info.Filename;