Skip to content

[Swiftify] Add vsnprintf test #82357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 test/Interop/C/swiftify-import/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ module CommentsClang {
header "comments.h"
export *
}
module VsnprintfClang {
header "vsnprintf.h"
export *
}
15 changes: 15 additions & 0 deletions test/Interop/C/swiftify-import/Inputs/vsnprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <stdarg.h>
#include <stddef.h>

#define __counted_by(x) __attribute__((__counted_by__(x)))
#define __printf(string_index, first_to_check) \
__attribute__((__format__(__printf__, string_index, first_to_check)))

#ifndef __cplusplus
#define __restrict restrict
#endif

int vsnprintf(char *__restrict __counted_by(__size) __str, size_t __size,
const char *__restrict __format, va_list foo_args) __printf(3, 0);
21 changes: 21 additions & 0 deletions test/Interop/C/swiftify-import/vsnprintf.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// REQUIRES: swift_feature_SafeInteropWrappers

// RUN: %target-swift-ide-test -print-module -module-to-print=VsnprintfClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers -Xcc -Wno-nullability-completeness | %FileCheck %s

// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Vsnprintf.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness %s

// Check that ClangImporter correctly infers and expands @_SwiftifyImport macro for vsnprintf with __counted_by and va_list parameters.

import VsnprintfClang
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest using import Darwin instead and marking this test as a darwin-only test


// CHECK: /// This is an auto-generated wrapper for safer interop
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func vsnprintf(_ __str: UnsafeMutableBufferPointer<CChar>, _ __format: UnsafePointer<CChar>!, _ foo_args: CVaListPointer) -> Int32

@inlinable
public func callVsnprintf(_ p: UnsafeMutableBufferPointer<CChar>) {
unsafe withVaList([CLongLong(42), CInt(1337)]) { args in
_ = unsafe vsnprintf(p.baseAddress!, p.count, "%lld %d", args)
}
}