|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// This utility takes a Swift interface and outputs a Swift file calling every |
| 14 | +// function in that Swift interface. This is useful for testing purposes, to |
| 15 | +// trigger imports of all declarations in clang modules. |
| 16 | + |
| 17 | +// Usage: swift-function-caller-generator <module-name> <swiftinterface-path> |
| 18 | + |
1 | 19 | import SwiftParser |
2 | 20 | import SwiftSyntax |
3 | 21 | import SwiftSyntaxMacros |
@@ -25,7 +43,7 @@ class SwiftMacroTestGen: SyntaxVisitor { |
25 | 43 | exit(1) |
26 | 44 | } |
27 | 45 | if CommandLine.argc < 3 { |
28 | | - printError("missing module name (passed 1 argument, expected 2)") |
| 46 | + printError("missing file name (passed 1 argument, expected 2)") |
29 | 47 | exit(1) |
30 | 48 | } |
31 | 49 | let contents = read(file: CommandLine.arguments[2]) |
@@ -113,30 +131,6 @@ class TypeAliasReplacer: SyntaxRewriter { |
113 | 131 | } |
114 | 132 | } |
115 | 133 |
|
116 | | -func read(file path: String) -> String { |
117 | | - guard let f = fopen(path, "r") else { |
118 | | - printError("could not open file \(path)") |
119 | | - exit(1) |
120 | | - } |
121 | | - if fseek(f, 0, SEEK_END) != 0 { |
122 | | - printError("could not read file \(path)") |
123 | | - exit(1) |
124 | | - } |
125 | | - let len = Int(ftell(f)) |
126 | | - if len < 0 { |
127 | | - printError("could not read size of file \(path)") |
128 | | - exit(1) |
129 | | - } |
130 | | - rewind(f) |
131 | | - let contents = String( |
132 | | - unsafeUninitializedCapacity: len, |
133 | | - initializingUTF8With: { stringBuffer in |
134 | | - fread(UnsafeMutableRawPointer(stringBuffer.baseAddress!), 1, len, f) |
135 | | - }) |
136 | | - fclose(f) |
137 | | - return contents |
138 | | -} |
139 | | - |
140 | 134 | func createBody(_ f: FunctionDeclSyntax, selfParam: TokenSyntax?) -> CodeBlockSyntax { |
141 | 135 | var call = createCall(f) |
142 | 136 | if let selfParam { |
@@ -311,6 +305,32 @@ extension AttributeListSyntax.Element { |
311 | 305 | } |
312 | 306 | } |
313 | 307 |
|
| 308 | +// MARK: I/O utils |
| 309 | +// These call libc functions to avoid dealing with Foundation on non-Apple platforms |
314 | 310 | func printError(_ s: String) { |
315 | 311 | fputs("error: \(s)\n", stderr) |
316 | 312 | } |
| 313 | + |
| 314 | +func read(file path: String) -> String { |
| 315 | + guard let f = fopen(path, "r") else { |
| 316 | + printError("could not open file \(path)") |
| 317 | + exit(1) |
| 318 | + } |
| 319 | + if fseek(f, 0, SEEK_END) != 0 { |
| 320 | + printError("could not read file \(path)") |
| 321 | + exit(1) |
| 322 | + } |
| 323 | + let len = Int(ftell(f)) |
| 324 | + if len < 0 { |
| 325 | + printError("could not read size of file \(path)") |
| 326 | + exit(1) |
| 327 | + } |
| 328 | + rewind(f) |
| 329 | + let contents = String( |
| 330 | + unsafeUninitializedCapacity: len, |
| 331 | + initializingUTF8With: { stringBuffer in |
| 332 | + fread(UnsafeMutableRawPointer(stringBuffer.baseAddress!), 1, len, f) |
| 333 | + }) |
| 334 | + fclose(f) |
| 335 | + return contents |
| 336 | +} |
0 commit comments