Skip to content

Commit fb1433b

Browse files
committed
Move search for l-value to r-value cast into a visitor and remove the redundant 'isCounteAttributedType'.
1 parent ac24f04 commit fb1433b

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,6 +4274,24 @@ static Address emitArraySubscriptGEP(CodeGenFunction &CGF, Address addr,
42744274
return Address(eltPtr, CGF.ConvertTypeForMem(eltType), eltAlign);
42754275
}
42764276

4277+
namespace {
4278+
4279+
/// StructFieldAccess is a simple visitor class to grab the first l-value to
4280+
/// r-value cast Expr.
4281+
struct StructFieldAccess
4282+
: public ConstStmtVisitor<StructFieldAccess, const Expr *> {
4283+
const Expr *VisitCastExpr(const CastExpr *E) {
4284+
if (E->getCastKind() == CK_LValueToRValue)
4285+
return E;
4286+
return Visit(E->getSubExpr());
4287+
}
4288+
const Expr *VisitParenExpr(const ParenExpr *E) {
4289+
return Visit(E->getSubExpr());
4290+
}
4291+
};
4292+
4293+
} // end anonymous namespace
4294+
42774295
/// The offset of a field from the beginning of the record.
42784296
static bool getFieldOffsetInBits(CodeGenFunction &CGF, const RecordDecl *RD,
42794297
const FieldDecl *Field, int64_t &Offset) {
@@ -4343,6 +4361,11 @@ void CodeGenFunction::EmitCountedByBoundsChecking(
43434361
if (!ME || !ME->getMemberDecl()->getType()->isCountAttributedType())
43444362
return;
43454363

4364+
if (!Addr.isValid()) {
4365+
LValue LV = EmitCheckedLValue(E, TCK_MemberAccess);
4366+
Addr = LV.getAddress();
4367+
}
4368+
43464369
const LangOptions::StrictFlexArraysLevelKind StrictFlexArraysLevel =
43474370
getLangOpts().getStrictFlexArraysLevel();
43484371
if (FlexibleArray &&
@@ -4527,28 +4550,14 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
45274550
E->getBase());
45284551

45294552
if (SanOpts.has(SanitizerKind::ArrayBounds)) {
4530-
const Expr *Base = E->getBase();
4531-
while (true) {
4532-
if (const auto *CE = dyn_cast<CastExpr>(Base)) {
4533-
if (CE->getCastKind() == CK_LValueToRValue)
4534-
break;
4535-
Base = CE->getSubExpr();
4536-
} else if (const auto *PE = dyn_cast<ParenExpr>(Base)) {
4537-
Base = PE->getSubExpr();
4538-
} else {
4539-
break;
4540-
}
4541-
}
4553+
StructFieldAccess Visitor;
4554+
const Expr *Base = Visitor.Visit(E->getBase());
45424555

4543-
if (const auto *CE = dyn_cast<CastExpr>(Base);
4556+
if (const auto *CE = dyn_cast_if_present<CastExpr>(Base);
45444557
CE && CE->getCastKind() == CK_LValueToRValue)
4545-
if (const auto *ME = dyn_cast<MemberExpr>(CE->getSubExpr());
4546-
ME && ME->getMemberDecl()->getType()->isCountAttributedType()) {
4547-
LValue LV = EmitCheckedLValue(Base, TCK_MemberAccess);
4548-
EmitCountedByBoundsChecking(CE->getSubExpr(), Idx, LV.getAddress(),
4549-
E->getIdx()->getType(), ptrType, Accessed,
4550-
/*FlexibleArray=*/false);
4551-
}
4558+
EmitCountedByBoundsChecking(CE, Idx, Address::invalid(),
4559+
E->getIdx()->getType(), ptrType, Accessed,
4560+
/*FlexibleArray=*/false);
45524561
}
45534562
}
45544563

0 commit comments

Comments
 (0)