Skip to content

Commit 907c558

Browse files
kkjeerMandeep Singh Grang
authored andcommitted
Introduce temporary bindings to BoundsCastExprs (#694)
Cherry-picked from commit 82f665e Author: Katherine Kjeer <[email protected]> Commit: GitHub <[email protected]> Introduce temporary bindings to BoundsCastExprs (#694) * Account for cast kind in StmtProfiler::VisitBoundsCastExpr and CanonBounds::CompareImpl * Don't treat dynamic and assume bounds casts as value preserving; don't compare types on bounds expressions * Introduce a temporary binding expression for all bounds cast expressions * Use the temporary bindings introduced in bounds cast expressions to infer bounds * Remove unused PruneTemporaryBindings * Treat e and the usage of a temporary binding of e as equal * Remove llvm_unreachable from CanonBounds::Compare * Account for bounds cast expressions in GetTempBinding * Treat bounds casts as value preserving in CheckBoundsDelarations::EqualValue (introducing the assumption that E1 and E2 have been evaluated) * Remove function call test from bounds-decl-challenges * Add AST dump tests for bounds cast expression temporaries * Fix indentation * More formatting fixes * Revert treating bounds casts as value preserving in EqualValue * Add comment about lack of bounds expression types * Prune temporary bindings from member expression bounds * Account for checked scope information in PruneTemporaryBindings
1 parent 4bd1634 commit 907c558

File tree

13 files changed

+269
-150
lines changed

13 files changed

+269
-150
lines changed

clang/include/clang/AST/CanonBounds.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ namespace clang {
6262
const T *E1 = dyn_cast<T>(Raw1);
6363
const T *E2 = dyn_cast<T>(Raw2);
6464
if (!E1 || !E2) {
65-
llvm_unreachable("dyn_cast failed");
6665
return Result::LessThan;
6766
}
6867
return Lexicographic::CompareImpl(E1, E2);

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4951,7 +4951,7 @@ class Sema {
49514951
/// InferLValueBounds - infer a bounds expression for an lvalue.
49524952
/// The bounds determine whether the lvalue to which an
49534953
/// expression evaluates in in range.
4954-
BoundsExpr *InferLValueBounds(Expr *E);
4954+
BoundsExpr *InferLValueBounds(Expr *E, bool InCheckedScope);
49554955

49564956
/// CreateTypeBasedBounds: the bounds that can be inferred from
49574957
/// the type alone.
@@ -4975,7 +4975,7 @@ class Sema {
49754975

49764976
/// InferLValueTargetBounds - infer the bounds for the
49774977
/// target of an lvalue.
4978-
BoundsExpr *InferLValueTargetBounds(Expr *E);
4978+
BoundsExpr *InferLValueTargetBounds(Expr *E, bool InCheckedScope);
49794979

49804980
/// InferRValueBounds - infer a bounds expression for an rvalue.
49814981
/// The bounds determine whether the rvalue to which an
@@ -4985,6 +4985,7 @@ class Sema {
49854985
/// for an nt_array is included in the bounds (it gives
49864986
/// us physical bounds, not logical bounds).
49874987
BoundsExpr *InferRValueBounds(Expr *E,
4988+
bool InCheckedScope,
49884989
bool IncludeNullTerminator = false);
49894990

49904991
BoundsExpr *ExpandToRange(Expr *Base, BoundsExpr *B);

clang/lib/AST/CanonBounds.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ namespace {
3636
case CK_ArrayToPointerDecay:
3737
case CK_FunctionToPointerDecay:
3838
case CK_NullToPointer:
39-
case CK_AssumePtrBounds:
40-
case CK_DynamicPtrBounds:
4139
return true;
4240
default:
4341
return false;
@@ -323,14 +321,26 @@ Result Lexicographic::CompareExpr(const Expr *Arg1, const Expr *Arg2) {
323321

324322
// The use of an expression temporary is equal to the
325323
// value of the binding expression.
326-
if (BoundsValueExpr *BV1 = dyn_cast<BoundsValueExpr>(E1))
327-
if (BV1->getTemporaryBinding() == E2)
324+
if (BoundsValueExpr *BV1 = dyn_cast<BoundsValueExpr>(E1)) {
325+
CHKCBindTemporaryExpr *Binding = BV1->getTemporaryBinding();
326+
if (Binding == E2)
328327
return Result::Equal;
329328

330-
if (BoundsValueExpr *BV2 = dyn_cast<BoundsValueExpr>(E2))
331-
if (BV2->getTemporaryBinding() == E1)
329+
if (Binding)
330+
if (CompareExpr(Binding->getSubExpr(), E2) == Result::Equal)
331+
return Result::Equal;
332+
}
333+
334+
if (BoundsValueExpr *BV2 = dyn_cast<BoundsValueExpr>(E2)) {
335+
CHKCBindTemporaryExpr *Binding = BV2->getTemporaryBinding();
336+
if (Binding == E1)
332337
return Result::Equal;
333338

339+
if (Binding)
340+
if (CompareExpr(Binding->getSubExpr(), E1) == Result::Equal)
341+
return Result::Equal;
342+
}
343+
334344
// Compare expressions structurally, recursively invoking
335345
// comparison for subcomponents. If that fails, consult
336346
// EquivExprs to see if the expressions are considered
@@ -446,8 +456,11 @@ Result Lexicographic::CompareExpr(const Expr *Arg1, const Expr *Arg2) {
446456
// - Pointer arithmetic where the pointer referent types are the same
447457
// size, checkedness is the same, and the integer types are the
448458
// same size/signedness.
449-
Cmp = CompareTypeIgnoreCheckedness(E1ChildExpr->getType(),
450-
E2ChildExpr->getType());
459+
460+
// Bounds expressions don't have types.
461+
if (!isa<BoundsExpr>(E1ChildExpr))
462+
Cmp = CompareTypeIgnoreCheckedness(E1ChildExpr->getType(), E2ChildExpr->getType());
463+
451464
if (Cmp != Result::Equal)
452465
return CheckEquivExprs(Cmp, E1, E2);
453466
} else
@@ -754,9 +767,14 @@ Lexicographic::CompareImpl(const PositionalParameterExpr *E1,
754767
Result
755768
Lexicographic::CompareImpl(const BoundsCastExpr *E1,
756769
const BoundsCastExpr *E2) {
757-
Result Cmp = CompareExpr(E1->getBoundsExpr(), E2->getBoundsExpr());
770+
Result Cmp = CompareInteger(E1->getCastKind(), E2->getCastKind());
758771
if (Cmp != Result::Equal)
759772
return Cmp;
773+
774+
Cmp = CompareExpr(E1->getBoundsExpr(), E2->getBoundsExpr());
775+
if (Cmp != Result::Equal)
776+
return Cmp;
777+
760778
return CompareType(E1->getType(), E2->getType());
761779
}
762780

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ void StmtProfiler::VisitInteropTypeExpr(
13211321

13221322
void StmtProfiler::VisitBoundsCastExpr(const BoundsCastExpr *S) {
13231323
VisitExplicitCastExpr(S);
1324+
ID.AddInteger(S->getCastKind());
13241325
}
13251326

13261327
void StmtProfiler::VisitPositionalParameterExpr(

0 commit comments

Comments
 (0)