Skip to content

[ClangImporter] Use external_source_symbol to identify Swift decls #27355

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

Merged
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
28 changes: 2 additions & 26 deletions lib/ClangImporter/ClangAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,33 +563,9 @@ bool importer::isNSNotificationGlobal(const clang::NamedDecl *decl) {
}

bool importer::hasNativeSwiftDecl(const clang::Decl *decl) {
for (auto annotation : decl->specific_attrs<clang::AnnotateAttr>()) {
if (annotation->getAnnotation() == SWIFT_NATIVE_ANNOTATION_STRING) {
if (auto *attr = decl->getAttr<clang::ExternalSourceSymbolAttr>())
if (attr->getGeneratedDeclaration() && attr->getLanguage() == "Swift")
return true;
}
}

if (auto *category = dyn_cast<clang::ObjCCategoryDecl>(decl)) {
clang::SourceLocation categoryNameLoc = category->getCategoryNameLoc();
if (categoryNameLoc.isMacroID()) {
// Climb up to the top-most macro invocation.
clang::ASTContext &clangCtx = category->getASTContext();
clang::SourceManager &SM = clangCtx.getSourceManager();

clang::SourceLocation macroCaller =
SM.getImmediateMacroCallerLoc(categoryNameLoc);
while (macroCaller.isMacroID()) {
categoryNameLoc = macroCaller;
macroCaller = SM.getImmediateMacroCallerLoc(categoryNameLoc);
}

StringRef macroName = clang::Lexer::getImmediateMacroName(
categoryNameLoc, SM, clangCtx.getLangOpts());
if (macroName == "SWIFT_EXTENSION")
return true;
}
}

return false;
}

Expand Down
17 changes: 4 additions & 13 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,19 +513,6 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
if (!triple.isOSDarwin())
invocationArgStrs.insert(invocationArgStrs.end(),
{"-fobjc-runtime=ios-7.0"});

// Define macros that Swift bridging headers use.
invocationArgStrs.insert(invocationArgStrs.end(), {
"-DSWIFT_CLASS_EXTRA=__attribute__((__annotate__("
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
"-DSWIFT_PROTOCOL_EXTRA=__attribute__((__annotate__("
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
"-DSWIFT_EXTENSION_EXTRA=__attribute__((__annotate__("
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
"-DSWIFT_ENUM_EXTRA=__attribute__((__annotate__("
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
});

} else {
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
invocationArgStrs.insert(invocationArgStrs.end(),
Expand Down Expand Up @@ -569,6 +556,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,

// Request new APIs from UIKit.
"-DSWIFT_SDK_OVERLAY_UIKIT_EPOCH=2",

// Backwards compatibility for headers that were checking this instead of
// '__swift__'.
"-DSWIFT_CLASS_EXTRA=",
});

// Get the version of this compiler and pass it to C/Objective-C
Expand Down
2 changes: 0 additions & 2 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ enum class SpecialMethodKind {
NSDictionarySubscriptGetter
};

#define SWIFT_NATIVE_ANNOTATION_STRING "__swift native"

#define SWIFT_PROTOCOL_SUFFIX "Protocol"
#define SWIFT_CFTYPE_SUFFIX "Ref"

Expand Down
11 changes: 11 additions & 0 deletions test/ClangImporter/Inputs/enum-objc.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
@import Foundation;

#ifndef SWIFT_ENUM_EXTRA
# define SWIFT_ENUM_EXTRA
#endif

#define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))
#define SWIFT_ENUM_NAMED(_type, _name, SWIFT_NAME) enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_NAME); enum SWIFT_COMPILE_NAME(SWIFT_NAME) __attribute__((enum_extensibility(open))) SWIFT_ENUM_EXTRA _name : _type
#define SWIFT_EXHAUSTIVE_ENUM(_type, _name) enum _name : _type _name; enum __attribute__((enum_extensibility(closed))) SWIFT_ENUM_EXTRA _name : _type

#pragma clang attribute push( \
__attribute__((external_source_symbol(language="Swift", \
defined_in="Mixed",generated_declaration))), \
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))

typedef SWIFT_ENUM_NAMED(NSInteger, ObjCEnum, "SwiftEnum") {
ObjCEnumOne = 1,
ObjCEnumTwo,
Expand All @@ -20,6 +29,8 @@ enum BareForwardEnum;
enum ForwardEnumWithUnderlyingType : int;
typedef SWIFT_ENUM_NAMED(NSInteger, ForwardObjCEnum, "ForwardSwiftEnum");

#pragma clang attribute pop

void forwardBarePointer(enum BareForwardEnum * _Nonnull);
void forwardWithUnderlyingValue(enum ForwardEnumWithUnderlyingType);
void forwardWithUnderlyingPointer(enum ForwardEnumWithUnderlyingType * _Nonnull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ struct PureClangType {

#ifndef SWIFT_CLASS_EXTRA
# define SWIFT_CLASS_EXTRA
#endif
#ifndef SWIFT_PROTOCOL_EXTRA
# define SWIFT_PROTOCOL_EXTRA
#endif

Expand All @@ -32,6 +34,11 @@ struct PureClangType {
__attribute__((swift_name(SWIFT_NAME))) SWIFT_PROTOCOL_EXTRA
#endif

#pragma clang attribute push( \
__attribute__((external_source_symbol(language="Swift", \
defined_in="Mixed",generated_declaration))), \
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))

SWIFT_CLASS("SwiftClass")
__attribute__((objc_root_class))
@interface SwiftClass
Expand All @@ -43,10 +50,6 @@ __attribute__((objc_root_class))
- (void)extensionMethod;
@end

@interface SwiftClass (Category)
- (void)categoryMethod:(struct PureClangType)arg;
@end

SWIFT_PROTOCOL_NAMED("CustomName")
@protocol SwiftProtoWithCustomName
@end
Expand All @@ -68,3 +71,9 @@ convertToProto(SwiftClassWithCustomName *_Nonnull obj);
SWIFT_CLASS("BOGUS")
@interface BogusClass
@end

# pragma clang attribute pop

@interface SwiftClass (Category)
- (void)categoryMethod:(struct PureClangType)arg;
@end
11 changes: 11 additions & 0 deletions test/IDE/Inputs/print_clang_header_swift_name.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
@import Foundation;

#ifndef SWIFT_ENUM_EXTRA
# define SWIFT_ENUM_EXTRA
#endif

#define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))

#define SWIFT_ENUM(_type, _name) enum _name : _type _name; enum __attribute__((enum_extensibility(open))) SWIFT_ENUM_EXTRA _name : _type

#pragma clang attribute push( \
__attribute__((external_source_symbol(language="Swift", \
defined_in="Mixed",generated_declaration))), \
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))

typedef SWIFT_ENUM(NSInteger, Normal) {
NormalOne = 0,
NormalTwo,
Expand All @@ -24,3 +33,5 @@ typedef SWIFT_ENUM_NAMED(NSInteger, ObjCEnumTwo, "SwiftEnumTwo") {
SwiftEnumTwoB,
SwiftEnumTwoC
};

#pragma clang attribute pop
5 changes: 4 additions & 1 deletion test/IDE/Inputs/swift_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
#ifndef SWIFT_ENUM
# define SWIFT_ENUM(_name) \
enum _name _name; \
enum __attribute__((enum_extensibility(open))) SWIFT_ENUM_EXTRA _name
enum __attribute__((enum_extensibility(open))) \
__attribute__((external_source_symbol(language="Swift", \
defined_in="swift_name", generated_declaration))) \
SWIFT_ENUM_EXTRA _name
#endif

// Renaming global variables.
Expand Down
10 changes: 0 additions & 10 deletions test/IDE/Inputs/swift_name_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

#define SWIFT_NAME(X) __attribute__((swift_name(#X)))

#ifndef SWIFT_ENUM_EXTRA
# define SWIFT_ENUM_EXTRA
#endif

#ifndef SWIFT_ENUM
# define SWIFT_ENUM(_type, _name) \
enum _name : _type _name; \
enum SWIFT_ENUM_EXTRA _name : _type
#endif

// Renaming classes
SWIFT_NAME(SomeClass)
@interface SNSomeClass : NSObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ struct PureClangType {
#ifndef SWIFT_CLASS_EXTRA
# define SWIFT_CLASS_EXTRA
#endif
#ifndef SWIFT_PROTOCOL_EXTRA
# define SWIFT_PROTOCOL_EXTRA
#endif

#ifndef SWIFT_CLASS
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_CLASS_EXTRA
Expand All @@ -21,15 +24,16 @@ struct PureClangType {
__attribute__((swift_name(SWIFT_NAME))) SWIFT_PROTOCOL_EXTRA
#endif

#pragma clang attribute push( \
__attribute__((external_source_symbol(language="Swift", \
defined_in="Mixed",generated_declaration))), \
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))

SWIFT_CLASS("SwiftClass")
__attribute__((objc_root_class))
@interface SwiftClass
@end

@interface SwiftClass (Category)
- (void)categoryMethod:(struct PureClangType)arg;
@end

SWIFT_PROTOCOL_NAMED("CustomNameType")
@protocol SwiftProtoWithCustomName
@end
Expand All @@ -45,3 +49,9 @@ convertToProto(SwiftClassWithCustomName *_Nonnull obj);
SWIFT_CLASS("BOGUS")
@interface BogusClass
@end

# pragma clang attribute pop

@interface SwiftClass (Category)
- (void)categoryMethod:(struct PureClangType)arg;
@end