Skip to content

Commit 01add3a

Browse files
authored
[PrintAsObjC] Defend against macros named 'any' (#12396)
SourceKit uses a clever pragma to automatically attach an attribute to every declaration in the file. However, the form of this pragma (clang attribute push) uses a parenthesized list marked with 'any', which turns out to be a name that people sometimes use for macros. Guard against this using the push_macro pragma (originally from MSVC), under the assumption that anyone who supports the highly-use-specific 'external_source_symbol' attribute probably implements this more common extension. This does make the generated header a little uglier, but it's not like it was really pretty to begin with. rdar://problem/34168022
1 parent bec95b2 commit 01add3a

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,21 +2371,6 @@ class ModuleWriter {
23712371
"# define __has_warning(x) 0\n"
23722372
"#endif\n"
23732373
"\n"
2374-
"#if __has_attribute(external_source_symbol)\n"
2375-
"# define SWIFT_STRINGIFY(str) #str\n"
2376-
"# define SWIFT_MODULE_NAMESPACE_PUSH(module_name) "
2377-
"_Pragma(SWIFT_STRINGIFY(clang attribute "
2378-
"push(__attribute__((external_source_symbol(language=\"Swift\", "
2379-
"defined_in=module_name, generated_declaration))), "
2380-
"apply_to=any(function, enum, objc_interface, objc_category, "
2381-
"objc_protocol))))\n"
2382-
"# define SWIFT_MODULE_NAMESPACE_POP "
2383-
"_Pragma(\"clang attribute pop\")\n"
2384-
"#else\n"
2385-
"# define SWIFT_MODULE_NAMESPACE_PUSH(module_name)\n"
2386-
"# define SWIFT_MODULE_NAMESPACE_POP\n"
2387-
"#endif\n"
2388-
"\n"
23892374
"#if __has_include(<swift/objc-prologue.h>)\n"
23902375
"# include <swift/objc-prologue.h>\n"
23912376
"#endif\n"
@@ -2749,9 +2734,20 @@ class ModuleWriter {
27492734
"#pragma clang diagnostic ignored \"-Wunknown-pragmas\"\n"
27502735
"#pragma clang diagnostic ignored \"-Wnullability\"\n"
27512736
"\n"
2752-
"SWIFT_MODULE_NAMESPACE_PUSH(\"" << M.getNameStr() << "\")\n"
2737+
"#if __has_attribute(external_source_symbol)\n"
2738+
"# pragma push_macro(\"any\")\n"
2739+
"# undef any\n"
2740+
"# pragma clang attribute push("
2741+
"__attribute__((external_source_symbol(language=\"Swift\", "
2742+
"defined_in=\"" << M.getNameStr() << "\",generated_declaration))), "
2743+
"apply_to=any(function,enum,objc_interface,objc_category,"
2744+
"objc_protocol))\n"
2745+
"# pragma pop_macro(\"any\")\n"
2746+
"#endif\n\n"
27532747
<< os.str()
2754-
<< "SWIFT_MODULE_NAMESPACE_POP\n"
2748+
<< "#if __has_attribute(external_source_symbol)\n"
2749+
"# pragma clang attribute pop\n"
2750+
"#endif\n"
27552751
"#pragma clang diagnostic pop\n";
27562752
return false;
27572753
}

test/PrintAsObjC/Inputs/comments-expected-output.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,5 +451,7 @@ SWIFT_CLASS("_TtC8comments13UnorderedList")
451451
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
452452
@end
453453

454-
SWIFT_MODULE_NAMESPACE_POP
454+
#if __has_attribute(external_source_symbol)
455+
# pragma clang attribute pop
456+
#endif
455457
#pragma clang diagnostic pop
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// FIXME: BEGIN -enable-source-import hackaround
4+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift
5+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift
6+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/Foundation.swift
7+
// FIXME: END -enable-source-import hackaround
8+
9+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) %s -typecheck -emit-objc-header-path %t/interfering-macros.h
10+
// RUN: %check-in-clang -fsyntax-only -Werror %t/interfering-macros.h -D'any=UNWANTED_MACRO_SUBSTITUTION'
11+
12+
// REQUIRES: objc_interop
13+
14+
import Foundation
15+
16+
@objc public class Test : NSObject {
17+
public var str: String = ""
18+
public var strongProp: Any?
19+
}

0 commit comments

Comments
 (0)