File tree Expand file tree Collapse file tree 6 files changed +77
-7
lines changed Expand file tree Collapse file tree 6 files changed +77
-7
lines changed Original file line number Diff line number Diff line change @@ -1702,6 +1702,10 @@ class CustomAttr final : public DeclAttribute {
1702
1702
bool isArgUnsafe () const ;
1703
1703
void setArgIsUnsafe (bool unsafe) { isArgUnsafeBit = unsafe; }
1704
1704
1705
+ // / Whether this custom attribute is a macro attached to the given
1706
+ // / declaration.
1707
+ bool isAttachedMacro (const Decl *decl) const ;
1708
+
1705
1709
Expr *getSemanticInit () const { return semanticInit; }
1706
1710
void setSemanticInit (Expr *expr) { semanticInit = expr; }
1707
1711
Original file line number Diff line number Diff line change 23
23
#include " swift/AST/IndexSubset.h"
24
24
#include " swift/AST/LazyResolver.h"
25
25
#include " swift/AST/Module.h"
26
+ #include " swift/AST/NameLookupRequests.h"
26
27
#include " swift/AST/ParameterList.h"
27
28
#include " swift/AST/TypeCheckRequests.h"
28
29
#include " swift/AST/TypeRepr.h"
@@ -2336,6 +2337,21 @@ bool CustomAttr::isArgUnsafe() const {
2336
2337
return isArgUnsafeBit;
2337
2338
}
2338
2339
2340
+ bool CustomAttr::isAttachedMacro (const Decl *decl) const {
2341
+ auto &ctx = decl->getASTContext ();
2342
+ auto *dc = decl->getInnermostDeclContext ();
2343
+
2344
+ auto attrDecl = evaluateOrDefault (
2345
+ ctx.evaluator ,
2346
+ CustomAttrDeclRequest{const_cast <CustomAttr *>(this ), dc},
2347
+ nullptr );
2348
+
2349
+ if (!attrDecl)
2350
+ return false ;
2351
+
2352
+ return attrDecl.dyn_cast <MacroDecl *>();
2353
+ }
2354
+
2339
2355
DeclarationAttr::DeclarationAttr (SourceLoc atLoc, SourceRange range,
2340
2356
MacroRole role,
2341
2357
ArrayRef<MacroIntroducedDeclName> peerNames,
Original file line number Diff line number Diff line change @@ -2855,6 +2855,11 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
2855
2855
case DAK_Custom: {
2856
2856
auto abbrCode = S.DeclTypeAbbrCodes [CustomDeclAttrLayout::Code];
2857
2857
auto theAttr = cast<CustomAttr>(DA);
2858
+
2859
+ // Macro attributes are not serialized.
2860
+ if (theAttr->isAttachedMacro (D))
2861
+ return ;
2862
+
2858
2863
auto typeID = S.addTypeRef (theAttr->getType ());
2859
2864
if (!typeID && !S.allowCompilerErrors ()) {
2860
2865
llvm::PrettyStackTraceString message (" CustomAttr has no type" );
Original file line number Diff line number Diff line change
1
+ import SwiftSyntax
2
+ import SwiftSyntaxBuilder
3
+ import _SwiftSyntaxMacros
4
+
5
+ public struct StringifyMacro : ExpressionMacro {
6
+ public static func expansion(
7
+ of macro: MacroExpansionExprSyntax , in context: inout MacroExpansionContext
8
+ ) -> ExprSyntax {
9
+ guard let argument = macro. argumentList. first? . expression else {
10
+ // FIXME: Create a diagnostic for the missing argument?
11
+ return ExprSyntax ( macro)
12
+ }
13
+
14
+ return " ( \( argument) , \( StringLiteralExprSyntax ( content: argument. description) ) ) "
15
+ }
16
+ }
17
+
18
+ public struct MyWrapperMacro : AccessorDeclarationMacro {
19
+ public static func expansion(
20
+ of node: AttributeSyntax ,
21
+ attachedTo declaration: DeclSyntax ,
22
+ in context: inout MacroExpansionContext
23
+ ) throws -> [ AccessorDeclSyntax ] {
24
+ return [ ]
25
+ }
26
+ }
27
+
28
+ public struct WrapAllProperties : MemberAttributeMacro {
29
+ public static func expansion(
30
+ of node: AttributeSyntax ,
31
+ attachedTo parent: DeclSyntax ,
32
+ annotating member: DeclSyntax ,
33
+ in context: inout MacroExpansionContext
34
+ ) throws -> [ AttributeSyntax ] {
35
+ return [ ]
36
+ }
37
+ }
38
+
Original file line number Diff line number Diff line change 1
- @expression public macro publicStringify< T> ( _ value: T ) -> ( T , String ) = SomeModule . StringifyMacro
1
+ @expression public macro publicStringify< T> ( _ value: T ) -> ( T , String ) = #externalMacro ( module : " MacroDefinition " , type : " StringifyMacro " )
2
2
3
- @expression macro internalStringify< T> ( _ value: T ) -> ( T , String ) = SomeModule . StringifyMacro
3
+ @expression macro internalStringify< T> ( _ value: T ) -> ( T , String ) = #externalMacro ( module : " MacroDefinition " , type : " StringifyMacro " )
4
4
5
- @attached ( accessor) public macro myWrapper: Void = SomeModule. MyWrapperMacro
5
+ @attached ( accessor) public macro myWrapper: Void = #externalMacro( module: " MacroDefinition" , type: " MyWrapperMacro" )
6
+
7
+ @attached ( memberAttributes) public macro wrapAllProperties( ) = #externalMacro( module: " MacroDefinition " , type: " WrapAllProperties " )
8
+
9
+ // Make sure that macro custom attributes are not serialized.
10
+ @wrapAllProperties
11
+ public struct S {
12
+ public var value : Int
13
+ }
Original file line number Diff line number Diff line change 2
2
3
3
// RUN: %empty-directory(%t)
4
4
// RUN: %empty-directory(%t-scratch)
5
- // RUN: %target-swift-frontend -emit-module -o %t/def_macros.swiftmodule %S/Inputs/def_macros.swift -module-name def_macros -enable-experimental-feature Macros
6
- // RUN: %target-swift-frontend -typecheck -I%t -verify %s -verify-ignore-unknown -enable-experimental-feature Macros
5
+ // RUN: %target-build-swift -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/def_macro_plugin.swift -g -no-toolchain-stdlib-rpath
6
+ // RUN: %target-swift-frontend -emit-module -o %t/def_macros.swiftmodule %S/Inputs/def_macros.swift -module-name def_macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
7
+ // RUN: %target-swift-frontend -typecheck -I%t -verify %s -verify-ignore-unknown -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir
7
8
// REQUIRES: OS=macosx
8
9
9
10
import def_macros
10
11
11
12
func test( a: Int , b: Int ) {
12
13
_ = #publicStringify ( a + b)
13
- // expected-error@-1{{external macro implementation type 'SomeModule.StringifyMacro' could not be found for macro 'publicStringify'; the type must be public and provided via '-load-plugin-library'}}
14
14
15
15
_ = #internalStringify ( a + b)
16
16
// expected-error@-1{{no macro named 'internalStringify'}}
17
17
}
18
18
19
19
struct TestStruct {
20
20
@myWrapper var x : Int
21
- // expected-error@-1{{external macro implementation type 'SomeModule.MyWrapperMacro' could not be found for macro 'myWrapper'}}
22
21
}
You can’t perform that action at this time.
0 commit comments