Skip to content

Commit 5d98611

Browse files
committed
Complete unit tests and fix all failures
1 parent 7cafffa commit 5d98611

File tree

7 files changed

+108
-23
lines changed

7 files changed

+108
-23
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public struct Driver {
124124
/// The path to the imported Objective-C header.
125125
public let importedObjCHeader: VirtualPath?
126126

127+
/// The path to the pch for the imported Objective-C header.
128+
public var bridgingPrecompiledHeader: VirtualPath?
129+
127130
/// Path to the dependencies file.
128131
public let dependenciesFilePath: VirtualPath?
129132

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ extension Driver {
103103
try addCommonFrontendOptions(commandLine: &commandLine)
104104
// FIXME: MSVC runtime flags
105105

106+
if let importedObjCHeader = importedObjCHeader {
107+
commandLine.appendFlag(.importObjcHeader)
108+
if !parsedOptions.contains(.pchOutputDir), let pch = bridgingPrecompiledHeader {
109+
commandLine.appendPath(pch)
110+
} else {
111+
commandLine.appendPath(importedObjCHeader)
112+
if compilerMode != .singleCompile {
113+
commandLine.appendFlag(.pchDisableValidation)
114+
}
115+
}
116+
}
117+
106118
if parsedOptions.hasArgument(.parseAsLibrary, .emitLibrary) {
107119
commandLine.appendFlag(.parseAsLibrary)
108120
}

Sources/SwiftDriver/Jobs/EmitModuleJob.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ extension Driver {
6262

6363
try addCommonFrontendOptions(commandLine: &commandLine)
6464
// FIXME: Add MSVC runtime library flags
65+
66+
if let importedObjCHeader = importedObjCHeader {
67+
commandLine.appendFlag(.importObjcHeader)
68+
commandLine.appendPath(importedObjCHeader)
69+
}
6570

6671
try addCommonModuleOptions(commandLine: &commandLine, outputs: &outputs)
6772

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ extension Driver {
155155
try commandLine.appendAll(.Xllvm, from: &parsedOptions)
156156
try commandLine.appendAll(.Xcc, from: &parsedOptions)
157157

158-
if let importedObjCHeader = importedObjCHeader {
159-
commandLine.appendFlag(.importObjcHeader)
160-
commandLine.appendPath(importedObjCHeader)
161-
}
162-
163158
// Repl Jobs may include -module-name depending on the selected REPL (LLDB or integrated).
164159
if compilerMode != .repl {
165160
commandLine.appendFlags("-module-name", moduleName)

Sources/SwiftDriver/Jobs/GeneratePCHJob.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import Foundation
1314
import TSCBasic
1415
import TSCUtility
1516

@@ -25,12 +26,46 @@ extension Driver {
2526
commandLine.appendFlag(.emitPch)
2627

2728
try addCommonFrontendOptions(commandLine: &commandLine)
29+
30+
if let importedObjCHeader = importedObjCHeader {
31+
commandLine.appendFlag(.importObjcHeader)
32+
commandLine.appendPath(importedObjCHeader)
33+
}
34+
2835
try commandLine.appendLast(.indexStorePath, from: &parsedOptions)
2936

37+
if parsedOptions.hasArgument(.serializeDiagnostics), let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
38+
commandLine.appendFlag(.serializeDiagnosticsPath)
39+
let path: VirtualPath
40+
if let modulePath = parsedOptions.getLastArgument(.emitModulePath) {
41+
var outputBase = (outputDirectory as NSString).appendingPathComponent(input.file.basenameWithoutExt)
42+
outputBase.append("-")
43+
// TODO: does this hash need to be persistent?
44+
let code = UInt(bitPattern: modulePath.asSingle.hashValue)
45+
outputBase.append(String(code, radix: 36))
46+
path = try VirtualPath(path: outputBase.appendingFileTypeExtension(.diagnostics))
47+
} else {
48+
// FIXME: should have '-.*' at the end of the filename, similar to llvm::sys::fs::createTemporaryFile
49+
path = .temporary(RelativePath(input.file.basenameWithoutExt.appendingFileTypeExtension(.diagnostics)))
50+
}
51+
commandLine.appendPath(path)
52+
outputs.append(.init(file: path, type: .diagnostics))
53+
}
54+
55+
// FIXME: should have '-.*' at the end of the filename, similar to llvm::sys::fs::createTemporaryFile
56+
let pchFileName = input.file.basenameWithoutExt.appendingFileTypeExtension(.pch)
57+
let output: VirtualPath
3058
if let outputDirectory = parsedOptions.getLastArgument(.pchOutputDir)?.asSingle {
31-
outputs.append(.init(file: try VirtualPath(path: outputDirectory), type: .pch))
59+
let outputPath = (outputDirectory as NSString).appendingPathComponent(pchFileName)
60+
output = try VirtualPath(path: outputPath)
3261
try commandLine.appendLast(.pchOutputDir, from: &parsedOptions)
62+
} else {
63+
output = .temporary(RelativePath(pchFileName))
64+
commandLine.appendFlag(.o)
65+
commandLine.appendPath(output)
3366
}
67+
outputs.append(.init(file: output, type: .pch))
68+
bridgingPrecompiledHeader = output
3469

3570
return Job(
3671
kind: .generatePCH,

Sources/SwiftDriver/Jobs/MergeModuleJob.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ extension Driver {
4343

4444
try addCommonFrontendOptions(commandLine: &commandLine)
4545
// FIXME: Add MSVC runtime library flags
46+
47+
if let importedObjCHeader = importedObjCHeader {
48+
commandLine.appendFlag(.importObjcHeader)
49+
commandLine.appendPath(importedObjCHeader)
50+
}
4651

4752
try addCommonModuleOptions(commandLine: &commandLine, outputs: &outputs)
4853

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
import XCTest
12+
import Foundation
1313
import SwiftDriver
1414
import TSCBasic
15+
import XCTest
1516

1617
final class SwiftDriverTests: XCTestCase {
1718
func testParsing() throws {
@@ -1210,19 +1211,19 @@ final class SwiftDriverTests: XCTestCase {
12101211
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
12111212
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
12121213
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1214+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1215+
XCTAssertEqual(plannedJobs[0].outputs[0].file, .temporary(RelativePath("TestInputHeader.pch")))
1216+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
12131217
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
1214-
// TODO check outputs for pch
1218+
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-o")))
1219+
XCTAssert(plannedJobs[0].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.pch")))))
12151220

12161221
XCTAssertEqual(plannedJobs[1].kind, .compile)
12171222
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
12181223
XCTAssertEqual(plannedJobs[1].inputs[0].file, try VirtualPath(path: "foo.swift"))
12191224
XCTAssert(plannedJobs[1].commandLine.contains(.flag("-import-objc-header")))
1220-
// TODO check value of flag
1225+
XCTAssert(plannedJobs[1].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.pch")))))
12211226
}
1222-
// ^^^^^^^^^^^^^
1223-
// RUN: %target-build-swift -typecheck -driver-print-jobs -import-objc-header %S/Inputs/bridging-header.h %s 2>&1 | %FileCheck %s -check-prefix=YESPCHJOB
1224-
// YESPCHJOB: {{.*}}swift{{c?(\.exe)?"?}} -frontend {{.*}} -emit-pch -o {{.*}}bridging-header-{{.*}}.pch
1225-
// YESPCHJOB: {{.*}}swift{{c?(\.exe)?"?}} -frontend {{.*}} -import-objc-header {{.*}}bridging-header-{{.*}}.pch
12261227

12271228
do {
12281229
var driver = try Driver(args: ["swiftc", "-typecheck", "-disable-bridging-pch", "-import-objc-header", "TestInputHeader.h", "foo.swift"])
@@ -1244,10 +1245,14 @@ final class SwiftDriverTests: XCTestCase {
12441245
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
12451246
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
12461247
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1248+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1249+
XCTAssertEqual(plannedJobs[0].outputs[0].file, .temporary(RelativePath("TestInputHeader.pch")))
1250+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
12471251
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
12481252
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-index-store-path")))
12491253
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "idx"))))
1250-
// TODO check outputs for pch
1254+
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-o")))
1255+
XCTAssert(plannedJobs[0].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.pch")))))
12511256

12521257
XCTAssertEqual(plannedJobs[1].kind, .compile)
12531258
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
@@ -1263,10 +1268,12 @@ final class SwiftDriverTests: XCTestCase {
12631268
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
12641269
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
12651270
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1271+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1272+
XCTAssertEqual(plannedJobs[0].outputs[0].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1273+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
12661274
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
12671275
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-pch-output-dir")))
12681276
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/pch"))))
1269-
// TODO check outputs for pch
12701277

12711278
XCTAssertEqual(plannedJobs[1].kind, .compile)
12721279
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
@@ -1283,10 +1290,12 @@ final class SwiftDriverTests: XCTestCase {
12831290
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
12841291
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
12851292
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1293+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1294+
XCTAssertEqual(plannedJobs[0].outputs[0].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1295+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
12861296
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
12871297
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-pch-output-dir")))
12881298
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/pch"))))
1289-
// TODO check outputs for pch
12901299

12911300
XCTAssertEqual(plannedJobs[1].kind, .compile)
12921301
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
@@ -1327,11 +1336,16 @@ final class SwiftDriverTests: XCTestCase {
13271336
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
13281337
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13291338
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1330-
// TODO -serialize-diagnostics-path {{.*}}bridging-header-{{.*}}.dia{{"?}}
1339+
XCTAssertEqual(plannedJobs[0].outputs.count, 2)
1340+
XCTAssertEqual(plannedJobs[0].outputs[0].file, .temporary(RelativePath("TestInputHeader.dia")))
1341+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .diagnostics)
1342+
XCTAssertEqual(plannedJobs[0].outputs[1].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1343+
XCTAssertEqual(plannedJobs[0].outputs[1].type, .pch)
1344+
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-serialize-diagnostics-path")))
1345+
XCTAssert(plannedJobs[0].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.dia")))))
13311346
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
13321347
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-pch-output-dir")))
13331348
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/pch"))))
1334-
// TODO check outputs for pch
13351349

13361350
XCTAssertEqual(plannedJobs[1].kind, .compile)
13371351
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
@@ -1342,22 +1356,32 @@ final class SwiftDriverTests: XCTestCase {
13421356
do {
13431357
var driver = try Driver(args: ["swiftc", "-typecheck", "-import-objc-header", "TestInputHeader.h", "-pch-output-dir", "/pch", "-serialize-diagnostics", "foo.swift", "-emit-module", "-emit-module-path", "/module-path-dir"])
13441358
let plannedJobs = try driver.planBuild()
1345-
XCTAssertEqual(plannedJobs.count, 2)
1359+
XCTAssertEqual(plannedJobs.count, 3)
13461360

13471361
XCTAssertEqual(plannedJobs[0].kind, .generatePCH)
13481362
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
13491363
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13501364
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1351-
// TODO -serialize-diagnostics-path {{.*}}/pch/bridging-header-{{.*}}.dia{{"?}}
1365+
XCTAssertEqual(plannedJobs[0].outputs.count, 2)
1366+
XCTAssertNotNil(plannedJobs[0].outputs[0].file.name.range(of: #"/pch/TestInputHeader-.*.dia"#, options: .regularExpression))
1367+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .diagnostics)
1368+
XCTAssertEqual(plannedJobs[0].outputs[1].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1369+
XCTAssertEqual(plannedJobs[0].outputs[1].type, .pch)
1370+
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-serialize-diagnostics-path")))
1371+
XCTAssert(plannedJobs[0].commandLine.contains {
1372+
guard case .path(let path) = $0 else { return false }
1373+
return path.name.range(of: #"/pch/TestInputHeader-.*.dia"#, options: .regularExpression) != nil
1374+
})
13521375
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
13531376
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-pch-output-dir")))
13541377
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/pch"))))
1355-
// TODO check outputs for pch
13561378

13571379
XCTAssertEqual(plannedJobs[1].kind, .compile)
13581380
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
13591381
XCTAssertEqual(plannedJobs[1].inputs[0].file, try VirtualPath(path: "foo.swift"))
13601382
XCTAssert(plannedJobs[1].commandLine.contains(.flag("-pch-disable-validation")))
1383+
1384+
// FIXME: validate that merge module is correct job and that it has correct inputs and flags
13611385
}
13621386

13631387
do {
@@ -1369,10 +1393,12 @@ final class SwiftDriverTests: XCTestCase {
13691393
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
13701394
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13711395
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1396+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1397+
XCTAssertEqual(plannedJobs[0].outputs[0].file, try VirtualPath(path: "/pch/TestInputHeader.pch"))
1398+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
13721399
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
13731400
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-pch-output-dir")))
13741401
XCTAssert(plannedJobs[0].commandLine.contains(.path(try VirtualPath(path: "/pch"))))
1375-
// TODO check outputs for pch
13761402

13771403
XCTAssertEqual(plannedJobs[1].kind, .compile)
13781404
XCTAssertEqual(plannedJobs[1].inputs.count, 1)
@@ -1389,9 +1415,13 @@ final class SwiftDriverTests: XCTestCase {
13891415
XCTAssertEqual(plannedJobs[0].inputs.count, 1)
13901416
XCTAssertEqual(plannedJobs[0].inputs[0].file, .relative(RelativePath("TestInputHeader.h")))
13911417
XCTAssertEqual(plannedJobs[0].inputs[0].type, .objcHeader)
1418+
XCTAssertEqual(plannedJobs[0].outputs.count, 1)
1419+
XCTAssertEqual(plannedJobs[0].outputs[0].file, .temporary(RelativePath("TestInputHeader.pch")))
1420+
XCTAssertEqual(plannedJobs[0].outputs[0].type, .pch)
13921421
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-O")))
13931422
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-emit-pch")))
1394-
// TODO check outputs for pch
1423+
XCTAssert(plannedJobs[0].commandLine.contains(.flag("-o")))
1424+
XCTAssert(plannedJobs[0].commandLine.contains(.path(.temporary(RelativePath("TestInputHeader.pch")))))
13951425

13961426
XCTAssertEqual(plannedJobs[1].kind, .compile)
13971427
XCTAssertEqual(plannedJobs[1].inputs.count, 1)

0 commit comments

Comments
 (0)