Skip to content

Commit caa5be8

Browse files
authored
[ClangImporter] Use external_source_symbol to identify Swift decls (#27355)
...rather than replacing particular macros with an 'annotate' attribute and then looking for that. This isn't /really/ any particular win except maybe ever-so-slightly faster module imports (with one fewer attribute being added to each declaration in a mixed-source header). This doesn't remove the SWIFT_CLASS_EXTRA, SWIFT_PROTOCOL_EXTRA, or SWIFT_ENUM_EXTRA macros from PrintAsObjC (note that SWIFT_EXTENSION_EXTRA was never used). They're not exactly needed anymore, but they're not doing any harm if someone else is using them.
1 parent c5a27ef commit caa5be8

File tree

9 files changed

+59
-60
lines changed

9 files changed

+59
-60
lines changed

lib/ClangImporter/ClangAdapter.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -563,33 +563,9 @@ bool importer::isNSNotificationGlobal(const clang::NamedDecl *decl) {
563563
}
564564

565565
bool importer::hasNativeSwiftDecl(const clang::Decl *decl) {
566-
for (auto annotation : decl->specific_attrs<clang::AnnotateAttr>()) {
567-
if (annotation->getAnnotation() == SWIFT_NATIVE_ANNOTATION_STRING) {
566+
if (auto *attr = decl->getAttr<clang::ExternalSourceSymbolAttr>())
567+
if (attr->getGeneratedDeclaration() && attr->getLanguage() == "Swift")
568568
return true;
569-
}
570-
}
571-
572-
if (auto *category = dyn_cast<clang::ObjCCategoryDecl>(decl)) {
573-
clang::SourceLocation categoryNameLoc = category->getCategoryNameLoc();
574-
if (categoryNameLoc.isMacroID()) {
575-
// Climb up to the top-most macro invocation.
576-
clang::ASTContext &clangCtx = category->getASTContext();
577-
clang::SourceManager &SM = clangCtx.getSourceManager();
578-
579-
clang::SourceLocation macroCaller =
580-
SM.getImmediateMacroCallerLoc(categoryNameLoc);
581-
while (macroCaller.isMacroID()) {
582-
categoryNameLoc = macroCaller;
583-
macroCaller = SM.getImmediateMacroCallerLoc(categoryNameLoc);
584-
}
585-
586-
StringRef macroName = clang::Lexer::getImmediateMacroName(
587-
categoryNameLoc, SM, clangCtx.getLangOpts());
588-
if (macroName == "SWIFT_EXTENSION")
589-
return true;
590-
}
591-
}
592-
593569
return false;
594570
}
595571

lib/ClangImporter/ClangImporter.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,19 +513,6 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
513513
if (!triple.isOSDarwin())
514514
invocationArgStrs.insert(invocationArgStrs.end(),
515515
{"-fobjc-runtime=ios-7.0"});
516-
517-
// Define macros that Swift bridging headers use.
518-
invocationArgStrs.insert(invocationArgStrs.end(), {
519-
"-DSWIFT_CLASS_EXTRA=__attribute__((__annotate__("
520-
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
521-
"-DSWIFT_PROTOCOL_EXTRA=__attribute__((__annotate__("
522-
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
523-
"-DSWIFT_EXTENSION_EXTRA=__attribute__((__annotate__("
524-
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
525-
"-DSWIFT_ENUM_EXTRA=__attribute__((__annotate__("
526-
"\"" SWIFT_NATIVE_ANNOTATION_STRING "\")))",
527-
});
528-
529516
} else {
530517
bool EnableCXXInterop = LangOpts.EnableCXXInterop;
531518
invocationArgStrs.insert(invocationArgStrs.end(),
@@ -569,6 +556,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
569556

570557
// Request new APIs from UIKit.
571558
"-DSWIFT_SDK_OVERLAY_UIKIT_EPOCH=2",
559+
560+
// Backwards compatibility for headers that were checking this instead of
561+
// '__swift__'.
562+
"-DSWIFT_CLASS_EXTRA=",
572563
});
573564

574565
// Get the version of this compiler and pass it to C/Objective-C

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ enum class SpecialMethodKind {
220220
NSDictionarySubscriptGetter
221221
};
222222

223-
#define SWIFT_NATIVE_ANNOTATION_STRING "__swift native"
224-
225223
#define SWIFT_PROTOCOL_SUFFIX "Protocol"
226224
#define SWIFT_CFTYPE_SUFFIX "Ref"
227225

test/ClangImporter/Inputs/enum-objc.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
@import Foundation;
22

3+
#ifndef SWIFT_ENUM_EXTRA
4+
# define SWIFT_ENUM_EXTRA
5+
#endif
6+
37
#define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))
48
#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
59
#define SWIFT_EXHAUSTIVE_ENUM(_type, _name) enum _name : _type _name; enum __attribute__((enum_extensibility(closed))) SWIFT_ENUM_EXTRA _name : _type
610

11+
#pragma clang attribute push( \
12+
__attribute__((external_source_symbol(language="Swift", \
13+
defined_in="Mixed",generated_declaration))), \
14+
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))
15+
716
typedef SWIFT_ENUM_NAMED(NSInteger, ObjCEnum, "SwiftEnum") {
817
ObjCEnumOne = 1,
918
ObjCEnumTwo,
@@ -20,6 +29,8 @@ enum BareForwardEnum;
2029
enum ForwardEnumWithUnderlyingType : int;
2130
typedef SWIFT_ENUM_NAMED(NSInteger, ForwardObjCEnum, "ForwardSwiftEnum");
2231

32+
#pragma clang attribute pop
33+
2334
void forwardBarePointer(enum BareForwardEnum * _Nonnull);
2435
void forwardWithUnderlyingValue(enum ForwardEnumWithUnderlyingType);
2536
void forwardWithUnderlyingPointer(enum ForwardEnumWithUnderlyingType * _Nonnull);

test/ClangImporter/MixedSource/Inputs/mixed-framework/Mixed.framework/Headers/Mixed.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ struct PureClangType {
77

88
#ifndef SWIFT_CLASS_EXTRA
99
# define SWIFT_CLASS_EXTRA
10+
#endif
11+
#ifndef SWIFT_PROTOCOL_EXTRA
1012
# define SWIFT_PROTOCOL_EXTRA
1113
#endif
1214

@@ -32,6 +34,11 @@ struct PureClangType {
3234
__attribute__((swift_name(SWIFT_NAME))) SWIFT_PROTOCOL_EXTRA
3335
#endif
3436

37+
#pragma clang attribute push( \
38+
__attribute__((external_source_symbol(language="Swift", \
39+
defined_in="Mixed",generated_declaration))), \
40+
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))
41+
3542
SWIFT_CLASS("SwiftClass")
3643
__attribute__((objc_root_class))
3744
@interface SwiftClass
@@ -43,10 +50,6 @@ __attribute__((objc_root_class))
4350
- (void)extensionMethod;
4451
@end
4552

46-
@interface SwiftClass (Category)
47-
- (void)categoryMethod:(struct PureClangType)arg;
48-
@end
49-
5053
SWIFT_PROTOCOL_NAMED("CustomName")
5154
@protocol SwiftProtoWithCustomName
5255
@end
@@ -68,3 +71,9 @@ convertToProto(SwiftClassWithCustomName *_Nonnull obj);
6871
SWIFT_CLASS("BOGUS")
6972
@interface BogusClass
7073
@end
74+
75+
# pragma clang attribute pop
76+
77+
@interface SwiftClass (Category)
78+
- (void)categoryMethod:(struct PureClangType)arg;
79+
@end

test/IDE/Inputs/print_clang_header_swift_name.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
@import Foundation;
22

3+
#ifndef SWIFT_ENUM_EXTRA
4+
# define SWIFT_ENUM_EXTRA
5+
#endif
6+
37
#define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))
48

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

11+
#pragma clang attribute push( \
12+
__attribute__((external_source_symbol(language="Swift", \
13+
defined_in="Mixed",generated_declaration))), \
14+
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))
15+
716
typedef SWIFT_ENUM(NSInteger, Normal) {
817
NormalOne = 0,
918
NormalTwo,
@@ -24,3 +33,5 @@ typedef SWIFT_ENUM_NAMED(NSInteger, ObjCEnumTwo, "SwiftEnumTwo") {
2433
SwiftEnumTwoB,
2534
SwiftEnumTwoC
2635
};
36+
37+
#pragma clang attribute pop

test/IDE/Inputs/swift_name.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#ifndef SWIFT_ENUM
88
# define SWIFT_ENUM(_name) \
99
enum _name _name; \
10-
enum __attribute__((enum_extensibility(open))) SWIFT_ENUM_EXTRA _name
10+
enum __attribute__((enum_extensibility(open))) \
11+
__attribute__((external_source_symbol(language="Swift", \
12+
defined_in="swift_name", generated_declaration))) \
13+
SWIFT_ENUM_EXTRA _name
1114
#endif
1215

1316
// Renaming global variables.

test/IDE/Inputs/swift_name_objc.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22

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

5-
#ifndef SWIFT_ENUM_EXTRA
6-
# define SWIFT_ENUM_EXTRA
7-
#endif
8-
9-
#ifndef SWIFT_ENUM
10-
# define SWIFT_ENUM(_type, _name) \
11-
enum _name : _type _name; \
12-
enum SWIFT_ENUM_EXTRA _name : _type
13-
#endif
14-
155
// Renaming classes
166
SWIFT_NAME(SomeClass)
177
@interface SNSomeClass : NSObject

test/SourceKit/Inputs/libIDE-mock-sdk/Mixed.framework/Headers/Mixed.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ struct PureClangType {
66
#ifndef SWIFT_CLASS_EXTRA
77
# define SWIFT_CLASS_EXTRA
88
#endif
9+
#ifndef SWIFT_PROTOCOL_EXTRA
10+
# define SWIFT_PROTOCOL_EXTRA
11+
#endif
912

1013
#ifndef SWIFT_CLASS
1114
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_CLASS_EXTRA
@@ -21,15 +24,16 @@ struct PureClangType {
2124
__attribute__((swift_name(SWIFT_NAME))) SWIFT_PROTOCOL_EXTRA
2225
#endif
2326

27+
#pragma clang attribute push( \
28+
__attribute__((external_source_symbol(language="Swift", \
29+
defined_in="Mixed",generated_declaration))), \
30+
apply_to=any(function,enum,objc_interface,objc_category,objc_protocol))
31+
2432
SWIFT_CLASS("SwiftClass")
2533
__attribute__((objc_root_class))
2634
@interface SwiftClass
2735
@end
2836

29-
@interface SwiftClass (Category)
30-
- (void)categoryMethod:(struct PureClangType)arg;
31-
@end
32-
3337
SWIFT_PROTOCOL_NAMED("CustomNameType")
3438
@protocol SwiftProtoWithCustomName
3539
@end
@@ -45,3 +49,9 @@ convertToProto(SwiftClassWithCustomName *_Nonnull obj);
4549
SWIFT_CLASS("BOGUS")
4650
@interface BogusClass
4751
@end
52+
53+
# pragma clang attribute pop
54+
55+
@interface SwiftClass (Category)
56+
- (void)categoryMethod:(struct PureClangType)arg;
57+
@end

0 commit comments

Comments
 (0)