Skip to content

Commit 787e0fc

Browse files
Merge pull request #66734 from adrian-prantl/110841130-5.9
SILDebugScopes: Don't ignore ConditionalClauseInitializerScope
2 parents 1b9cc10 + 74d94ee commit 787e0fc

File tree

10 files changed

+114
-53
lines changed

10 files changed

+114
-53
lines changed

include/swift/AST/ASTScope.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,6 @@ class ConditionalClauseInitializerScope final : public ASTScopeImpl {
990990
SourceRange
991991
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
992992
std::string getClassName() const override;
993-
bool ignoreInDebugInfo() const override { return true; }
994993

995994
private:
996995
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);

lib/SILGen/SILGenFunction.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,52 @@ SILGenFunction::SILGenFunction(SILGenModule &SGM, SILFunction &F,
5555
assert(DC && "creating SGF without a DeclContext?");
5656
B.setInsertionPoint(createBasicBlock());
5757
B.setCurrentDebugScope(F.getDebugScope());
58+
59+
// Populate VarDeclScopeMap.
5860
SourceLoc SLoc = F.getLocation().getSourceLoc();
5961
if (SF && SLoc) {
6062
FnASTScope = ast_scope::ASTScopeImpl::findStartingScopeForLookup(SF, SLoc);
6163
ScopeMap.insert({{FnASTScope, nullptr}, F.getDebugScope()});
64+
65+
// Collect all variable declarations in this scope.
66+
struct Consumer : public namelookup::AbstractASTScopeDeclConsumer {
67+
const ast_scope::ASTScopeImpl *ASTScope;
68+
VarDeclScopeMapTy &VarDeclScopeMap;
69+
Consumer(const ast_scope::ASTScopeImpl *ASTScope,
70+
VarDeclScopeMapTy &VarDeclScopeMap)
71+
: ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
72+
73+
bool consume(ArrayRef<ValueDecl *> values,
74+
NullablePtr<DeclContext> baseDC) override {
75+
LLVM_DEBUG(ASTScope->print(llvm::errs(), 0, false, false));
76+
for (auto &value : values) {
77+
LLVM_DEBUG({
78+
if (value->hasName())
79+
llvm::dbgs() << "+ " << value->getBaseIdentifier() << "\n";
80+
});
81+
82+
// FIXME: ASTs coming out of the autodiff transformation trigger this.
83+
// assert((VarDeclScopeMap.count(value) == 0 ||
84+
// VarDeclScopeMap[value] == ASTScope) &&
85+
// "VarDecl appears twice");
86+
VarDeclScopeMap.insert({value, ASTScope});
87+
}
88+
return false;
89+
}
90+
bool lookInMembers(const DeclContext *) const override { return false; }
91+
#ifndef NDEBUG
92+
void startingNextLookupStep() override {}
93+
void finishingLookup(std::string) const override {}
94+
bool isTargetLookup() const override { return false; }
95+
#endif
96+
};
97+
const_cast<ast_scope::ASTScopeImpl *>(FnASTScope)
98+
->preOrderChildrenDo([&](ast_scope::ASTScopeImpl *ASTScope) {
99+
if (!ASTScope->ignoreInDebugInfo()) {
100+
Consumer consumer(ASTScope, VarDeclScopeMap);
101+
ASTScope->lookupLocalsOrMembers(consumer);
102+
}
103+
});
62104
}
63105
}
64106

@@ -202,8 +244,6 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
202244
SourceLoc SLoc = Loc.getSourceLoc();
203245
if (!SF || LastSourceLoc == SLoc)
204246
return nullptr;
205-
// Prime VarDeclScopeMap.
206-
auto Scope = getOrCreateScope(SLoc);
207247
if (ForMetaInstruction)
208248
if (ValueDecl *ValDecl = Loc.getAsASTNode<ValueDecl>()) {
209249
// The source location of a VarDecl isn't necessarily in the same scope
@@ -212,7 +252,7 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
212252
if (ValueScope != VarDeclScopeMap.end())
213253
return getOrCreateScope(ValueScope->second, F.getDebugScope());
214254
}
215-
return Scope;
255+
return getOrCreateScope(SLoc);
216256
}
217257

218258
const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
@@ -406,32 +446,6 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
406446
return ParentScope->InlinedCallSite != InlinedAt ? FnScope : ParentScope;
407447
}
408448

409-
// Collect all variable declarations in this scope.
410-
struct Consumer : public namelookup::AbstractASTScopeDeclConsumer {
411-
const ast_scope::ASTScopeImpl *ASTScope;
412-
VarDeclScopeMapTy &VarDeclScopeMap;
413-
Consumer(const ast_scope::ASTScopeImpl *ASTScope,
414-
VarDeclScopeMapTy &VarDeclScopeMap)
415-
: ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
416-
417-
bool consume(ArrayRef<ValueDecl *> values,
418-
NullablePtr<DeclContext> baseDC) override {
419-
for (auto &value : values) {
420-
assert(VarDeclScopeMap.count(value) == 0 && "VarDecl appears twice");
421-
VarDeclScopeMap.insert({value, ASTScope});
422-
}
423-
return false;
424-
}
425-
bool lookInMembers(const DeclContext *) const override { return false; }
426-
#ifndef NDEBUG
427-
void startingNextLookupStep() override {}
428-
void finishingLookup(std::string) const override {}
429-
bool isTargetLookup() const override { return false; }
430-
#endif
431-
};
432-
Consumer consumer(ASTScope, VarDeclScopeMap);
433-
ASTScope->lookupLocalsOrMembers(consumer);
434-
435449
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
436450
if (auto Parent = ASTScope->getParent().getPtrOrNull())
437451
if (Parent->getSourceRangeOfThisASTNode() ==

test/DebugInfo/case-scope2.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ func consume<T>(_ t: T) {}
99
public func f(_ s1: Set<Int>?, _ s2: Set<Int>?) {
1010
switch (s1, s2) {
1111
case (nil, let a), (let a, nil):
12-
// CHECK: debug_value {{.*}} $Optional<Set<Int>>, let, name "a", {{.*}}:[[@LINE-1]]:18, scope [[S1]]
12+
// CHECK: debug_value {{.*}} $Optional<Set<Int>>, let, name "a", {{.*}}:11:18, scope [[S2]]
1313
consume(a)
1414
case (let a?, _):
15-
// CHECK: debug_value {{.*}} $Set<Int>, let, name "a", {{.*}}:[[@LINE-1]]:13, scope [[S3]]
15+
// CHECK: debug_value {{.*}} $Set<Int>, let, name "a", {{.*}}:14:13, scope [[S3]]
1616
consume((a))
1717
}
1818
}

test/DebugInfo/case-scope3.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-swift-frontend -module-name a -parse-as-library -emit-sil -g %s | %FileCheck %s
2+
3+
enum E {
4+
case integerValue(Int?)
5+
}
6+
7+
func getE() -> E? { return .integerValue(0) }
8+
9+
func f() -> Int {
10+
if case .integerValue(let nextValue) = getE(), let nextValue = nextValue {
11+
// CHECK: sil_scope [[F:[0-9]+]] { loc "{{.*}}":9:6 parent @$s1a1fSiyF
12+
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":10:5 parent [[F]] }
13+
// CHECK: sil_scope [[S1:[0-9]+]] { loc "{{.*}}":10:44 parent [[S0]] }
14+
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":10:44 parent [[S0]] }
15+
// CHECK: sil_scope [[S3:[0-9]+]] { loc "{{.*}}":10:68 parent [[S2]] }
16+
// CHECK: sil_scope [[S4:[0-9]+]] { loc "{{.*}}":10:78 parent [[S3]] }
17+
// CHECK: debug_value {{.*}}: $Optional<Int>, let, name "nextValue", {{.*}}:10:31, scope [[S2]]
18+
// CHECK: debug_value {{.*}}: $Int, let, name "nextValue", {{.*}}:10:56, scope [[S3]]
19+
return nextValue
20+
}
21+
return 0
22+
}

test/DebugInfo/guard-let-scope.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func f(c: AnyObject??) {
1010
// CHECK: sil_scope [[S5:[0-9]+]] { {{.*}} parent [[S3]] }
1111
// CHECK: sil_scope [[S6:[0-9]+]] { loc "{{.*}}":7:3 parent [[S5]] }
1212
// CHECK: sil_scope [[S7:[0-9]+]] { loc "{{.*}}":7:17 parent [[S6]] }
13-
// CHECK: sil_scope [[S8:[0-9]+]] { loc "{{.*}}":7:28 parent [[S7]] }
13+
// CHECK: sil_scope [[S8:[0-9]+]] { loc "{{.*}}":7:17 parent [[S6]] }
14+
// CHECK: sil_scope [[S9:[0-9]+]] { loc "{{.*}}":7:28 parent [[S8]] }
15+
// CHECK: sil_scope [[S10:[0-9]+]] { loc "{{.*}}":7:28 parent [[S8]] }
1416
// CHECK: debug_value %{{.*}} : $Optional<Optional<AnyObject>>, let, name "x"{{.*}} scope [[S5]]
15-
// CHECK: debug_value %{{.*}} : $Optional<AnyObject>, let, name "x", {{.*}} scope [[S7]]
16-
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S8]]
17+
// CHECK: debug_value %{{.*}} : $Optional<AnyObject>, let, name "x", {{.*}} scope [[S8]]
18+
// CHECK: debug_value %{{.*}} : $AnyObject, let, name "x", {{.*}} scope [[S10]]
1719
fatalError()
1820
}
19-
// CHECK: function_ref {{.*3use.*}} scope [[S8]]
21+
// CHECK: function_ref {{.*3use.*}} scope [[S10]]
2022
use(x)
2123
}

test/DebugInfo/guard-let-scope2.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ public func f(x: String?) throws {
1616
s = SomeObject()
1717
return s != nil
1818
}
19-
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}} parent @{{.*}}1f
20-
// CHECK: sil_scope [[S2:[0-9]+]] { {{.*}} parent [[S1]] }
21-
// CHECK: sil_scope [[S3:[0-9]+]] { {{.*}} parent [[S1]] }
22-
// CHECK: sil_scope [[S4:[0-9]+]] { {{.*}} parent [[S2]] }
23-
// CHECK: alloc_stack {{.*}} $SomeObject, let, name "s", {{.*}} scope [[S4]]
19+
// CHECK: sil_scope [[S1:[0-9]+]] { {{.*}}:13:13 parent @{{.*}}1f
20+
// CHECK: sil_scope [[S2:[0-9]+]] { {{.*}}:14:7 parent [[S1]] }
21+
// CHECK: sil_scope [[S3:[0-9]+]] { {{.*}}:14:26 parent [[S1]] }
22+
// CHECK: sil_scope [[S4:[0-9]+]] { {{.*}}:25:3 parent [[S2]] }
23+
// CHECK: sil_scope [[S5:[0-9]+]] { {{.*}}:25:17 parent [[S4]] }
24+
// CHECK: alloc_stack {{.*}} $SomeObject, let, name "s", {{.*}} scope [[S5]]
2425
guard let s = s else {
2526
assert(false)
2627
return

test/DebugInfo/guard-let-scope3.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ public class S {
77
private var c = [Int : C?]()
88
public func f(_ i: Int) throws -> C {
99
guard let x = c[i], let x else {
10-
// CHECK: sil_scope [[P:[0-9]+]] { loc "{{.*}}":[[@LINE-1]]:5
11-
// CHECK: sil_scope [[X1:[0-9]+]] { loc "{{.*}}":[[@LINE-2]]:19 parent [[P]]
12-
// CHECK: sil_scope [[X2:[0-9]+]] { loc "{{.*}}":[[@LINE-3]]:29 parent [[X1]]
13-
// CHECK: sil_scope [[GUARD:[0-9]+]] { loc "{{.*}}":[[@LINE-4]]:36 parent [[P]]
10+
// CHECK: sil_scope [[P:[0-9]+]] { loc "{{.*}}":9:5
11+
// CHECK: sil_scope [[X1_RHS:[0-9]+]] { loc "{{.*}}":9:19 parent [[P]]
12+
// CHECK: sil_scope [[X1:[0-9]+]] { loc "{{.*}}":9:19 parent [[P]]
13+
// CHECK: sil_scope [[X2:[0-9]+]] { loc "{{.*}}":9:29 parent [[X1]]
14+
// CHECK: sil_scope [[GUARD:[0-9]+]] { loc "{{.*}}":9:36 parent [[P]]
1415
// CHECK: debug_value {{.*}} : $Optional<C>, let, name "x", {{.*}}, scope [[X1]]
1516
// CHECK: debug_value {{.*}} : $C, let, name "x", {{.*}}, scope [[X2]]
16-
// CHECK-NEXT: scope [[X2]]
17+
// FIXME: This source location is a little wild.
18+
// CHECK-NEXT: strong_retain{{.*}}:[[@LINE+4]]:12, scope [[X2]]
1719
throw MyError()
1820
// CHECK: function_ref {{.*}}MyError{{.*}}:[[@LINE-1]]:13, scope [[GUARD]]
1921
}

test/DebugInfo/guard-let-scope4.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-frontend -g -emit-sil %s -parse-as-library -module-name a | %FileCheck %s
2+
open class C {
3+
public func fun() {}
4+
5+
public func run() {
6+
{ [weak self] in
7+
guard let self else { fatalError("cannot happen") }
8+
// CHECK: sil_scope [[LAMBDA:[0-9]+]] { loc "{{.*}}":6:5
9+
// CHECK: sil_scope [[BODY:[0-9]+]] { loc "{{.*}}":6:19 parent [[LAMBDA]]
10+
// CHECK: sil_scope [[LET:[0-9]+]] { loc "{{.*}}":7:7 parent [[BODY]]
11+
// CHECK: sil_scope [[GUARD:[0-9]+]] { loc "{{.*}}":7:17 parent [[LET]]
12+
// CHECK: debug_value {{.*}} : $C, let, name "self", {{.*}}, scope [[GUARD]]
13+
// CHECK: function_ref {{.*}}3fun{{.*}}, scope [[GUARD]]
14+
// CHECK-NEXT: apply {{.*}}, scope [[GUARD]]
15+
self.fun()
16+
}()
17+
}
18+
}

test/DebugInfo/if-let-scope.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// RUN: %target-swift-frontend -g -emit-sil %s -parse-as-library -module-name a | %FileCheck %s
22
func use<T>(_ t: T) {}
33
public func f(value: String?) {
4-
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":[[@LINE-1]]:13
4+
// CHECK: sil_scope [[S0:[0-9]+]] { loc "{{.*}}":3:13
55
if let value, let value = Int(value) {
6-
// CHECK: sil_scope [[S1:[0-9]+]] { loc "{{.*}}":[[@LINE-1]]:10
7-
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":[[@LINE-2]]:29 parent [[S1]] }
6+
// CHECK: sil_scope [[S1:[0-9]+]] { loc "{{.*}}":5:3
7+
// CHECK: sil_scope [[S2:[0-9]+]] { loc "{{.*}}":5:10
8+
// CHECK: sil_scope [[S3:[0-9]+]] { loc "{{.*}}":5:29 parent [[S2]] }
9+
// CHECK: sil_scope [[S4:[0-9]+]] { loc "{{.*}}":5:29 parent [[S2]] }
10+
// CHECK: sil_scope [[S5:[0-9]+]] { loc "{{.*}}":5:40 parent [[S4]] }
811
// CHECK: debug_value {{.*}} : $Optional<String>, let, name "value", {{.*}}, scope [[S0]]
9-
// CHECK: debug_value {{.*}} : $String, let, name "value", {{.*}}, scope [[S1]]
10-
// CHECK: debug_value {{.*}} : $Int, let, name "value", {{.*}}, scope [[S2]]
12+
// CHECK: debug_value {{.*}} : $String, let, name "value", {{.*}}, scope [[S2]]
13+
// CHECK: debug_value {{.*}} : $Int, let, name "value", {{.*}}, scope [[S4]]
1114
use((value))
1215
}
1316
}

test/SILGen/switch-case-debug-descriptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ enum E {
99
func test1(_ e: E) {
1010
switch e { // SCOPE: sil_scope [[test1_switch:[0-9]+]] {{.*}}:[[@LINE]]:3
1111
case .one(let payload), .two(let payload): // SCOPE-NEXT: sil_scope [[test1_case1:[0-9]+]] {{.*}}:[[@LINE]]:3 parent [[test1_switch]]
12-
print(payload)
12+
print(payload) // SCOPE-NEXT: sil_scope {{.*}}:[[@LINE]]:5 parent [[test1_case1]]
1313
case .three(let payload): // SCOPE-NEXT: sil_scope [[test1_case2:[0-9]+]] {{.*}}:[[@LINE]]:3 parent [[test1_switch]]
1414
print(payload)
1515
}
@@ -43,7 +43,7 @@ func test4(_ e: E) {
4343
print(x) // SCOPE-NEXT: sil_scope {{.*}}:[[@LINE]]:5 parent [[test4_case1]]
4444
fallthrough
4545
case .two(let x): // SCOPE-NEXT: sil_scope [[test4_case2:[0-9]+]] {{.*}}:[[@LINE]]:3 parent [[test4_switch]]
46-
print(x)
46+
print(x) // SCOPE-NEXT: {{.*}}:[[@LINE]]:5 parent [[test4_case2]]
4747
fallthrough
4848
default: // SCOPE-NEXT: sil_scope [[test4_default:[0-9]+]] {{.*}}:[[@LINE]]:3 parent [[test4_switch]]
4949
print("default") // SCOPE: sil_scope [[test4_default1:[0-9]+]] {{.*}}:[[@LINE]]:5

0 commit comments

Comments
 (0)