Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ set(swift_stdlib_unittest_framework_depends)
set(swift_stdlib_unittest_compile_flags
"-Xfrontend" "-disable-objc-attr-requires-foundation-module")

if (SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER")
endif()

if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
list(APPEND swift_stdlib_unittest_platform_sources
GetOSVersion.mm)
Expand Down
19 changes: 19 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,15 @@ public func runAllTests() {
}
}

#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER

@_silgen_name("swift_leaks_startTrackingObjects")
func startTrackingObjects(_: UnsafePointer<CChar>)
@_silgen_name("swift_leaks_stopTrackingObjects")
func stopTrackingObjects(_: UnsafePointer<CChar>) -> Int

#endif

public final class TestSuite {
public init(_ name: String) {
self.name = name
Expand Down Expand Up @@ -1225,6 +1234,11 @@ public final class TestSuite {
f()
}
let test = _testByName(testName)

#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
startTrackingObjects(name)
#endif

switch test.code {
case .single(let code):
precondition(
Expand All @@ -1234,6 +1248,11 @@ public final class TestSuite {
case .parameterized(code: let code, _):
code(parameter!)
}

#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
Copy link
Contributor

@gribozavr gribozavr Jun 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is SWIFT_RUNTIME_ENABLE_LEAK_CHECKER actually passed to the Swift code? I can't find CMake code that does this.

Copy link
Contributor

@gribozavr gribozavr Jun 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This build configuration has to be passed on the swiftc command line (-DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER) when building the library. I don't think we are doing this currently for swiftc. I think we are only doing it for clang.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I don't think we want the leak checker enabled for the entire test suite just yet is the thing. It will significantly slow down the test suite.

Copy link
Contributor

@gribozavr gribozavr Jun 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only slows down the leaks-checking-enabled builds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument is there in the build script already according to @gottesmm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an end-to-end test works, then we're good :)

Copy link
Contributor

@gottesmm gottesmm Jun 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodaFi

@gribozavr is correct. You need this:

--- a/stdlib/private/StdlibUnittest/CMakeLists.txt
+++ b/stdlib/private/StdlibUnittest/CMakeLists.txt
@@ -5,6 +5,10 @@ set(swift_stdlib_unittest_framework_depends)
set(swift_stdlib_unittest_compile_flags
    "-Xfrontend" "-disable-objc-attr-requires-foundation-module")

+if (SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
+  list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_RUNTIME_ENABLE_LEAK_CHECKER")
+endif()
+
if(SWIFT_HOST_VARIANT MATCHES "${SWIFT_DARWIN_VARIANTS}")
list(APPEND swift_stdlib_unittest_platform_sources
                 GetOSVersion.mm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, my bad. Fixed.

_ = stopTrackingObjects(name)
#endif

if let f = _testTearDownCode {
f()
}
Expand Down
30 changes: 30 additions & 0 deletions test/1_stdlib/Leak.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-run-simple-swift | FileCheck %s
// REQUIRES: leak-checker

import StdlibUnittest

class VictimObject {
var field1, field2: String
var block: () -> () = {}

init(field1: String, field2: String) {
self.field1 = field1
self.field2 = field2
self.block = {
self.field1 = self.field1 + self.field2
self.field2 = self.field2 + self.field1
}
}
}

let LeaksTests = TestSuite("Leaks")

// CHECK: [ RUN ] Leaks.Known leak
// CHECK: {"name":"Leaks", "swift_count": 2, "objc_count": 0, "swift_objects": [{"type": "nominal", "name": "C4main12VictimObject", "kind": "Class"},{"type": "unknown", "kind": "HeapLocalVariable"}], "objc_objects": []}
// CHECK: [ OK ] Leaks.Known leak
LeaksTests.test("Known leak") {
_ = VictimObject(field1: "Leak", field2: "Checker")
}

runAllTests()

3 changes: 3 additions & 0 deletions test/lit.site.cfg.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ if "@SWIFT_ASAN_BUILD@" == "TRUE":
else:
config.available_features.add('no_asan')

if "@SWIFT_RUNTIME_ENABLE_LEAK_CHECKER@" == "TRUE":
config.available_features.add('leak-checker')

if '@SWIFT_TOOLS_ENABLE_LTO@'.lower() in ['full', 'thin']:
config.available_features.add('lto')
else:
Expand Down