@@ -64,10 +64,12 @@ unsigned findIndexInRange(Decl *D, const Range &Decls) {
64
64
65
65
// / Return the element at \p N in \p Decls .
66
66
template <typename Range> Decl *getElementAt (const Range &Decls, unsigned N) {
67
- assert (std::distance (Decls.begin (), Decls.end ()) > N);
68
- auto I = Decls.begin ();
69
- std::advance (I, N);
70
- return *I;
67
+ for (auto I = Decls.begin (), E = Decls.end (); I != E; ++I) {
68
+ if (N == 0 )
69
+ return *I;
70
+ --N;
71
+ }
72
+ return nullptr ;
71
73
}
72
74
73
75
// / Find the equivalent \c DeclContext with \p DC from \p SF AST.
@@ -85,6 +87,8 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
85
87
SmallVector<unsigned , 4 > IndexStack;
86
88
do {
87
89
auto *D = newDC->getAsDecl ();
90
+ if (!D)
91
+ return nullptr ;
88
92
auto *parentDC = newDC->getParent ();
89
93
unsigned N;
90
94
@@ -93,10 +97,12 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
93
97
// DeclContext -> AbstractStorageDecl -> AccessorDecl
94
98
// We need to push the index of the accessor within the accessor list
95
99
// of the storage.
96
- auto accessorN =
97
- findIndexInRange (accessor, accessor->getStorage ()->getAllAccessors ());
100
+ auto *storage = accessor->getStorage ();
101
+ if (!storage)
102
+ return nullptr ;
103
+ auto accessorN = findIndexInRange (accessor, storage->getAllAccessors ());
98
104
IndexStack.push_back (accessorN);
99
- D = accessor-> getStorage () ;
105
+ D = storage ;
100
106
}
101
107
102
108
if (auto parentSF = dyn_cast<SourceFile>(parentDC))
@@ -137,7 +143,9 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
137
143
D = getElementAt (storage->getAllAccessors (), accessorN);
138
144
}
139
145
140
- newDC = cast<DeclContext>(D);
146
+ newDC = dyn_cast<DeclContext>(D);
147
+ if (!newDC)
148
+ return nullptr ;
141
149
} while (!IndexStack.empty ());
142
150
143
151
assert (newDC->getContextKind () == DC->getContextKind ());
0 commit comments