Files
swift-mirror/test/Interop/Cxx/class/access/private-fileid-diagnostics.swift
John Hui bdf22948ce [cxx-interop] Allow Swift to access non-public C++ members (#79093)
This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID,
which will specify where Swift extensions of that class will be allowed
to access its non-public members, e.g.:

    class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate
the implementation of their C++ classes to Swift, without breaking
encapsulation and indiscriminately exposing those classes' private and
protected fields.

As an implementation detail of this feature, this patch introduces an
abstraction for file ID strings, FileIDStr, which represent a parsed pair
of module name/file name.

rdar://137764620
2025-02-18 11:22:44 -08:00

60 lines
2.4 KiB
Swift

// RUN: split-file %s %t
// RUN: %target-swift-frontend -typecheck -verify %t/some/subdir/file1.swift -verify-additional-file %t/Cxx/include/cxx-header.h -I %t/Cxx/include -cxx-interoperability-mode=default -module-name main -suppress-remarks
// This test uses -verify-additional-file, which do not work well on Windows:
// UNSUPPORTED: OS=windows-msvc
//--- Cxx/include/module.modulemap
module CxxModule {
requires cplusplus
header "cxx-header.h"
}
//--- Cxx/include/cxx-header.h
#ifndef __CXX_HEADER_H
#define __CXX_HEADER_H
class __attribute__((__swift_attr__("private_fileid:main/file1.swift"))) OK {
private:
void priv(void) const {}
};
class
__attribute__((__swift_attr__("private_fileid:main/file1.swift"))) // expected-note {{SWIFT_PRIVATE_FILEID annotation found here}}
__attribute__((__swift_attr__("private_fileid:main/file2.swift"))) // expected-note {{SWIFT_PRIVATE_FILEID annotation found here}}
MultipleAnnotations {}; // expected-error {{multiple SWIFT_PRIVATE_FILEID annotations were found on 'MultipleAnnotations'}}
class
__attribute__((__swift_attr__("private_fileid:main/file1.swift"))) // expected-note {{SWIFT_PRIVATE_FILEID annotation found here}}
__attribute__((__swift_attr__("private_fileid:main/file1.swift"))) // expected-note {{SWIFT_PRIVATE_FILEID annotation found here}}
RepeatedAnnotations {}; // expected-error {{multiple SWIFT_PRIVATE_FILEID annotations were found on 'RepeatedAnnotations'}}
class
__attribute__((__swift_attr__("private_fileid:main/some/subdir/file1.swift")))
WithSubdir {}; // expected-warning@-1 {{SWIFT_PRIVATE_FILEID annotation on 'WithSubdir' does not have a valid file ID}}
class
__attribute__((__swift_attr__("private_fileid:main/file1")))
MissingExtension {}; // expected-warning@-1 {{SWIFT_PRIVATE_FILEID annotation on 'MissingExtension' does not have a valid file ID}}
class
__attribute__((__swift_attr__("private_fileid:main/file1.swift")))
IncompleteType;
class IncompleteType {};
#endif /* CXX_HEADER_H */
//--- some/subdir/file1.swift
import CxxModule
extension OK { func ext() { priv() } } // should work as expected
extension MultipleAnnotations {}
extension RepeatedAnnotations {}
extension WithSubdir {}
extension MissingExtension {}
extension IncompleteType {}
//--- some/subdir/file2.swift
import CxxModule
// This file is referenced in the MultipleAnnotations test case.
// We don't need to create it here but we may as well do so.