Skip to content

Commit b56a62e

Browse files
committed
Clean up
1 parent d1705a5 commit b56a62e

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

include/swift/AST/FileUnit.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,13 @@ class FileUnit : public DeclContext, public ASTAllocated<FileUnit> {
210210
/// The order of the results is not guaranteed to be meaningful.
211211
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {}
212212

213-
/// Finds all top-level decls in this file with all macros recursively
214-
/// expanded.
213+
/// Finds all top-level decls in this file with their auxiliary decls such as
214+
/// macro expansions.
215215
///
216216
/// This does a simple local lookup, not recursively looking through imports.
217217
/// The order of the results is not guaranteed to be meaningful.
218-
void getExpandedTopLevelDecls(SmallVectorImpl<Decl*> &results) const;
218+
void getTopLevelDeclsWithAuxiliaryDecls(
219+
SmallVectorImpl<Decl*> &results) const;
219220

220221
virtual void
221222
getExportedPrespecializations(SmallVectorImpl<Decl *> &results) const {}

lib/AST/Module.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,18 +3911,16 @@ void FileUnit::getTopLevelDeclsWhereAttributesMatch(
39113911
Results.erase(newEnd, Results.end());
39123912
}
39133913

3914-
void FileUnit::getExpandedTopLevelDecls(SmallVectorImpl<Decl*> &results) const {
3914+
void FileUnit::getTopLevelDeclsWithAuxiliaryDecls(
3915+
SmallVectorImpl<Decl*> &results) const {
39153916
SmallVector<Decl *, 32> nonExpandedDecls;
39163917
nonExpandedDecls.reserve(results.capacity());
39173918
getTopLevelDecls(nonExpandedDecls);
39183919
for (auto *decl : nonExpandedDecls) {
3919-
if (auto *med = dyn_cast<MacroExpansionDecl>(decl)) {
3920-
med->visitAuxiliaryDecls([&](Decl *decl) {
3921-
results.push_back(decl);
3922-
});
3923-
} else {
3924-
results.push_back(decl);
3925-
}
3920+
decl->visitAuxiliaryDecls([&](Decl *auxDecl) {
3921+
results.push_back(auxDecl);
3922+
});
3923+
results.push_back(decl);
39263924
}
39273925
}
39283926

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6133,7 +6133,7 @@ void Serializer::writeAST(ModuleOrSourceFile DC) {
61336133

61346134
// FIXME: Switch to a visitor interface?
61356135
SmallVector<Decl *, 32> fileDecls;
6136-
nextFile->getExpandedTopLevelDecls(fileDecls);
6136+
nextFile->getTopLevelDeclsWithAuxiliaryDecls(fileDecls);
61376137

61386138
for (auto D : fileDecls) {
61396139
if (isa<ImportDecl>(D) || isa<IfConfigDecl>(D) ||

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,8 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
11771177
"""
11781178
11791179
class \(context.createUniqueName("name")) {
1180-
func hello() {
1180+
func hello() -> String {
1181+
\(body.statements)
11811182
}
11821183
}
11831184
""",
@@ -1187,7 +1188,8 @@ public struct DefineAnonymousTypesMacro: DeclarationMacro {
11871188
case apple
11881189
case banana
11891190
1190-
func hello() {
1191+
func hello() -> String {
1192+
\(body.statements)
11911193
}
11921194
}
11931195
"""

test/Macros/macro_expand.swift

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// UN: %empty-directory(%t)
2-
// UN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -swift-version 5 -I %swift-host-lib-dir -L %swift-host-lib-dir -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath -swift-version 5
33
// RUNx: %target-swift-frontend -dump-ast -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser 2>&1 | %FileCheck --check-prefix CHECK-AST %s
44

55
// Diagnostics testing
@@ -12,11 +12,9 @@
1212

1313
// RUN: %FileCheck %s --check-prefix CHECK-MACRO-PRINTED < %t/macro-printing.txt
1414

15-
// FIXME(rdar://106053984) Debug info SIL testing (re-enable -g)
16-
// UN: %target-swift-frontend -swift-version 5 -emit-sil -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - | %FileCheck --check-prefix CHECK-SIL %s
15+
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-SIL %s
1716

18-
// FIXME(rdar://106053984) Debug info IR testing (re-enable -g)
19-
// UN: %target-swift-frontend -swift-version 5 -emit-ir -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - | %FileCheck --check-prefix CHECK-IR %s
17+
// RUN: %target-swift-frontend -swift-version 5 -emit-ir -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s
2018

2119
// Execution testing
2220
// RUN: %target-build-swift -swift-version 5 -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
@@ -83,10 +81,10 @@ struct Bad {}
8381
func testFileID(a: Int, b: Int) {
8482
// CHECK: MacroUser/macro_expand.swift
8583
print("Result is \(#customFileID)")
86-
// HECK-SIL: sil_scope [[MACRO_SCOPE:[0-9]+]] { loc "{{.*}}":1:1 parent @$s9MacroUser10testFileID1a1bySi_SitF06customdE0fMf_ {{.*}} }
87-
// HECK-SIL: sil_scope [[SRC_SCOPE:[0-9]+]] { loc "{{.*}}macro_expand.swift":[[@LINE-2]]
88-
// HECK-SIL: sil_scope {{[0-9]+}} { loc "{{.*}}":1:1 parent [[MACRO_SCOPE]] inlined_at [[SRC_SCOPE]] }
89-
// HECK-IR: !DISubprogram(name: "customFileID", linkageName: "$s9MacroUser10testFileID1a1bySi_SitF06customdE0fMf_"
84+
// CHECK-SIL: sil_scope [[MACRO_SCOPE:[0-9]+]] { loc "{{.*}}":1:1 parent @$s9MacroUser10testFileID1a1bySi_SitF06customdE0fMf_ {{.*}} }
85+
// CHECK-SIL: sil_scope [[SRC_SCOPE:[0-9]+]] { loc "{{.*}}macro_expand.swift":[[@LINE-2]]
86+
// CHECK-SIL: sil_scope {{[0-9]+}} { loc "{{.*}}":1:1 parent [[MACRO_SCOPE]] inlined_at [[SRC_SCOPE]] }
87+
// CHECK-IR: !DISubprogram(name: "customFileID", linkageName: "$s9MacroUser10testFileID1a1bySi_SitF06customdE0fMf_"
9088
9189
9290
// CHECK: Builtin result is MacroUser/macro_expand.swift
@@ -230,26 +228,32 @@ func testNestedDeclInExpr() {
230228
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne))
231229
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro")
232230

233-
//#bitwidthNumberedStructs("MyIntGlobal")
234-
//
235-
//#bitwidthNumberedStructs("MyIntGlobalTwo", blah: false)
236-
//
237-
//let blah = false
238-
//#bitwidthNumberedStructs("MyIntGlobalThree", blah: blah)
231+
// FIXME: Macros producing arbitrary names are not supported yet
232+
#if false
233+
#bitwidthNumberedStructs("MyIntGlobal")
234+
235+
#bitwidthNumberedStructs("MyIntGlobalTwo", blah: false)
236+
237+
let blah = false
238+
#bitwidthNumberedStructs("MyIntGlobalThree", blah: blah)
239+
#endif
239240

240241
// Test unqualified lookup from within a macro expansion
241242
@freestanding(declaration, names: named(StructWithUnqualifiedLookup))
242243
macro structWithUnqualifiedLookup() = #externalMacro(module: "MacroDefinition", type: "DefineStructWithUnqualifiedLookupMacro")
243-
let world = 3 // to be used by the macro expansion below
244-
#structWithUnqualifiedLookup()
245-
_ = StructWithUnqualifiedLookup().foo()
246244

247245
@freestanding(declaration)
248246
macro anonymousTypes(_: () -> String) = #externalMacro(module: "MacroDefinition", type: "DefineAnonymousTypesMacro")
249247

250-
#anonymousTypes { "hello" }
251248

252-
// CECK-SIL: $s9MacroUser14anonymousTypesfMf0_4namefMu_
249+
// FIXME: Global freestanding macros not yet supported in script mode.
250+
#if false
251+
let world = 3 // to be used by the macro expansion below
252+
#structWithUnqualifiedLookup()
253+
_ = StructWithUnqualifiedLookup().foo()
254+
255+
#anonymousTypes { "hello" }
256+
#endif
253257

254258
func testFreestandingMacroExpansion() {
255259
// Explicit structs to force macros to be parsed as decl.
@@ -309,7 +313,5 @@ func testFreestandingMacroExpansion() {
309313
// print(MyIntGlobal64.self)
310314

311315
#anonymousTypes { "hello" }
312-
// expected-note @-1 2 {{in expansion of macro 'anonymousTypes' here}}
313-
// HECK-SIL: $s9MacroUser016testFreestandingA9ExpansionyyF14anonymousTypesfMf1_4namefMu_
314316
}
315317
testFreestandingMacroExpansion()

0 commit comments

Comments
 (0)