Skip to content

Commit bbb0dba

Browse files
authored
[clang][AST] Add RecordDecl::getNumFields() (#170022)
Not sure why that didn't exist yet, but we have quite a few places using the same `std::distance` pattern.
1 parent dc5ce79 commit bbb0dba

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4524,6 +4524,11 @@ class RecordDecl : public TagDecl {
45244524
return field_begin() == field_end();
45254525
}
45264526

4527+
/// Returns the number of fields (non-static data members) in this record.
4528+
unsigned getNumFields() const {
4529+
return std::distance(field_begin(), field_end());
4530+
}
4531+
45274532
/// noload_fields - Iterate over the fields stored in this record
45284533
/// that are currently loaded; don't attempt to retrieve anything
45294534
/// from an external source.

clang/lib/AST/ComparisonCategories.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool ComparisonCategoryInfo::ValueInfo::hasValidIntValue() const {
4949
// Before we attempt to get the value of the first field, ensure that we
5050
// actually have one (and only one) field.
5151
const auto *Record = VD->getType()->getAsCXXRecordDecl();
52-
if (std::distance(Record->field_begin(), Record->field_end()) != 1 ||
52+
if (Record->getNumFields() != 1 ||
5353
!Record->field_begin()->getType()->isIntegralOrEnumerationType())
5454
return false;
5555

clang/lib/AST/ExprConstant.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,8 +3971,7 @@ static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
39713971
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
39723972
NumBases = CXXRD->getNumBases();
39733973

3974-
*Res = APValue(APValue::UninitStruct(), NumBases,
3975-
std::distance(RD->field_begin(), RD->field_end()));
3974+
*Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
39763975

39773976
SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
39783977
// we need to traverse backwards
@@ -5529,8 +5528,8 @@ static bool handleDefaultInitValue(QualType T, APValue &Result) {
55295528
Result = APValue((const FieldDecl *)nullptr);
55305529
return true;
55315530
}
5532-
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
5533-
std::distance(RD->field_begin(), RD->field_end()));
5531+
Result =
5532+
APValue(APValue::UninitStruct(), RD->getNumBases(), RD->getNumFields());
55345533

55355534
unsigned Index = 0;
55365535
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
@@ -7184,7 +7183,7 @@ static bool HandleConstructorCall(const Expr *E, const LValue &This,
71847183
if (!Result.hasValue()) {
71857184
if (!RD->isUnion())
71867185
Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
7187-
std::distance(RD->field_begin(), RD->field_end()));
7186+
RD->getNumFields());
71887187
else
71897188
// A union starts with no active member.
71907189
Result = APValue((const FieldDecl*)nullptr);
@@ -8135,8 +8134,7 @@ class BufferToAPValueConverter {
81358134
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
81368135
NumBases = CXXRD->getNumBases();
81378136

8138-
APValue ResultVal(APValue::UninitStruct(), NumBases,
8139-
std::distance(RD->field_begin(), RD->field_end()));
8137+
APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
81408138

81418139
// Visit the base classes.
81428140
if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
@@ -11146,7 +11144,7 @@ static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
1114611144
assert(!RD->isUnion() && "Expected non-union class type");
1114711145
const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
1114811146
Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
11149-
std::distance(RD->field_begin(), RD->field_end()));
11147+
RD->getNumFields());
1115011148

1115111149
if (RD->isInvalidDecl()) return false;
1115211150
const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
@@ -11342,7 +11340,7 @@ bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
1134211340

1134311341
if (!Result.hasValue())
1134411342
Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11345-
std::distance(RD->field_begin(), RD->field_end()));
11343+
RD->getNumFields());
1134611344
unsigned ElementNo = 0;
1134711345
bool Success = true;
1134811346

@@ -11549,8 +11547,7 @@ bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
1154911547
if (ClosureClass->isInvalidDecl())
1155011548
return false;
1155111549

11552-
const size_t NumFields =
11553-
std::distance(ClosureClass->field_begin(), ClosureClass->field_end());
11550+
const size_t NumFields = ClosureClass->getNumFields();
1155411551

1155511552
assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
1155611553
E->capture_init_end()) &&

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,7 @@ CGHLSLRuntime::handleStructSemanticLoad(
816816
const llvm::StructType *ST = cast<StructType>(Type);
817817
const clang::RecordDecl *RD = Decl->getType()->getAsRecordDecl();
818818

819-
assert(std::distance(RD->field_begin(), RD->field_end()) ==
820-
ST->getNumElements());
819+
assert(RD->getNumFields() == ST->getNumElements());
821820

822821
llvm::Value *Aggregate = llvm::PoisonValue::get(Type);
823822
auto FieldDecl = RD->field_begin();
@@ -849,8 +848,7 @@ CGHLSLRuntime::handleStructSemanticStore(
849848
RD = Decl->getType()->getAsRecordDecl();
850849
assert(RD);
851850

852-
assert(std::distance(RD->field_begin(), RD->field_end()) ==
853-
ST->getNumElements());
851+
assert(RD->getNumFields() == ST->getNumElements());
854852

855853
auto FieldDecl = RD->field_begin();
856854
for (unsigned I = 0; I < ST->getNumElements(); ++I) {

clang/lib/Sema/CodeCompleteConsumer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@ unsigned CodeCompleteConsumer::OverloadCandidate::getNumParams() const {
539539
return Template->getTemplateParameters()->size();
540540

541541
if (Kind == CK_Aggregate) {
542-
unsigned Count =
543-
std::distance(AggregateType->field_begin(), AggregateType->field_end());
542+
unsigned Count = AggregateType->getNumFields();
544543
if (const auto *CRD = dyn_cast<CXXRecordDecl>(AggregateType))
545544
Count += CRD->getNumBases();
546545
return Count;

0 commit comments

Comments
 (0)