Skip to content

Commit b960275

Browse files
committed
Untangle the dependency on swiftStdlibStubs from swiftRuntime that I introduced
rdar://35525730
1 parent 89e972f commit b960275

File tree

3 files changed

+50
-36
lines changed

3 files changed

+50
-36
lines changed

include/swift/Runtime/Debug.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define _SWIFT_RUNTIME_DEBUG_HELPERS_
1919

2020
#include <llvm/Support/Compiler.h>
21+
#include <cstdarg>
22+
#include <cstdio>
2123
#include <stdint.h>
2224
#include "swift/Runtime/Config.h"
2325
#include "swift/Runtime/Unreachable.h"
@@ -222,6 +224,39 @@ void _swift_reportToDebugger(uintptr_t flags, const char *message,
222224
SWIFT_RUNTIME_STDLIB_SPI
223225
bool _swift_reportFatalErrorsToDebugger;
224226

227+
LLVM_ATTRIBUTE_ALWAYS_INLINE
228+
inline static int swift_asprintf(char **strp, const char *fmt, ...) {
229+
va_list args;
230+
va_start(args, fmt);
231+
#if defined(_WIN32)
232+
#pragma GCC diagnostic push
233+
#pragma GCC diagnostic ignored "-Wuninitialized"
234+
int len = _vscprintf(fmt, args);
235+
#pragma GCC diagnostic pop
236+
if (len < 0) {
237+
va_end(args);
238+
return -1;
239+
}
240+
char *buffer = static_cast<char *>(malloc(len + 1));
241+
if (!buffer) {
242+
va_end(args);
243+
return -1;
244+
}
245+
int result = vsprintf(buffer, fmt, args);
246+
if (result < 0) {
247+
va_end(args);
248+
free(buffer);
249+
return -1;
250+
}
251+
*strp = buffer;
252+
#else
253+
int result = vasprintf(strp, fmt, args);
254+
#endif
255+
va_end(args);
256+
return result;
257+
}
258+
259+
225260
// namespace swift
226261
}
227262

stdlib/public/runtime/SwiftObject.mm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,12 +1391,22 @@ static bool usesNativeSwiftReferenceCounting_nonNull(
13911391
int32_t line) {
13921392
bool isEscaping =
13931393
object != nullptr && !object->refCounts.isUniquelyReferenced();
1394+
1395+
// Print a message if the closure escaped.
13941396
if (isEscaping) {
1395-
auto *fatalErr = reinterpret_cast<const unsigned char *>("Fatal error");
1396-
auto *message = reinterpret_cast<const unsigned char *>(
1397-
"closure argument was escaped in withoutActuallyEscaping block");
1398-
_swift_stdlib_reportFatalErrorInFile(fatalErr, 11, message, 62, filename,
1399-
filenameLength, line, 0 /* flags */);
1397+
auto *message = "Fatal error: closure argument was escaped in "
1398+
"withoutActuallyEscaping block";
1399+
auto messageLength = strlen(message);
1400+
1401+
if (_swift_reportFatalErrorsToDebugger)
1402+
_swift_reportToDebugger(RuntimeErrorFlagFatal, message);
1403+
1404+
char *log;
1405+
swift_asprintf(&log, "%.*s: file %.*s, line %" PRIu32 "\n", messageLength,
1406+
message, filenameLength, filename, line);
1407+
1408+
swift_reportError(RuntimeErrorFlagFatal, log);
1409+
free(log);
14001410
}
14011411
return isEscaping;
14021412
}

stdlib/public/stubs/Assert.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,6 @@ using namespace swift;
2323

2424
bool swift::_swift_reportFatalErrorsToDebugger = true;
2525

26-
static int swift_asprintf(char **strp, const char *fmt, ...) {
27-
va_list args;
28-
va_start(args, fmt);
29-
#if defined(_WIN32)
30-
#pragma GCC diagnostic push
31-
#pragma GCC diagnostic ignored "-Wuninitialized"
32-
int len = _vscprintf(fmt, args);
33-
#pragma GCC diagnostic pop
34-
if (len < 0) {
35-
va_end(args);
36-
return -1;
37-
}
38-
char *buffer = static_cast<char *>(malloc(len + 1));
39-
if (!buffer) {
40-
va_end(args);
41-
return -1;
42-
}
43-
int result = vsprintf(buffer, fmt, args);
44-
if (result < 0) {
45-
va_end(args);
46-
free(buffer);
47-
return -1;
48-
}
49-
*strp = buffer;
50-
#else
51-
int result = vasprintf(strp, fmt, args);
52-
#endif
53-
va_end(args);
54-
return result;
55-
}
56-
5726
static void logPrefixAndMessageToDebugger(
5827
const unsigned char *prefix, int prefixLength,
5928
const unsigned char *message, int messageLength

0 commit comments

Comments
 (0)