Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions clang/lib/Sema/SemaBounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3858,7 +3858,8 @@ namespace {
ExprCreatorUtil::CreateImplicitCast(S, TargetDeclRef, Kind, TargetTy);

// Record equality between the target and initializer.
RecordEqualityWithTarget(TargetDeclRef, TargetExpr, Init, State);
RecordEqualityWithTarget(TargetDeclRef, TargetExpr, Init,
/*AllowTempEquality=*/true, State);
}

if (D->isInvalidDecl())
Expand Down Expand Up @@ -4537,9 +4538,15 @@ namespace {
OriginalValue,
CSS, State);
UpdateEquivExprsAfterAssignment(LValue, OriginalValue, CSS, State);
UpdateSameValueAfterAssignment(LValue, OriginalValue,
OriginalValueUsesLValue, CSS, State);
RecordEqualityWithTarget(LValue, Target, Src, State);
// We can only record equality between Target and Src if Src does
// not use the value of LValue. If UpdateSameValueAfterAssignment
// did not modify the contents of State.SameValue, then no expressions
// equivalent to Src use the value of LValue, so we can record equality
// between Target and Src.
bool AllowTempEquality =
UpdateSameValueAfterAssignment(LValue, OriginalValue,
OriginalValueUsesLValue, CSS, State);
RecordEqualityWithTarget(LValue, Target, Src, AllowTempEquality, State);

StateUpdated = true;
return ResultBounds;
Expand Down Expand Up @@ -4687,7 +4694,11 @@ namespace {
// that modifies the expression LValue.
//
// OriginalValue is the value of LValue before the assignment.
void UpdateSameValueAfterAssignment(Expr *LValue, Expr *OriginalValue,
//
// UpdateSameValue returns true if the contents of SameValue were
// not modified, i.e. if no expressions in SameValue used the value
// of LValue.
bool UpdateSameValueAfterAssignment(Expr *LValue, Expr *OriginalValue,
bool OriginalValueUsesLValue,
CheckedScopeSpecifier CSS,
CheckingState &State) {
Expand All @@ -4710,25 +4721,38 @@ namespace {
if (AdjustedE && !EqualExprsContainsExpr(State.SameValue, AdjustedE))
State.SameValue.push_back(AdjustedE);
}
return State.SameValue.size() == PrevSameValue.size();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it sufficient to check that State.SameValue.size() == PrevSameValue.size() to be sure that no expressions in SameValue use the value of LValue? What if the number of elements in SameValue remain the same but one or more of these elements are modified?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the original value uses the value of LValue, then the value used to replace uses of LValue in SameValue will be null, which means that any expressions in SameValue that use the value of LValue will be removed from SameValue.

I think this could be clearer in the code; I'll work on cleaning up the solution.

}

// RecordEqualityWithTarget updates the checking state to record equality
// implied by an assignment or initializer of the form LValue = Src,
// where Target is an rvalue expression that is the value of LValue.
//
// AllowTempEquality is true if it is permissible to record temporary
// equality between Target and Src in State.TargetSrcEquality if necessary.
// The purpose of TargetSrcEquality is to record equality between target
// and source expressions only for checking bounds after the current
// statement (unlike State.EquivExprs, this equality does not persist
// across statements). However, not all target/source pairs should be added
// to TargetSrcEquality. For example, in an assignment x = x + 1, SameValue
// will be empty since x + 1 uses the value of x. However, x => x + 1
// should not be added to TargetSrcEquality.
//
// State.SameValue is assumed to contain expressions that produce the same
// value as Source.
// value as Src.
void RecordEqualityWithTarget(Expr *LValue, Expr *Target, Expr *Src,
bool AllowTempEquality,
CheckingState &State) {
if (!LValue)
return;
LValue = LValue->IgnoreParens();

// Certain kinds of expressions (e.g. member expressions) are not allowed
// to be included in EquivExprs. For these expressions, we record
// temporary equality between Target and Src in TargetSrcEquality instead
// of in EquivExprs. If Src is allowed in EquivExprs, SameValue will
// contain at least one expression that produces the same value as Src.
// temporary equality (if permitted by AllowTempEquality) between Target
// and Src in TargetSrcEquality instead of in EquivExprs. If Src is
// allowed in EquivExprs, SameValue will contain at least one expression
// that produces the same value as Src.
bool TargetAllowedInEquivExprs = !isa<MemberExpr>(LValue);
bool SrcAllowedInEquivExprs = State.SameValue.size() > 0;

Expand Down Expand Up @@ -4766,7 +4790,8 @@ namespace {
// This temporary equality information will be used to validate the
// bounds context after checking the current top-level CFG statement,
// but does not persist across checking CFG statements.
if (Src && (!TargetAllowedInEquivExprs || !SrcAllowedInEquivExprs)) {
if (AllowTempEquality && Src &&
(!TargetAllowedInEquivExprs || !SrcAllowedInEquivExprs)) {
CHKCBindTemporaryExpr *Temp = GetTempBinding(Src);
if (Temp)
State.TargetSrcEquality[Target] = CreateTemporaryUse(Temp);
Expand Down