From c15f293fca844d29956f6cbf45a6f5bb340e8f4f Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 31 Aug 2023 18:05:40 -0700 Subject: [PATCH 01/50] build: clean up dependency tracking for portability `:` is not a valid file system character but is used to namespace the imported targets. This is then used to create the stamp file. Sanitize the name prior to use as a stamp file name. --- cmake/modules/AddPureSwift.cmake | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index fc7ded0db64b5..a5954c114cf6d 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -5,17 +5,16 @@ function(force_target_link_libraries TARGET) cmake_parse_arguments(ARGS "" "" "PUBLIC" ${ARGN}) foreach(DEPENDENCY ${ARGS_PUBLIC}) - target_link_libraries(${TARGET} PRIVATE - ${DEPENDENCY} - ) + target_link_libraries(${TARGET} PRIVATE ${DEPENDENCY}) add_dependencies(${TARGET} ${DEPENDENCY}) - add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift - COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift + string(REGEX REPLACE [<>:\"/\\|?*] _ sanitized ${DEPENDENCY}) + add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift + COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift DEPENDS ${DEPENDENCY} ) target_sources(${TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/forced-${DEPENDENCY}-dep.swift + ${CMAKE_CURRENT_BINARY_DIR}/forced-${sanitized}-dep.swift ) endforeach() endfunction() From f15a99bbc02a0263620b7ecdf75b7922d442360d Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 31 Aug 2023 18:06:29 -0700 Subject: [PATCH 02/50] build: quote possibly empty parameters `CMAKE_SHARED_LIBRARY_PREFIX` is empty on Windows. This accounts for that and quotes the parameter which we otherwise will see as an error due to insufficient parameters being passed. --- lib/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 846d165345b56..8675e6098410c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -43,10 +43,10 @@ if (SWIFT_SWIFT_PARSER) # Determine the SwiftSyntax shared library files that were built as # part of earlyswiftsyntax. - list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND ${CMAKE_SHARED_LIBRARY_PREFIX} + list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "${CMAKE_SHARED_LIBRARY_PREFIX}" OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES) list(TRANSFORM SWIFT_SYNTAX_SHARED_LIBRARIES APPEND - ${CMAKE_SHARED_LIBRARY_SUFFIX} + "${CMAKE_SHARED_LIBRARY_SUFFIX}" OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES) # Interface library to collect swiftinterfaces and swiftmodules from From 2d6a66e13fc5e66666b7119eed3e9f59449d946a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 1 Sep 2023 09:58:49 -0700 Subject: [PATCH 03/50] swift-plugin-server: adjust API for portability This begins reworking the API to be less POSIX centric and more generally usable. The API was defined in terms of `dlopen`/`dlsym` rather than the better suited `llvm::sys::DynamicLibrary` APIs which would avoid most of the work that needs to be done here for platform specifics. --- .../CSwiftPluginServer/PluginServer.cpp | 103 ++++++++++++++---- .../CSwiftPluginServer/include/PluginServer.h | 7 +- .../swift-plugin-server.swift | 2 +- 3 files changed, 86 insertions(+), 26 deletions(-) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp index 707b40a5d182c..b84bdeaa173f7 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp @@ -13,11 +13,17 @@ #include "PluginServer.h" #include "swift/ABI/MetadataValues.h" #include "swift/Demangling/Demangle.h" +#include "llvm/Support/DynamicLibrary.h" +#if defined(_WIN32) +#include +#elif defined(__unix__) || defined(__APPLE__) #include +#include +#endif + #include #include -#include using namespace swift; @@ -32,6 +38,46 @@ struct ConnectionHandle { } // namespace const void *PluginServer_createConnection(const char **errorMessage) { +#if defined(_WIN32) + struct unique_fd { + unique_fd(int fd) : fd_(fd) {} + unique_fd(const unique_fd &) = delete; + unique_fd &operator=(const unique_fd &) = delete; + unique_fd &operator=(unique_fd &&) = delete; + unique_fd(unique_fd &&uf) : fd_(uf.fd_) { uf.fd_ = -1; } + ~unique_fd() { if (fd_ > 0) _close(fd_); } + + int operator*() const { return fd_; } + int release() { int fd = fd_; fd_ = -1; return fd; } + + private: + int fd_; + }; + + unique_fd ifd{_dup(_fileno(stdin))}; + if (*ifd < 0) { + *errorMessage = _strerror(nullptr); + return nullptr; + } + + if (_close(_fileno(stdin)) < 0) { + *errorMessage = _strerror(nullptr); + return nullptr; + } + + unique_fd ofd{_dup(_fileno(stdout))}; + if (*ofd < 0) { + *errorMessage = _strerror(nullptr); + return nullptr; + } + + if (_dup2(_fileno(stderr), _fileno(stdout)) < 0) { + *errorMessage = _strerror(nullptr); + return nullptr; + } + + return new ConnectionHandle(ifd.release(), ofd.release()); +#else // Duplicate the `stdin` file descriptor, which we will then use for // receiving messages from the plugin host. auto inputFD = dup(STDIN_FILENO); @@ -65,37 +111,48 @@ const void *PluginServer_createConnection(const char **errorMessage) { // Open a message channel for communicating with the plugin host. return new ConnectionHandle(inputFD, outputFD); +#endif } -void PluginServer_destroyConnection(const void *connHandle) { - const auto *conn = static_cast(connHandle); - delete conn; +void PluginServer_destroyConnection(const void *server) { + delete static_cast(server); } -long PluginServer_read(const void *connHandle, void *data, - unsigned long nbyte) { - const auto *conn = static_cast(connHandle); - return ::read(conn->inputFD, data, nbyte); +size_t PluginServer_read(const void *server, void *data, size_t nbyte) { + const auto *connection = static_cast(server); +#if defined(_WIN32) + return _read(connection->inputFD, data, nbyte); +#else + return ::read(connection->inputFD, data, nbyte); +#endif } -long PluginServer_write(const void *connHandle, const void *data, - unsigned long nbyte) { - const auto *conn = static_cast(connHandle); - return ::write(conn->outputFD, data, nbyte); +size_t PluginServer_write(const void *server, const void *data, size_t nbyte) { + const auto *connection = static_cast(server); +#if defined(_WIN32) + return _write(connection->outputFD, data, nbyte); +#else + return ::write(connection->outputFD, data, nbyte); +#endif } -void *PluginServer_dlopen(const char *filename, const char **errorMessage) { - auto *handle = ::dlopen(filename, RTLD_LAZY | RTLD_LOCAL); - if (!handle) { - *errorMessage = dlerror(); - } - return handle; +void *PluginServer_load(const char *plugin, const char **errorMessage) { + // Use a static allocation for the error as the client will not release the + // string. POSIX 2008 (IEEE-1003.1-2008) specifies that it is implementation + // defined if `dlerror` is re-entrant. Take advantage of that and make it + // thread-unsafe. This ensures that the string outlives the call permitting + // the client to duplicate it. + static std::string error; + auto library = llvm::sys::DynamicLibrary::getLibrary(plugin, &error); + if (library.isValid()) + return library.getOSSpecificHandle(); + *errorMessage = error.c_str(); + return nullptr; } const void *PluginServer_lookupMacroTypeMetadataByExternalName( const char *moduleName, const char *typeName, void *libraryHint, const char **errorMessage) { - // Look up the type metadata accessor as a struct, enum, or class. const Demangle::Node::Kind typeKinds[] = { Demangle::Node::Kind::Structure, @@ -108,8 +165,12 @@ const void *PluginServer_lookupMacroTypeMetadataByExternalName( auto symbolName = mangledNameForTypeMetadataAccessor(moduleName, typeName, typeKind); - auto *handle = libraryHint ? libraryHint : RTLD_DEFAULT; - accessorAddr = ::dlsym(handle, symbolName.c_str()); +#if !defined(_WIN32) + if (libraryHint == nullptr) + libraryHint = RTLD_DEFAULT; +#endif + accessorAddr = llvm::sys::DynamicLibrary{libraryHint} + .getAddressOfSymbol(symbolName.c_str()); if (accessorAddr) break; } diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index dba78fedf8139..94ea5b36dea43 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -29,18 +29,17 @@ const void *PluginServer_createConnection(const char **errorMessage); void PluginServer_destroyConnection(const void *connHandle); /// Read bytes from the IPC communication handle. -long PluginServer_read(const void *connHandle, void *data, unsigned long nbyte); +size_t PluginServer_read(const void *connHandle, void *data, size_t nbyte); /// Write bytes to the IPC communication handle. -long PluginServer_write(const void *connHandle, const void *data, - unsigned long nbyte); +size_t PluginServer_write(const void *connHandle, const void *data, size_t nbyte); //===----------------------------------------------------------------------===// // Dynamic link //===----------------------------------------------------------------------===// /// Load a dynamic link library, and return the handle. -void *PluginServer_dlopen(const char *filename, const char **errorMessage); +void *PluginServer_load(const char *filename, const char **errorMessage); /// Resolve a type metadata by a pair of the module name and the type name. /// 'libraryHint' is a diff --git a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift index e1f8bae8b898a..801c96ff06b7e 100644 --- a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift +++ b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift @@ -47,7 +47,7 @@ extension SwiftPluginServer: PluginProvider { /// Load a macro implementation from the dynamic link library. func loadPluginLibrary(libraryPath: String, moduleName: String) throws { var errorMessage: UnsafePointer? - guard let dlHandle = PluginServer_dlopen(libraryPath, &errorMessage) else { + guard let dlHandle = PluginServer_load(libraryPath, &errorMessage) else { throw PluginServerError(message: String(cString: errorMessage!)) } loadedLibraryPlugins[moduleName] = dlHandle From be13e0b5d606f15faeb10a192b86220a7b6fbccc Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 10:39:14 -0700 Subject: [PATCH 04/50] Update PluginServer.h Include `stdint.h` for non-MSVC compilers. --- .../Sources/CSwiftPluginServer/include/PluginServer.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 94ea5b36dea43..09384a1a2ad79 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -13,6 +13,8 @@ #ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H #define SWIFT_PLUGINSERVER_PLUGINSERVER_H +#include + #ifdef __cplusplus extern "C" { #endif From 4eb70d94de9a0db329d0f2c9608baa02f06da1fd Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 18:56:34 -0700 Subject: [PATCH 05/50] Update PluginServer.h --- .../Sources/CSwiftPluginServer/include/PluginServer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 09384a1a2ad79..92e62c3aa7bc4 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -13,6 +13,7 @@ #ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H #define SWIFT_PLUGINSERVER_PLUGINSERVER_H +#include #include #ifdef __cplusplus From 89067bfc1f29ec86c3d0beecee2b3b7ecaf92b96 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 19:20:56 -0700 Subject: [PATCH 06/50] Update swift-plugin-server.swift --- .../Sources/swift-plugin-server/swift-plugin-server.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift index 801c96ff06b7e..8c06e6dcd5d5a 100644 --- a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift +++ b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift @@ -172,7 +172,7 @@ final class PluginHostConnection: MessageConnection { var ptr = buffer.baseAddress! while (bytesToWrite > 0) { - let writtenSize = PluginServer_write(handle, ptr, UInt(bytesToWrite)) + let writtenSize = PluginServer_write(handle, ptr, Int(bytesToWrite)) if (writtenSize <= 0) { // error e.g. broken pipe. break @@ -193,7 +193,7 @@ final class PluginHostConnection: MessageConnection { var ptr = buffer.baseAddress! while bytesToRead > 0 { - let readSize = PluginServer_read(handle, ptr, UInt(bytesToRead)) + let readSize = PluginServer_read(handle, ptr, Int(bytesToRead)) if (readSize <= 0) { // 0: EOF (the host closed), -1: Broken pipe (the host crashed?) break; From 2227f8e90291b5e6aa10576c6a390413cddad625 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 11:04:24 -0700 Subject: [PATCH 07/50] [Macros] Replace use of `free` for Swift-allocated pointer We cannot depend on Swift's unsafe pointers using `malloc` under the hood, so don't use `free` on them. Instead, expose entry points from Swift to C++ to free any Swift-allocated data structures that need to be deallocated from C++. --- lib/ASTGen/Sources/ASTGen/Macros.swift | 13 +++++++++++++ lib/Sema/TypeCheckMacros.cpp | 13 +++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index 4ee025884bd42..fd3348504246e 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -167,6 +167,11 @@ func allocateUTF8String( } } +@_cdecl("swift_ASTGen_freeString") +public func freeString(pointer: UnsafePointer?) { + pointer?.deallocate() +} + /// Diagnostics produced here. enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage { case thrownError(Error) @@ -410,6 +415,14 @@ func checkMacroDefinition( } } +@_cdecl("swift_ASTGen_freeExpansionReplacements") +public func freeExpansionReplacements( + pointer: UnsafeMutablePointer?, + numReplacements: Int +) { + UnsafeMutableBufferPointer(start: pointer, count: numReplacements).deallocate() +} + // Make an expansion result for '@_cdecl' function caller. func makeExpansionOutputResult( expandedSource: String?, diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index 07eeebbc10baf..385dac2818162 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -49,6 +49,8 @@ using namespace swift; extern "C" void *swift_ASTGen_resolveMacroType(const void *macroType); extern "C" void swift_ASTGen_destroyMacro(void *macro); +extern "C" void swift_ASTGen_freeString(const char *str); + extern "C" void *swift_ASTGen_resolveExecutableMacro( const char *moduleName, ptrdiff_t moduleNameLength, const char *typeName, ptrdiff_t typeNameLength, @@ -64,6 +66,9 @@ extern "C" ptrdiff_t swift_ASTGen_checkMacroDefinition( ptrdiff_t **replacementsPtr, ptrdiff_t *numReplacements ); +extern "C" void swift_ASTGen_freeExpansionReplacements( + ptrdiff_t *replacementsPtr, + ptrdiff_t numReplacements); extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro( void *diagEngine, void *macro, uint8_t externalKind, @@ -197,8 +202,8 @@ MacroDefinition MacroDefinitionRequest::evaluate( // Clean up after the call. SWIFT_DEFER { - free(externalMacroNamePtr); - free(replacements); + swift_ASTGen_freeString(externalMacroNamePtr); + swift_ASTGen_freeExpansionReplacements(replacements, numReplacements); }; if (checkResult < 0 && ctx.CompletionCallback) { @@ -1054,7 +1059,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion, evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy( {evaluatedSourceAddress, (size_t)evaluatedSourceLength}, adjustMacroExpansionBufferName(*discriminator)); - free((void *)evaluatedSourceAddress); + swift_ASTGen_freeString(evaluatedSourceAddress); break; #else ctx.Diags.diagnose(loc, diag::macro_unsupported); @@ -1333,7 +1338,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy( {evaluatedSourceAddress, (size_t)evaluatedSourceLength}, adjustMacroExpansionBufferName(*discriminator)); - free((void *)evaluatedSourceAddress); + swift_ASTGen_freeString(evaluatedSourceAddress); break; #else attachedTo->diagnose(diag::macro_unsupported); From c640550bafca4bb0f3a6ef1a29d40a56f7722b10 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 11:32:33 -0700 Subject: [PATCH 08/50] Use MallocAllocator Allocate/Deallocate consistently. Buyer beware: the `MallocAllocator` does not, in fact, use `malloc`, so one cannot pair an allocation using `MallocAllocator` with an actual `free`. --- lib/AST/CASTBridging.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/AST/CASTBridging.cpp b/lib/AST/CASTBridging.cpp index 5b935d4cebb11..ca5fe428fe90f 100644 --- a/lib/AST/CASTBridging.cpp +++ b/lib/AST/CASTBridging.cpp @@ -27,6 +27,8 @@ static SourceLoc getSourceLocFromPointer(const void *loc) { namespace { struct BridgedDiagnosticImpl { + typedef llvm::MallocAllocator Allocator; + InFlightDiagnostic inFlight; std::vector textBlobs; @@ -37,8 +39,10 @@ namespace { ~BridgedDiagnosticImpl() { inFlight.flush(); + + Allocator allocator; for (auto text: textBlobs) { - free((void*)text.data()); + allocator.Deallocate(text.data(), text.size()); } } }; @@ -51,8 +55,8 @@ BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, long textLen) { StringRef origText{ reinterpret_cast(textPtr), size_t(textLen)}; - llvm::MallocAllocator mallocAlloc; - StringRef text = origText.copy(mallocAlloc); + BridgedDiagnosticImpl::Allocator alloc; + StringRef text = origText.copy(alloc); SourceLoc loc = getSourceLocFromPointer(sourceLocPtr); @@ -102,8 +106,8 @@ void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diagPtr, StringRef origReplaceText{ reinterpret_cast(newTextPtr), size_t(newTextLen)}; - llvm::MallocAllocator mallocAlloc; - StringRef replaceText = origReplaceText.copy(mallocAlloc); + BridgedDiagnosticImpl::Allocator alloc; + StringRef replaceText = origReplaceText.copy(alloc); BridgedDiagnosticImpl *impl = static_cast(diagPtr); impl->textBlobs.push_back(replaceText); From 22cee9088f5d04ed5877387efd3a49958f4d8025 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 12:13:35 -0700 Subject: [PATCH 09/50] [Swift bridging] Use Swift(U)Int consistently for bridging --- include/swift/AST/CASTBridging.h | 27 ++++++++++++--------------- include/swift/Basic/CBasicBridging.h | 5 ++++- lib/AST/CASTBridging.cpp | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/swift/AST/CASTBridging.h b/include/swift/AST/CASTBridging.h index 4699a291a1fc2..e30951d490749 100644 --- a/include/swift/AST/CASTBridging.h +++ b/include/swift/AST/CASTBridging.h @@ -39,12 +39,9 @@ SWIFT_BEGIN_NULLABILITY_ANNOTATIONS -typedef long SwiftInt; -typedef unsigned long SwiftUInt; - typedef struct { const void *_Nullable data; - long numElements; + SwiftInt numElements; } BridgedArrayRef; typedef struct { @@ -65,7 +62,7 @@ typedef struct { void *_Nullable TrailingCommaLoc; } BridgedTupleTypeElement; -typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : long { +typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedRequirementReprKind : SwiftInt { /// A type bound T : P, where T is a type that depends on a generic /// parameter and P is some type that should bound T, either as a concrete /// supertype or a protocol to which T must conform. @@ -92,7 +89,7 @@ typedef struct { } BridgedRequirementRepr; /// Diagnostic severity when reporting diagnostics. -typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long { +typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : SwiftInt { BridgedFatalError, BridgedError, BridgedWarning, @@ -102,7 +99,7 @@ typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedDiagnosticSeverity : long { typedef void* BridgedDiagnostic; -typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : long { +typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : SwiftInt { /// An expanded macro. BridgedExpandedMacro = 0, /// An external macro, spelled with either the old spelling (Module.Type) @@ -130,7 +127,7 @@ BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, BridgedDiagnosticSeverity severity, const void *_Nullable sourceLoc, const unsigned char *_Nullable text, - long textLen); + SwiftInt textLen); /// Highlight a source range as part of the diagnostic. void SwiftDiagnostic_highlight( @@ -141,14 +138,14 @@ void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diag, const void *_Nullable replaceStartLoc, const void *_Nullable replaceEndLoc, const unsigned char *_Nullable newText, - long newTextLen); + SwiftInt newTextLen); /// Finish the given diagnostic and emit it. void SwiftDiagnostic_finish(BridgedDiagnostic diag); BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str, - long len); + SwiftInt len); void *SwiftImportDecl_create(void *, void *, void *, char, void *, BridgedArrayRef, BridgedArrayRef); @@ -173,11 +170,11 @@ void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc); void *SwiftStringLiteralExpr_create(void *ctx, const unsigned char *_Nullable string, - long len, void *TokenLoc); + SwiftInt len, void *TokenLoc); void *SwiftIntegerLiteralExpr_create(void *ctx, const unsigned char *_Nullable string, - long len, void *TokenLoc); + SwiftInt len, void *TokenLoc); void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc); @@ -194,7 +191,7 @@ void *SingleValueStmtExpr_createWithWrappedBranches(void *ctx, void *S, void *IfStmt_create(void *ctx, void *ifLoc, void *cond, void *_Nullable then, void *_Nullable elseLoc, void *_Nullable elseStmt); -typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : long { +typedef enum ENUM_EXTENSIBILITY_ATTR(open) ASTNodeKind : SwiftInt { ASTNodeKindExpr, ASTNodeKindStmt, ASTNodeKindDecl @@ -208,7 +205,7 @@ struct ASTNodeBridged { void *BraceStmt_create(void *ctx, void *lbloc, BridgedArrayRef elements, void *rbloc); -void *BridgedSourceLoc_advanced(void *loc, long len); +void *BridgedSourceLoc_advanced(void *loc, SwiftInt len); void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc, void *_Nullable argName, void *_Nullable paramLoc, @@ -282,7 +279,7 @@ void *GenericParamList_create(void *ctx, void *lAngleLoc, BridgedArrayRef reqs, void *rAngleLoc); void *GenericTypeParamDecl_create(void *ctx, void *declContext, BridgedIdentifier name, void *nameLoc, - void *_Nullable eachLoc, long index, + void *_Nullable eachLoc, SwiftInt index, _Bool isParameterPack); void GenericTypeParamDecl_setInheritedType(void *ctx, void *Param, void *ty); diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 86e7d5081d6dc..dae2f42ec374f 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -46,9 +46,12 @@ extern "C" { #endif +typedef long SwiftInt; +typedef unsigned long SwiftUInt; + typedef struct BridgedData { const char *_Nullable baseAddress; - unsigned long size; + SwiftUInt size; } BridgedData; void BridgedData_free(BridgedData data); diff --git a/lib/AST/CASTBridging.cpp b/lib/AST/CASTBridging.cpp index ca5fe428fe90f..83afd905825bf 100644 --- a/lib/AST/CASTBridging.cpp +++ b/lib/AST/CASTBridging.cpp @@ -149,7 +149,7 @@ void *SwiftImportDecl_create(void *ctx, void *dc, void *importLoc, char kind, getSourceLocFromPointer(kindLoc), std::move(importPath).get()); } -void *BridgedSourceLoc_advanced(void *loc, long len) { +void *BridgedSourceLoc_advanced(void *loc, SwiftInt len) { SourceLoc l = getSourceLocFromPointer(loc).getAdvancedLoc(len); return const_cast(l.getOpaquePointerValue()); } @@ -586,7 +586,7 @@ void *GenericParamList_create(void *ctx, void *lAngleLoc, void *GenericTypeParamDecl_create(void *ctx, void *declContext, BridgedIdentifier name, void *nameLoc, - void *_Nullable eachLoc, long index, + void *_Nullable eachLoc, SwiftInt index, bool isParameterPack) { return GenericTypeParamDecl::createParsed( static_cast(declContext), @@ -699,6 +699,6 @@ bool Plugin_waitForNextMessage(PluginHandle handle, BridgedData *out) { auto size = message.size(); auto outPtr = malloc(size); memcpy(outPtr, message.data(), size); - *out = BridgedData{(const char *)outPtr, (unsigned long)size}; + *out = BridgedData{(const char *)outPtr, (SwiftUInt)size}; return false; } From ada25352da28609736875d77f9ec368fd17fba62 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 12:18:33 -0700 Subject: [PATCH 10/50] Define the C++ Swift(U)Int types using the same logic as SwiftShims The C type tha corresponds to Swift's pointer-sized `Int` and `UInt` types varies from one platform to the next. The canonical C types are `ptrdiff_t` and `size_t`, but we're in the depths of the compiler we can't include the C library headers that provide them because they introduce cyclic module dependencies. Sigh. SwiftShims has some logic to compute these types. However, SwiftShims is part of the Swift runtime, not the compiler, so those headers cannot be included here. So, we clone the logic and simplify it somewhat for our use case. This fixes truncation issues on Windows, where the uses of `unsigned long` and `long` for Swift(U)Int are incorrect. --- include/swift/Basic/CBasicBridging.h | 78 ++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 16 deletions(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index dae2f42ec374f..b430128bc4860 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -20,21 +20,62 @@ // it causes importing the "Darwin"/"Glibc" overlay module. That violates // layering. i.e. Darwin overlay is created by Swift compiler. -#if __clang__ -// Provide macros to temporarily suppress warning about the use of -// _Nullable and _Nonnull. -#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \ - _Pragma("clang diagnostic push") \ - _Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \ - _Pragma("clang assume_nonnull begin") - -#define SWIFT_END_NULLABILITY_ANNOTATIONS \ - _Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end") +// NOTE: Partially ported from SwiftShim's SwiftStdint.h. We cannot include +// that header here because it belongs to the runtime, but we need the same +// logic for interoperability with Swift code in the compiler itself. +// stdint.h is provided by Clang, but it dispatches to libc's stdint.h. As a +// result, using stdint.h here would pull in Darwin module (which includes +// libc). This creates a dependency cycle, so we can't use stdint.h in +// SwiftShims. +// On Linux, the story is different. We get the error message +// "/usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not +// found" +// This is a known Clang/Ubuntu bug. + +// Clang has been defining __INTxx_TYPE__ macros for a long time. +// __UINTxx_TYPE__ are defined only since Clang 3.5. +#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) +#include +typedef int64_t __swiftc_int64_t; +typedef uint64_t __swiftc_uint64_t; +typedef int32_t __swiftc_int32_t; +typedef uint32_t __swiftc_uint32_t; +typedef intptr_t __swiftc_intptr_t; +typedef uintptr_t __swiftc_uintptr_t; #else -#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS -#define SWIFT_END_NULLABILITY_ANNOTATIONS -#define _Nullable -#define _Nonnull +typedef __INT64_TYPE__ __swiftc_int64_t; +#ifdef __UINT64_TYPE__ +typedef __UINT64_TYPE__ __swiftc_uint64_t; +#else +typedef unsigned __INT64_TYPE__ __swiftc_uint64_t; +#endif + +typedef __INT32_TYPE__ __swiftc_int32_t; +#ifdef __UINT32_TYPE__ +typedef __UINT32_TYPE__ __swiftc_uint32_t; +#else +typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; +#endif + +#define __swiftc_join3(a,b,c) a ## b ## c + +#define __swiftc_intn_t(n) __swiftc_join3(__swiftc_int, n, _t) +#define __swiftc_uintn_t(n) __swiftc_join3(__swiftc_uint, n, _t) + +#if defined(_MSC_VER) && !defined(__clang__) +#if defined(_WIN32) +typedef __swiftc_int32_t SwiftInt; +typedef __swiftc_uint32_t SwiftUInt; +#elif defined(_WIN64) +typedef __swiftc_int64_t SwiftInt; +typedef __swiftc_uint64_t SwiftUInt; +#else +#error unknown windows pointer width +#endif +#else +typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt; +typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; +#endif #endif SWIFT_BEGIN_NULLABILITY_ANNOTATIONS @@ -46,8 +87,13 @@ extern "C" { #endif -typedef long SwiftInt; -typedef unsigned long SwiftUInt; +typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature { +#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \ +FeatureName, +#include "swift/Basic/Features.def" +} BridgedFeature; + +SWIFT_BEGIN_ASSUME_NONNULL typedef struct BridgedData { const char *_Nullable baseAddress; From 5c48b709b6a80a1f833f44e49d48047f2a62c035 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 12:40:46 -0700 Subject: [PATCH 11/50] Switch order of _WIN64/_WIN32 checks in computation of C types for Int/UInt 64-bit Windows defines both _WIN64 and _WIN32, so the logic here would always end up defining 32-bit C types for Swift's `Int` and `UInt`. Fix the ordering to check for 64-bit first, then 32-bit second. Note that the SwiftShims version of this code has always been wrong, but it's completely benign because SwiftShims is only used in the Swift runtime itself, which is built with Clang (on all platforms), and doesn't need to go through this code path. Still, we fix it in both places, so we don't get a nasty surprise if the SwiftShims version of the header later gets included in a non-Clang C++ compiler. --- include/swift/Basic/CBasicBridging.h | 8 ++++---- stdlib/public/SwiftShims/swift/shims/SwiftStdint.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index b430128bc4860..0291a21e709ee 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -63,12 +63,12 @@ typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; #define __swiftc_uintn_t(n) __swiftc_join3(__swiftc_uint, n, _t) #if defined(_MSC_VER) && !defined(__clang__) -#if defined(_WIN32) -typedef __swiftc_int32_t SwiftInt; -typedef __swiftc_uint32_t SwiftUInt; -#elif defined(_WIN64) +#if defined(_WIN64) typedef __swiftc_int64_t SwiftInt; typedef __swiftc_uint64_t SwiftUInt; +#elif defined(_WIN32) +typedef __swiftc_int32_t SwiftInt; +typedef __swiftc_uint32_t SwiftUInt; #else #error unknown windows pointer width #endif diff --git a/stdlib/public/SwiftShims/swift/shims/SwiftStdint.h b/stdlib/public/SwiftShims/swift/shims/SwiftStdint.h index 7b83e90d72799..cd8d808cbd6a2 100644 --- a/stdlib/public/SwiftShims/swift/shims/SwiftStdint.h +++ b/stdlib/public/SwiftShims/swift/shims/SwiftStdint.h @@ -71,12 +71,12 @@ typedef unsigned __INT8_TYPE__ __swift_uint8_t; #define __swift_uintn_t(n) __swift_join3(__swift_uint, n, _t) #if defined(_MSC_VER) && !defined(__clang__) -#if defined(_WIN32) -typedef __swift_int32_t __swift_intptr_t; -typedef __swift_uint32_t __swift_uintptr_t; -#elif defined(_WIN64) +#if defined(_WIN64) typedef __swift_int64_t __swift_intptr_t; typedef __swift_uint64_t __swift_uintptr_t; +#elif defined(_WIN32) +typedef __swift_int32_t __swift_intptr_t; +typedef __swift_uint32_t __swift_uintptr_t; #else #error unknown windows pointer width #endif From 9b073f56e0bc33c529062fe350c032247a7bdfdd Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 14:47:58 -0700 Subject: [PATCH 12/50] Fix handling of Swift(U)Int for Windows --- include/swift/Basic/CBasicBridging.h | 7 ++++++- lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 0291a21e709ee..eca0520d9bfb3 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -34,7 +34,12 @@ // Clang has been defining __INTxx_TYPE__ macros for a long time. // __UINTxx_TYPE__ are defined only since Clang 3.5. -#if !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) +#if defined(_MSC_VER) && !defined(__clang__) +typedef __int64 __swiftc_int64_t; +typedef unsigned __int64 __swiftc_uint64_t; +typedef int __swiftc_int32_t; +typedef unsigned int __swiftc_uint32_t; +#elif !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) #include typedef int64_t __swiftc_int64_t; typedef uint64_t __swiftc_uint64_t; diff --git a/lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift b/lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift index c0b98123881d9..1ab7a991c5e43 100644 --- a/lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift +++ b/lib/ASTGen/Sources/LLVMJSON/LLVMJSON.swift @@ -41,7 +41,7 @@ public struct LLVMJSON { /// Decode a JSON data to a Swift value. public static func decode(_ type: T.Type, from json: UnsafeBufferPointer) throws -> T { - let data = BridgedData(baseAddress: json.baseAddress, size: UInt(json.count)) + let data = BridgedData(baseAddress: json.baseAddress, size: SwiftUInt(json.count)) let valuePtr = JSON_deserializedValue(data) defer { JSON_value_delete(valuePtr) } From 225e03b08b67c29e2758b9d89837a1312c86771e Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 15:16:23 -0700 Subject: [PATCH 13/50] Apply suggestions from code review Co-authored-by: Saleem Abdulrasool --- include/swift/Basic/CBasicBridging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index eca0520d9bfb3..47965ce21fac4 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -54,6 +54,7 @@ typedef __UINT64_TYPE__ __swiftc_uint64_t; #else typedef unsigned __INT64_TYPE__ __swiftc_uint64_t; #endif +#endif typedef __INT32_TYPE__ __swiftc_int32_t; #ifdef __UINT32_TYPE__ @@ -81,7 +82,6 @@ typedef __swiftc_uint32_t SwiftUInt; typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt; typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; #endif -#endif SWIFT_BEGIN_NULLABILITY_ANNOTATIONS From b68953f8d080a029e3f89e6270b6815af95de4c2 Mon Sep 17 00:00:00 2001 From: Doug Gregor Date: Fri, 1 Sep 2023 18:27:15 -0700 Subject: [PATCH 14/50] Apply suggestions from code review Co-authored-by: Saleem Abdulrasool --- include/swift/Basic/CBasicBridging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 47965ce21fac4..24b4796c92c2b 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -54,7 +54,6 @@ typedef __UINT64_TYPE__ __swiftc_uint64_t; #else typedef unsigned __INT64_TYPE__ __swiftc_uint64_t; #endif -#endif typedef __INT32_TYPE__ __swiftc_int32_t; #ifdef __UINT32_TYPE__ @@ -62,6 +61,7 @@ typedef __UINT32_TYPE__ __swiftc_uint32_t; #else typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; #endif +#endif #define __swiftc_join3(a,b,c) a ## b ## c From 9f53ff21cedb4380ab1a6bc691c7edca46117eae Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 1 Sep 2023 20:50:36 -0700 Subject: [PATCH 15/50] ASTGen: adjust for type changes The type definitions do not uniformly import as `Swift.Int` and `Swift.UInt`. Add some casts to accommodate the type conversions. --- lib/ASTGen/Sources/ASTGen/ASTGen.swift | 3 ++- lib/ASTGen/Sources/ASTGen/Generics.swift | 3 ++- lib/ASTGen/Sources/ASTGen/Macros.swift | 10 +++++----- lib/ASTGen/Sources/ASTGen/Misc.swift | 1 + lib/ASTGen/Sources/ASTGen/PluginHost.swift | 4 ++-- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/ASTGen.swift b/lib/ASTGen/Sources/ASTGen/ASTGen.swift index 4aa3321190403..7a57715000942 100644 --- a/lib/ASTGen/Sources/ASTGen/ASTGen.swift +++ b/lib/ASTGen/Sources/ASTGen/ASTGen.swift @@ -1,11 +1,12 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax extension Array { public func withBridgedArrayRef(_ c: (BridgedArrayRef) -> T) -> T { withUnsafeBytes { buf in - c(BridgedArrayRef(data: buf.baseAddress!, numElements: count)) + c(BridgedArrayRef(data: buf.baseAddress!, numElements: SwiftInt(count))) } } } diff --git a/lib/ASTGen/Sources/ASTGen/Generics.swift b/lib/ASTGen/Sources/ASTGen/Generics.swift index 22163381143b0..5226846e62b57 100644 --- a/lib/ASTGen/Sources/ASTGen/Generics.swift +++ b/lib/ASTGen/Sources/ASTGen/Generics.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax @@ -36,7 +37,7 @@ extension ASTGenVisitor { return .decl( GenericTypeParamDecl_create( - self.ctx, self.declContext, name, nameLoc, eachLoc, genericParameterIndex, + self.ctx, self.declContext, name, nameLoc, eachLoc, SwiftInt(genericParameterIndex), eachLoc != nil)) } } diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index fd3348504246e..bea7df7337358 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -283,7 +283,7 @@ func checkMacroDefinition( if module == "Builtin" { switch type { case "ExternalMacro": - return BridgedMacroDefinitionKind.builtinExternalMacro.rawValue + return Int(BridgedMacroDefinitionKind.builtinExternalMacro.rawValue) default: // Warn about the unknown builtin. @@ -328,7 +328,7 @@ func checkMacroDefinition( ] ) ) - return BridgedMacroDefinitionKind.externalMacro.rawValue + return Int(BridgedMacroDefinitionKind.externalMacro.rawValue) case let .expansion(expansionSyntax, replacements: _) where expansionSyntax.macro.text == "externalMacro": @@ -367,7 +367,7 @@ func checkMacroDefinition( // Form the "ModuleName.TypeName" result string. (externalMacroPointer.pointee, externalMacroLength.pointee) = allocateUTF8String("\(module).\(type)", nullTerminated: true) - return BridgedMacroDefinitionKind.externalMacro.rawValue + return Int(BridgedMacroDefinitionKind.externalMacro.rawValue) case let .expansion(expansionSyntax, replacements: replacements): // Provide the expansion syntax. @@ -378,7 +378,7 @@ func checkMacroDefinition( // If there are no replacements, we're done. if replacements.isEmpty { - return BridgedMacroDefinitionKind.expandedMacro.rawValue + return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue) } // The replacements are triples: (startOffset, endOffset, parameter index). @@ -393,7 +393,7 @@ func checkMacroDefinition( replacementsPtr.pointee = replacementBuffer.baseAddress numReplacementsPtr.pointee = replacements.count - return BridgedMacroDefinitionKind.expandedMacro.rawValue + return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue) } } catch let errDiags as DiagnosticsError { let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr) diff --git a/lib/ASTGen/Sources/ASTGen/Misc.swift b/lib/ASTGen/Sources/ASTGen/Misc.swift index bb63fd0b1b181..422821744e6b1 100644 --- a/lib/ASTGen/Sources/ASTGen/Misc.swift +++ b/lib/ASTGen/Sources/ASTGen/Misc.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax diff --git a/lib/ASTGen/Sources/ASTGen/PluginHost.swift b/lib/ASTGen/Sources/ASTGen/PluginHost.swift index 33a87ca898eb2..2d48e5d59d6d0 100644 --- a/lib/ASTGen/Sources/ASTGen/PluginHost.swift +++ b/lib/ASTGen/Sources/ASTGen/PluginHost.swift @@ -121,7 +121,7 @@ struct CompilerPlugin { private func sendMessage(_ message: HostToPluginMessage) throws { let hadError = try LLVMJSON.encoding(message) { (data) -> Bool in - return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: UInt(data.count))) + return Plugin_sendMessage(opaqueHandle, BridgedData(baseAddress: data.baseAddress, size: SwiftUInt(data.count))) } if hadError { throw PluginError.failedToSendMessage @@ -343,7 +343,7 @@ class PluginDiagnosticsEngine { else { return nil } - return bufferBaseAddress.advanced(by: offset) + return bufferBaseAddress.advanced(by: SwiftInt(offset)) } /// C++ source location from a position value from a plugin. From 5f678b468a82e4917fb05858d9cdc252146855dd Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 1 Sep 2023 14:27:54 -0700 Subject: [PATCH 16/50] AST,Basic: update support for process execution on Windows Implement process launching on Windows to support macros. Prefer to use the LLVM types wherever possible. The pipes are converted into file descriptors as the types are internal to the process. This allows us to have similar paths on both sides and avoid having to drag in `Windows.h` for the definition of `HANDLE`. This is the core missing functionality for Windows to support macros. --- include/swift/AST/PluginRegistry.h | 14 ++-- include/swift/Basic/Program.h | 12 ++-- lib/AST/PluginLoader.cpp | 5 ++ lib/AST/PluginRegistry.cpp | 53 +++++++++++---- lib/Basic/Program.cpp | 101 +++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 28 deletions(-) diff --git a/include/swift/AST/PluginRegistry.h b/include/swift/AST/PluginRegistry.h index e2f1fcb0f64a6..8ee553c8903c4 100644 --- a/include/swift/AST/PluginRegistry.h +++ b/include/swift/AST/PluginRegistry.h @@ -52,14 +52,13 @@ class LoadedExecutablePlugin { /// Represents the current process of the executable plugin. struct PluginProcess { - const llvm::sys::procid_t pid; - const int inputFileDescriptor; - const int outputFileDescriptor; + const llvm::sys::ProcessInfo process; + const int input; + const int output; bool isStale = false; - PluginProcess(llvm::sys::procid_t pid, int inputFileDescriptor, - int outputFileDescriptor); - + PluginProcess(llvm::sys::ProcessInfo process, int input, int output) + : process(process), input(input), output(output) {} ~PluginProcess(); ssize_t write(const void *buf, size_t nbyte) const; @@ -138,7 +137,8 @@ class LoadedExecutablePlugin { llvm::erase_value(onReconnect, fn); } - llvm::sys::procid_t getPid() { return Process->pid; } + llvm::sys::procid_t getPid() { return Process->process.Pid; } + llvm::sys::process_t getProcess() { return Process->process.Process; } NullTerminatedStringRef getExecutablePath() { return {ExecutablePath.c_str(), ExecutablePath.size()}; diff --git a/include/swift/Basic/Program.h b/include/swift/Basic/Program.h index 0a4acffe23f82..6efa12b40773a 100644 --- a/include/swift/Basic/Program.h +++ b/include/swift/Basic/Program.h @@ -40,14 +40,12 @@ int ExecuteInPlace(const char *Program, const char **args, const char **env = nullptr); struct ChildProcessInfo { - llvm::sys::procid_t Pid; - int WriteFileDescriptor; - int ReadFileDescriptor; + llvm::sys::ProcessInfo ProcessInfo; + int Write; + int Read; - ChildProcessInfo(llvm::sys::procid_t Pid, int WriteFileDescriptor, - int ReadFileDescriptor) - : Pid(Pid), WriteFileDescriptor(WriteFileDescriptor), - ReadFileDescriptor(ReadFileDescriptor) {} + ChildProcessInfo(llvm::sys::ProcessInfo ProcessInfo, int Write, int Read) + : ProcessInfo(ProcessInfo), Write(Write), Read(Read) {} }; /// This function executes the program using the argument provided. diff --git a/lib/AST/PluginLoader.cpp b/lib/AST/PluginLoader.cpp index 2f89336577107..3a78138fb766f 100644 --- a/lib/AST/PluginLoader.cpp +++ b/lib/AST/PluginLoader.cpp @@ -41,8 +41,13 @@ PluginRegistry *PluginLoader::getRegistry() { static StringRef pluginModuleNameStringFromPath(StringRef path) { // Plugin library must be named 'lib${module name}(.dylib|.so|.dll)'. // FIXME: Shared library prefix might be different between platforms. +#if defined(_WIN32) + constexpr StringRef libPrefix{}; + constexpr StringRef libSuffix = ".dll"; +#else constexpr StringRef libPrefix = "lib"; constexpr StringRef libSuffix = LTDL_SHLIB_EXT; +#endif StringRef filename = llvm::sys::path::filename(path); if (filename.starts_with(libPrefix) && filename.ends_with(libSuffix)) { diff --git a/lib/AST/PluginRegistry.cpp b/lib/AST/PluginRegistry.cpp index 25cdcf62d6fe5..6ddec5387c321 100644 --- a/lib/AST/PluginRegistry.cpp +++ b/lib/AST/PluginRegistry.cpp @@ -75,7 +75,9 @@ void *LoadedLibraryPlugin::getAddressOfSymbol(const char *symbolName) { auto &cached = resolvedSymbols[symbolName]; if (cached) return cached; -#if !defined(_WIN32) +#if defined(_WIN32) + cached = GetProcAddress(static_cast(handle), symbolName); +#else cached = dlsym(handle, symbolName); #endif return cached; @@ -153,9 +155,8 @@ llvm::Error LoadedExecutablePlugin::spawnIfNeeded() { return llvm::errorCodeToError(childInfo.getError()); } - Process = std::make_unique(childInfo->Pid, - childInfo->ReadFileDescriptor, - childInfo->WriteFileDescriptor); + Process = std::make_unique(childInfo->ProcessInfo, + childInfo->Read, childInfo->Write); // Call "on reconnect" callbacks. for (auto *callback : onReconnect) { @@ -165,15 +166,15 @@ llvm::Error LoadedExecutablePlugin::spawnIfNeeded() { return llvm::Error::success(); } -LoadedExecutablePlugin::PluginProcess::PluginProcess(llvm::sys::procid_t pid, - int inputFileDescriptor, - int outputFileDescriptor) - : pid(pid), inputFileDescriptor(inputFileDescriptor), - outputFileDescriptor(outputFileDescriptor) {} - LoadedExecutablePlugin::PluginProcess::~PluginProcess() { - close(inputFileDescriptor); - close(outputFileDescriptor); +#if defined(_WIN32) + _close(input); + _close(output); + CloseHandle(process.Process); +#else + close(input); + close(output); +#endif } LoadedExecutablePlugin::~LoadedExecutablePlugin() { @@ -184,6 +185,17 @@ LoadedExecutablePlugin::~LoadedExecutablePlugin() { ssize_t LoadedExecutablePlugin::PluginProcess::read(void *buf, size_t nbyte) const { +#if defined(_WIN32) + size_t nread = 0; + while (nread < nbyte) { + int n = _read(input, static_cast(buf) + nread, + std::min(static_cast(UINT32_MAX), nbyte - nread)); + if (n <= 0) + break; + nread += n; + } + return nread; +#else ssize_t bytesToRead = nbyte; void *ptr = buf; @@ -206,10 +218,22 @@ ssize_t LoadedExecutablePlugin::PluginProcess::read(void *buf, } return nbyte - bytesToRead; +#endif } ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf, size_t nbyte) const { +#if defined(_WIN32) + size_t nwritten = 0; + while (nwritten < nbyte) { + int n = _write(output, static_cast(buf) + nwritten, + std::min(static_cast(UINT32_MAX), nbyte - nwritten)); + if (n <= 0) + break; + nwritten += n; + } + return nwritten; +#else ssize_t bytesToWrite = nbyte; const void *ptr = buf; @@ -231,13 +255,14 @@ ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf, bytesToWrite -= writtenSize; } return nbyte - bytesToWrite; +#endif } llvm::Error LoadedExecutablePlugin::sendMessage(llvm::StringRef message) const { ssize_t writtenSize = 0; if (dumpMessaging) { - llvm::dbgs() << "->(plugin:" << Process->pid << ") " << message << "\n"; + llvm::dbgs() << "->(plugin:" << Process->process.Pid << ") " << message << '\n'; } const char *data = message.data(); @@ -297,7 +322,7 @@ llvm::Expected LoadedExecutablePlugin::waitForNextMessage() const { } if (dumpMessaging) { - llvm::dbgs() << "<-(plugin:" << Process->pid << ") " << message << "\n"; + llvm::dbgs() << "<-(plugin:" << Process->process.Pid << ") " << message << "\n"; } return message; diff --git a/lib/Basic/Program.cpp b/lib/Basic/Program.cpp index 8c322f865a6fa..b0f2cc838f788 100644 --- a/lib/Basic/Program.cpp +++ b/lib/Basic/Program.cpp @@ -18,7 +18,11 @@ #include "llvm/Config/config.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Program.h" +#if defined(_WIN32) +#include "llvm/Support/Windows/WindowsSupport.h" +#endif +#include #include #if HAVE_POSIX_SPAWN @@ -29,6 +33,12 @@ #include #endif +#if defined(_WIN32) +#define WIN32_LEAN_AND_MEAN +#include +#include +#endif + using namespace swift; int swift::ExecuteInPlace(const char *Program, const char **args, @@ -178,6 +188,97 @@ swift::ExecuteWithPipe(llvm::StringRef program, return ChildProcessInfo(pid, p1.write, p2.read); } +#elif defined(_WIN32) + +llvm::ErrorOr +swift::ExecuteWithPipe(llvm::StringRef program, + llvm::ArrayRef args, + llvm::Optional> env) { + using unique_handle = std::unique_ptr; + enum { PI_READ, PI_WRITE }; + + unique_handle input[2] = { + {INVALID_HANDLE_VALUE, CloseHandle}, + {INVALID_HANDLE_VALUE, CloseHandle}, + }; + unique_handle output[2] = { + {INVALID_HANDLE_VALUE, CloseHandle}, + {INVALID_HANDLE_VALUE, CloseHandle}, + }; + unique_handle error{INVALID_HANDLE_VALUE, CloseHandle}; + HANDLE hRead = INVALID_HANDLE_VALUE, hWrite = INVALID_HANDLE_VALUE; + SECURITY_ATTRIBUTES saAttrs{sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; + + if (!CreatePipe(&hRead, &hWrite, &saAttrs, 0)) + return std::error_code(GetLastError(), std::system_category()); + output[PI_READ].reset(hRead); + output[PI_WRITE].reset(hWrite); + + if (!SetHandleInformation(output[PI_READ].get(), HANDLE_FLAG_INHERIT, FALSE)) + return std::error_code(GetLastError(), std::system_category()); + + if (!CreatePipe(&hRead, &hWrite, &saAttrs, 0)) + return std::error_code(GetLastError(), std::system_category()); + input[PI_READ].reset(hRead); + input[PI_WRITE].reset(hWrite); + + if (!SetHandleInformation(input[PI_WRITE].get(), HANDLE_FLAG_INHERIT, FALSE)) + return std::error_code(GetLastError(), std::system_category()); + + if (!DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), + GetCurrentProcess(), &hWrite, DUPLICATE_SAME_ACCESS, + TRUE, DUPLICATE_SAME_ACCESS)) + return std::error_code(GetLastError(), std::system_category()); + error.reset(hWrite); + + STARTUPINFO si = {0}; + si.cb = sizeof(si); + si.hStdInput = input[PI_READ].get(); + si.hStdOutput = output[PI_WRITE].get(); + si.hStdError = error.get(); + si.dwFlags = STARTF_USESTDHANDLES; + + llvm::SmallVector executable; + if (std::error_code ec = llvm::sys::windows::widenPath(program, executable)) + return ec; + + std::vector components; + components.push_back(program); + components.assign(args.begin(), args.end()); + llvm::ErrorOr commandline = + llvm::sys::flattenWindowsCommandLine(components); + if (!commandline) + return commandline.getError(); + + std::vector command(commandline->size() + 1, 0); + std::copy(commandline->begin(), commandline->end(), command.begin()); + + PROCESS_INFORMATION pi = {0}; + if (!CreateProcessW(executable.data(), + command.data(), nullptr, nullptr, TRUE, 0, nullptr, + nullptr, &si, &pi)) + return std::error_code(GetLastError(), std::system_category()); + + unique_handle hThread{pi.hThread, CloseHandle}; + unique_handle hProcess{pi.hProcess, CloseHandle}; + + int ifd = _open_osfhandle(reinterpret_cast(input[PI_WRITE].get()), 0); + if (ifd < 0) + return std::error_code(errno, std::system_category()); + input[PI_WRITE].release(); + + int ofd = _open_osfhandle(reinterpret_cast(output[PI_READ].get()), 0); + if (ofd < 0) { + _close(ifd); + return std::error_code(errno, std::system_category()); + } + output[PI_READ].release(); + + llvm::sys::ProcessInfo proc; + proc.Process = pi.hProcess; + return ChildProcessInfo(proc, ifd, ofd); +} + #else // HAVE_UNISTD_H llvm::ErrorOr From 5ffa171af5e8e7ab2b808d8e5fd67dfd894b43c9 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 16:49:02 -0700 Subject: [PATCH 17/50] Update PluginRegistry.cpp --- lib/AST/PluginRegistry.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/AST/PluginRegistry.cpp b/lib/AST/PluginRegistry.cpp index 6ddec5387c321..beceff366b7d2 100644 --- a/lib/AST/PluginRegistry.cpp +++ b/lib/AST/PluginRegistry.cpp @@ -207,7 +207,7 @@ ssize_t LoadedExecutablePlugin::PluginProcess::read(void *buf, while (bytesToRead > 0) { ssize_t readingSize = std::min(ssize_t(INT32_MAX), bytesToRead); - ssize_t readSize = ::read(inputFileDescriptor, ptr, readingSize); + ssize_t readSize = ::read(input, ptr, readingSize); if (readSize <= 0) { // 0: EOF (the plugin exited?), -1: error (e.g. broken pipe.) // FIXME: Mark the plugin 'stale' and relaunch later. @@ -245,7 +245,7 @@ ssize_t LoadedExecutablePlugin::PluginProcess::write(const void *buf, while (bytesToWrite > 0) { ssize_t writingSize = std::min(ssize_t(INT32_MAX), bytesToWrite); - ssize_t writtenSize = ::write(outputFileDescriptor, ptr, writingSize); + ssize_t writtenSize = ::write(output, ptr, writingSize); if (writtenSize <= 0) { // -1: error (e.g. broken pipe,) // FIXME: Mark the plugin 'stale' and relaunch later. From d7e411bdafb73242c77aa1e017a3f23a99fe9a48 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 18:53:24 -0700 Subject: [PATCH 18/50] Update Program.cpp --- lib/Basic/Program.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Basic/Program.cpp b/lib/Basic/Program.cpp index b0f2cc838f788..ac82fe52d4657 100644 --- a/lib/Basic/Program.cpp +++ b/lib/Basic/Program.cpp @@ -185,7 +185,9 @@ swift::ExecuteWithPipe(llvm::StringRef program, #endif close(p1.read); close(p2.write); - return ChildProcessInfo(pid, p1.write, p2.read); + llvm::sys::ProcessInfo proc; + proc.Pid = pid; + return ChildProcessInfo(proc, p1.write, p2.read); } #elif defined(_WIN32) From 880ebd477995074f02a2050d798e4189c9ab0628 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 1 Sep 2023 19:11:27 -0700 Subject: [PATCH 19/50] ASTGen: account for alternate path separators Windows supports `\` and `/` as path separators. Account for both in computing the basename. This repairs a few test failures on Windows. --- .../Sources/ASTGen/SourceManager+MacroExpansionContext.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift b/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift index 9f74cb212aa62..ce7facc258308 100644 --- a/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift +++ b/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift @@ -43,7 +43,7 @@ extension String { /// Retrieve the base name of a string that represents a path, removing the /// directory. var basename: String { - guard let lastSlash = lastIndex(of: "/") else { + guard let lastSlash = lastIndex(where: { ["/", "\\"].contains($0) }) else { return self } From 615ac94e0c8a125b74ba067803c6b1ab2dea2d98 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 10:51:00 -0700 Subject: [PATCH 20/50] ASTGen: special case the Apple and Linux platforms There is no real reason to split the behaviour here as this is a file system primitive. Special case Apple and Linux platforms to address feedback. --- .../ASTGen/SourceManager+MacroExpansionContext.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift b/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift index ce7facc258308..b42fbc6186d7d 100644 --- a/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift +++ b/lib/ASTGen/Sources/ASTGen/SourceManager+MacroExpansionContext.swift @@ -43,7 +43,13 @@ extension String { /// Retrieve the base name of a string that represents a path, removing the /// directory. var basename: String { - guard let lastSlash = lastIndex(where: { ["/", "\\"].contains($0) }) else { + guard let lastSlash = lastIndex(where: { +#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(Android) || os(Linux) + ["/"].contains($0) +#else + ["/", "\\"].contains($0) +#endif + }) else { return self } From dae120bfb027665609475782e71aaebb98c78078 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 1 Sep 2023 18:10:07 -0700 Subject: [PATCH 21/50] SourceKit: handle Windows paths for request options Windows paths involve a `:`. Ideally, we would just use `;` as the separator rather than `:`, but for now just limit the split count to 2. --- tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp index ff383ae105462..1f72f36c5f9e6 100644 --- a/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp +++ b/tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp @@ -427,7 +427,7 @@ static bool setSyntacticMacroExpansions(sourcekitd_object_t req, SmallVector expansions; for (std::string &opt : opts.RequestOptions) { SmallVector args; - StringRef(opt).split(args, ":"); + StringRef(opt).split(args, ":", /*maxSplits=*/2); unsigned line, column; if (args.size() != 3 || args[0].getAsInteger(10, line) || From 80f5d5299b49616baa455a0b0ca208d15ed50206 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 11:41:03 -0700 Subject: [PATCH 22/50] tests: replace shell with python for portability Replace the process_fine_grained_swiftdeps.sh with a python equivalent (which also preserves the horrendous handling of YAML and even "faithfully" replicates the horrible global variables). This enables a number of tests on Windows although the instigating macro test is not yet enabled due to the need for further tweaks to the tests. --- .../pch-bridging-header-deps-fine.swift | 18 +++++------ test/Frontend/dependencies-fine.swift | 24 +++++++-------- .../dependencies-preservation-fine.swift | 8 ++--- .../Dependencies/function-fine.swift | 8 ++--- .../function-return-type-fine.swift | 8 ++--- .../protocol-conformer-ext-fine.swift | 8 ++--- .../protocol-conformer-fine.swift | 8 ++--- ...nce-dependencies-dynamic-lookup-fine.swift | 8 ++--- .../reference-dependencies-fine.swift | 8 ++--- .../reference-dependencies-members-fine.swift | 8 ++--- .../Dependencies/struct-member-fine.swift | 8 ++--- .../Dependencies/subscript-fine.swift | 8 ++--- .../Dependencies/typealias-fine.swift | 8 ++--- test/Incremental/Dependencies/var-fine.swift | 6 +--- test/Inputs/process_fine_grained_swiftdeps.py | 30 +++++++++++++++++++ test/Inputs/process_fine_grained_swiftdeps.sh | 9 ------ test/Macros/macro_swiftdeps.swift | 12 ++++---- 17 files changed, 76 insertions(+), 111 deletions(-) create mode 100644 test/Inputs/process_fine_grained_swiftdeps.py delete mode 100755 test/Inputs/process_fine_grained_swiftdeps.sh diff --git a/test/ClangImporter/pch-bridging-header-deps-fine.swift b/test/ClangImporter/pch-bridging-header-deps-fine.swift index 4f21828ba3d32..4dbb1fea1812c 100644 --- a/test/ClangImporter/pch-bridging-header-deps-fine.swift +++ b/test/ClangImporter/pch-bridging-header-deps-fine.swift @@ -1,30 +1,26 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: rm -f %t.* -// + // Generate a bridging PCH, use it in a swift file, and check that the swift file's .swiftdeps // mention the .h the PCH was generated from, and any .h files included in it. -// + // RUN: %target-swift-frontend -emit-pch -o %t.pch %/S/Inputs/chained-unit-test-bridging-header-to-pch.h // RUN: %target-swift-frontend -module-name test -c -emit-dependencies-path %t.d -emit-reference-dependencies-path %t.swiftdeps -primary-file %s -import-objc-header %t.pch // RUN: %FileCheck --check-prefix CHECK-DEPS %s < %t.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS --enable-yaml-compatibility %s < %t-processed.swiftdeps // RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 --enable-yaml-compatibility %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -module-name test -c -emit-dependencies-path %t.persistent.d -emit-reference-dependencies-path %t.persistent.swiftdeps -primary-file %s -import-objc-header %/S/Inputs/chained-unit-test-bridging-header-to-pch.h -pch-output-dir %t/pch // RUN: %FileCheck --check-prefix CHECK-DEPS %s < %t.persistent.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.persistent.swiftdeps %t-processed.persistent.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.persistent.swiftdeps > %t-processed.persistent.swiftdeps // RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS --enable-yaml-compatibility %s < %t-processed.persistent.swiftdeps // RUN: %FileCheck --check-prefix CHECK-SWIFTDEPS2 --enable-yaml-compatibility %s < %t-processed.persistent.swiftdeps print(app_function(1)) -// CHECK-DEPS: pch-bridging-header-deps-fine.o : {{.*}}SOURCE_DIR{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}app-bridging-header-to-pch.h {{.*}}SOURCE_DIR{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}chained-unit-test-bridging-header-to-pch.h +// CHECK-DEPS: pch-bridging-header-deps-fine.o : {{.*}}{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}app-bridging-header-to-pch.h {{.*}}{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}chained-unit-test-bridging-header-to-pch.h -// CHECK-SWIFTDEPS: externalDepend {{.*}} 'SOURCE_DIR{{/|\\\\}}test{{/|\\\\}}ClangImporter{{/|\\\\}}Inputs{{/|\\\\}}app-bridging-header-to-pch.h' -// CHECK-SWIFTDEPS: externalDepend {{.*}} 'SOURCE_DIR{{/|\\\\}}test{{/|\\\\}}ClangImporter{{/|\\\\}}Inputs{{/|\\\\}}chained-unit-test-bridging-header-to-pch.h' +// CHECK-SWIFTDEPS: externalDepend {{.*}} '{{.*}}{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}app-bridging-header-to-pch.h' +// CHECK-SWIFTDEPS: externalDepend {{.*}} '{{.*}}{{/|\\}}test{{/|\\}}ClangImporter{{/|\\}}Inputs{{/|\\}}chained-unit-test-bridging-header-to-pch.h' // CHECK-SWIFTDEPS2-NOT: {{.*}}.pch diff --git a/test/Frontend/dependencies-fine.swift b/test/Frontend/dependencies-fine.swift index f7256e925cfb0..e540d1b59f5f2 100644 --- a/test/Frontend/dependencies-fine.swift +++ b/test/Frontend/dependencies-fine.swift @@ -1,17 +1,13 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -emit-dependencies-path - -resolve-imports "%S/../Inputs/empty file.swift" | %FileCheck -check-prefix=CHECK-BASIC %s // RUN: %target-swift-frontend -emit-reference-dependencies-path - -typecheck -primary-file "%S/../Inputs/empty file.swift" > %t.swiftdeps -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-BASIC-YAML %s <%t-processed.swiftdeps // RUN: %target-swift-frontend -emit-dependencies-path %t.d -emit-reference-dependencies-path %t.swiftdeps -typecheck -primary-file "%S/../Inputs/empty file.swift" // RUN: %FileCheck -check-prefix=CHECK-BASIC %s < %t.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-BASIC-YAML %s < %t-processed.swiftdeps // CHECK-BASIC-LABEL: - : @@ -20,7 +16,7 @@ // CHECK-BASIC-NOT: {{ }}:{{ }} // CHECK-BASIC-YAML-NOT: externalDepend {{.*}}empty -// CHECK-BASIC-YAML: externalDepend {{.*}} '{{.*}}Swift.swiftmodule{{(/.+[.]swiftmodule)?}}' +// CHECK-BASIC-YAML: externalDepend {{.*}} '{{.*}}Swift.swiftmodule{{/|\\}}{{(.+[.]swiftmodule)?}}' // RUN: %target-swift-frontend -emit-dependencies-path %t.d -emit-reference-dependencies-path %t.swiftdeps -typecheck "%S/../Inputs/empty file.swift" 2>&1 | %FileCheck -check-prefix=NO-PRIMARY-FILE %s @@ -48,7 +44,7 @@ // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -disable-objc-attr-requires-foundation-module -import-objc-header %S/Inputs/dependencies/extra-header.h -track-system-dependencies -emit-dependencies-path - -resolve-imports %s | %FileCheck -check-prefix=CHECK-IMPORT-TRACK-SYSTEM %s // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -disable-objc-attr-requires-foundation-module -import-objc-header %S/Inputs/dependencies/extra-header.h -emit-reference-dependencies-path %t.swiftdeps -typecheck -primary-file %s -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-IMPORT-YAML %s <%t-processed.swiftdeps // CHECK-IMPORT-LABEL: - : @@ -87,14 +83,14 @@ // CHECK-IMPORT-TRACK-SYSTEM-NOT: {{[^\\]}}: // CHECK-IMPORT-YAML-NOT: externalDepend {{.*}}dependencies-fine.swift -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\}}Swift.swiftmodule{{(/.+[.]swiftmodule)?}}' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\}}Swift.swiftmodule{{/|\\}}{{(.+[.]swiftmodule)?}}' // CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies/$$$$$.h' -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies{{/|\\\\}}UserClangModule.h' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies{{/|\\}}UserClangModule.h' // CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies/extra-header.h' -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies{{/|\\\\}}module.modulemap' -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\\\}}ObjectiveC.swift' -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\\\}}Foundation.swift' -// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\\\}}CoreGraphics.swift' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}Inputs/dependencies{{/|\\}}module.modulemap' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\}}ObjectiveC.swift' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\}}Foundation.swift' +// CHECK-IMPORT-YAML-DAG: externalDepend {{.*}} '{{.*}}{{/|\\}}CoreGraphics.swift' // CHECK-ERROR-YAML: # Dependencies are unknown because a compilation error occurred. diff --git a/test/Frontend/dependencies-preservation-fine.swift b/test/Frontend/dependencies-preservation-fine.swift index 4e2a29865a789..ab54154aeba37 100644 --- a/test/Frontend/dependencies-preservation-fine.swift +++ b/test/Frontend/dependencies-preservation-fine.swift @@ -1,7 +1,3 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // This test verifies that copies of dependency files are preserved after a // compilation. For example, if the first compilation produces 'foo.swiftdeps', // a second compilation should move 'foo.swiftdeps' to 'foo.swiftdeps~', then @@ -11,7 +7,7 @@ // First, produce the dependency files and verify their contents. // RUN: %target-swift-frontend -emit-reference-dependencies-path %t.swiftdeps -typecheck -primary-file "%S/../Inputs/empty file.swift" -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK %s < %t-processed.swiftdeps // CHECK-NOT: topLevel{{.*}}EmptyStruct{{.*}}true @@ -22,7 +18,7 @@ // file. // RUN: %target-swift-frontend -emit-reference-dependencies-path %t.swiftdeps -typecheck -primary-file %S/../Inputs/global_resilience.swift // RUN: %FileCheck -check-prefix=CHECK %s < %t.swiftdeps~ -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-OVERWRITTEN %s < %t-processed.swiftdeps // CHECK-OVERWRITTEN:topLevel{{.*}}EmptyStruct{{.*}}true diff --git a/test/Incremental/Dependencies/function-fine.swift b/test/Incremental/Dependencies/function-fine.swift index e72ab6f29cc53..42cf0b00f9f24 100644 --- a/test/Incremental/Dependencies/function-fine.swift +++ b/test/Incremental/Dependencies/function-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private func testParamType(_: InterestingType) {} diff --git a/test/Incremental/Dependencies/function-return-type-fine.swift b/test/Incremental/Dependencies/function-return-type-fine.swift index f9ffb1d4f3c90..0852a9b06dc3a 100644 --- a/test/Incremental/Dependencies/function-return-type-fine.swift +++ b/test/Incremental/Dependencies/function-return-type-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private func testReturnType() -> InterestingType { fatalError() } diff --git a/test/Incremental/Dependencies/protocol-conformer-ext-fine.swift b/test/Incremental/Dependencies/protocol-conformer-ext-fine.swift index 49330bda51a75..53b3ea9745d9e 100644 --- a/test/Incremental/Dependencies/protocol-conformer-ext-fine.swift +++ b/test/Incremental/Dependencies/protocol-conformer-ext-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private struct Test {} diff --git a/test/Incremental/Dependencies/protocol-conformer-fine.swift b/test/Incremental/Dependencies/protocol-conformer-fine.swift index 9642793aee895..f73f340739dae 100644 --- a/test/Incremental/Dependencies/protocol-conformer-fine.swift +++ b/test/Incremental/Dependencies/protocol-conformer-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private struct Test : InterestingProto {} diff --git a/test/Incremental/Dependencies/reference-dependencies-dynamic-lookup-fine.swift b/test/Incremental/Dependencies/reference-dependencies-dynamic-lookup-fine.swift index 3c0c74e79c9eb..2d24d68b8d1c1 100644 --- a/test/Incremental/Dependencies/reference-dependencies-dynamic-lookup-fine.swift +++ b/test/Incremental/Dependencies/reference-dependencies-dynamic-lookup-fine.swift @@ -1,15 +1,11 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %empty-directory(%t) // RUN: cp %s %t/main.swift // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -primary-file %t/main.swift -emit-reference-dependencies-path - > %t.swiftdeps // Check that the output is deterministic. // RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -primary-file %t/main.swift -emit-reference-dependencies-path - > %t-2.swiftdeps -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t-2.swiftdeps %t-2-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t-2.swiftdeps > %t-2-processed.swiftdeps // RUN: diff %t-processed.swiftdeps %t-2-processed.swiftdeps // RUN: %FileCheck %s < %t-processed.swiftdeps diff --git a/test/Incremental/Dependencies/reference-dependencies-fine.swift b/test/Incremental/Dependencies/reference-dependencies-fine.swift index e2fba6628bf13..1081e31a7f309 100644 --- a/test/Incremental/Dependencies/reference-dependencies-fine.swift +++ b/test/Incremental/Dependencies/reference-dependencies-fine.swift @@ -1,7 +1,3 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %empty-directory(%t) // RUN: cp %s %t/main.swift @@ -10,8 +6,8 @@ // RUN: %target-swift-frontend -typecheck -primary-file %t/main.swift %S/../Inputs/reference-dependencies-helper.swift -emit-reference-dependencies-path - > %t-2.swiftdeps // Merge each entry onto one line and sort to overcome order differences -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t-2.swiftdeps %t-2-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t-2.swiftdeps > %t-2-processed.swiftdeps // RUN: diff %t-processed.swiftdeps %t-2-processed.swiftdeps // RUN: %FileCheck -check-prefix=NEGATIVE %s < %t-processed.swiftdeps diff --git a/test/Incremental/Dependencies/reference-dependencies-members-fine.swift b/test/Incremental/Dependencies/reference-dependencies-members-fine.swift index 0fec187e1d91d..bfe167373208d 100644 --- a/test/Incremental/Dependencies/reference-dependencies-members-fine.swift +++ b/test/Incremental/Dependencies/reference-dependencies-members-fine.swift @@ -1,15 +1,11 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %empty-directory(%t) // RUN: cp %s %t/main.swift // RUN: %target-swift-frontend -typecheck -primary-file %t/main.swift %S/../Inputs/reference-dependencies-members-helper.swift -emit-reference-dependencies-path - > %t.swiftdeps // RUN: %target-swift-frontend -typecheck -primary-file %t/main.swift %S/../Inputs/reference-dependencies-members-helper.swift -emit-reference-dependencies-path - > %t-2.swiftdeps -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t-2.swiftdeps %t-2-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t-2.swiftdeps > %t-2-processed.swiftdeps // RUN: diff %t-processed.swiftdeps %t-2-processed.swiftdeps diff --git a/test/Incremental/Dependencies/struct-member-fine.swift b/test/Incremental/Dependencies/struct-member-fine.swift index 629d5e4ed3b10..b723c68989f5e 100644 --- a/test/Incremental/Dependencies/struct-member-fine.swift +++ b/test/Incremental/Dependencies/struct-member-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private struct Wrapper { diff --git a/test/Incremental/Dependencies/subscript-fine.swift b/test/Incremental/Dependencies/subscript-fine.swift index 8907034198080..994a4bd61cfe1 100644 --- a/test/Incremental/Dependencies/subscript-fine.swift +++ b/test/Incremental/Dependencies/subscript-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps struct Wrapper { diff --git a/test/Incremental/Dependencies/typealias-fine.swift b/test/Incremental/Dependencies/typealias-fine.swift index b9e27e265b69f..af140c8605b83 100644 --- a/test/Incremental/Dependencies/typealias-fine.swift +++ b/test/Incremental/Dependencies/typealias-fine.swift @@ -1,13 +1,9 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DNEW -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-NEW -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps private struct Wrapper { diff --git a/test/Incremental/Dependencies/var-fine.swift b/test/Incremental/Dependencies/var-fine.swift index 89aafb9452132..90b1d75a83bd1 100644 --- a/test/Incremental/Dependencies/var-fine.swift +++ b/test/Incremental/Dependencies/var-fine.swift @@ -1,10 +1,6 @@ -// REQUIRES: shell -// Also uses awk: -// XFAIL OS=windows - // RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/InterestingType.swift -DOLD -emit-reference-dependencies-path %t.swiftdeps -module-name main | %FileCheck %s -check-prefix=CHECK-OLD -// RUN: %S/../../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t.swiftdeps %t-processed.swiftdeps +// RUN: %{python} %S/../../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t.swiftdeps > %t-processed.swiftdeps // RUN: %FileCheck -check-prefix=CHECK-DEPS %s < %t-processed.swiftdeps diff --git a/test/Inputs/process_fine_grained_swiftdeps.py b/test/Inputs/process_fine_grained_swiftdeps.py new file mode 100644 index 0000000000000..da499bb26ed4a --- /dev/null +++ b/test/Inputs/process_fine_grained_swiftdeps.py @@ -0,0 +1,30 @@ + +import os +import re +import subprocess +import sys + +output = subprocess.run([ + sys.argv[1], + "--to-yaml", + "--input-filename={}".format(sys.argv[2]), + "--output-filename=-" + ], stdout=subprocess.PIPE) +entries = [] +k = a = c = n = s = p = '' +for line in output.stdout.decode('utf-8').split('\n'): + if 'kind:' in line: + k = line.split()[1] + if 'aspect:' in line: + a = line.split()[1] + if 'context:' in line: + c = line.split()[1] + if 'name:' in line: + n = ' '.join(line.split()[1:]) + if 'sequenceNumber:' in line: + s = line.split()[1] + if 'isProvides:' in line: + p = line.split()[1] + entries.append(' '.join([k, a, c, n, p])) +entries.sort() +print('\n'.join(entries)) diff --git a/test/Inputs/process_fine_grained_swiftdeps.sh b/test/Inputs/process_fine_grained_swiftdeps.sh deleted file mode 100755 index fb91c3875e95e..0000000000000 --- a/test/Inputs/process_fine_grained_swiftdeps.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env sh -# Fine-grained swiftdeps files use multiple lines for each graph node. -# Compress such a file so that each entry is one line of the form: -# -# Also sort for consistency, since the node order can vary. - -${1} --to-yaml --input-filename=${2} --output-filename=${3}.tmp - -awk '/kind:/ {k = $2}; /aspect:/ {a = $2}; /context:/ {c = $2}; /name/ {n = $2}; /sequenceNumber/ {s = $2}; /isProvides:/ {print k, a, c, n, $2}' < ${3}.tmp | sort > ${3} \ No newline at end of file diff --git a/test/Macros/macro_swiftdeps.swift b/test/Macros/macro_swiftdeps.swift index 2b6438e7ffada..07b45d73361b9 100644 --- a/test/Macros/macro_swiftdeps.swift +++ b/test/Macros/macro_swiftdeps.swift @@ -34,7 +34,7 @@ // RUN: -primary-file %t/src/macro_library.swift \ // RUN: -emit-reference-dependencies-path %t/macro_library.swiftdeps \ // RUN: -emit-dependencies-path %t/macro_library.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/macro_library.swiftdeps %t/macro_library.swiftdeps.processed +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t/macro_library.swiftdeps > %t/macro_library.swiftdeps.processed // RUN: %FileCheck --check-prefix WITH_PLUGIN %s < %t/macro_library.swiftdeps.processed //#-- Without macro (no -D USE_MACRO) @@ -46,7 +46,7 @@ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ // RUN: -emit-reference-dependencies-path %t/without_macro.swiftdeps \ // RUN: -emit-dependencies-path %t/without_macro.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/without_macro.swiftdeps %t/without_macro.swiftdeps.processed +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t/without_macro.swiftdeps > %t/without_macro.swiftdeps.processed // RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/without_macro.swiftdeps.processed //#-- With macro - primary (-D USE_MACRO) @@ -59,7 +59,7 @@ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ // RUN: -emit-reference-dependencies-path %t/with_macro_primary.swiftdeps \ // RUN: -emit-dependencies-path %t/with_macro_primary.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/with_macro_primary.swiftdeps %t/with_macro_primary.swiftdeps.processed +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t/with_macro_primary.swiftdeps > %t/with_macro_primary.swiftdeps.processed // RUN: %FileCheck --check-prefix WITH_PLUGIN %s < %t/with_macro_primary.swiftdeps.processed //#-- With macro - non-primary (-D USE_MACRO) @@ -72,11 +72,11 @@ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ // RUN: -emit-reference-dependencies-path %t/with_macro_nonprimary.swiftdeps \ // RUN: -emit-dependencies-path %t/with_macro_nonprimary.d -// RUN: %S/../Inputs/process_fine_grained_swiftdeps.sh %swift-dependency-tool %t/with_macro_nonprimary.swiftdeps %t/with_macro_nonprimary.swiftdeps.processed +// RUN: %{python} %S/../Inputs/process_fine_grained_swiftdeps.py %swift-dependency-tool %t/with_macro_nonprimary.swiftdeps > %t/with_macro_nonprimary.swiftdeps.processed // RUN: %FileCheck --check-prefix WITHOUT_PLUGIN %s < %t/with_macro_nonprimary.swiftdeps.processed -// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}mock-plugin' false -// WITH_PLUGIN: externalDepend interface '' 'BUILD_DIR{{.*}}libMacroDefinition.{{(dylib|so|dll)}}' false +// WITH_PLUGIN: externalDepend interface '' '{{.*}}mock-plugin' false +// WITH_PLUGIN: externalDepend interface '' '{{.*}}MacroDefinition.{{(dylib|so|dll)}}' false // WITHOUT_PLUGIN-NOT: MacroDefinition // WITHOUT_PLUGIN-NOT: mock-plugin From a6cd8eeb3e5493b5e1a0af0ffec243a5142d8301 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 11:58:35 -0700 Subject: [PATCH 23/50] Update process_fine_grained_swiftdeps.py --- test/Inputs/process_fine_grained_swiftdeps.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/Inputs/process_fine_grained_swiftdeps.py b/test/Inputs/process_fine_grained_swiftdeps.py index da499bb26ed4a..21e405f1b79e0 100644 --- a/test/Inputs/process_fine_grained_swiftdeps.py +++ b/test/Inputs/process_fine_grained_swiftdeps.py @@ -4,6 +4,11 @@ import subprocess import sys +# Fine-grained swiftdeps files use multiple lines for each graph node. +# Compress such a file so that each entry is one line of the form: +# +# Also sort for consistency, since the node order can vary. + output = subprocess.run([ sys.argv[1], "--to-yaml", From ea22e661e01e40239459759bc2eaca8181577ebe Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 16:47:31 -0700 Subject: [PATCH 24/50] Update process_fine_grained_swiftdeps.py `python3 -m black %s` --- test/Inputs/process_fine_grained_swiftdeps.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/test/Inputs/process_fine_grained_swiftdeps.py b/test/Inputs/process_fine_grained_swiftdeps.py index 21e405f1b79e0..5396d68c2aadb 100644 --- a/test/Inputs/process_fine_grained_swiftdeps.py +++ b/test/Inputs/process_fine_grained_swiftdeps.py @@ -1,6 +1,4 @@ -import os -import re import subprocess import sys @@ -9,27 +7,30 @@ # # Also sort for consistency, since the node order can vary. -output = subprocess.run([ +output = subprocess.run( + [ sys.argv[1], "--to-yaml", "--input-filename={}".format(sys.argv[2]), - "--output-filename=-" - ], stdout=subprocess.PIPE) + "--output-filename=-", + ], + stdout=subprocess.PIPE, +) entries = [] -k = a = c = n = s = p = '' -for line in output.stdout.decode('utf-8').split('\n'): - if 'kind:' in line: +k = a = c = n = s = p = "" +for line in output.stdout.decode("utf-8").split("\n"): + if "kind:" in line: k = line.split()[1] - if 'aspect:' in line: + if "aspect:" in line: a = line.split()[1] - if 'context:' in line: + if "context:" in line: c = line.split()[1] - if 'name:' in line: + if "name:" in line: n = ' '.join(line.split()[1:]) - if 'sequenceNumber:' in line: + if "sequenceNumber:" in line: s = line.split()[1] - if 'isProvides:' in line: + if "isProvides:" in line: p = line.split()[1] entries.append(' '.join([k, a, c, n, p])) entries.sort() -print('\n'.join(entries)) +print("\n".join(entries)) From 3981f75a3caa01546928364557eae12382c539b6 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 20:47:10 -0700 Subject: [PATCH 25/50] tests: add missing `env` The `env` is required to set the environment variable as executing with environment variables is a SysV Shell thing and is not generally portable. This is required to support Windows. --- test/Macros/macro_plugin_basic.swift | 2 +- test/Macros/macro_plugin_error.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Macros/macro_plugin_basic.swift b/test/Macros/macro_plugin_basic.swift index 1280c11f937a1..e89c24d240436 100644 --- a/test/Macros/macro_plugin_basic.swift +++ b/test/Macros/macro_plugin_basic.swift @@ -11,7 +11,7 @@ // RUN: -o %t/mock-plugin \ // RUN: %t/plugin.c -// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ +// RUN: env SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ // RUN: -typecheck -verify \ // RUN: -swift-version 5 \ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ diff --git a/test/Macros/macro_plugin_error.swift b/test/Macros/macro_plugin_error.swift index ebab709d4798c..d5a0b9fa631ea 100644 --- a/test/Macros/macro_plugin_error.swift +++ b/test/Macros/macro_plugin_error.swift @@ -11,7 +11,7 @@ // RUN: -o %t/mock-plugin \ // RUN: %t/plugin.c -// RUN: SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ +// RUN: env SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ // RUN: -typecheck -verify \ // RUN: -swift-version 5 -enable-experimental-feature Macros \ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ From cf14a6bee4a3b4c448b16c9901f65b566be6e2d1 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 20:49:50 -0700 Subject: [PATCH 26/50] tests: remove gratuitous use of `tee` Replace the use of `tee` with proper redirection for the output. This reduces the dependency on Unix tools for portability. --- test/Macros/macro_plugin_basic.swift | 2 +- test/Macros/macro_plugin_error.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Macros/macro_plugin_basic.swift b/test/Macros/macro_plugin_basic.swift index e89c24d240436..59cbca6a8ee62 100644 --- a/test/Macros/macro_plugin_basic.swift +++ b/test/Macros/macro_plugin_basic.swift @@ -17,7 +17,7 @@ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ // RUN: -module-name MyApp \ // RUN: %t/test.swift \ -// RUN: 2>&1 | tee %t/macro-expansions.txt +// RUN: > %t/macro-expansions.txt 2>&1 // RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt diff --git a/test/Macros/macro_plugin_error.swift b/test/Macros/macro_plugin_error.swift index d5a0b9fa631ea..1b98a815039c1 100644 --- a/test/Macros/macro_plugin_error.swift +++ b/test/Macros/macro_plugin_error.swift @@ -17,7 +17,7 @@ // RUN: -load-plugin-executable %t/mock-plugin#TestPlugin \ // RUN: -module-name MyApp \ // RUN: %t/test.swift \ -// RUN: 2>&1 | tee %t/macro-expansions.txt +// RUN: > %t/macro-expansions.txt 2>&1 // RUN: %FileCheck -strict-whitespace %s < %t/macro-expansions.txt From b010ca77354cad4ea4a4a3e0c6fa15e314d0a1ef Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 19:16:27 -0700 Subject: [PATCH 27/50] test: introduce a new `%swift-plugin-dir` macro Use this to define the macro location rather than the "host" dir (which is actually for the build and not the host). Furthermore, on Windows, the build dir is /usr/lib/swift as the host content is in the SDK. This prepares the tests for Windows. --- test/IDE/complete_optionset.swift | 5 +- test/Macros/macro_expand_other.swift | 5 +- test/Macros/option_set.swift | 2 +- test/ModuleInterface/Observable.swift | 2 +- test/lit.cfg | 46 +++++++++++++++---- test/stdlib/Observation/Observable.swift | 4 +- .../ObservableAvailabilityCycle.swift | 4 +- .../Observation/ObservableDidSetWillSet.swift | 2 +- 8 files changed, 49 insertions(+), 21 deletions(-) diff --git a/test/IDE/complete_optionset.swift b/test/IDE/complete_optionset.swift index 271e618324efd..fd3917c37e2d4 100644 --- a/test/IDE/complete_optionset.swift +++ b/test/IDE/complete_optionset.swift @@ -1,6 +1,7 @@ -// REQUIRES: swift_swift_parser // RUN: %empty-directory(%t) -// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t -plugin-path %swift-host-lib-dir/plugins +// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t -plugin-path %swift-plugin-dir + +// REQUIRES: swift_swift_parser @OptionSet struct ShippingOptions { diff --git a/test/Macros/macro_expand_other.swift b/test/Macros/macro_expand_other.swift index d6c21f7e0f3f2..fe71345912697 100644 --- a/test/Macros/macro_expand_other.swift +++ b/test/Macros/macro_expand_other.swift @@ -4,12 +4,13 @@ // RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath // Diagnostics testing -// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS +// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS // Execution testing -// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser +// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser // RUN: %target-codesign %t/main // RUN: %target-run %t/main | %FileCheck %s + // REQUIRES: swift_swift_parser, executable_test @freestanding(expression) macro stringify(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro") diff --git a/test/Macros/option_set.swift b/test/Macros/option_set.swift index eda8136594651..ce9d87ba81a50 100644 --- a/test/Macros/option_set.swift +++ b/test/Macros/option_set.swift @@ -1,6 +1,6 @@ // REQUIRES: swift_swift_parser, executable_test -// RUN: %target-run-simple-swift(-Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins -emit-tbd -emit-tbd-path %t.tbd) +// RUN: %target-run-simple-swift(-Xfrontend -plugin-path -Xfrontend %swift-plugin-dir -emit-tbd -emit-tbd-path %t.tbd) import Swift diff --git a/test/ModuleInterface/Observable.swift b/test/ModuleInterface/Observable.swift index 59a571a226659..ce62c8d99be32 100644 --- a/test/ModuleInterface/Observable.swift +++ b/test/ModuleInterface/Observable.swift @@ -1,5 +1,5 @@ // RUN: %empty-directory(%t) -// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -plugin-path %swift-host-lib-dir/plugins -disable-availability-checking +// RUN: %target-swift-emit-module-interface(%t/Library.swiftinterface) %s -module-name Library -plugin-path %swift-plugin-dir -disable-availability-checking // RUN: %target-swift-typecheck-module-from-interface(%t/Library.swiftinterface) -module-name Library -disable-availability-checking // RUN: %FileCheck %s < %t/Library.swiftinterface diff --git a/test/lit.cfg b/test/lit.cfg index 76d61f614b7dd..35d6ac55029a7 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -524,7 +524,14 @@ shutil.rmtree(completion_cache_path, ignore_errors=True) ccp_opt = "-completion-cache-path %r" % completion_cache_path lit_config.note("Using code completion cache: " + completion_cache_path) -config.swift_host_lib_dir = make_path(config.swift_lib_dir, 'swift', 'host') +if kIsWindows: + config.swift_plugin_dir = config.swift_bin_dir + # FIXME: this is a workaround for the collsion between the build toolchain + # and the current build, this should be `make_path(config.swift_lib_dir, 'swift')` + config.swift_build_lib_dir = make_path(config.swift_lib_dir, 'swift', 'host') +else: + config.swift_plugin_dir = make_path(config.swift_lib_dir, 'swift', 'host', 'plugins') + config.swift_build_lib_dir = make_path(config.swift_lib_dir, 'swift', 'host') if platform.system() == 'Darwin': config.swift_driver = ( @@ -534,8 +541,14 @@ if platform.system() == 'Darwin': "env SDKROOT=%s %r -toolchain-stdlib-rpath -Xlinker -rpath -Xlinker /usr/lib/swift %s %s %s" % (shell_quote(config.host_sdkroot), config.swiftc, mcp_opt, config.swift_test_options, config.swift_driver_test_options)) config.host_build_swift = ( - "%s -sdk %s -target %s -I %s -L %s" - % (config.swiftc_driver, config.host_sdkroot, config.host_triple, config.swift_host_lib_dir, config.swift_host_lib_dir)) + "%s -sdk %s -target %s -no-toolchain-stdlib-rpath -I %s -L %s" % ( + config.swiftc_driver, + config.host_sdkroot, + config.host_triple, + config.swift_build_lib_dir, + config.swift_build_lib_dir + ) + ) else: config.swift_driver = ( "%r %s %s %s" @@ -543,18 +556,31 @@ else: if kIsWindows: config.swift_driver += " -libc " + config.swift_stdlib_msvc_runtime config.swiftc_driver = ( - "%r -toolchain-stdlib-rpath %s %s %s" - % (config.swiftc, mcp_opt, config.swift_test_options, config.swift_driver_test_options)) + "%r %s %s %s %s" % ( + config.swiftc, + '' if kIsWindows else '-toolchain-stdlib-rpath', + mcp_opt, + config.swift_test_options, + config.swift_driver_test_options, + ) + ) # Parse the host triple. (host_cpu, host_vendor, host_os, host_vers) = re.match('([^-]+)-([^-]+)-([^0-9-]+)(.*)', config.host_triple).groups() toolchain_lib_dir = make_path(config.swift_lib_dir, 'swift', host_os) config.host_build_swift = ( - "%s -target %s -I %s -L %s -Xlinker -rpath -Xlinker %s" - % (config.swiftc_driver, config.host_triple, config.swift_host_lib_dir, config.swift_host_lib_dir, toolchain_lib_dir)) + "%s -target %s -g %s -I %s -L %s %s" % ( + config.swiftc_driver, + config.host_triple, + '' if kIsWindows else '-no-toolchain-stdlib-rpath', + config.swift_build_lib_dir, + config.swift_build_lib_dir, + '' if kIsWindows else '-Xlinker -rpath -Xlinker {}'.format(toolchain_lib_dir) + ) + ) config.substitutions.append( ('%llvm_obj_root', config.llvm_obj_root) ) config.substitutions.append( ('%swift-lib-dir', config.swift_lib_dir) ) -config.substitutions.append( ('%swift-host-lib-dir', config.swift_host_lib_dir)) +config.substitutions.append( ('%swift-plugin-dir', config.swift_plugin_dir) ) config.substitutions.append( ('%llvm_src_root', config.llvm_src_root) ) config.substitutions.append( ('%swift_obj_root', config.swift_obj_root) ) config.substitutions.append( ('%swift_src_root', config.swift_src_root) ) @@ -2651,9 +2677,9 @@ config.substitutions.append(('%llvm-cov', config.llvm_cov)) if hasattr(config, 'target_library_path_var'): host_library_env = ( "/usr/bin/env " + - construct_library_path_env([config.swift_host_lib_dir])) + construct_library_path_env([make_path(config.swift_lib_dir, 'swift', 'host')])) else: - host_library_env = "env" + host_library_env = "" config.substitutions.append(('%host-library-env', host_library_env)) diff --git a/test/stdlib/Observation/Observable.swift b/test/stdlib/Observation/Observable.swift index 83fa7dca33103..a01117ebb5431 100644 --- a/test/stdlib/Observation/Observable.swift +++ b/test/stdlib/Observation/Observable.swift @@ -1,9 +1,9 @@ // REQUIRES: swift_swift_parser, executable_test -// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-plugin-dir) // Run this test via the swift-plugin-server -// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -external-plugin-path -Xfrontend %swift-host-lib-dir/plugins#%swift-plugin-server) +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -parse-as-library -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -external-plugin-path -Xfrontend %swift-plugin-dir#%swift-plugin-server) // REQUIRES: observation // REQUIRES: concurrency diff --git a/test/stdlib/Observation/ObservableAvailabilityCycle.swift b/test/stdlib/Observation/ObservableAvailabilityCycle.swift index 74a305d7216f3..7e1f20bd352fb 100644 --- a/test/stdlib/Observation/ObservableAvailabilityCycle.swift +++ b/test/stdlib/Observation/ObservableAvailabilityCycle.swift @@ -1,8 +1,8 @@ // REQUIRES: swift_swift_parser -// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-host-lib-dir/plugins#%swift-plugin-server -primary-file %s %S/Inputs/ObservableClass.swift +// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-plugin-dir#%swift-plugin-server -primary-file %s %S/Inputs/ObservableClass.swift -// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-host-lib-dir/plugins#%swift-plugin-server %s -primary-file %S/Inputs/ObservableClass.swift +// RUN: %target-swift-frontend -typecheck -parse-as-library -external-plugin-path %swift-plugin-dir#%swift-plugin-server %s -primary-file %S/Inputs/ObservableClass.swift // REQUIRES: observation // REQUIRES: concurrency diff --git a/test/stdlib/Observation/ObservableDidSetWillSet.swift b/test/stdlib/Observation/ObservableDidSetWillSet.swift index ffa9ecb649700..e990d2d1881e8 100644 --- a/test/stdlib/Observation/ObservableDidSetWillSet.swift +++ b/test/stdlib/Observation/ObservableDidSetWillSet.swift @@ -1,6 +1,6 @@ // REQUIRES: swift_swift_parser, executable_test -// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-host-lib-dir/plugins) | %FileCheck %s +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -Xfrontend -plugin-path -Xfrontend %swift-plugin-dir) | %FileCheck %s // REQUIRES: observation // REQUIRES: concurrency From af332c34ed009d81e5111182d567cb99f89314c0 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 11:42:57 -0700 Subject: [PATCH 28/50] test: mark a job incompatible with Swift parser Due to the fact that we do not statically link the toolchain, we cannot currently support this test. Mark it as unavailable currently with the early syntax parser enabled. --- test/Driver/windows-link-job.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/test/Driver/windows-link-job.swift b/test/Driver/windows-link-job.swift index eb2cb06891a73..6ed7832bf5c06 100644 --- a/test/Driver/windows-link-job.swift +++ b/test/Driver/windows-link-job.swift @@ -6,5 +6,6 @@ // swift-frontend cannot be copied to another location with bootstrapping because // it will not find the libswiftCore library with its relative RPATH. // UNSUPPORTED: swift_in_compiler +// UNSUPPORTED: swift_swift_parser // CHECK: {{^}}clang From b36f8380f0c04f4d075425f57bb06726bac79670 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 3 Sep 2023 21:04:23 -0700 Subject: [PATCH 29/50] test: loosen expectations to support Windows The paths are not handled uniformly which makes it difficult to match the build directory pattern. This is needed to handle the tests on Windows. --- test/Macros/macro_plugin_basic.swift | 6 +++--- test/Macros/macro_plugin_error.swift | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/Macros/macro_plugin_basic.swift b/test/Macros/macro_plugin_basic.swift index 59cbca6a8ee62..3997136d46e61 100644 --- a/test/Macros/macro_plugin_basic.swift +++ b/test/Macros/macro_plugin_basic.swift @@ -23,10 +23,10 @@ // CHECK: ->(plugin:[[#PID:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}} // CHECK: <-(plugin:[[#PID]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}} -// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}} +// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testString","typeName":"TestStringMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"{{.+}}test.swift","line":5,"offset":301},"source":"#testString(123)"}}} // CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"\"123\"\n + \"foo \""}} -// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testStringWithError","typeName":"TestStringWithErrorMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":336},"source":"#testStringWithError(321)"}}} -// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[{"fixIts":[],"highlights":[],"message":"message from plugin","notes":[],"position":{"fileName":"BUILD_DIR{{.*}}test.swift","offset":336},"severity":"error"}],"expandedSource":"\"bar\""}} +// CHECK: ->(plugin:[[#PID]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"testStringWithError","typeName":"TestStringWithErrorMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"{{.+}}test.swift","line":6,"offset":336},"source":"#testStringWithError(321)"}}} +// CHECK: <-(plugin:[[#PID]]) {"expandFreestandingMacroResult":{"diagnostics":[{"fixIts":[],"highlights":[],"message":"message from plugin","notes":[],"position":{"fileName":"{{.*}}test.swift","offset":336},"severity":"error"}],"expandedSource":"\"bar\""}} //--- test.swift @freestanding(expression) macro testString(_: Any) -> String = #externalMacro(module: "TestPlugin", type: "TestStringMacro") diff --git a/test/Macros/macro_plugin_error.swift b/test/Macros/macro_plugin_error.swift index 1b98a815039c1..f0e3d719f842b 100644 --- a/test/Macros/macro_plugin_error.swift +++ b/test/Macros/macro_plugin_error.swift @@ -23,14 +23,14 @@ // CHECK: ->(plugin:[[#PID1:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION:]]}}} // CHECK-NEXT: <-(plugin:[[#PID1]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}} -// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":6,"offset":[[#]]},"source":"#fooMacro(1)"}}} +// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"{{.+}}test.swift","line":6,"offset":[[#]]},"source":"#fooMacro(1)"}}} // CHECK-NEXT: <-(plugin:[[#PID1]]) {"invalidResponse":{}} -// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":8,"offset":[[#]]},"source":"#fooMacro(2)"}}} +// CHECK-NEXT: ->(plugin:[[#PID1]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"{{.+}}test.swift","line":8,"offset":[[#]]},"source":"#fooMacro(2)"}}} // ^ This messages causes the mock plugin exit because there's no matching expected message. // CHECK: ->(plugin:[[#PID2:]]) {"getCapability":{"capability":{"protocolVersion":[[#PROTOCOL_VERSION]]}}} // CHECK-NEXT: <-(plugin:[[#PID2]]) {"getCapabilityResult":{"capability":{"protocolVersion":1}}} -// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"BUILD_DIR{{.+}}test.swift","line":10,"offset":[[#]]},"source":"#fooMacro(3)"}}} +// CHECK-NEXT: ->(plugin:[[#PID2]]) {"expandFreestandingMacro":{"discriminator":"$s{{.+}}","macro":{"moduleName":"TestPlugin","name":"fooMacro","typeName":"FooMacro"},"macroRole":"expression","syntax":{"kind":"expression","location":{"column":19,"fileID":"MyApp/test.swift","fileName":"{{.+}}test.swift","line":10,"offset":[[#]]},"source":"#fooMacro(3)"}}} // CHECK-NEXT: <-(plugin:[[#PID2:]]) {"expandFreestandingMacroResult":{"diagnostics":[],"expandedSource":"3.description"}} //--- test.swift From 74a6533ae1ed465d241eacba9fa2b90757a13873 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 08:51:34 -0700 Subject: [PATCH 30/50] test: sink `-no-toolchain-stdlib-rpath` into lit This is a platform specific option and should be handled by lit patterns rather than encoded into the tests. --- test/Macros/macro_expand.swift | 2 +- test/Macros/macro_expand_other.swift | 2 +- test/Macros/macro_plugin_broken.swift | 3 +-- test/Macros/macro_plugin_searchorder.swift | 9 +++------ test/Macros/macro_swiftdeps.swift | 3 +-- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/test/Macros/macro_expand.swift b/test/Macros/macro_expand.swift index 0ed70ec49448d..1e98a656e5db3 100644 --- a/test/Macros/macro_expand.swift +++ b/test/Macros/macro_expand.swift @@ -1,7 +1,7 @@ // REQUIRES: swift_swift_parser, executable_test // RUN: %empty-directory(%t) -// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5 +// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift // Diagnostics testing // RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS diff --git a/test/Macros/macro_expand_other.swift b/test/Macros/macro_expand_other.swift index fe71345912697..cac2ca04f9983 100644 --- a/test/Macros/macro_expand_other.swift +++ b/test/Macros/macro_expand_other.swift @@ -1,7 +1,7 @@ // Expanding macros that are defined in terms of other macros. // RUN: %empty-directory(%t) -// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath +// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift // Diagnostics testing // RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS diff --git a/test/Macros/macro_plugin_broken.swift b/test/Macros/macro_plugin_broken.swift index 6ec89f4050ffb..cd20d18a96c83 100644 --- a/test/Macros/macro_plugin_broken.swift +++ b/test/Macros/macro_plugin_broken.swift @@ -6,8 +6,7 @@ // RUN: %host-build-swift \ // RUN: -swift-version 5 -o %t/broken-plugin \ // RUN: -module-name=TestPlugin \ -// RUN: %t/broken_plugin.swift \ -// RUN: -g -no-toolchain-stdlib-rpath -swift-version 5 +// RUN: %t/broken_plugin.swift // RUN: not %swift-target-frontend \ // RUN: -typecheck \ diff --git a/test/Macros/macro_plugin_searchorder.swift b/test/Macros/macro_plugin_searchorder.swift index 562144fa86b48..0a451a01d6577 100644 --- a/test/Macros/macro_plugin_searchorder.swift +++ b/test/Macros/macro_plugin_searchorder.swift @@ -18,8 +18,7 @@ // RUN: -emit-library -o %t/lib/plugins/%target-library-name(MacroDefinition) \ // RUN: -module-name MacroDefinition \ // RUN: -D PLUGIN_PATH \ -// RUN: %t/src/MacroDefinition.swift \ -// RUN: -g -no-toolchain-stdlib-rpath +// RUN: %t/src/MacroDefinition.swift //#-- For -load-plugin-library // RUN: %host-build-swift \ @@ -27,8 +26,7 @@ // RUN: -emit-library -o %t/lib/tmp/%target-library-name(MacroDefinition) \ // RUN: -module-name MacroDefinition \ // RUN: -D LOAD_PLUGIN_LIBRARY \ -// RUN: %t/src/MacroDefinition.swift \ -// RUN: -g -no-toolchain-stdlib-rpath +// RUN: %t/src/MacroDefinition.swift //#-- For -external-plugin-path // RUN: %host-build-swift \ @@ -36,8 +34,7 @@ // RUN: -emit-library -o %t/external/%target-library-name(MacroDefinition) \ // RUN: -module-name MacroDefinition \ // RUN: -D EXTERNAL_PLUGIN_PATH \ -// RUN: %t/src/MacroDefinition.swift \ -// RUN: -g -no-toolchain-stdlib-rpath +// RUN: %t/src/MacroDefinition.swift //#-- For -load-plugin-executable // RUN: %clang \ diff --git a/test/Macros/macro_swiftdeps.swift b/test/Macros/macro_swiftdeps.swift index 07b45d73361b9..6345119246b5f 100644 --- a/test/Macros/macro_swiftdeps.swift +++ b/test/Macros/macro_swiftdeps.swift @@ -12,8 +12,7 @@ // RUN: -swift-version 5 \ // RUN: -emit-library -o %t/plugin/%target-library-name(MacroDefinition) \ // RUN: -module-name MacroDefinition \ -// RUN: %S/Inputs/syntax_macro_definitions.swift \ -// RUN: -g -no-toolchain-stdlib-rpath +// RUN: %S/Inputs/syntax_macro_definitions.swift //#-- Prepare the macro executable plugin. // RUN: %clang \ From f871032d263b3203beb4fbc4783057fba4dae14a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 10:32:59 -0700 Subject: [PATCH 31/50] Update lit.cfg --- test/lit.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 35d6ac55029a7..852f241018e70 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -572,8 +572,8 @@ else: config.swiftc_driver, config.host_triple, '' if kIsWindows else '-no-toolchain-stdlib-rpath', - config.swift_build_lib_dir, - config.swift_build_lib_dir, + config.swift_host_lib_dir, + config.swift_host_lib_dir, '' if kIsWindows else '-Xlinker -rpath -Xlinker {}'.format(toolchain_lib_dir) ) ) From e9baa2275761469c6ca0d6c78ec6ddcfcdacee92 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 11:00:50 -0700 Subject: [PATCH 32/50] test: introduce a helper for building mock plugins Introduce a helper for mock plugins as `-rpath` is not a portable construct. This allows running more of the macro tests on Windows. --- test/Macros/lit.local.cfg | 24 ++++++++++++++++++++++ test/Macros/macro_plugin_basic.swift | 8 +------- test/Macros/macro_plugin_error.swift | 8 +------- test/Macros/macro_plugin_searchorder.swift | 8 +------- test/Macros/macro_swiftdeps.swift | 8 +------- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/test/Macros/lit.local.cfg b/test/Macros/lit.local.cfg index 521ceea67705e..dd010ab88930a 100644 --- a/test/Macros/lit.local.cfg +++ b/test/Macros/lit.local.cfg @@ -1,3 +1,27 @@ # '-enable-experimental-feature Macros' requires an asserts build. if 'asserts' not in config.available_features: config.unsupported = True + +config.subsitutions = list(config.substitutions) + +def get_target_os(): + import re + (run_cpu, run_vendor, run_os, run_version) = re.match('([^-]+)-([^-]+)-([^0-9]+)(.*)', config.variant_triple).groups() + return run_os + +if get_target_os() in ['windows-msvc']: + config.substitutions.insert( + 0, + ( + '%swift-build-cxx-plugin', + '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin' + ) + ) +else: + config.substitutions.insert( + 0, + ( + '%swift-build-cxx-plugin', + '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir' + ) + ) diff --git a/test/Macros/macro_plugin_basic.swift b/test/Macros/macro_plugin_basic.swift index 3997136d46e61..6bcfa7a89d264 100644 --- a/test/Macros/macro_plugin_basic.swift +++ b/test/Macros/macro_plugin_basic.swift @@ -3,13 +3,7 @@ // RUN: %empty-directory(%t) // RUN: split-file %s %t -// RUN: %clang \ -// RUN: -isysroot %host_sdk \ -// RUN: -I %swift_src_root/include \ -// RUN: -L %swift-lib-dir -l_swiftMockPlugin \ -// RUN: -Wl,-rpath,%swift-lib-dir \ -// RUN: -o %t/mock-plugin \ -// RUN: %t/plugin.c +// RUN: %swift-build-cxx-plugin -o %t/mock-plugin %t/plugin.c // RUN: env SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ // RUN: -typecheck -verify \ diff --git a/test/Macros/macro_plugin_error.swift b/test/Macros/macro_plugin_error.swift index f0e3d719f842b..e50edef4dc89b 100644 --- a/test/Macros/macro_plugin_error.swift +++ b/test/Macros/macro_plugin_error.swift @@ -3,13 +3,7 @@ // RUN: %empty-directory(%t) // RUN: split-file %s %t -// RUN: %clang \ -// RUN: -isysroot %host_sdk \ -// RUN: -I %swift_src_root/include \ -// RUN: -L %swift-lib-dir -l_swiftMockPlugin \ -// RUN: -Wl,-rpath,%swift-lib-dir \ -// RUN: -o %t/mock-plugin \ -// RUN: %t/plugin.c +// RUN: %swift-build-cxx-plugin -o %t/mock-plugin %t/plugin.c // RUN: env SWIFT_DUMP_PLUGIN_MESSAGING=1 %swift-target-frontend \ // RUN: -typecheck -verify \ diff --git a/test/Macros/macro_plugin_searchorder.swift b/test/Macros/macro_plugin_searchorder.swift index 0a451a01d6577..2373e09c95ac2 100644 --- a/test/Macros/macro_plugin_searchorder.swift +++ b/test/Macros/macro_plugin_searchorder.swift @@ -37,13 +37,7 @@ // RUN: %t/src/MacroDefinition.swift //#-- For -load-plugin-executable -// RUN: %clang \ -// RUN: -isysroot %host_sdk \ -// RUN: -I %swift_src_root/include \ -// RUN: -L %swift-lib-dir -l_swiftMockPlugin \ -// RUN: -Wl,-rpath,%swift-lib-dir \ -// RUN: -o %t/libexec/MacroDefinitionPlugin \ -// RUN: %t/src/MacroDefinition.c +// RUN: %swift-build-cxx-plugin -o %t/libexec/MacroDefinitionPlugin %t/src/MacroDefinition.c //#-- Expect -load-plugin-library // RUN: %target-build-swift %t/src/test.swift \ diff --git a/test/Macros/macro_swiftdeps.swift b/test/Macros/macro_swiftdeps.swift index 6345119246b5f..118ef1cc364ad 100644 --- a/test/Macros/macro_swiftdeps.swift +++ b/test/Macros/macro_swiftdeps.swift @@ -15,13 +15,7 @@ // RUN: %S/Inputs/syntax_macro_definitions.swift //#-- Prepare the macro executable plugin. -// RUN: %clang \ -// RUN: -isysroot %host_sdk \ -// RUN: -I %swift_src_root/include \ -// RUN: -L %swift-lib-dir -l_swiftMockPlugin \ -// RUN: -Wl,-rpath,%swift-lib-dir \ -// RUN: -o %t/mock-plugin \ -// RUN: %t/src/plugin.c +// RUN: %swift-build-cxx-plugin -o %t/mock-plugin %t/src/plugin.c //#-- Prepare the macro library. // RUN: %target-swift-frontend \ From 7dc77416248bdee59c3b5f61b65e881945e228f4 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 14:52:18 -0700 Subject: [PATCH 33/50] Update windows-link-job.swift Fix an incorrect alteration to the test --- test/Driver/windows-link-job.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Driver/windows-link-job.swift b/test/Driver/windows-link-job.swift index 6ed7832bf5c06..78fe8873b0ac2 100644 --- a/test/Driver/windows-link-job.swift +++ b/test/Driver/windows-link-job.swift @@ -1,7 +1,7 @@ // RUN: %empty-directory(%t/DISTINCTIVE-WINDOWS-PATH/usr/bin) // RUN: %empty-directory(%t/DISTINCTIVE-WINDOWS-PATH/usr/lib) // RUN: %hardlink-or-copy(from: %swift_frontend_plain, to: %t/DISTINCTIVE-WINDOWS-PATH/usr/bin/swiftc) -// RUN: %host-library-env PATH= %t/DISTINCTIVE-WINDOWS-PATH/usr/bin/swiftc -target x86_64-unknown-windows-msvc -### -module-name link -emit-library %s 2>&1 | %FileCheck %s +// RUN: env PATH= %t/DISTINCTIVE-WINDOWS-PATH/usr/bin/swiftc -target x86_64-unknown-windows-msvc -### -module-name link -emit-library %s 2>&1 | %FileCheck %s // swift-frontend cannot be copied to another location with bootstrapping because // it will not find the libswiftCore library with its relative RPATH. From 1e9d950f574b4085e4d2e523ab0cca00e92458cf Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 15:18:36 -0700 Subject: [PATCH 34/50] Driver: virtualise the plugin path handling The plugin layouts are different across platforms. Move this into a virtual method and allow replacement. On Windows, the plugins are placed into the `bin` directory as the DLLs should always be co-located to ensure that the proper DLLs are found (there is no concept of RPATH). --- include/swift/Driver/ToolChain.h | 5 +++- lib/Driver/DarwinToolChains.cpp | 26 ++++++++++++++++++ lib/Driver/ToolChains.cpp | 24 +---------------- lib/Driver/ToolChains.h | 12 ++++++++- lib/Driver/UnixToolChains.cpp | 26 ++++++++++++++++++ lib/Driver/WindowsToolChains.cpp | 11 ++++++++ test/Macros/lit.local.cfg | 27 ++++++++++--------- .../serialize_plugin_search_paths.swift | 4 +-- 8 files changed, 96 insertions(+), 39 deletions(-) diff --git a/include/swift/Driver/ToolChain.h b/include/swift/Driver/ToolChain.h index b50c720ead1e3..5829dd543fd40 100644 --- a/include/swift/Driver/ToolChain.h +++ b/include/swift/Driver/ToolChain.h @@ -340,7 +340,10 @@ class ToolChain { void addLinkRuntimeLib(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &Arguments, StringRef LibName) const; - + + virtual void addPluginArguments(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &Arguments) const {} + /// Validates arguments passed to the toolchain. /// /// An override point for platform-specific subclasses to customize the diff --git a/lib/Driver/DarwinToolChains.cpp b/lib/Driver/DarwinToolChains.cpp index a06f1cc77d0b0..9dc66f83a6ead 100644 --- a/lib/Driver/DarwinToolChains.cpp +++ b/lib/Driver/DarwinToolChains.cpp @@ -24,6 +24,7 @@ #include "swift/Driver/Compilation.h" #include "swift/Driver/Driver.h" #include "swift/Driver/Job.h" +#include "swift/IDETool/CompilerInvocation.h" #include "swift/Option/Options.h" #include "clang/Basic/DarwinSDKInfo.h" #include "clang/Basic/Version.h" @@ -123,6 +124,31 @@ std::string toolchains::Darwin::sanitizerRuntimeLibName(StringRef Sanitizer, .str(); } +void +toolchains::Darwin::addPluginArguments(const ArgList &Args, + ArgStringList &Arguments) const { + SmallString<64> pluginPath; + auto programPath = getDriver().getSwiftProgramPath(); + CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( + programPath, /*shared=*/true, pluginPath); + + auto defaultPluginPath = pluginPath; + llvm::sys::path::append(defaultPluginPath, "host", "plugins"); + + // Default plugin path. + Arguments.push_back("-plugin-path"); + Arguments.push_back(Args.MakeArgString(defaultPluginPath)); + + // Local plugin path. + llvm::sys::path::remove_filename(pluginPath); // Remove "swift" + llvm::sys::path::remove_filename(pluginPath); // Remove "lib" + llvm::sys::path::append(pluginPath, "local", "lib"); + llvm::sys::path::append(pluginPath, "swift"); + llvm::sys::path::append(pluginPath, "host", "plugins"); + Arguments.push_back("-plugin-path"); + Arguments.push_back(Args.MakeArgString(pluginPath)); +} + static void addLinkRuntimeLibRPath(const ArgList &Args, ArgStringList &Arguments, StringRef DarwinLibName, diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index e232a46962a9c..a290cc5e1d27b 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -376,30 +376,8 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI, // options. inputArgs.AddAllArgs(arguments, options::OPT_plugin_search_Group); addPlatformSpecificPluginFrontendArgs(OI, output, inputArgs, arguments); - - // Toolchain-relative plugin paths - { - SmallString<64> pluginPath; - auto programPath = getDriver().getSwiftProgramPath(); - CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( - programPath, /*shared=*/true, pluginPath); - auto defaultPluginPath = pluginPath; - llvm::sys::path::append(defaultPluginPath, "host", "plugins"); - - // Default plugin path. - arguments.push_back("-plugin-path"); - arguments.push_back(inputArgs.MakeArgString(defaultPluginPath)); - - // Local plugin path. - llvm::sys::path::remove_filename(pluginPath); // Remove "swift" - llvm::sys::path::remove_filename(pluginPath); // Remove "lib" - llvm::sys::path::append(pluginPath, "local", "lib"); - llvm::sys::path::append(pluginPath, "swift"); - llvm::sys::path::append(pluginPath, "host", "plugins"); - arguments.push_back("-plugin-path"); - arguments.push_back(inputArgs.MakeArgString(pluginPath)); - } + addPluginArguments(inputArgs, arguments); // Pass through any subsystem flags. inputArgs.AddAllArgs(arguments, options::OPT_Xllvm); diff --git a/lib/Driver/ToolChains.h b/lib/Driver/ToolChains.h index c552d6b81246e..a7aab90990a1d 100644 --- a/lib/Driver/ToolChains.h +++ b/lib/Driver/ToolChains.h @@ -65,7 +65,10 @@ class LLVM_LIBRARY_VISIBILITY Darwin : public ToolChain { const JobContext &context) const override; InvocationInfo constructInvocation(const StaticLinkJobAction &job, const JobContext &context) const override; - + + void addPluginArguments(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &Arguments) const override; + void validateArguments(DiagnosticEngine &diags, const llvm::opt::ArgList &args, StringRef defaultTarget) const override; @@ -113,8 +116,12 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain { public: Windows(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {} ~Windows() = default; + std::string sanitizerRuntimeLibName(StringRef Sanitizer, bool shared = true) const override; + + void addPluginArguments(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &Arguments) const override; }; class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain { @@ -168,6 +175,9 @@ class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain { ~GenericUnix() = default; std::string sanitizerRuntimeLibName(StringRef Sanitizer, bool shared = true) const override; + + void addPluginArguments(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &Arguments) const override; }; class LLVM_LIBRARY_VISIBILITY Android : public GenericUnix { diff --git a/lib/Driver/UnixToolChains.cpp b/lib/Driver/UnixToolChains.cpp index 389afb821dd61..61498c144a28b 100644 --- a/lib/Driver/UnixToolChains.cpp +++ b/lib/Driver/UnixToolChains.cpp @@ -21,6 +21,7 @@ #include "swift/Driver/Compilation.h" #include "swift/Driver/Driver.h" #include "swift/Driver/Job.h" +#include "swift/IDETool/CompilerInvocation.h" #include "swift/Option/Options.h" #include "swift/Option/SanitizerOptions.h" #include "clang/Basic/Version.h" @@ -47,6 +48,31 @@ toolchains::GenericUnix::sanitizerRuntimeLibName(StringRef Sanitizer, .str(); } +void +toolchains::GenericUnix::addPluginArguments(const ArgList &Args, + ArgStringList &Arguments) const { + SmallString<64> pluginPath; + auto programPath = getDriver().getSwiftProgramPath(); + CompilerInvocation::computeRuntimeResourcePathFromExecutablePath( + programPath, /*shared=*/true, pluginPath); + + auto defaultPluginPath = pluginPath; + llvm::sys::path::append(defaultPluginPath, "host", "plugins"); + + // Default plugin path. + Arguments.push_back("-plugin-path"); + Arguments.push_back(Args.MakeArgString(defaultPluginPath)); + + // Local plugin path. + llvm::sys::path::remove_filename(pluginPath); // Remove "swift" + llvm::sys::path::remove_filename(pluginPath); // Remove "lib" + llvm::sys::path::append(pluginPath, "local", "lib"); + llvm::sys::path::append(pluginPath, "swift"); + llvm::sys::path::append(pluginPath, "host", "plugins"); + Arguments.push_back("-plugin-path"); + Arguments.push_back(Args.MakeArgString(pluginPath)); +} + ToolChain::InvocationInfo toolchains::GenericUnix::constructInvocation(const InterpretJobAction &job, const JobContext &context) const { diff --git a/lib/Driver/WindowsToolChains.cpp b/lib/Driver/WindowsToolChains.cpp index 5001fa33fd2a5..481daa7ba3640 100644 --- a/lib/Driver/WindowsToolChains.cpp +++ b/lib/Driver/WindowsToolChains.cpp @@ -44,6 +44,17 @@ std::string toolchains::Windows::sanitizerRuntimeLibName(StringRef Sanitizer, .str(); } +void +toolchains::Windows::addPluginArguments(const ArgList &Args, + ArgStringList &Arguments) const { + SmallString<261> LibraryPath = StringRef(getDriver().getSwiftProgramPath()); + llvm::sys::path::remove_filename(LibraryPath); // Remove `swift` + + // Default plugin path. + Arguments.push_back("-plugin-path"); + Arguments.push_back(Args.MakeArgString(LibraryPath)); +} + ToolChain::InvocationInfo toolchains::Windows::constructInvocation(const DynamicLinkJobAction &job, const JobContext &context) const { diff --git a/test/Macros/lit.local.cfg b/test/Macros/lit.local.cfg index dd010ab88930a..1ae3d3a80fb24 100644 --- a/test/Macros/lit.local.cfg +++ b/test/Macros/lit.local.cfg @@ -10,18 +10,21 @@ def get_target_os(): return run_os if get_target_os() in ['windows-msvc']: - config.substitutions.insert( - 0, - ( - '%swift-build-cxx-plugin', - '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin' + config.substitutions.insert(0, ('%target-abi', 'WIN')) + config.substitutions.insert( + 0, + ( + '%swift-build-cxx-plugin', + '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin' + ) ) - ) else: - config.substitutions.insert( - 0, - ( - '%swift-build-cxx-plugin', - '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir' + # FIXME(compnerd) do all the targets we currently support use SysV ABI? + config.substitutions.insert(0, ('%target-abi', 'SYSV')) + config.substitutions.insert( + 0, + ( + '%swift-build-cxx-plugin', + '%clang -isysroot %host_sdk -I %swift_src_root/include -L %swift-lib-dir -l_swiftMockPlugin -Xlinker -rpath -Xlinker %swift-lib-dir' + ) ) - ) diff --git a/test/Macros/serialize_plugin_search_paths.swift b/test/Macros/serialize_plugin_search_paths.swift index cd4b53fc4725f..9295e124f8e46 100644 --- a/test/Macros/serialize_plugin_search_paths.swift +++ b/test/Macros/serialize_plugin_search_paths.swift @@ -16,5 +16,5 @@ // CHECK: -external-plugin-path {{.*}}plugins#{{.*}}swift-plugin-server // CHECK: -load-plugin-library {{.*}}MacroDefinition.{{dylib|so|dll}} // CHECK: -load-plugin-executable {{.*}}mock-plugin#TestPlugin -// CHECK: -plugin-path {{.*}}plugins -// CHECK: -plugin-path {{.*}}plugins +// CHECK-SYSV:-plugin-path {{.*}}plugins +// CHECK-SYSV:-plugin-path {{.*}}plugins From 998a5d9a5aa185671ce079c810651979537a356c Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 4 Sep 2023 16:12:56 -0700 Subject: [PATCH 35/50] ASTGen: add a workaround for CSC conformance visibility A thrown error is stored as `any Error` which does not conform to `CustomStringConvertible`. When we perform the conversion via the `String(describing:)` initialiser, for some reason the runtime does not find the conformance and the error is not translated properly. Explicitly casting to `PluginError` resolves the missing conformance and fixes a test failure. --- lib/ASTGen/Sources/ASTGen/Macros.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index bea7df7337358..4ead5f21ecc98 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -183,7 +183,11 @@ enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage { var message: String { switch self { case .thrownError(let error): - return String(describing: error) + if let err = error as? PluginError { + return err.description + } else { + return String(describing: error) + } case .oldStyleExternalMacro: return "external macro definitions are now written using #externalMacro" From 5802d5956d1d98cb83c425c0b54e983ec9a4fd67 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 5 Sep 2023 17:11:19 -0700 Subject: [PATCH 36/50] Update CBasicBridging.h --- include/swift/Basic/CBasicBridging.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 24b4796c92c2b..4d1367a6f3d75 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -83,6 +83,23 @@ typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt; typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; #endif +#if __clang__ +// Provide macros to temporarily suppress warning about the use of +// _Nullable and _Nonnull. +#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wnullability-extension\"") \ + _Pragma("clang assume_nonnull begin") + +#define SWIFT_END_NULLABILITY_ANNOTATIONS \ + _Pragma("clang diagnostic pop") _Pragma("clang assume_nonnull end") +#else +#define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS +#define SWIFT_END_NULLABILITY_ANNOTATIONS +#define _Nullable +#define _Nonnull +#endif + SWIFT_BEGIN_NULLABILITY_ANNOTATIONS #ifdef __cplusplus From be30bc3ef67fa10efd047d36340c76de478eb19f Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 5 Sep 2023 19:22:40 -0700 Subject: [PATCH 37/50] Update CBasicBridging.h --- include/swift/Basic/CBasicBridging.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 4d1367a6f3d75..0f543753d68a0 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -100,8 +100,6 @@ typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; #define _Nonnull #endif -SWIFT_BEGIN_NULLABILITY_ANNOTATIONS - #ifdef __cplusplus extern "C" { @@ -115,6 +113,7 @@ FeatureName, #include "swift/Basic/Features.def" } BridgedFeature; +SWIFT_BEGIN_NULLABILITY_ANNOTATIONS SWIFT_BEGIN_ASSUME_NONNULL typedef struct BridgedData { From 7ddafec217dfef8ecf1ee04de622f7b0d67f541c Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 5 Sep 2023 21:58:44 -0700 Subject: [PATCH 38/50] Update CBasicBridging.h --- include/swift/Basic/CBasicBridging.h | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/include/swift/Basic/CBasicBridging.h b/include/swift/Basic/CBasicBridging.h index 0f543753d68a0..0835b111bee69 100644 --- a/include/swift/Basic/CBasicBridging.h +++ b/include/swift/Basic/CBasicBridging.h @@ -100,6 +100,8 @@ typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; #define _Nonnull #endif +SWIFT_BEGIN_NULLABILITY_ANNOTATIONS + #ifdef __cplusplus extern "C" { @@ -107,15 +109,6 @@ extern "C" { #endif -typedef enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature { -#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \ -FeatureName, -#include "swift/Basic/Features.def" -} BridgedFeature; - -SWIFT_BEGIN_NULLABILITY_ANNOTATIONS -SWIFT_BEGIN_ASSUME_NONNULL - typedef struct BridgedData { const char *_Nullable baseAddress; SwiftUInt size; From bc2930118548916d592571e7ba45ca61a70d28ae Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 07:23:19 -0700 Subject: [PATCH 39/50] Update lit.cfg --- test/lit.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/lit.cfg b/test/lit.cfg index 852f241018e70..35d6ac55029a7 100644 --- a/test/lit.cfg +++ b/test/lit.cfg @@ -572,8 +572,8 @@ else: config.swiftc_driver, config.host_triple, '' if kIsWindows else '-no-toolchain-stdlib-rpath', - config.swift_host_lib_dir, - config.swift_host_lib_dir, + config.swift_build_lib_dir, + config.swift_build_lib_dir, '' if kIsWindows else '-Xlinker -rpath -Xlinker {}'.format(toolchain_lib_dir) ) ) From 4e8edd48e6c00031c66c5bc7eb3394cae5c7c902 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 08:18:57 -0700 Subject: [PATCH 40/50] Update Generics.swift --- lib/ASTGen/Sources/ASTGen/Generics.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ASTGen/Sources/ASTGen/Generics.swift b/lib/ASTGen/Sources/ASTGen/Generics.swift index 5226846e62b57..4976560b85f91 100644 --- a/lib/ASTGen/Sources/ASTGen/Generics.swift +++ b/lib/ASTGen/Sources/ASTGen/Generics.swift @@ -19,7 +19,7 @@ extension ASTGenVisitor { func visit(_ node: GenericParameterSyntax) -> ASTNode { var nodeName = node.name.text let name = nodeName.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let nameLoc = self.base.advanced(by: node.name.position.utf8Offset).raw let eachLoc = node.each.map { self.base.advanced(by: $0.position.utf8Offset).raw } From 796c4ceafe1cbf6e5fdc47259da0395350b700da Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 08:20:53 -0700 Subject: [PATCH 41/50] Update PluginHost.swift --- lib/ASTGen/Sources/ASTGen/PluginHost.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/PluginHost.swift b/lib/ASTGen/Sources/ASTGen/PluginHost.swift index 2d48e5d59d6d0..7edb51aaf0854 100644 --- a/lib/ASTGen/Sources/ASTGen/PluginHost.swift +++ b/lib/ASTGen/Sources/ASTGen/PluginHost.swift @@ -277,7 +277,7 @@ class PluginDiagnosticsEngine { SwiftDiagnostic_create( cxxDiagnosticEngine, bridgedSeverity, cxxSourceLocation(at: position), - messageBuffer.baseAddress, messageBuffer.count) + messageBuffer.baseAddress, SwiftInt(messageBuffer.count)) } // Emit highlights @@ -296,7 +296,7 @@ class PluginDiagnosticsEngine { var newText = change.newText newText.withUTF8 { textBuffer in SwiftDiagnostic_fixItReplace( - diag, startLoc, endLoc, textBuffer.baseAddress, textBuffer.count) + diag, startLoc, endLoc, textBuffer.baseAddress, SwiftInt(textBuffer.count)) } } @@ -343,7 +343,7 @@ class PluginDiagnosticsEngine { else { return nil } - return bufferBaseAddress.advanced(by: SwiftInt(offset)) + return bufferBaseAddress.advanced(by: offset) } /// C++ source location from a position value from a plugin. From 940123967967b0671c89a7f538dfb0fc872cdb87 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 08:31:32 -0700 Subject: [PATCH 42/50] ASTGen: more type changes --- lib/ASTGen/Sources/ASTGen/Decls.swift | 13 +++++++------ lib/ASTGen/Sources/ASTGen/Diagnostics.swift | 9 +++++---- lib/ASTGen/Sources/ASTGen/Exprs.swift | 7 ++++--- lib/ASTGen/Sources/ASTGen/Literals.swift | 5 +++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/Decls.swift b/lib/ASTGen/Sources/ASTGen/Decls.swift index 815bb696b137b..6c725e5642ef0 100644 --- a/lib/ASTGen/Sources/ASTGen/Decls.swift +++ b/lib/ASTGen/Sources/ASTGen/Decls.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax @@ -8,7 +9,7 @@ extension ASTGenVisitor { let equalLoc = self.base.advanced(by: node.initializer.equal.position.utf8Offset).raw var nameText = node.identifier.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let nameLoc = self.base.advanced(by: node.identifier.position.utf8Offset).raw let genericParams = node.genericParameterClause.map { self.visit($0).rawValue } @@ -29,7 +30,7 @@ extension ASTGenVisitor { let loc = self.base.advanced(by: node.position.utf8Offset).raw var nameText = node.identifier.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let genericParams = node.genericParameterClause @@ -52,7 +53,7 @@ extension ASTGenVisitor { let loc = self.base.advanced(by: node.position.utf8Offset).raw var nameText = node.identifier.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let out = ClassDecl_create(ctx, loc, name, loc, declContext) @@ -95,7 +96,7 @@ extension ASTGenVisitor { // Swift AST represents "_" as nil. var text = nodeFirstName.text firstName = text.withUTF8 { buf in - SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } } else { firstName = nil @@ -104,7 +105,7 @@ extension ASTGenVisitor { if let nodeSecondName = node.secondName { var text = nodeSecondName.text secondName = text.withUTF8 { buf in - SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } } else { secondName = nil @@ -124,7 +125,7 @@ extension ASTGenVisitor { var nameText = node.identifier.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let returnType: ASTNode? diff --git a/lib/ASTGen/Sources/ASTGen/Diagnostics.swift b/lib/ASTGen/Sources/ASTGen/Diagnostics.swift index 7071029c43983..e0a41b819d7b4 100644 --- a/lib/ASTGen/Sources/ASTGen/Diagnostics.swift +++ b/lib/ASTGen/Sources/ASTGen/Diagnostics.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftDiagnostics import SwiftSyntax @@ -37,7 +38,7 @@ fileprivate func emitDiagnosticParts( let diag = mutableMessage.withUTF8 { messageBuffer in SwiftDiagnostic_create( diagEnginePtr, bridgedSeverity, sourceLoc(at: position), - messageBuffer.baseAddress, messageBuffer.count + messageBuffer.baseAddress, SwiftInt(messageBuffer.count) ) } @@ -76,7 +77,7 @@ fileprivate func emitDiagnosticParts( newText.withUTF8 { textBuffer in SwiftDiagnostic_fixItReplace( diag, replaceStartLoc, replaceEndLoc, - textBuffer.baseAddress, textBuffer.count + textBuffer.baseAddress, SwiftInt(textBuffer.count) ) } } @@ -149,7 +150,7 @@ extension SourceManager { SwiftDiagnostic_create( cxxDiagnosticEngine, bridgedSeverity, cxxSourceLocation(for: node, at: position), - messageBuffer.baseAddress, messageBuffer.count + messageBuffer.baseAddress, SwiftInt(messageBuffer.count) ) } @@ -202,7 +203,7 @@ extension SourceManager { newText.withUTF8 { textBuffer in SwiftDiagnostic_fixItReplace( diag, replaceStartLoc, replaceEndLoc, - textBuffer.baseAddress, textBuffer.count + textBuffer.baseAddress, SwiftInt(textBuffer.count) ) } } diff --git a/lib/ASTGen/Sources/ASTGen/Exprs.swift b/lib/ASTGen/Sources/ASTGen/Exprs.swift index 38097c72cc218..3a93da93a6d49 100644 --- a/lib/ASTGen/Sources/ASTGen/Exprs.swift +++ b/lib/ASTGen/Sources/ASTGen/Exprs.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax @@ -34,7 +35,7 @@ extension ASTGenVisitor { var text = node.identifier.text let id = text.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } return .expr(SwiftIdentifierExpr_create(ctx, id, loc)) @@ -45,7 +46,7 @@ extension ASTGenVisitor { var text = node.identifier.text let id = text.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } return .expr(SwiftIdentifierExpr_create(ctx, id, loc)) @@ -56,7 +57,7 @@ extension ASTGenVisitor { let base = visit(node.base!).rawValue var nameText = node.name.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } return .expr(UnresolvedDotExpr_create(ctx, base, loc, name, loc)) diff --git a/lib/ASTGen/Sources/ASTGen/Literals.swift b/lib/ASTGen/Sources/ASTGen/Literals.swift index 1653d09ac348d..c80eedf8ee1a2 100644 --- a/lib/ASTGen/Sources/ASTGen/Literals.swift +++ b/lib/ASTGen/Sources/ASTGen/Literals.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax @@ -8,7 +9,7 @@ extension ASTGenVisitor { var segment = node.segments.first!.as(StringSegmentSyntax.self)!.content.text return .expr( segment.withUTF8 { buf in - return SwiftStringLiteralExpr_create(ctx, buf.baseAddress, buf.count, loc) + return SwiftStringLiteralExpr_create(ctx, buf.baseAddress, SwiftInt(buf.count), loc) }) } @@ -17,7 +18,7 @@ extension ASTGenVisitor { var segment = node.digits.text return .expr( segment.withUTF8 { buf in - return SwiftIntegerLiteralExpr_create(ctx, buf.baseAddress, buf.count, loc) + return SwiftIntegerLiteralExpr_create(ctx, buf.baseAddress, SwiftInt(buf.count), loc) }) } From c102ce85056a4fc5c7ebe73a13f80bc524afe3b1 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 12:00:19 -0700 Subject: [PATCH 43/50] Update Types.swift --- lib/ASTGen/Sources/ASTGen/Types.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/Types.swift b/lib/ASTGen/Sources/ASTGen/Types.swift index eb2be48a8a89b..3bb73ef19d184 100644 --- a/lib/ASTGen/Sources/ASTGen/Types.swift +++ b/lib/ASTGen/Sources/ASTGen/Types.swift @@ -1,4 +1,5 @@ import CASTBridging +import CBasicBridging import SwiftParser import SwiftSyntax @@ -8,7 +9,7 @@ extension ASTGenVisitor { var text = node.name.text let id = text.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } guard let generics = node.genericArgumentClause else { @@ -38,7 +39,7 @@ extension ASTGenVisitor { var nameText = nameToken.text let name = nameText.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let nameLoc = self.base.advanced(by: nameToken.position.utf8Offset).raw @@ -175,12 +176,12 @@ extension ASTGenVisitor { for element in elementList { var nameText = element.name?.text let name = nameText?.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let nameLoc = element.name.map { self.base.advanced(by: $0.position.utf8Offset).raw } var secondNameText = element.secondName?.text let secondName = secondNameText?.withUTF8 { buf in - return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } let secondNameLoc = element.secondName.map { self.base.advanced(by: $0.position.utf8Offset).raw From 9fd7a0f2c6de2603d7187f5c84d53b7d6acb5b25 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 13:59:47 -0700 Subject: [PATCH 44/50] Update Exprs.swift --- lib/ASTGen/Sources/ASTGen/Exprs.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ASTGen/Sources/ASTGen/Exprs.swift b/lib/ASTGen/Sources/ASTGen/Exprs.swift index 3a93da93a6d49..b02ad452a37e6 100644 --- a/lib/ASTGen/Sources/ASTGen/Exprs.swift +++ b/lib/ASTGen/Sources/ASTGen/Exprs.swift @@ -79,7 +79,7 @@ extension ASTGenVisitor { return nil } return name.withUTF8 { buf in - SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count) + SwiftASTContext_getIdentifier(ctx, buf.baseAddress, SwiftInt(buf.count)) } } let labelLocs: [UnsafeMutableRawPointer] = node.map { From b23238fbecc3027813f361272f13a86bb6eec0fd Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 16:25:24 -0700 Subject: [PATCH 45/50] Update CASTBridging.cpp --- lib/AST/CASTBridging.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/AST/CASTBridging.cpp b/lib/AST/CASTBridging.cpp index 83afd905825bf..85a10183a0ac4 100644 --- a/lib/AST/CASTBridging.cpp +++ b/lib/AST/CASTBridging.cpp @@ -52,7 +52,7 @@ BridgedDiagnostic SwiftDiagnostic_create(void *diagnosticEngine, BridgedDiagnosticSeverity severity, const void *sourceLocPtr, const unsigned char *textPtr, - long textLen) { + SwiftInt textLen) { StringRef origText{ reinterpret_cast(textPtr), size_t(textLen)}; BridgedDiagnosticImpl::Allocator alloc; @@ -99,7 +99,7 @@ void SwiftDiagnostic_fixItReplace(BridgedDiagnostic diagPtr, const void *replaceStartLocPtr, const void *replaceEndLocPtr, const unsigned char *newTextPtr, - long newTextLen) { + SwiftInt newTextLen) { SourceLoc startLoc = getSourceLocFromPointer(replaceStartLocPtr); SourceLoc endLoc = getSourceLocFromPointer(replaceEndLocPtr); @@ -122,7 +122,7 @@ void SwiftDiagnostic_finish(BridgedDiagnostic diagPtr) { BridgedIdentifier SwiftASTContext_getIdentifier(void *ctx, const unsigned char *_Nullable str, - long len) { + SwiftInt len) { return const_cast( static_cast(ctx) ->getIdentifier( @@ -220,7 +220,7 @@ void *SwiftIdentifierExpr_create(void *ctx, BridgedIdentifier base, void *loc) { void *SwiftStringLiteralExpr_create(void *ctx, const unsigned char *_Nullable string, - long len, void *TokenLoc) { + SwiftInt len, void *TokenLoc) { ASTContext &Context = *static_cast(ctx); auto stringRef = Context.AllocateCopy( StringRef{reinterpret_cast(string), size_t(len)}); @@ -230,7 +230,7 @@ void *SwiftStringLiteralExpr_create(void *ctx, void *SwiftIntegerLiteralExpr_create(void *ctx, const unsigned char *_Nullable string, - long len, void *TokenLoc) { + SwiftInt len, void *TokenLoc) { ASTContext &Context = *static_cast(ctx); auto stringRef = Context.AllocateCopy( StringRef{reinterpret_cast(string), size_t(len)}); From 48b95939724081b8abc9f85b5db03e246011956f Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 31 Aug 2023 18:05:02 -0700 Subject: [PATCH 46/50] build: adjust the build to support non-Apple environments Account for import libraries and the associated layout difference on platforms (e.g. DLLs are placed in `bin`). This is required to enable building the macro path on Windows. --- cmake/modules/AddSwift.cmake | 3 + lib/CMakeLists.txt | 60 +++++++++++++------ .../cmake/modules/AddSwiftSourceKit.cmake | 3 + tools/driver/CMakeLists.txt | 2 + 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 328af19adc60d..35faa2ddb9adf 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -570,6 +570,9 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) else() message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${ASRLF_BOOTSTRAPPING_MODE}'") endif() + else() + target_link_directories(${target} PRIVATE + ${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH}) endif() if(SWIFT_SWIFT_PARSER) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 8675e6098410c..6fa081e792c8c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -49,6 +49,12 @@ if (SWIFT_SWIFT_PARSER) "${CMAKE_SHARED_LIBRARY_SUFFIX}" OUTPUT_VARIABLE SWIFT_SYNTAX_SHARED_LIBRARIES) + list(TRANSFORM SWIFT_SYNTAX_MODULES PREPEND "${CMAKE_IMPORT_LIBRARY_PREFIX}" + OUTPUT_VARIABLE SWIFT_SYNTAX_IMPORT_LIBRARIES) + list(TRANSFORM SWIFT_SYNTAX_IMPORT_LIBRARIES APPEND + "${CMAKE_IMPORT_LIBRARY_SUFFIX}" OUTPUT_VARIABLE + SWIFT_SYNTAX_IMPORT_LIBRARIES) + # Interface library to collect swiftinterfaces and swiftmodules from # SwiftSyntax add_library(swiftSyntaxLibraries INTERFACE) @@ -67,27 +73,47 @@ if (SWIFT_SWIFT_PARSER) ) endif() - add_custom_command( - OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" - DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}" - COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib} - ${add_origin_rpath} - ) - - add_custom_target(copy_swiftSyntaxLibrary_${sharedlib} - DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" - COMMENT "Copying ${sharedlib}" - ) - - swift_install_in_component( - PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" - DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host" - COMPONENT compiler - ) + if(CMAKE_SYSTEM_NAME MATCHES Windows) + add_custom_command(OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${sharedlib} + DEPENDS "${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/bin/${sharedlib}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/bin/${sharedlib} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${sharedlib}) + add_custom_target(copy_swiftSyntaxLibrary_${sharedlib} + DEPENDS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${sharedlib} + COMMENT "copying ${sharedlib}") + swift_install_in_component(PROGRAMS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${sharedlib} + DESTINATION bin + COMPONENT compiler) + else() + add_custom_command(OUTPUT "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" + DEPENDS "${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib}" + COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SWIFT_SYNTAX_LIBRARIES_BUILD_DIR}/${sharedlib} ${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib} + ${add_origin_rpath}) + add_custom_target(copy_swiftSyntaxLibrary_${sharedlib} + DEPENDS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" + COMMENT "Copying ${sharedlib}") + swift_install_in_component(PROGRAMS "${SWIFT_HOST_LIBRARIES_DEST_DIR}/${sharedlib}" + DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/host" + COMPONENT compiler) + endif() add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${sharedlib}) endforeach() + if(CMAKE_SYSTEM_NAME MATCHES Windows) + foreach(implib ${SWIFT_SYNTAX_IMPORT_LIBRARIES}) + add_custom_command(OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + DEPENDS ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host/${implib} + COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host/${implib} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib}) + add_custom_target(copy_swiftSyntaxLibrary_${implib} + DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + COMMENT "Copying ${implib}") + swift_install_in_component(PROGRAMS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + DESTINATION lib + COMPONENT compiler) + add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${implib}) + endforeach() + endif() + # Copy all of the Swift modules from earlyswiftsyntax so they can be found # in the same relative place within the build directory as in the final # toolchain. diff --git a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake index 7e2e1bc16e763..5514431eea9ce 100644 --- a/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake +++ b/tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake @@ -162,6 +162,9 @@ function(add_sourcekit_swift_runtime_link_flags target path HAS_SWIFT_MODULES) file(RELATIVE_PATH relative_hostlib_path "${path}" "${SWIFTLIB_DIR}/host") list(APPEND RPATH_LIST "$ORIGIN/${relative_hostlib_path}") endif() + else() + target_link_directories(${target} PRIVATE + ${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH}) endif() if(SWIFT_SWIFT_PARSER) diff --git a/tools/driver/CMakeLists.txt b/tools/driver/CMakeLists.txt index 649510e8771fa..9c2e5ed6486a9 100644 --- a/tools/driver/CMakeLists.txt +++ b/tools/driver/CMakeLists.txt @@ -5,6 +5,8 @@ # parser is built in. function(add_swift_parser_link_libraries target) if(SWIFT_SWIFT_PARSER) + target_link_directories(${target} PRIVATE + ${SWIFT_PATH_TO_SWIFT_SDK}/usr/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}/${SWIFT_HOST_VARIANT_ARCH}) target_link_libraries(${target} PRIVATE swiftCore) From 0ff4ac674ef5aae9063d76272d9353e252a8ca0b Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 19:31:19 -0700 Subject: [PATCH 47/50] build: fix the build of the toolchain This wires up the new macro properly into the build machinery to ensure that the `distribution` target properly builds and installs the dependencies. This fixes the missing `swift-plugin-server` on Windows. --- cmake/modules/AddPureSwift.cmake | 18 +++++++++++++++--- tools/swift-plugin-server/CMakeLists.txt | 7 ++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cmake/modules/AddPureSwift.cmake b/cmake/modules/AddPureSwift.cmake index a5954c114cf6d..ad177634dbca9 100644 --- a/cmake/modules/AddPureSwift.cmake +++ b/cmake/modules/AddPureSwift.cmake @@ -269,7 +269,8 @@ function(add_pure_swift_host_tool name) # Option handling set(options) - set(single_parameter_options) + set(single_parameter_options + SWIFT_COMPONENT) set(multiple_parameter_options DEPENDENCIES SWIFT_DEPENDENCIES) @@ -323,6 +324,17 @@ function(add_pure_swift_host_tool name) target_include_directories(${name} PUBLIC ${SWIFT_HOST_LIBRARIES_DEST_DIR}) - # Export this target. - set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) + if(NOT APSHT_SWIFT_COMPONENT STREQUAL no_component) + add_dependencies(${APSHT_SWIFT_COMPONENT} ${name}) + swift_install_in_component(TARGETS ${name} + COMPONENT ${APSHT_SWIFT_COMPONENT} + RUNTIME DESTINATION bin) + swift_is_installing_component(${APSHT_SWIFT_COMPONENT} is_installing) + endif() + + if(NOT is_installing) + set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name}) + else() + set_property(GLOBAL APPEND PROPERTY SWIFT_EXPORTS ${name}) + endif() endfunction() diff --git a/tools/swift-plugin-server/CMakeLists.txt b/tools/swift-plugin-server/CMakeLists.txt index 09332309b10c9..1d1b88b22bd8a 100644 --- a/tools/swift-plugin-server/CMakeLists.txt +++ b/tools/swift-plugin-server/CMakeLists.txt @@ -16,6 +16,8 @@ if (SWIFT_SWIFT_PARSER) DEPENDENCIES swiftDemangling $ + SWIFT_COMPONENT + compiler SWIFT_DEPENDENCIES SwiftSyntax::SwiftSyntaxMacros SwiftSyntax::SwiftSyntaxMacroExpansion @@ -25,9 +27,4 @@ if (SWIFT_SWIFT_PARSER) target_include_directories(swift-plugin-server PRIVATE Sources/CSwiftPluginServer/include ) - swift_install_in_component(TARGETS swift-plugin-server - RUNTIME - DESTINATION bin - COMPONENT compiler - ) endif() From c79cd306f994f51a2da25cbd94dbd8dbec06e9bf Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 19:37:50 -0700 Subject: [PATCH 48/50] Update LibcShims.cpp Add missing include for `atexit`. --- stdlib/public/stubs/LibcShims.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/public/stubs/LibcShims.cpp b/stdlib/public/stubs/LibcShims.cpp index 4c6e29e2a64b3..0350c515503bd 100644 --- a/stdlib/public/stubs/LibcShims.cpp +++ b/stdlib/public/stubs/LibcShims.cpp @@ -22,6 +22,7 @@ #endif #include +#include #include "swift/shims/LibcShims.h" From 667a1f868ef3164abd71aa740a78e9ce8947f650 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 8 Sep 2023 16:06:07 -0700 Subject: [PATCH 49/50] build: copy the import libraries to the correct location This fixes the test suite when performing a clean build. The libraries were being copied to the wrong location after the generalisation changes made earlier. --- lib/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6fa081e792c8c..7d61c18522a1c 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -101,13 +101,13 @@ if (SWIFT_SWIFT_PARSER) if(CMAKE_SYSTEM_NAME MATCHES Windows) foreach(implib ${SWIFT_SYNTAX_IMPORT_LIBRARIES}) - add_custom_command(OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + add_custom_command(OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/windows/${SWIFT_HOST_VARIANT_ARCH}/${implib} DEPENDS ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host/${implib} - COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host/${implib} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib}) + COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/lib/swift/host/${implib} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/windows/${SWIFT_HOST_VARIANT_ARCH}/${implib}) add_custom_target(copy_swiftSyntaxLibrary_${implib} - DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/windows/${SWIFT_HOST_VARIANT_ARCH}/${implib} COMMENT "Copying ${implib}") - swift_install_in_component(PROGRAMS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/lib/swift/windows/${implib} + swift_install_in_component(PROGRAMS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/swift/windows/${SWIFT_HOST_VARIANT_ARCH}/${implib} DESTINATION lib COMPONENT compiler) add_dependencies(swiftSyntaxLibraries copy_swiftSyntaxLibrary_${implib}) From 49bc58fc887539bcc6c1cb3c63602ce307515992 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 6 Sep 2023 10:48:05 -0700 Subject: [PATCH 50/50] swift-plugin-server: remove standard headers Redefine the types rather than use the standard headers due to the circular dependency between Darwin and Swift. --- .../CSwiftPluginServer/PluginServer.cpp | 4 +- .../CSwiftPluginServer/include/PluginServer.h | 66 ++++++++++++++++++- .../swift-plugin-server.swift | 12 ++-- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp index b84bdeaa173f7..a689f18d8ebc8 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/PluginServer.cpp @@ -118,7 +118,7 @@ void PluginServer_destroyConnection(const void *server) { delete static_cast(server); } -size_t PluginServer_read(const void *server, void *data, size_t nbyte) { +SwiftInt PluginServer_read(const void *server, void *data, SwiftUInt nbyte) { const auto *connection = static_cast(server); #if defined(_WIN32) return _read(connection->inputFD, data, nbyte); @@ -127,7 +127,7 @@ size_t PluginServer_read(const void *server, void *data, size_t nbyte) { #endif } -size_t PluginServer_write(const void *server, const void *data, size_t nbyte) { +SwiftInt PluginServer_write(const void *server, const void *data, SwiftUInt nbyte) { const auto *connection = static_cast(server); #if defined(_WIN32) return _write(connection->outputFD, data, nbyte); diff --git a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h index 92e62c3aa7bc4..d30c48b86dbc9 100644 --- a/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h +++ b/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h @@ -13,8 +13,68 @@ #ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H #define SWIFT_PLUGINSERVER_PLUGINSERVER_H -#include +// NOTE: Partially ported from SwiftShim's SwiftStdint.h. We cannot include +// that header here because it belongs to the runtime, but we need the same +// logic for interoperability with Swift code in the compiler itself. +// stdint.h is provided by Clang, but it dispatches to libc's stdint.h. As a +// result, using stdint.h here would pull in Darwin module (which includes +// libc). This creates a dependency cycle, so we can't use stdint.h in +// SwiftShims. +// On Linux, the story is different. We get the error message +// "/usr/include/x86_64-linux-gnu/sys/types.h:146:10: error: 'stddef.h' file not +// found" +// This is a known Clang/Ubuntu bug. + +// Clang has been defining __INTxx_TYPE__ macros for a long time. +// __UINTxx_TYPE__ are defined only since Clang 3.5. +#if defined(_MSC_VER) && !defined(__clang__) +typedef __int64 __swiftc_int64_t; +typedef unsigned __int64 __swiftc_uint64_t; +typedef int __swiftc_int32_t; +typedef unsigned int __swiftc_uint32_t; +#elif !defined(__APPLE__) && !defined(__linux__) && !defined(__OpenBSD__) && !defined(__wasi__) #include +typedef int64_t __swiftc_int64_t; +typedef uint64_t __swiftc_uint64_t; +typedef int32_t __swiftc_int32_t; +typedef uint32_t __swiftc_uint32_t; +typedef intptr_t __swiftc_intptr_t; +typedef uintptr_t __swiftc_uintptr_t; +#else +typedef __INT64_TYPE__ __swiftc_int64_t; +#ifdef __UINT64_TYPE__ +typedef __UINT64_TYPE__ __swiftc_uint64_t; +#else +typedef unsigned __INT64_TYPE__ __swiftc_uint64_t; +#endif + +typedef __INT32_TYPE__ __swiftc_int32_t; +#ifdef __UINT32_TYPE__ +typedef __UINT32_TYPE__ __swiftc_uint32_t; +#else +typedef unsigned __INT32_TYPE__ __swiftc_uint32_t; +#endif +#endif + +#define __swiftc_join3(a,b,c) a ## b ## c + +#define __swiftc_intn_t(n) __swiftc_join3(__swiftc_int, n, _t) +#define __swiftc_uintn_t(n) __swiftc_join3(__swiftc_uint, n, _t) + +#if defined(_MSC_VER) && !defined(__clang__) +#if defined(_WIN64) +typedef __swiftc_int64_t SwiftInt; +typedef __swiftc_uint64_t SwiftUInt; +#elif defined(_WIN32) +typedef __swiftc_int32_t SwiftInt; +typedef __swiftc_uint32_t SwiftUInt; +#else +#error unknown windows pointer width +#endif +#else +typedef __swiftc_intn_t(__INTPTR_WIDTH__) SwiftInt; +typedef __swiftc_uintn_t(__INTPTR_WIDTH__) SwiftUInt; +#endif #ifdef __cplusplus extern "C" { @@ -32,10 +92,10 @@ const void *PluginServer_createConnection(const char **errorMessage); void PluginServer_destroyConnection(const void *connHandle); /// Read bytes from the IPC communication handle. -size_t PluginServer_read(const void *connHandle, void *data, size_t nbyte); +SwiftInt PluginServer_read(const void *connHandle, void *data, SwiftUInt nbyte); /// Write bytes to the IPC communication handle. -size_t PluginServer_write(const void *connHandle, const void *data, size_t nbyte); +SwiftInt PluginServer_write(const void *connHandle, const void *data, SwiftUInt nbyte); //===----------------------------------------------------------------------===// // Dynamic link diff --git a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift index 8c06e6dcd5d5a..ad117e4d8e50e 100644 --- a/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift +++ b/tools/swift-plugin-server/Sources/swift-plugin-server/swift-plugin-server.swift @@ -172,13 +172,13 @@ final class PluginHostConnection: MessageConnection { var ptr = buffer.baseAddress! while (bytesToWrite > 0) { - let writtenSize = PluginServer_write(handle, ptr, Int(bytesToWrite)) + let writtenSize = PluginServer_write(handle, ptr, SwiftUInt(bytesToWrite)) if (writtenSize <= 0) { // error e.g. broken pipe. break } - ptr = ptr.advanced(by: writtenSize) - bytesToWrite -= writtenSize + ptr = ptr.advanced(by: Int(writtenSize)) + bytesToWrite -= Int(writtenSize) } return buffer.count - bytesToWrite } @@ -193,13 +193,13 @@ final class PluginHostConnection: MessageConnection { var ptr = buffer.baseAddress! while bytesToRead > 0 { - let readSize = PluginServer_read(handle, ptr, Int(bytesToRead)) + let readSize = PluginServer_read(handle, ptr, SwiftUInt(bytesToRead)) if (readSize <= 0) { // 0: EOF (the host closed), -1: Broken pipe (the host crashed?) break; } - ptr = ptr.advanced(by: readSize) - bytesToRead -= readSize + ptr = ptr.advanced(by: Int(readSize)) + bytesToRead -= Int(readSize) } return buffer.count - bytesToRead }