Skip to content

Commit d846e0c

Browse files
committed
Merge branch 'main' of github.com:apple/swift into maxd/main-merge
# Conflicts: # include/swift/Runtime/ThreadLocalStorage.h
2 parents dc24d16 + ac56bfb commit d846e0c

File tree

24 files changed

+931
-29
lines changed

24 files changed

+931
-29
lines changed

benchmark/single-source/ChaCha.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,6 @@ func checkResult(_ plaintext: [UInt8]) {
363363
}
364364

365365
@inline(never)
366-
@_assemblyVision
367366
public func run_ChaCha(_ n: Int) {
368367
let key = Array(repeating: UInt8(1), count: 32)
369368
let nonce = Array(repeating: UInt8(2), count: 12)

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ ERROR(lex_unprintable_ascii_character,none,
107107
"unprintable ASCII character found in source file", ())
108108
ERROR(lex_invalid_utf8,none,
109109
"invalid UTF-8 found in source file", ())
110+
111+
NOTE(lex_experimental_regex_strawperson,none,
112+
"'%0'", (StringRef))
113+
110114
ERROR(lex_single_quote_string,none,
111115
"single-quoted string literal found, use '\"'", ())
112116
ERROR(lex_invalid_curly_quote,none,

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ namespace swift {
144144
/// PackageDescription version to compile for.
145145
version::Version PackageDescriptionVersion;
146146

147+
/// Enable experimental string processing
148+
bool EnableExperimentalRegex = false;
149+
147150
/// Disable API availability checking.
148151
bool DisableAvailabilityChecking = false;
149152

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ def disable_deserialization_recovery :
469469
Flag<["-"], "disable-deserialization-recovery">,
470470
HelpText<"Don't attempt to recover from missing xrefs (etc) in swiftmodules">;
471471

472+
def enable_experimental_regex :
473+
Flag<["-"], "enable-experimental-regex">,
474+
HelpText<"Enable experimental string processing">;
475+
472476
def disable_availability_checking : Flag<["-"],
473477
"disable-availability-checking">,
474478
HelpText<"Disable checking for potentially unavailable APIs">;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef EXPERIMENTAL_REGEX_BRIDGING
2+
#define EXPERIMENTAL_REGEX_BRIDGING
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef const char *(* ParseRegexStrawperson)(const char *);
9+
10+
void Parser_registerParseRegexStrawperson(ParseRegexStrawperson fn);
11+
12+
#ifdef __cplusplus
13+
} // extern "C"
14+
#endif
15+
16+
#endif // EXPERIMENTAL_REGEX_BRIDGING
17+
18+
19+
//const char* experimental_regex_strawperson(const char *in);
20+

include/swift/Parse/Lexer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,15 @@ class Lexer {
607607
bool lexUnknown(bool EmitDiagnosticsIfToken);
608608

609609
NulCharacterKind getNulCharacterKind(const char *Ptr) const;
610+
611+
/// Emit diagnostics for single-quote string and suggest replacement
612+
/// with double-quoted equivalent.
613+
///
614+
/// Or, if we're in strawperson mode, we will emit a custom
615+
/// error message instead, determined by the Swift library.
616+
void diagnoseSingleQuoteStringLiteral(const char *TokStart,
617+
const char *TokEnd);
618+
610619
};
611620

612621
/// A lexer that can lex trivia into its pieces

include/swift/Runtime/ThreadLocalStorage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ static_assert(std::is_same<__swift_thread_key_t, DWORD>::value,
121121
# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create
122122
# define SWIFT_THREAD_GETSPECIFIC _stdlib_thread_getspecific
123123
# define SWIFT_THREAD_SETSPECIFIC _stdlib_thread_setspecific
124-
# else
124+
125+
# elif !defined(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
125126
// Otherwise use the pthread API.
126127
# include <pthread.h>
127128
# define SWIFT_THREAD_KEY_CREATE pthread_key_create

include/swift/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ module OptimizerBridging {
88
export *
99
}
1010

11+
module ExperimentalRegexBridging {
12+
header "Parse/ExperimentalRegexBridging.h"
13+
export *
14+
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
471471
= A->getOption().matches(OPT_enable_deserialization_recovery);
472472
}
473473

474+
// Experimental string processing
475+
Opts.EnableExperimentalRegex |=
476+
Args.hasArg(OPT_enable_experimental_regex);
477+
474478
Opts.DisableAvailabilityChecking |=
475479
Args.hasArg(OPT_disable_availability_checking);
476480
Opts.CheckAPIAvailabilityOnly |=

lib/Parse/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ _swift_gyb_target_sources(swiftParse PRIVATE
3131
target_link_libraries(swiftParse PRIVATE
3232
swiftAST
3333
swiftSyntax
34-
swiftSyntaxParse)
34+
swiftSyntaxParse
35+
)
3536

3637
add_dependencies(swiftParse swift-parse-syntax-generated-headers)
3738

lib/Parse/Lexer.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
// FIXME: Figure out if this can be migrated to LLVM.
3131
#include "clang/Basic/CharInfo.h"
3232

33+
// Regex parser delivered via libSwift
34+
#include "swift/Parse/ExperimentalRegexBridging.h"
35+
static ParseRegexStrawperson parseRegexStrawperson = nullptr;
36+
void Parser_registerParseRegexStrawperson(ParseRegexStrawperson fn) {
37+
parseRegexStrawperson = fn;
38+
}
39+
3340
#include <limits>
3441

3542
using namespace swift;
@@ -1788,13 +1795,27 @@ static void validateMultilineIndents(const Token &Str,
17881795

17891796
/// Emit diagnostics for single-quote string and suggest replacement
17901797
/// with double-quoted equivalent.
1791-
static void diagnoseSingleQuoteStringLiteral(const char *TokStart,
1792-
const char *TokEnd,
1793-
DiagnosticEngine *D) {
1798+
///
1799+
/// Or, if we're in experimental regex mode, we will emit a custom
1800+
/// error message instead, determined by the Swift library.
1801+
void Lexer::diagnoseSingleQuoteStringLiteral(const char *TokStart,
1802+
const char *TokEnd) {
17941803
assert(*TokStart == '\'' && TokEnd[-1] == '\'');
1795-
if (!D)
1804+
if (!Diags) // or assert?
17961805
return;
17971806

1807+
auto startLoc = Lexer::getSourceLoc(TokStart);
1808+
auto endLoc = Lexer::getSourceLoc(TokEnd);
1809+
1810+
if (LangOpts.EnableExperimentalRegex) {
1811+
if (parseRegexStrawperson) {
1812+
auto copy = std::string(TokStart, TokEnd-TokStart);
1813+
auto msg = parseRegexStrawperson(copy.c_str());
1814+
assert(msg != nullptr);
1815+
Diags->diagnose(startLoc, diag::lex_experimental_regex_strawperson, msg);
1816+
}
1817+
}
1818+
17981819
SmallString<32> replacement;
17991820
replacement.push_back('"');
18001821
const char *Ptr = TokStart + 1;
@@ -1826,9 +1847,8 @@ static void diagnoseSingleQuoteStringLiteral(const char *TokStart,
18261847
replacement.append(OutputPtr, Ptr - 1);
18271848
replacement.push_back('"');
18281849

1829-
D->diagnose(Lexer::getSourceLoc(TokStart), diag::lex_single_quote_string)
1830-
.fixItReplaceChars(Lexer::getSourceLoc(TokStart),
1831-
Lexer::getSourceLoc(TokEnd), replacement);
1850+
Diags->diagnose(startLoc, diag::lex_single_quote_string)
1851+
.fixItReplaceChars(startLoc, endLoc, replacement);
18321852
}
18331853

18341854
/// lexStringLiteral:
@@ -1895,7 +1915,7 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) {
18951915
if (QuoteChar == '\'') {
18961916
assert(!IsMultilineString && CustomDelimiterLen == 0 &&
18971917
"Single quoted string cannot have custom delimitor, nor multiline");
1898-
diagnoseSingleQuoteStringLiteral(TokStart, CurPtr, Diags);
1918+
diagnoseSingleQuoteStringLiteral(TokStart, CurPtr);
18991919
}
19001920

19011921
if (wasErroneous)

libswift/Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

9+
add_subdirectory(ExperimentalRegex)
910
add_subdirectory(SIL)
1011
add_subdirectory(Optimizer)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_libswift_module(ExperimentalRegex
10+
Regex.swift
11+
)
12+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ExperimentalRegexBridging
2+
3+
public func experimental_regex_strawperson(
4+
_ ptr: UnsafePointer<CChar>?
5+
) -> UnsafePointer<CChar>? {
6+
guard let s = ptr else { return nil }
7+
8+
let str = "Hello, \(String(cString:s))"
9+
let count = str.utf8.count + 1
10+
return str.withCString {
11+
assert($0[count-1] == 0)
12+
let ptr = UnsafeMutablePointer<CChar>.allocate(capacity: count)
13+
ptr.initialize(from: $0, count: count)
14+
return UnsafePointer(ptr)
15+
}
16+
}
17+
18+
public func registerParser() {
19+
Parser_registerParseRegexStrawperson({ experimental_regex_strawperson($0) })
20+
}

libswift/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_libswift_module(Optimizer
10-
DEPENDS SIL)
10+
DEPENDS SIL ExperimentalRegex)
1111

1212
add_subdirectory(Analysis)
1313
add_subdirectory(InstructionPasses)

libswift/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
import SIL
1414
import OptimizerBridging
15+
import ExperimentalRegex
1516

1617
@_cdecl("initializeLibSwift")
1718
public func initializeLibSwift() {
1819
registerSILClasses()
1920
registerSwiftPasses()
21+
registerParser()
2022
}
2123

2224
private func registerPass(

stdlib/public/LLVMSupport/ErrorHandling.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <stdarg.h>
2828

29-
#if defined(__APPLE__)
29+
#if SWIFT_STDLIB_HAS_ASL
3030
#include <asl.h>
3131
#elif defined(__ANDROID__)
3232
#include <android/log.h>

stdlib/public/Reflection/MetadataSource.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#include "swift/Reflection/MetadataSource.h"
1414

15-
#include <sstream>
16-
1715
using namespace swift;
1816
using namespace reflection;
1917

stdlib/public/runtime/Errors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#include <execinfo.h>
4848
#endif
4949

50-
#if defined(__APPLE__)
50+
#if SWIFT_STDLIB_HAS_ASL
5151
#include <asl.h>
5252
#elif defined(__ANDROID__)
5353
#include <android/log.h>

stdlib/public/runtime/Metadata.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@
4242
// Avoid defining macro max(), min() which conflict with std::max(), std::min()
4343
#define NOMINMAX
4444
#include <windows.h>
45-
#else
46-
#include <sys/mman.h>
47-
#include <unistd.h>
48-
// WASI doesn't support dynamic linking yet.
49-
#if !defined(__wasi__)
50-
#include <dlfcn.h>
51-
#endif // !defined(__wasi__)
5245
#endif
5346
#if SWIFT_PTRAUTH
5447
#include <ptrauth.h>
@@ -65,10 +58,6 @@ extern "C" void _objc_setClassCopyFixupHandler(void (* _Nonnull newFixupHandler)
6558
#include "swift/Runtime/Debug.h"
6659
#include "Private.h"
6760

68-
#if defined(__APPLE__)
69-
#include <mach/vm_page_size.h>
70-
#endif
71-
7261
#if SWIFT_OBJC_INTEROP
7362
#include "ObjCRuntimeGetImageNameFromClass.h"
7463
#endif

stdlib/public/stubs/Stubs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
#include <cstdio>
4343
#include <cstdlib>
4444
#include <cstring>
45+
#if defined(__CYGWIN__) || defined(__HAIKU__)
4546
#include <sstream>
47+
#endif
4648
#if defined(__OpenBSD__) || defined(__ANDROID__) || defined(__linux__) || defined(__wasi__) || defined(_WIN32)
4749
#include <locale.h>
4850
#if defined(_WIN32)
@@ -288,6 +290,7 @@ static bool swift_stringIsSignalingNaN(const char *nptr) {
288290
return false;
289291
}
290292

293+
#if defined(__CYGWIN__) || defined(__HAIKU__)
291294
// This implementation should only be used on platforms without the
292295
// relevant strto* functions, such as Cygwin or Haiku.
293296
// Note that using this currently causes test failures.
@@ -308,6 +311,7 @@ T _swift_strto(const char *nptr, char **endptr) {
308311

309312
return ParsedValue;
310313
}
314+
#endif
311315

312316
#if defined(__OpenBSD__) || defined(_WIN32) || defined(__CYGWIN__) || defined(__HAIKU__)
313317
#define NEED_SWIFT_STRTOD_L
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-regex
2+
// REQUIRES: libswift
3+
4+
var s = 'abc'
5+
// expected-note@-1{{'Hello, 'abc''}}
6+
// expected-error@-2{{single-quoted string literal found, use '"'}}

0 commit comments

Comments
 (0)