Skip to content

[SourceKit] Refactor main 'handleRequestImpl' function #63090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions tools/SourceKit/tools/sourcekitd/include/sourcekitd/Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,17 @@ class RequestDict {
sourcekitd_object_t Dict;

public:
explicit RequestDict(sourcekitd_object_t Dict) : Dict(Dict) {}

sourcekitd_uid_t getUID(SourceKit::UIdent Key);
Optional<llvm::StringRef> getString(SourceKit::UIdent Key);
Optional<RequestDict> getDictionary(SourceKit::UIdent Key);
explicit RequestDict(sourcekitd_object_t Dict) : Dict(Dict) {
sourcekitd_request_retain(Dict);
}
RequestDict(const RequestDict &other) : RequestDict(other.Dict) {}
~RequestDict() {
sourcekitd_request_release(Dict);
}
Comment on lines +131 to +137
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may trigger unnecessary retain/release traffic, but this should make the request dictionary handling safer.


sourcekitd_uid_t getUID(SourceKit::UIdent Key) const;
Optional<llvm::StringRef> getString(SourceKit::UIdent Key) const;
Optional<RequestDict> getDictionary(SourceKit::UIdent Key) const;

/// Populate the vector with an array of C strings.
/// \param isOptional true if the key is optional. If false and the key is
Expand All @@ -141,16 +147,17 @@ class RequestDict {
/// the array does not contain strings.
bool getStringArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<const char *> &Arr,
bool isOptional);
bool isOptional) const;
bool getUIDArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<sourcekitd_uid_t> &Arr,
bool isOptional);
bool isOptional) const;

bool dictionaryArrayApply(SourceKit::UIdent key,
llvm::function_ref<bool(RequestDict)> applier);
bool
dictionaryArrayApply(SourceKit::UIdent key,
llvm::function_ref<bool(RequestDict)> applier) const;

bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional);
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key);
bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional) const;
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key) const;
};

/// Initialize the sourcekitd client library. Returns true if this is the first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,19 +706,19 @@ ResponseBuilder::Dictionary ResponseBuilder::Array::appendDictionary() {
// Internal RequestDict Implementation
//===----------------------------------------------------------------------===//

sourcekitd_uid_t RequestDict::getUID(UIdent Key) {
sourcekitd_uid_t RequestDict::getUID(UIdent Key) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
return Object ? Object->getUID() : nullptr;
}

Optional<StringRef> RequestDict::getString(UIdent Key) {
Optional<StringRef> RequestDict::getString(UIdent Key) const {
if (auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key))) {
return Object->getString();
}
return None;
}

Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) {
Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) const {
SKDDictionary *DictObject = nullptr;
if (auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key))) {
DictObject = dyn_cast<SKDDictionary>(Object);
Expand All @@ -728,7 +728,7 @@ Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) {

bool RequestDict::getStringArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<const char *> &Arr,
bool isOptional) {
bool isOptional) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return !isOptional;
Expand All @@ -748,7 +748,7 @@ bool RequestDict::getStringArray(SourceKit::UIdent Key,

bool RequestDict::getUIDArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<sourcekitd_uid_t> &Arr,
bool isOptional) {
bool isOptional) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return !isOptional;
Expand All @@ -767,7 +767,8 @@ bool RequestDict::getUIDArray(SourceKit::UIdent Key,
}

bool RequestDict::dictionaryArrayApply(
SourceKit::UIdent Key, llvm::function_ref<bool(RequestDict)> Applier) {
SourceKit::UIdent Key,
llvm::function_ref<bool(RequestDict)> Applier) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return true;
Expand All @@ -784,15 +785,15 @@ bool RequestDict::dictionaryArrayApply(
}

bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
bool isOptional) {
bool isOptional) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return !isOptional;
Val = Object->getInt64().getValueOr(0);
return false;
}

Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) const {
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
if (!Object)
return None;
Expand Down
17 changes: 9 additions & 8 deletions tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,11 @@ ResponseBuilder::Dictionary ResponseBuilder::Array::appendDictionary() {
return Dictionary(dict);
}

sourcekitd_uid_t RequestDict::getUID(UIdent Key) {
sourcekitd_uid_t RequestDict::getUID(UIdent Key) const {
return sourcekitd_uid_t(xpc_dictionary_get_uint64(Dict, Key.c_str()));
}

Optional<StringRef> RequestDict::getString(UIdent Key) {
Optional<StringRef> RequestDict::getString(UIdent Key) const {
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xobj)
return None;
Expand All @@ -297,7 +297,7 @@ Optional<StringRef> RequestDict::getString(UIdent Key) {
xpc_string_get_length(xobj));
}

Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) {
Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) const {
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xobj)
return None;
Expand All @@ -308,7 +308,7 @@ Optional<RequestDict> RequestDict::getDictionary(SourceKit::UIdent Key) {

bool RequestDict::getStringArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<const char *> &Arr,
bool isOptional) {
bool isOptional) const {
xpc_object_t xarr = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xarr)
return !isOptional;
Expand All @@ -327,7 +327,7 @@ bool RequestDict::getStringArray(SourceKit::UIdent Key,

bool RequestDict::getUIDArray(SourceKit::UIdent Key,
llvm::SmallVectorImpl<sourcekitd_uid_t> &Arr,
bool isOptional) {
bool isOptional) const {
xpc_object_t xarr = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xarr)
return !isOptional;
Expand All @@ -345,7 +345,8 @@ bool RequestDict::getUIDArray(SourceKit::UIdent Key,
}

bool RequestDict::dictionaryArrayApply(
SourceKit::UIdent key, llvm::function_ref<bool(RequestDict)> applier) {
SourceKit::UIdent key,
llvm::function_ref<bool(RequestDict)> applier) const {
xpc_object_t xarr = xpc_dictionary_get_value(Dict, key.c_str());
if (!xarr || xpc_get_type(xarr) != XPC_TYPE_ARRAY)
return true;
Expand All @@ -361,15 +362,15 @@ bool RequestDict::dictionaryArrayApply(
}

bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
bool isOptional) {
bool isOptional) const {
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xobj)
return !isOptional;
Val = xpc_int64_get_value(xobj);
return false;
}

Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) const {
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
if (!xobj)
return None;
Expand Down
Loading