Skip to content

Commit d4a757b

Browse files
committed
[SourceKit] Refactor main 'handleRequestImpl' function
Previously, SourceKit requests are handled in a single 'handleRequestImpl'/'handleSemanticRequest' with basically a giant 'if (ReqUID == RequstXXX)' branches. Some request parameters are extracted at the top of the function, but not all request kind used all of them. Factor out each handling logic to its own function, and check/extract request parameters in it. This makes it clear that which request uses what parameters, and how they are handled.
1 parent ffac122 commit d4a757b

File tree

4 files changed

+683
-209
lines changed

4 files changed

+683
-209
lines changed

tools/SourceKit/tools/sourcekitd/include/sourcekitd/Internal.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,17 @@ class RequestDict {
128128
sourcekitd_object_t Dict;
129129

130130
public:
131-
explicit RequestDict(sourcekitd_object_t Dict) : Dict(Dict) {}
132-
133-
sourcekitd_uid_t getUID(SourceKit::UIdent Key);
134-
Optional<llvm::StringRef> getString(SourceKit::UIdent Key);
135-
Optional<RequestDict> getDictionary(SourceKit::UIdent Key);
131+
explicit RequestDict(sourcekitd_object_t Dict) : Dict(Dict) {
132+
sourcekitd_request_retain(Dict);
133+
}
134+
RequestDict(const RequestDict &other) : RequestDict(other.Dict) {}
135+
~RequestDict() {
136+
sourcekitd_request_release(Dict);
137+
}
138+
139+
sourcekitd_uid_t getUID(SourceKit::UIdent Key) const;
140+
Optional<llvm::StringRef> getString(SourceKit::UIdent Key) const;
141+
Optional<RequestDict> getDictionary(SourceKit::UIdent Key) const;
136142

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

149-
bool dictionaryArrayApply(SourceKit::UIdent key,
150-
llvm::function_ref<bool(RequestDict)> applier);
155+
bool
156+
dictionaryArrayApply(SourceKit::UIdent key,
157+
llvm::function_ref<bool(RequestDict)> applier) const;
151158

152-
bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional);
153-
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key);
159+
bool getInt64(SourceKit::UIdent Key, int64_t &Val, bool isOptional) const;
160+
Optional<int64_t> getOptionalInt64(SourceKit::UIdent Key) const;
154161
};
155162

156163
/// Initialize the sourcekitd client library. Returns true if this is the first

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-InProc.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -706,19 +706,19 @@ ResponseBuilder::Dictionary ResponseBuilder::Array::appendDictionary() {
706706
// Internal RequestDict Implementation
707707
//===----------------------------------------------------------------------===//
708708

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

714-
Optional<StringRef> RequestDict::getString(UIdent Key) {
714+
Optional<StringRef> RequestDict::getString(UIdent Key) const {
715715
if (auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key))) {
716716
return Object->getString();
717717
}
718718
return None;
719719
}
720720

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

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

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

769769
bool RequestDict::dictionaryArrayApply(
770-
SourceKit::UIdent Key, llvm::function_ref<bool(RequestDict)> Applier) {
770+
SourceKit::UIdent Key,
771+
llvm::function_ref<bool(RequestDict)> Applier) const {
771772
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
772773
if (!Object)
773774
return true;
@@ -784,15 +785,15 @@ bool RequestDict::dictionaryArrayApply(
784785
}
785786

786787
bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
787-
bool isOptional) {
788+
bool isOptional) const {
788789
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
789790
if (!Object)
790791
return !isOptional;
791792
Val = Object->getInt64().getValueOr(0);
792793
return false;
793794
}
794795

795-
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
796+
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) const {
796797
auto Object = static_cast<SKDObject *>(Dict)->get(SKDUIDFromUIdent(Key));
797798
if (!Object)
798799
return None;

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-XPC.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ ResponseBuilder::Dictionary ResponseBuilder::Array::appendDictionary() {
283283
return Dictionary(dict);
284284
}
285285

286-
sourcekitd_uid_t RequestDict::getUID(UIdent Key) {
286+
sourcekitd_uid_t RequestDict::getUID(UIdent Key) const {
287287
return sourcekitd_uid_t(xpc_dictionary_get_uint64(Dict, Key.c_str()));
288288
}
289289

290-
Optional<StringRef> RequestDict::getString(UIdent Key) {
290+
Optional<StringRef> RequestDict::getString(UIdent Key) const {
291291
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
292292
if (!xobj)
293293
return None;
@@ -297,7 +297,7 @@ Optional<StringRef> RequestDict::getString(UIdent Key) {
297297
xpc_string_get_length(xobj));
298298
}
299299

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

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

328328
bool RequestDict::getUIDArray(SourceKit::UIdent Key,
329329
llvm::SmallVectorImpl<sourcekitd_uid_t> &Arr,
330-
bool isOptional) {
330+
bool isOptional) const {
331331
xpc_object_t xarr = xpc_dictionary_get_value(Dict, Key.c_str());
332332
if (!xarr)
333333
return !isOptional;
@@ -345,7 +345,8 @@ bool RequestDict::getUIDArray(SourceKit::UIdent Key,
345345
}
346346

347347
bool RequestDict::dictionaryArrayApply(
348-
SourceKit::UIdent key, llvm::function_ref<bool(RequestDict)> applier) {
348+
SourceKit::UIdent key,
349+
llvm::function_ref<bool(RequestDict)> applier) const {
349350
xpc_object_t xarr = xpc_dictionary_get_value(Dict, key.c_str());
350351
if (!xarr || xpc_get_type(xarr) != XPC_TYPE_ARRAY)
351352
return true;
@@ -361,15 +362,15 @@ bool RequestDict::dictionaryArrayApply(
361362
}
362363

363364
bool RequestDict::getInt64(SourceKit::UIdent Key, int64_t &Val,
364-
bool isOptional) {
365+
bool isOptional) const {
365366
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
366367
if (!xobj)
367368
return !isOptional;
368369
Val = xpc_int64_get_value(xobj);
369370
return false;
370371
}
371372

372-
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) {
373+
Optional<int64_t> RequestDict::getOptionalInt64(SourceKit::UIdent Key) const {
373374
xpc_object_t xobj = xpc_dictionary_get_value(Dict, Key.c_str());
374375
if (!xobj)
375376
return None;

0 commit comments

Comments
 (0)