Skip to content

Commit 472d88d

Browse files
committed
[sourcekit] Sink UID handling into the API library
This fixes a layering violation where UID handling was provided by a dependent library, creating a dependency cycle if you tried to link the lower level libraries dynamically.
1 parent 152370e commit 472d88d

File tree

4 files changed

+78
-50
lines changed

4 files changed

+78
-50
lines changed

tools/SourceKit/tools/sourcekitd/bin/InProc/sourcekitdInProc.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,8 @@
3333

3434
using namespace SourceKit;
3535

36-
static llvm::sys::Mutex GlobalHandlersMtx;
37-
static sourcekitd_uid_handler_t UidMappingHandler;
38-
static sourcekitd_str_from_uid_handler_t StrFromUidMappingHandler;
39-
4036
static void postNotification(sourcekitd_response_t Notification);
4137

42-
void
43-
sourcekitd_set_uid_handler(sourcekitd_uid_handler_t handler) {
44-
llvm::sys::ScopedLock L(GlobalHandlersMtx);
45-
sourcekitd_uid_handler_t newHandler = Block_copy(handler);
46-
Block_release(UidMappingHandler);
47-
UidMappingHandler = newHandler;
48-
}
49-
50-
void
51-
sourcekitd_set_uid_handlers(sourcekitd_uid_from_str_handler_t uid_from_str,
52-
sourcekitd_str_from_uid_handler_t str_from_uid) {
53-
llvm::sys::ScopedLock L(GlobalHandlersMtx);
54-
55-
sourcekitd_uid_handler_t newUIDFromStrHandler = Block_copy(uid_from_str);
56-
Block_release(UidMappingHandler);
57-
UidMappingHandler = newUIDFromStrHandler;
58-
59-
sourcekitd_str_from_uid_handler_t newStrFromUIDHandler = Block_copy(str_from_uid);
60-
Block_release(StrFromUidMappingHandler);
61-
StrFromUidMappingHandler = newStrFromUIDHandler;
62-
}
63-
64-
sourcekitd_uid_t sourcekitd::SKDUIDFromUIdent(UIdent UID) {
65-
if (void *Tag = UID.getTag())
66-
return reinterpret_cast<sourcekitd_uid_t>(Tag);
67-
68-
if (UidMappingHandler) {
69-
sourcekitd_uid_t skduid = UidMappingHandler(UID.c_str());
70-
if (skduid) {
71-
UID.setTag(skduid);
72-
return skduid;
73-
}
74-
}
75-
76-
return reinterpret_cast<sourcekitd_uid_t>(UID.getAsOpaqueValue());
77-
}
78-
79-
UIdent sourcekitd::UIdentFromSKDUID(sourcekitd_uid_t uid) {
80-
if (StrFromUidMappingHandler)
81-
return UIdent(StrFromUidMappingHandler(uid));
82-
83-
return UIdent::getFromOpaqueValue(uid);
84-
}
85-
8638
static void getToolchainPrefixPath(llvm::SmallVectorImpl<char> &Path) {
8739
#if defined(_WIN32)
8840
MEMORY_BASIC_INFORMATION mbi;

tools/SourceKit/tools/sourcekitd/bin/XPC/Service/XPCService.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class SKUIDToUIDMap {
7171

7272
static SKUIDToUIDMap UIDMap;
7373

74-
sourcekitd_uid_t sourcekitd::SKDUIDFromUIdent(UIdent UID) {
74+
static sourcekitd_uid_t xpcSKDUIDFromUIdent(UIdent UID) {
7575
if (void *Tag = UID.getTag())
7676
return reinterpret_cast<sourcekitd_uid_t>(Tag);
7777

@@ -108,7 +108,7 @@ sourcekitd_uid_t sourcekitd::SKDUIDFromUIdent(UIdent UID) {
108108
return skduid;
109109
}
110110

111-
UIdent sourcekitd::UIdentFromSKDUID(sourcekitd_uid_t SKDUID) {
111+
static UIdent xpcUIdentFromSKDUID(sourcekitd_uid_t SKDUID) {
112112
// This should be used only for debugging/logging purposes.
113113

114114
UIdent UID = UIDMap.get(SKDUID);
@@ -341,6 +341,13 @@ static void fatal_error_handler(void *user_data, const std::string& reason,
341341
int main(int argc, const char *argv[]) {
342342
llvm::install_fatal_error_handler(fatal_error_handler, 0);
343343
sourcekitd::enableLogging("sourcekit-serv");
344+
sourcekitd_set_uid_handlers(
345+
^sourcekitd_uid_t(const char *uidStr) {
346+
return xpcSKDUIDFromUIdent(UIdent(uidStr));
347+
},
348+
^const char *(sourcekitd_uid_t uid) {
349+
return xpcUIdentFromSKDUID(uid).c_str();
350+
});
344351
sourcekitd::initializeService(
345352
getRuntimeLibPath(), getDiagnosticDocumentationPath(), postNotification);
346353

tools/SourceKit/tools/sourcekitd/lib/API/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_sourcekit_library(sourcekitdAPI
1212
sourcekitdAPI-Common.cpp
1313
TokenAnnotationsArray.cpp
1414
ExpressionTypeArray.cpp
15+
UIDHandling.cpp
1516
)
1617
target_link_libraries(sourcekitdAPI PRIVATE
1718
swiftBasic
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- UIDHandling.cpp --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SourceKit/Support/UIdent.h"
14+
#include "sourcekitd/Internal.h"
15+
16+
#include "llvm/Support/Mutex.h"
17+
18+
#include <Block.h>
19+
20+
using namespace SourceKit;
21+
22+
static llvm::sys::Mutex GlobalHandlersMtx;
23+
static sourcekitd_uid_handler_t UidMappingHandler;
24+
static sourcekitd_str_from_uid_handler_t StrFromUidMappingHandler;
25+
26+
void sourcekitd_set_uid_handler(sourcekitd_uid_handler_t handler) {
27+
llvm::sys::ScopedLock L(GlobalHandlersMtx);
28+
sourcekitd_uid_handler_t newHandler = Block_copy(handler);
29+
Block_release(UidMappingHandler);
30+
UidMappingHandler = newHandler;
31+
}
32+
33+
void sourcekitd_set_uid_handlers(
34+
sourcekitd_uid_from_str_handler_t uid_from_str,
35+
sourcekitd_str_from_uid_handler_t str_from_uid) {
36+
llvm::sys::ScopedLock L(GlobalHandlersMtx);
37+
38+
sourcekitd_uid_handler_t newUIDFromStrHandler = Block_copy(uid_from_str);
39+
Block_release(UidMappingHandler);
40+
UidMappingHandler = newUIDFromStrHandler;
41+
42+
sourcekitd_str_from_uid_handler_t newStrFromUIDHandler =
43+
Block_copy(str_from_uid);
44+
Block_release(StrFromUidMappingHandler);
45+
StrFromUidMappingHandler = newStrFromUIDHandler;
46+
}
47+
48+
sourcekitd_uid_t sourcekitd::SKDUIDFromUIdent(UIdent UID) {
49+
if (void *Tag = UID.getTag())
50+
return reinterpret_cast<sourcekitd_uid_t>(Tag);
51+
52+
if (UidMappingHandler) {
53+
sourcekitd_uid_t skduid = UidMappingHandler(UID.c_str());
54+
if (skduid) {
55+
UID.setTag(skduid);
56+
return skduid;
57+
}
58+
}
59+
60+
return reinterpret_cast<sourcekitd_uid_t>(UID.getAsOpaqueValue());
61+
}
62+
63+
UIdent sourcekitd::UIdentFromSKDUID(sourcekitd_uid_t uid) {
64+
if (StrFromUidMappingHandler)
65+
return UIdent(StrFromUidMappingHandler(uid));
66+
67+
return UIdent::getFromOpaqueValue(uid);
68+
}

0 commit comments

Comments
 (0)