Skip to content

Commit de1d139

Browse files
committed
[Coverage] Also walk MacroExpansionDecls
Make sure we walk macro expansion decls to handle cases where e.g a binding introduces control flow. This should be a pretty uncommon case since bindings introduced by macros aren't actually usable when expanded in local contexts, but handle it all the same.
1 parent 42cfd4c commit de1d139

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lib/SIL/IR/SILProfiler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,12 @@ bool shouldSkipExpr(Expr *E) {
258258
/// Whether the children of a decl that isn't explicitly handled should be
259259
/// walked.
260260
static bool shouldWalkIntoUnhandledDecl(const Decl *D) {
261-
// We want to walk into the initializer for a pattern binding decl. This
262-
// allows us to map LazyInitializerExprs.
263-
return isa<PatternBindingDecl>(D);
261+
// We want to walk into initializers for bindings, and the expansions of
262+
// MacroExpansionDecls, which will be nested within MacroExpansionExprs in
263+
// local contexts. We won't record any regions within the macro expansion,
264+
// but still need to walk to get accurate counter information in case e.g
265+
// there's a throwing function call in the expansion.
266+
return isa<PatternBindingDecl>(D) || isa<MacroExpansionDecl>(D);
264267
}
265268

266269
/// Whether the expression \c E could potentially throw an error.

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,15 @@ public struct SingleMemberStubMacro: DeclarationMacro {
22182218
}
22192219
}
22202220

2221+
public struct DeclMacroWithControlFlow: DeclarationMacro {
2222+
public static func expansion(
2223+
of node: some FreestandingMacroExpansionSyntax,
2224+
in context: some MacroExpansionContext
2225+
) throws -> [DeclSyntax] {
2226+
return ["let _ = .random() ? try throwingFn() : 0"]
2227+
}
2228+
}
2229+
22212230
public struct GenerateStubsForProtocolRequirementsMacro: PeerMacro, ExtensionMacro {
22222231
public static func expansion(
22232232
of node: AttributeSyntax,

test/Profiler/coverage_macros.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ macro id<T>(_: T) -> T = #externalMacro(module: "MacroDefinition", type: "TupleM
3131
@attached(body)
3232
macro BodyMacroWithControlFlow() = #externalMacro(module: "MacroDefinition", type: "BodyMacroWithControlFlow")
3333

34+
@freestanding(declaration, names: arbitrary)
35+
macro declMacroWithControlFlow() = #externalMacro(module: "MacroDefinition", type: "DeclMacroWithControlFlow")
36+
3437
// This needs to be matched up here due to the sorting of the SIL; just make
3538
// sure we emit the counter increments for the error branches.
3639
// CHECK-LABEL: sil hidden @$s15coverage_macros5test2Si_SityKF
@@ -129,3 +132,10 @@ func test7() throws {
129132
guard .random() else { return }
130133
print("hello")
131134
}
135+
136+
// CHECK-LABEL: sil_coverage_map{{.*}}s15coverage_macros5test8yyKF
137+
func test8() throws { // CHECK-NEXT: [[@LINE]]:21 -> [[@LINE+4]]:2 : 0
138+
guard .random() else { return } // CHECK-NEXT: [[@LINE]]:24 -> [[@LINE]]:34 : 1
139+
// CHECK-NEXT: [[@LINE-1]]:34 -> [[@LINE+2]]:2 : (0 - 1)
140+
#declMacroWithControlFlow // CHECK-NEXT: [[@LINE]]:28 -> [[@LINE+1]]:2 : ((0 - 1) - 3)
141+
}

0 commit comments

Comments
 (0)