return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
}
- /// The bound to claim that an array of unknown bound has.
- /// The value in MostDerivedArraySize is undefined in this case. So, set it
- /// to an arbitrary value that's likely to loudly break things if it's used.
- static const uint64_t AssumedSizeForUnsizedArray =
- std::numeric_limits<uint64_t>::max() / 2;
-
/// Determines if an LValue with the given LValueBase will have an unsized
/// array in its designator.
/// Find the path length and type of the most-derived subobject in the given
static unsigned
findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base,
ArrayRef<APValue::LValuePathEntry> Path,
- uint64_t &ArraySize, QualType &Type, bool &IsArray,
- bool &FirstEntryIsUnsizedArray) {
+ uint64_t &ArraySize, QualType &Type, bool &IsArray) {
// This only accepts LValueBases from APValues, and APValues don't support
// arrays that lack size info.
assert(!isBaseAnAllocSizeCall(Base) &&
for (unsigned I = 0, N = Path.size(); I != N; ++I) {
if (Type->isArrayType()) {
- const ArrayType *AT = Ctx.getAsArrayType(Type);
- Type = AT->getElementType();
+ const ConstantArrayType *CAT =
+ cast<ConstantArrayType>(Ctx.getAsArrayType(Type));
+ Type = CAT->getElementType();
+ ArraySize = CAT->getSize().getZExtValue();
MostDerivedLength = I + 1;
IsArray = true;
-
- if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) {
- ArraySize = CAT->getSize().getZExtValue();
- } else {
- assert(I == 0 && "unexpected unsized array designator");
- FirstEntryIsUnsizedArray = true;
- ArraySize = AssumedSizeForUnsizedArray;
- }
} else if (Type->isAnyComplexType()) {
const ComplexType *CT = Type->castAs<ComplexType>();
Type = CT->getElementType();
Entries.insert(Entries.end(), VEntries.begin(), VEntries.end());
if (V.getLValueBase()) {
bool IsArray = false;
- bool FirstIsUnsizedArray = false;
MostDerivedPathLength = findMostDerivedSubobject(
Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize,
- MostDerivedType, IsArray, FirstIsUnsizedArray);
+ MostDerivedType, IsArray);
MostDerivedIsArrayElement = IsArray;
- FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
}
}
}
// The value in MostDerivedArraySize is undefined in this case. So, set it
// to an arbitrary value that's likely to loudly break things if it's
// used.
- MostDerivedArraySize = AssumedSizeForUnsizedArray;
+ MostDerivedArraySize = std::numeric_limits<uint64_t>::max() / 2;
MostDerivedPathLength = Entries.size();
}
/// Update this designator to refer to the given base or member of this
MostDerivedArraySize = 2;
MostDerivedPathLength = Entries.size();
}
- void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
const APSInt &N);
/// Add N to the address of this subobject.
if (Invalid || !N) return;
uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue();
if (isMostDerivedAnUnsizedArray()) {
- diagnoseUnsizedArrayPointerArithmetic(Info, E);
// Can't verify -- trust that the user is doing the right thing (or if
// not, trust that the caller will catch the bad behavior).
// FIXME: Should we reject if this overflows, at least?
setInvalid();
return false;
}
- // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
- // must actually be at least one array element; even a VLA cannot have a
- // bound of zero. And if our index is nonzero, we already had a CCEDiag.
return true;
}
-void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
- const Expr *E) {
- Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed);
- // Do not set the designator as invalid: we can represent this situation,
- // and correct handling of __builtin_object_size requires us to do so.
-}
-
void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
const Expr *E,
const APSInt &N) {
IsNullPtr);
else {
assert(!InvalidBase && "APValues can't handle invalid LValue bases");
+ assert(!Designator.FirstEntryIsAnUnsizedArray &&
+ "Unsized array with a valid base?");
V = APValue(Base, Offset, Designator.Entries,
Designator.IsOnePastTheEnd, CallIndex, IsNullPtr);
}
if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base))
Designator.addDeclUnchecked(D, Virtual);
}
- void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
- if (!Designator.Entries.empty()) {
- Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array);
- Designator.setInvalid();
- return;
- }
-
- assert(getType(Base)->isPointerType() || getType(Base)->isArrayType());
+ void addUnsizedArray(EvalInfo &Info, QualType ElemTy) {
+ assert(Designator.Entries.empty() && getType(Base)->isPointerType());
+ assert(isBaseAnAllocSizeCall(Base) &&
+ "Only alloc_size bases can have unsized arrays");
Designator.FirstEntryIsAnUnsizedArray = true;
Designator.addUnsizedArrayUnchecked(ElemTy);
}
if (Sub.Invalid)
// A diagnostic will have already been produced.
return handler.failed();
- if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
+ if (Sub.isOnePastTheEnd()) {
if (Info.getLangOpts().CPlusPlus11)
- Info.FFDiag(E, Sub.isOnePastTheEnd()
- ? diag::note_constexpr_access_past_end
- : diag::note_constexpr_access_unsized_array)
- << handler.AccessKind;
+ Info.FFDiag(E, diag::note_constexpr_access_past_end)
+ << handler.AccessKind;
else
Info.FFDiag(E);
return handler.failed();
Result.setInvalid(E);
QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
- Result.addUnsizedArray(Info, E, Pointee);
+ Result.addUnsizedArray(Info, Pointee);
return true;
}
return true;
}
}
-
- case CK_ArrayToPointerDecay: {
+ case CK_ArrayToPointerDecay:
if (SubExpr->isGLValue()) {
if (!evaluateLValue(SubExpr, Result))
return false;
return false;
}
// The result is a pointer to the first element of the array.
- auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType());
- if (auto *CAT = dyn_cast<ConstantArrayType>(AT))
+ if (const ConstantArrayType *CAT
+ = Info.Ctx.getAsConstantArrayType(SubExpr->getType()))
Result.addArray(Info, E, CAT);
else
- Result.addUnsizedArray(Info, E, AT->getElementType());
+ Result.Designator.setInvalid();
return true;
- }
case CK_FunctionToPointerDecay:
return evaluateLValue(SubExpr, Result);
Result.setInvalid(E);
QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
- Result.addUnsizedArray(Info, E, PointeeTy);
+ Result.addUnsizedArray(Info, PointeeTy);
return true;
}
/// Please note: this function is specialized for how __builtin_object_size
/// views "objects".
///
-/// If this encounters an invalid RecordDecl or otherwise cannot determine the
-/// correct result, it will always return true.
+/// If this encounters an invalid RecordDecl, it will always return true.
static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
assert(!LVal.Designator.Invalid);
unsigned I = 0;
QualType BaseType = getType(Base);
if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
- // If we don't know the array bound, conservatively assume we're looking at
- // the final array element.
+ assert(isBaseAnAllocSizeCall(Base) &&
+ "Unsized array in non-alloc_size call?");
+ // If this is an alloc_size base, we should ignore the initial array index
++I;
BaseType = BaseType->castAs<PointerType>()->getPointeeType();
}