Skip to content

Commit 328e07a

Browse files
committed
address comments, add copyright header
1 parent b624318 commit 328e07a

File tree

1 file changed

+45
-25
lines changed

1 file changed

+45
-25
lines changed

tools/swift-function-caller-generator/Sources/swift-function-caller-generator/swift-function-caller-generator.swift

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
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+
119
import SwiftParser
220
import SwiftSyntax
321
import SwiftSyntaxMacros
@@ -25,7 +43,7 @@ class SwiftMacroTestGen: SyntaxVisitor {
2543
exit(1)
2644
}
2745
if CommandLine.argc < 3 {
28-
printError("missing module name (passed 1 argument, expected 2)")
46+
printError("missing file name (passed 1 argument, expected 2)")
2947
exit(1)
3048
}
3149
let contents = read(file: CommandLine.arguments[2])
@@ -113,30 +131,6 @@ class TypeAliasReplacer: SyntaxRewriter {
113131
}
114132
}
115133

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-
140134
func createBody(_ f: FunctionDeclSyntax, selfParam: TokenSyntax?) -> CodeBlockSyntax {
141135
var call = createCall(f)
142136
if let selfParam {
@@ -311,6 +305,32 @@ extension AttributeListSyntax.Element {
311305
}
312306
}
313307

308+
// MARK: I/O utils
309+
// These call libc functions to avoid dealing with Foundation on non-Apple platforms
314310
func printError(_ s: String) {
315311
fputs("error: \(s)\n", stderr)
316312
}
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

Comments
 (0)