Skip to content

Commit 59f8af3

Browse files
authored
[NFC][Clang] Adopt simplified getTrailingObjects in DeclFriend (#140081)
- Adopt non-templated `getTrailingObjects` in DeclFriend, DeclGroup, and DeclObjC - Use indexing into ArrayRef returned by `getTrailingObjects` and eliminate explicit OOB asserts.
1 parent f3d36b1 commit 59f8af3

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

clang/include/clang/AST/DeclFriend.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ class FriendDecl final
9090
: Decl(Decl::Friend, DC, L), Friend(Friend), FriendLoc(FriendL),
9191
EllipsisLoc(EllipsisLoc), UnsupportedFriend(false),
9292
NumTPLists(FriendTypeTPLists.size()) {
93-
for (unsigned i = 0; i < NumTPLists; ++i)
94-
getTrailingObjects<TemplateParameterList *>()[i] = FriendTypeTPLists[i];
93+
llvm::copy(FriendTypeTPLists, getTrailingObjects());
9594
}
9695

9796
FriendDecl(EmptyShell Empty, unsigned NumFriendTypeTPLists)
@@ -132,8 +131,7 @@ class FriendDecl final
132131
}
133132

134133
TemplateParameterList *getFriendTypeTemplateParameterList(unsigned N) const {
135-
assert(N < NumTPLists);
136-
return getTrailingObjects<TemplateParameterList *>()[N];
134+
return getTrailingObjects(NumTPLists)[N];
137135
}
138136

139137
/// If this friend declaration doesn't name a type, return the inner
@@ -153,10 +151,9 @@ class FriendDecl final
153151
/// Retrieves the source range for the friend declaration.
154152
SourceRange getSourceRange() const override LLVM_READONLY {
155153
if (TypeSourceInfo *TInfo = getFriendType()) {
156-
SourceLocation StartL =
157-
(NumTPLists == 0) ? getFriendLoc()
158-
: getTrailingObjects<TemplateParameterList *>()[0]
159-
->getTemplateLoc();
154+
SourceLocation StartL = (NumTPLists == 0)
155+
? getFriendLoc()
156+
: getTrailingObjects()[0]->getTemplateLoc();
160157
SourceLocation EndL = isPackExpansion() ? getEllipsisLoc()
161158
: TInfo->getTypeLoc().getEndLoc();
162159
return SourceRange(StartL, EndL);

clang/include/clang/AST/DeclGroup.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,10 @@ class DeclGroup final : private llvm::TrailingObjects<DeclGroup, Decl *> {
3737

3838
unsigned size() const { return NumDecls; }
3939

40-
Decl*& operator[](unsigned i) {
41-
assert (i < NumDecls && "Out-of-bounds access.");
42-
return getTrailingObjects<Decl *>()[i];
43-
}
40+
Decl *&operator[](unsigned i) { return getTrailingObjects(NumDecls)[i]; }
4441

4542
Decl* const& operator[](unsigned i) const {
46-
assert (i < NumDecls && "Out-of-bounds access.");
47-
return getTrailingObjects<Decl *>()[i];
43+
return getTrailingObjects(NumDecls)[i];
4844
}
4945
};
5046

clang/include/clang/AST/DeclObjC.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ class ObjCTypeParamList final
678678
/// Iterate through the type parameters in the list.
679679
using iterator = ObjCTypeParamDecl **;
680680

681-
iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
681+
iterator begin() { return getTrailingObjects(); }
682682

683683
iterator end() { return begin() + size(); }
684684

@@ -688,9 +688,7 @@ class ObjCTypeParamList final
688688
// Iterate through the type parameters in the list.
689689
using const_iterator = ObjCTypeParamDecl * const *;
690690

691-
const_iterator begin() const {
692-
return getTrailingObjects<ObjCTypeParamDecl *>();
693-
}
691+
const_iterator begin() const { return getTrailingObjects(); }
694692

695693
const_iterator end() const {
696694
return begin() + size();

clang/lib/AST/DeclGroup.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ DeclGroup* DeclGroup::Create(ASTContext &C, Decl **Decls, unsigned NumDecls) {
2828
DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
2929
assert(numdecls > 0);
3030
assert(decls);
31-
std::uninitialized_copy(decls, decls + numdecls,
32-
getTrailingObjects<Decl *>());
31+
std::uninitialized_copy(decls, decls + numdecls, getTrailingObjects());
3332
}

0 commit comments

Comments
 (0)