Skip to content

Commit cec764c

Browse files
committed
[NFC][Clang] Adopt simplified getTrailingObjects in Expr.cpp/h
Adopt non-templated and array-ref returning forms of `getTrailingObjects` in Expr.cpp/.h. Use ArrayRef forms to eliminate manual asserting for OOB index. Use llvm::copy() instead of std::copy() in some instances.
1 parent 5f53ca3 commit cec764c

File tree

3 files changed

+67
-100
lines changed

3 files changed

+67
-100
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ class PredefinedExpr final
20232023
void setFunctionName(StringLiteral *SL) {
20242024
assert(hasFunctionName() &&
20252025
"This PredefinedExpr has no storage for a function name!");
2026-
*getTrailingObjects<Stmt *>() = SL;
2026+
*getTrailingObjects() = SL;
20272027
}
20282028

20292029
public:
@@ -2050,13 +2050,13 @@ class PredefinedExpr final
20502050

20512051
StringLiteral *getFunctionName() {
20522052
return hasFunctionName()
2053-
? static_cast<StringLiteral *>(*getTrailingObjects<Stmt *>())
2053+
? static_cast<StringLiteral *>(*getTrailingObjects())
20542054
: nullptr;
20552055
}
20562056

20572057
const StringLiteral *getFunctionName() const {
20582058
return hasFunctionName()
2059-
? static_cast<StringLiteral *>(*getTrailingObjects<Stmt *>())
2059+
? static_cast<StringLiteral *>(*getTrailingObjects())
20602060
: nullptr;
20612061
}
20622062

@@ -2078,13 +2078,11 @@ class PredefinedExpr final
20782078

20792079
// Iterators
20802080
child_range children() {
2081-
return child_range(getTrailingObjects<Stmt *>(),
2082-
getTrailingObjects<Stmt *>() + hasFunctionName());
2081+
return child_range(getTrailingObjects(hasFunctionName()));
20832082
}
20842083

20852084
const_child_range children() const {
2086-
return const_child_range(getTrailingObjects<Stmt *>(),
2087-
getTrailingObjects<Stmt *>() + hasFunctionName());
2085+
return const_child_range(getTrailingObjects(hasFunctionName()));
20882086
}
20892087
};
20902088

@@ -2248,18 +2246,14 @@ class UnaryOperator final
22482246
private llvm::TrailingObjects<UnaryOperator, FPOptionsOverride> {
22492247
Stmt *Val;
22502248

2251-
size_t numTrailingObjects(OverloadToken<FPOptionsOverride>) const {
2252-
return UnaryOperatorBits.HasFPFeatures ? 1 : 0;
2253-
}
2254-
22552249
FPOptionsOverride &getTrailingFPFeatures() {
22562250
assert(UnaryOperatorBits.HasFPFeatures);
2257-
return *getTrailingObjects<FPOptionsOverride>();
2251+
return *getTrailingObjects();
22582252
}
22592253

22602254
const FPOptionsOverride &getTrailingFPFeatures() const {
22612255
assert(UnaryOperatorBits.HasFPFeatures);
2262-
return *getTrailingObjects<FPOptionsOverride>();
2256+
return *getTrailingObjects();
22632257
}
22642258

22652259
public:
@@ -2580,32 +2574,27 @@ class OffsetOfExpr final
25802574
}
25812575

25822576
const OffsetOfNode &getComponent(unsigned Idx) const {
2583-
assert(Idx < NumComps && "Subscript out of range");
2584-
return getTrailingObjects<OffsetOfNode>()[Idx];
2577+
return getTrailingObjects<OffsetOfNode>(NumComps)[Idx];
25852578
}
25862579

25872580
void setComponent(unsigned Idx, OffsetOfNode ON) {
2588-
assert(Idx < NumComps && "Subscript out of range");
2589-
getTrailingObjects<OffsetOfNode>()[Idx] = ON;
2581+
getTrailingObjects<OffsetOfNode>(NumComps)[Idx] = ON;
25902582
}
25912583

25922584
unsigned getNumComponents() const {
25932585
return NumComps;
25942586
}
25952587

25962588
Expr* getIndexExpr(unsigned Idx) {
2597-
assert(Idx < NumExprs && "Subscript out of range");
2598-
return getTrailingObjects<Expr *>()[Idx];
2589+
return getTrailingObjects<Expr *>(NumExprs)[Idx];
25992590
}
26002591

26012592
const Expr *getIndexExpr(unsigned Idx) const {
2602-
assert(Idx < NumExprs && "Subscript out of range");
2603-
return getTrailingObjects<Expr *>()[Idx];
2593+
return getTrailingObjects<Expr *>(NumExprs)[Idx];
26042594
}
26052595

26062596
void setIndexExpr(unsigned Idx, Expr* E) {
2607-
assert(Idx < NumComps && "Subscript out of range");
2608-
getTrailingObjects<Expr *>()[Idx] = E;
2597+
getTrailingObjects<Expr *>(NumComps)[Idx] = E;
26092598
}
26102599

26112600
unsigned getNumExpressions() const {
@@ -4619,12 +4608,12 @@ class ConvertVectorExpr final
46194608

46204609
FPOptionsOverride &getTrailingFPFeatures() {
46214610
assert(ConvertVectorExprBits.HasFPFeatures);
4622-
return *getTrailingObjects<FPOptionsOverride>();
4611+
return *getTrailingObjects();
46234612
}
46244613

46254614
const FPOptionsOverride &getTrailingFPFeatures() const {
46264615
assert(ConvertVectorExprBits.HasFPFeatures);
4627-
return *getTrailingObjects<FPOptionsOverride>();
4616+
return *getTrailingObjects();
46284617
}
46294618

46304619
public:
@@ -5705,13 +5694,11 @@ class DesignatedInitExpr final
57055694
unsigned getNumSubExprs() const { return NumSubExprs; }
57065695

57075696
Expr *getSubExpr(unsigned Idx) const {
5708-
assert(Idx < NumSubExprs && "Subscript out of range");
5709-
return cast<Expr>(getTrailingObjects<Stmt *>()[Idx]);
5697+
return cast<Expr>(getTrailingObjects(NumSubExprs)[Idx]);
57105698
}
57115699

57125700
void setSubExpr(unsigned Idx, Expr *E) {
5713-
assert(Idx < NumSubExprs && "Subscript out of range");
5714-
getTrailingObjects<Stmt *>()[Idx] = E;
5701+
getTrailingObjects(NumSubExprs)[Idx] = E;
57155702
}
57165703

57175704
/// Replaces the designator at index @p Idx with the series
@@ -5730,11 +5717,11 @@ class DesignatedInitExpr final
57305717

57315718
// Iterators
57325719
child_range children() {
5733-
Stmt **begin = getTrailingObjects<Stmt *>();
5720+
Stmt **begin = getTrailingObjects();
57345721
return child_range(begin, begin + NumSubExprs);
57355722
}
57365723
const_child_range children() const {
5737-
Stmt * const *begin = getTrailingObjects<Stmt *>();
5724+
Stmt *const *begin = getTrailingObjects();
57385725
return const_child_range(begin, begin + NumSubExprs);
57395726
}
57405727

@@ -5994,9 +5981,7 @@ class ParenListExpr final
59945981
return const_cast<ParenListExpr *>(this)->getExpr(Init);
59955982
}
59965983

5997-
Expr **getExprs() {
5998-
return reinterpret_cast<Expr **>(getTrailingObjects<Stmt *>());
5999-
}
5984+
Expr **getExprs() { return reinterpret_cast<Expr **>(getTrailingObjects()); }
60005985

60015986
ArrayRef<Expr *> exprs() { return llvm::ArrayRef(getExprs(), getNumExprs()); }
60025987

@@ -6011,12 +5996,10 @@ class ParenListExpr final
60115996

60125997
// Iterators
60135998
child_range children() {
6014-
return child_range(getTrailingObjects<Stmt *>(),
6015-
getTrailingObjects<Stmt *>() + getNumExprs());
5999+
return child_range(getTrailingObjects(getNumExprs()));
60166000
}
60176001
const_child_range children() const {
6018-
return const_child_range(getTrailingObjects<Stmt *>(),
6019-
getTrailingObjects<Stmt *>() + getNumExprs());
6002+
return const_child_range(getTrailingObjects(getNumExprs()));
60206003
}
60216004
};
60226005

@@ -6421,14 +6404,12 @@ class GenericSelectionExpr final
64216404
}
64226405

64236406
child_range children() {
6424-
return child_range(getTrailingObjects<Stmt *>(),
6425-
getTrailingObjects<Stmt *>() +
6426-
numTrailingObjects(OverloadToken<Stmt *>()));
6407+
return child_range(getTrailingObjects<Stmt *>(
6408+
numTrailingObjects(OverloadToken<Stmt *>())));
64276409
}
64286410
const_child_range children() const {
6429-
return const_child_range(getTrailingObjects<Stmt *>(),
6430-
getTrailingObjects<Stmt *>() +
6431-
numTrailingObjects(OverloadToken<Stmt *>()));
6411+
return const_child_range(getTrailingObjects<Stmt *>(
6412+
numTrailingObjects(OverloadToken<Stmt *>())));
64326413
}
64336414
};
64346415

@@ -6647,11 +6628,6 @@ class PseudoObjectExpr final
66476628
// in to Create, which is an index within the semantic forms.
66486629
// Note also that ASTStmtWriter assumes this encoding.
66496630

6650-
Expr **getSubExprsBuffer() { return getTrailingObjects<Expr *>(); }
6651-
const Expr * const *getSubExprsBuffer() const {
6652-
return getTrailingObjects<Expr *>();
6653-
}
6654-
66556631
PseudoObjectExpr(QualType type, ExprValueKind VK,
66566632
Expr *syntactic, ArrayRef<Expr*> semantic,
66576633
unsigned resultIndex);
@@ -6677,8 +6653,8 @@ class PseudoObjectExpr final
66776653
/// Return the syntactic form of this expression, i.e. the
66786654
/// expression it actually looks like. Likely to be expressed in
66796655
/// terms of OpaqueValueExprs bound in the semantic form.
6680-
Expr *getSyntacticForm() { return getSubExprsBuffer()[0]; }
6681-
const Expr *getSyntacticForm() const { return getSubExprsBuffer()[0]; }
6656+
Expr *getSyntacticForm() { return getTrailingObjects()[0]; }
6657+
const Expr *getSyntacticForm() const { return getTrailingObjects()[0]; }
66826658

66836659
/// Return the index of the result-bearing expression into the semantics
66846660
/// expressions, or PseudoObjectExpr::NoResult if there is none.
@@ -6691,7 +6667,7 @@ class PseudoObjectExpr final
66916667
Expr *getResultExpr() {
66926668
if (PseudoObjectExprBits.ResultIndex == 0)
66936669
return nullptr;
6694-
return getSubExprsBuffer()[PseudoObjectExprBits.ResultIndex];
6670+
return getTrailingObjects()[PseudoObjectExprBits.ResultIndex];
66956671
}
66966672
const Expr *getResultExpr() const {
66976673
return const_cast<PseudoObjectExpr*>(this)->getResultExpr();
@@ -6701,29 +6677,26 @@ class PseudoObjectExpr final
67016677

67026678
typedef Expr * const *semantics_iterator;
67036679
typedef const Expr * const *const_semantics_iterator;
6704-
semantics_iterator semantics_begin() {
6705-
return getSubExprsBuffer() + 1;
6706-
}
6680+
semantics_iterator semantics_begin() { return getTrailingObjects() + 1; }
67076681
const_semantics_iterator semantics_begin() const {
6708-
return getSubExprsBuffer() + 1;
6682+
return getTrailingObjects() + 1;
67096683
}
67106684
semantics_iterator semantics_end() {
6711-
return getSubExprsBuffer() + getNumSubExprs();
6685+
return getTrailingObjects() + getNumSubExprs();
67126686
}
67136687
const_semantics_iterator semantics_end() const {
6714-
return getSubExprsBuffer() + getNumSubExprs();
6688+
return getTrailingObjects() + getNumSubExprs();
67156689
}
67166690

67176691
ArrayRef<Expr*> semantics() {
6718-
return ArrayRef(semantics_begin(), semantics_end());
6692+
return getTrailingObjects(getNumSubExprs()).drop_front();
67196693
}
67206694
ArrayRef<const Expr*> semantics() const {
6721-
return ArrayRef(semantics_begin(), semantics_end());
6695+
return getTrailingObjects(getNumSubExprs()).drop_front();
67226696
}
67236697

67246698
Expr *getSemanticExpr(unsigned index) {
6725-
assert(index + 1 < getNumSubExprs());
6726-
return getSubExprsBuffer()[index + 1];
6699+
return getTrailingObjects(getNumSubExprs())[index + 1];
67276700
}
67286701
const Expr *getSemanticExpr(unsigned index) const {
67296702
return const_cast<PseudoObjectExpr*>(this)->getSemanticExpr(index);
@@ -6748,7 +6721,7 @@ class PseudoObjectExpr final
67486721
}
67496722
const_child_range children() const {
67506723
Stmt *const *cs = const_cast<Stmt *const *>(
6751-
reinterpret_cast<const Stmt *const *>(getSubExprsBuffer()));
6724+
reinterpret_cast<const Stmt *const *>(getTrailingObjects()));
67526725
return const_child_range(cs, cs + getNumSubExprs());
67536726
}
67546727

clang/lib/AST/Expr.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4449,11 +4449,10 @@ GenericSelectionExpr::GenericSelectionExpr(
44494449
GenericSelectionExprBits.GenericLoc = GenericLoc;
44504450
getTrailingObjects<Stmt *>()[getIndexOfControllingExpression()] =
44514451
ControllingExpr;
4452-
std::copy(AssocExprs.begin(), AssocExprs.end(),
4453-
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4454-
std::copy(AssocTypes.begin(), AssocTypes.end(),
4455-
getTrailingObjects<TypeSourceInfo *>() +
4456-
getIndexOfStartOfAssociatedTypes());
4452+
llvm::copy(AssocExprs,
4453+
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4454+
llvm::copy(AssocTypes, getTrailingObjects<TypeSourceInfo *>() +
4455+
getIndexOfStartOfAssociatedTypes());
44574456

44584457
setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
44594458
}
@@ -4477,11 +4476,10 @@ GenericSelectionExpr::GenericSelectionExpr(
44774476
GenericSelectionExprBits.GenericLoc = GenericLoc;
44784477
getTrailingObjects<TypeSourceInfo *>()[getIndexOfControllingType()] =
44794478
ControllingType;
4480-
std::copy(AssocExprs.begin(), AssocExprs.end(),
4481-
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4482-
std::copy(AssocTypes.begin(), AssocTypes.end(),
4483-
getTrailingObjects<TypeSourceInfo *>() +
4484-
getIndexOfStartOfAssociatedTypes());
4479+
llvm::copy(AssocExprs,
4480+
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4481+
llvm::copy(AssocTypes, getTrailingObjects<TypeSourceInfo *>() +
4482+
getIndexOfStartOfAssociatedTypes());
44854483

44864484
setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
44874485
}
@@ -4502,11 +4500,10 @@ GenericSelectionExpr::GenericSelectionExpr(
45024500
GenericSelectionExprBits.GenericLoc = GenericLoc;
45034501
getTrailingObjects<Stmt *>()[getIndexOfControllingExpression()] =
45044502
ControllingExpr;
4505-
std::copy(AssocExprs.begin(), AssocExprs.end(),
4506-
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4507-
std::copy(AssocTypes.begin(), AssocTypes.end(),
4508-
getTrailingObjects<TypeSourceInfo *>() +
4509-
getIndexOfStartOfAssociatedTypes());
4503+
llvm::copy(AssocExprs,
4504+
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4505+
llvm::copy(AssocTypes, getTrailingObjects<TypeSourceInfo *>() +
4506+
getIndexOfStartOfAssociatedTypes());
45104507

45114508
setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
45124509
}
@@ -4527,11 +4524,10 @@ GenericSelectionExpr::GenericSelectionExpr(
45274524
GenericSelectionExprBits.GenericLoc = GenericLoc;
45284525
getTrailingObjects<TypeSourceInfo *>()[getIndexOfControllingType()] =
45294526
ControllingType;
4530-
std::copy(AssocExprs.begin(), AssocExprs.end(),
4531-
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4532-
std::copy(AssocTypes.begin(), AssocTypes.end(),
4533-
getTrailingObjects<TypeSourceInfo *>() +
4534-
getIndexOfStartOfAssociatedTypes());
4527+
llvm::copy(AssocExprs,
4528+
getTrailingObjects<Stmt *>() + getIndexOfStartOfAssociatedExprs());
4529+
llvm::copy(AssocTypes, getTrailingObjects<TypeSourceInfo *>() +
4530+
getIndexOfStartOfAssociatedTypes());
45354531

45364532
setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
45374533
}
@@ -4780,9 +4776,7 @@ ParenListExpr::ParenListExpr(SourceLocation LParenLoc, ArrayRef<Expr *> Exprs,
47804776
: Expr(ParenListExprClass, QualType(), VK_PRValue, OK_Ordinary),
47814777
LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
47824778
ParenListExprBits.NumExprs = Exprs.size();
4783-
4784-
for (unsigned I = 0, N = Exprs.size(); I != N; ++I)
4785-
getTrailingObjects<Stmt *>()[I] = Exprs[I];
4779+
llvm::copy(Exprs, getTrailingObjects());
47864780
setDependence(computeDependence(this));
47874781
}
47884782

@@ -5043,16 +5037,18 @@ PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
50435037
: Expr(PseudoObjectExprClass, type, VK, OK_Ordinary) {
50445038
PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
50455039
PseudoObjectExprBits.ResultIndex = resultIndex + 1;
5046-
5047-
for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
5048-
Expr *E = (i == 0 ? syntax : semantics[i-1]);
5049-
getSubExprsBuffer()[i] = E;
5050-
5051-
if (isa<OpaqueValueExpr>(E))
5052-
assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr &&
5040+
MutableArrayRef<Expr *> Trail = getTrailingObjects(semantics.size() + 1);
5041+
Trail[0] = syntax;
5042+
llvm::copy(semantics, Trail.drop_front().begin());
5043+
5044+
#ifndef NDEBUG
5045+
llvm::for_each(semantics, [](const Expr *E) {
5046+
if (auto *OE = dyn_cast<OpaqueValueExpr>(E))
5047+
assert(OE->getSourceExpr() != nullptr &&
50535048
"opaque-value semantic expressions for pseudo-object "
50545049
"operations must have sources");
5055-
}
5050+
});
5051+
#endif
50565052

50575053
setDependence(computeDependence(this));
50585054
}
@@ -5240,7 +5236,7 @@ RecoveryExpr::RecoveryExpr(ASTContext &Ctx, QualType T, SourceLocation BeginLoc,
52405236
assert(!T.isNull());
52415237
assert(!llvm::is_contained(SubExprs, nullptr));
52425238

5243-
llvm::copy(SubExprs, getTrailingObjects<Expr *>());
5239+
llvm::copy(SubExprs, getTrailingObjects());
52445240
setDependence(computeDependence(this));
52455241
}
52465242

@@ -5307,9 +5303,7 @@ OMPArrayShapingExpr *OMPArrayShapingExpr::CreateEmpty(const ASTContext &Context,
53075303
}
53085304

53095305
void OMPIteratorExpr::setIteratorDeclaration(unsigned I, Decl *D) {
5310-
assert(I < NumIterators &&
5311-
"Idx is greater or equal the number of iterators definitions.");
5312-
getTrailingObjects<Decl *>()[I] = D;
5306+
getTrailingObjects<Decl *>(NumIterators)[I] = D;
53135307
}
53145308

53155309
void OMPIteratorExpr::setAssignmentLoc(unsigned I, SourceLocation Loc) {

0 commit comments

Comments
 (0)