Skip to content

Commit 22f01a9

Browse files
committed
[SwiftCompilerSources] add BridgedOStream and debugLog().
So we can trace optimizer passes using the same output stream as the C++ passes and don't get garbled output.
1 parent 658de4a commit 22f01a9

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public func precondition(_ condition: Bool, _ message: @autoclosure () -> String
5858
// Debugging Utilities
5959
//===----------------------------------------------------------------------===//
6060

61+
public func debugLog(prefix: Bool = true, _ message: @autoclosure () -> String) {
62+
let formatted = (prefix ? "### " : "") + message()
63+
formatted._withBridgedStringRef { ref in
64+
Bridged_dbgs().write(ref)
65+
}
66+
Bridged_dbgs().newLine()
67+
}
68+
6169
/// Let's lldb's `po` command not print any "internal" properties of the conforming type.
6270
///
6371
/// This is useful if the `description` already contains all the information of a type instance.

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ private let verbose = false
1616

1717
private func log(prefix: Bool = true, _ message: @autoclosure () -> String) {
1818
if verbose {
19-
print((prefix ? "### " : "") + message())
19+
debugLog(prefix: prefix, message())
2020
}
2121
}
2222

include/swift/Basic/BasicBridging.h

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,12 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature {
185185
#include "swift/Basic/Features.def"
186186
};
187187

188-
//===----------------------------------------------------------------------===//
189-
// MARK: OStream
190-
//===----------------------------------------------------------------------===//
191-
192-
BRIDGING_WRAPPER_NONNULL(llvm::raw_ostream, OStream)
193-
194188
//===----------------------------------------------------------------------===//
195189
// MARK: StringRef
196190
//===----------------------------------------------------------------------===//
197191

192+
class BridgedOStream;
193+
198194
class BridgedStringRef {
199195
const char *_Nullable Data;
200196
size_t Length;
@@ -250,6 +246,39 @@ BRIDGED_INLINE SwiftInt BridgedOwnedString_count(BridgedOwnedString str);
250246
SWIFT_NAME("getter:BridgedOwnedString.isEmpty(self:)")
251247
BRIDGED_INLINE bool BridgedOwnedString_empty(BridgedOwnedString str);
252248

249+
//===----------------------------------------------------------------------===//
250+
// MARK: OStream
251+
//===----------------------------------------------------------------------===//
252+
253+
class BridgedOStream {
254+
llvm::raw_ostream * _Nonnull os;
255+
256+
public:
257+
SWIFT_UNAVAILABLE("Use init(raw:) instead")
258+
BridgedOStream(llvm::raw_ostream * _Nonnull os) : os(os) {}
259+
260+
SWIFT_UNAVAILABLE("Use '.raw' instead")
261+
llvm::raw_ostream * _Nonnull unbridged() const { return os; }
262+
263+
void write(BridgedStringRef string) const;
264+
265+
void newLine() const;
266+
267+
void flush() const;
268+
};
269+
270+
SWIFT_NAME("getter:BridgedOStream.raw(self:)")
271+
inline void * _Nonnull BridgedOStream_getRaw(BridgedOStream bridged) {
272+
return bridged.unbridged();
273+
}
274+
275+
SWIFT_NAME("BridgedOStream.init(raw:)")
276+
inline BridgedOStream BridgedOStream_fromRaw(void * _Nonnull os) {
277+
return static_cast<llvm::raw_ostream *>(os);
278+
}
279+
280+
BridgedOStream Bridged_dbgs();
281+
253282
//===----------------------------------------------------------------------===//
254283
// MARK: SourceLoc
255284
//===----------------------------------------------------------------------===//

lib/Basic/BasicBridging.cpp

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

1313
#include "swift/Basic/Assertions.h"
1414
#include "swift/Basic/BasicBridging.h"
15+
#include "llvm/Support/Debug.h"
1516
#include "llvm/Support/JSON.h"
1617
#include "llvm/Support/raw_ostream.h"
1718

@@ -28,6 +29,26 @@ void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
2829
ASSERT_failure(msg, file, line, function);
2930
}
3031

32+
//===----------------------------------------------------------------------===//
33+
// MARK: BridgedOStream
34+
//===----------------------------------------------------------------------===//
35+
36+
void BridgedOStream::write(BridgedStringRef string) const {
37+
*os << string.unbridged();
38+
}
39+
40+
void BridgedOStream::newLine() const {
41+
os->write('\n');
42+
}
43+
44+
void BridgedOStream::flush() const {
45+
os->flush();
46+
}
47+
48+
BridgedOStream Bridged_dbgs() {
49+
return BridgedOStream(&llvm::dbgs());
50+
}
51+
3152
//===----------------------------------------------------------------------===//
3253
// MARK: BridgedStringRef
3354
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)