Skip to content

Commit 7ace136

Browse files
committed
ASTWalker support for custom attributes on properties.
When we encounter a custom attributes on a property (via a pattern binding), visit those custom attributes. Provides basic indexing support.
1 parent 56d450b commit 7ace136

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

lib/AST/ASTWalker.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,27 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
121121
bool visit(ParameterList *PL) {
122122
return inherited::visit(PL);
123123
}
124-
124+
125+
//===--------------------------------------------------------------------===//
126+
// Attributes
127+
//===--------------------------------------------------------------------===//
128+
bool visitCustomAttributes(Decl *D) {
129+
for (auto *customAttr : D->getAttrs().getAttributes<CustomAttr>()) {
130+
CustomAttr *mutableCustomAttr = const_cast<CustomAttr *>(customAttr);
131+
if (doIt(mutableCustomAttr->getTypeLoc()))
132+
return true;
133+
134+
if (auto arg = customAttr->getArg()) {
135+
if (auto newArg = doIt(arg))
136+
mutableCustomAttr->setArg(newArg);
137+
else
138+
return true;
139+
}
140+
}
141+
142+
return false;
143+
}
144+
125145
//===--------------------------------------------------------------------===//
126146
// Decls
127147
//===--------------------------------------------------------------------===//
@@ -151,6 +171,16 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
151171
}
152172

153173
bool visitPatternBindingDecl(PatternBindingDecl *PBD) {
174+
// If there is a single variable, walk it's attributes.
175+
bool isPropertyDelegateBackingProperty = false;
176+
if (auto singleVar = PBD->getSingleVar()) {
177+
if (visitCustomAttributes(singleVar))
178+
return true;
179+
180+
isPropertyDelegateBackingProperty =
181+
singleVar->getOriginalDelegatedProperty() != nullptr;
182+
}
183+
154184
unsigned idx = 0U-1;
155185
for (auto entry : PBD->getPatternList()) {
156186
++idx;
@@ -159,6 +189,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
159189
else
160190
return true;
161191
if (entry.getInit() &&
192+
!isPropertyDelegateBackingProperty &&
162193
(!entry.isInitializerSubsumed() ||
163194
Walker.shouldWalkIntoLazyInitializers())) {
164195
#ifndef NDEBUG

test/Index/property_delegates.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck -check-prefix=CHECK %s
2+
3+
@propertyDelegate
4+
public struct Wrapper<T> {
5+
public var value: T
6+
7+
public init(initialValue: T) {
8+
self.value = initialValue
9+
}
10+
11+
public init(body: () -> T) {
12+
self.value = body()
13+
}
14+
}
15+
16+
var globalInt: Int { return 17 }
17+
18+
public struct HasDelegates {
19+
@Wrapper
20+
public var x: Int = globalInt
21+
22+
@Wrapper(body: { globalInt })
23+
public var y: Int
24+
25+
@Wrapper(body: { return globalInt })
26+
public var z: Int
27+
}
28+
29+
func useMemberwiseInits(i: Int) {
30+
_ = HasDelegates(x: i)
31+
_ = HasDelegates(y: Wrapper(initialValue: i))
32+
}
33+
34+
// CHECK: 19:4 | struct/Swift | Wrapper | s:14swift_ide_test7WrapperV | Ref | rel: 0
35+
// CHECK: 20:14 | instance-property/Swift | x | s:14swift_ide_test12HasDelegatesV1xSivp | Def,RelChild | rel: 1
36+
// CHECK: 20:23 | variable/Swift | globalInt | s:14swift_ide_test9globalIntSivp | Ref,Read | rel: 0
37+
38+
// CHECK: 22:4 | struct/Swift | Wrapper | s:14swift_ide_test7WrapperV | Ref | rel: 0
39+
// CHECK: 22:20 | variable/Swift | globalInt | s:14swift_ide_test9globalIntSivp | Ref,Read | rel: 0
40+
41+
// CHECK: 25:4 | struct/Swift | Wrapper | s:14swift_ide_test7WrapperV | Ref | rel: 0
42+
// CHECK: 25:27 | variable/Swift | globalInt | s:14swift_ide_test9globalIntSivp | Ref,Read | rel: 0

0 commit comments

Comments
 (0)