-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clang] Followup for constexpr-unknown potential constant expressions. #151053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efriedma-quic
wants to merge
2
commits into
llvm:main
Choose a base branch
from
efriedma-quic:constexpr-unknown-potential-constant-part2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -465,49 +465,7 @@ namespace { | |||||||||
void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, | ||||||||||
const APSInt &N); | ||||||||||
/// Add N to the address of this subobject. | ||||||||||
void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { | ||||||||||
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? | ||||||||||
Entries.back() = PathEntry::ArrayIndex( | ||||||||||
Entries.back().getAsArrayIndex() + TruncatedN); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
// [expr.add]p4: For the purposes of these operators, a pointer to a | ||||||||||
// nonarray object behaves the same as a pointer to the first element of | ||||||||||
// an array of length one with the type of the object as its element type. | ||||||||||
bool IsArray = MostDerivedPathLength == Entries.size() && | ||||||||||
MostDerivedIsArrayElement; | ||||||||||
uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() | ||||||||||
: (uint64_t)IsOnePastTheEnd; | ||||||||||
uint64_t ArraySize = | ||||||||||
IsArray ? getMostDerivedArraySize() : (uint64_t)1; | ||||||||||
|
||||||||||
if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { | ||||||||||
// Calculate the actual index in a wide enough type, so we can include | ||||||||||
// it in the note. | ||||||||||
N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); | ||||||||||
(llvm::APInt&)N += ArrayIndex; | ||||||||||
assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); | ||||||||||
diagnosePointerArithmetic(Info, E, N); | ||||||||||
setInvalid(); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
ArrayIndex += TruncatedN; | ||||||||||
assert(ArrayIndex <= ArraySize && | ||||||||||
"bounds check succeeded for out-of-bounds index"); | ||||||||||
|
||||||||||
if (IsArray) | ||||||||||
Entries.back() = PathEntry::ArrayIndex(ArrayIndex); | ||||||||||
else | ||||||||||
IsOnePastTheEnd = (ArrayIndex != 0); | ||||||||||
} | ||||||||||
void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, LValue &LV); | ||||||||||
}; | ||||||||||
|
||||||||||
/// A scope at the end of which an object can need to be destroyed. | ||||||||||
|
@@ -1799,7 +1757,7 @@ namespace { | |||||||||
Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); | ||||||||||
|
||||||||||
if (checkNullPointer(Info, E, CSK_ArrayIndex)) | ||||||||||
Designator.adjustIndex(Info, E, Index); | ||||||||||
Designator.adjustIndex(Info, E, Index, *this); | ||||||||||
clearIsNullPointer(); | ||||||||||
} | ||||||||||
void adjustOffset(CharUnits N) { | ||||||||||
|
@@ -1907,6 +1865,54 @@ namespace { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, | ||||||||||
LValue &LV) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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? | ||||||||||
Entries.back() = | ||||||||||
PathEntry::ArrayIndex(Entries.back().getAsArrayIndex() + TruncatedN); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
// [expr.add]p4: For the purposes of these operators, a pointer to a | ||||||||||
// nonarray object behaves the same as a pointer to the first element of | ||||||||||
// an array of length one with the type of the object as its element type. | ||||||||||
bool IsArray = | ||||||||||
MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement; | ||||||||||
uint64_t ArrayIndex = | ||||||||||
IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd; | ||||||||||
uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1; | ||||||||||
|
||||||||||
if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { | ||||||||||
if (!Info.checkingPotentialConstantExpression() || | ||||||||||
!LV.AllowConstexprUnknown) { | ||||||||||
// Calculate the actual index in a wide enough type, so we can include | ||||||||||
// it in the note. | ||||||||||
N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); | ||||||||||
(llvm::APInt &)N += ArrayIndex; | ||||||||||
assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); | ||||||||||
diagnosePointerArithmetic(Info, E, N); | ||||||||||
} | ||||||||||
setInvalid(); | ||||||||||
return; | ||||||||||
} | ||||||||||
|
||||||||||
ArrayIndex += TruncatedN; | ||||||||||
assert(ArrayIndex <= ArraySize && | ||||||||||
"bounds check succeeded for out-of-bounds index"); | ||||||||||
|
||||||||||
if (IsArray) | ||||||||||
Entries.back() = PathEntry::ArrayIndex(ArrayIndex); | ||||||||||
else | ||||||||||
IsOnePastTheEnd = (ArrayIndex != 0); | ||||||||||
} | ||||||||||
|
||||||||||
static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); | ||||||||||
static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, | ||||||||||
const LValue &This, const Expr *E, | ||||||||||
|
@@ -5126,12 +5132,18 @@ static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, | |||||||||
if (const PointerType *PT = TargetQT->getAs<PointerType>()) | ||||||||||
TargetQT = PT->getPointeeType(); | ||||||||||
|
||||||||||
// Check this cast lands within the final derived-to-base subobject path. | ||||||||||
if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { | ||||||||||
Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) | ||||||||||
<< D.MostDerivedType << TargetQT; | ||||||||||
auto InvalidCast = [&]() { | ||||||||||
if (!Info.checkingPotentialConstantExpression() || | ||||||||||
!Result.AllowConstexprUnknown) { | ||||||||||
Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) | ||||||||||
<< D.MostDerivedType << TargetQT; | ||||||||||
} | ||||||||||
return false; | ||||||||||
} | ||||||||||
}; | ||||||||||
|
||||||||||
// Check this cast lands within the final derived-to-base subobject path. | ||||||||||
if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) | ||||||||||
return InvalidCast(); | ||||||||||
|
||||||||||
// Check the type of the final cast. We don't need to check the path, | ||||||||||
// since a cast can only be formed if the path is unique. | ||||||||||
|
@@ -5142,11 +5154,8 @@ static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, | |||||||||
FinalType = D.MostDerivedType->getAsCXXRecordDecl(); | ||||||||||
else | ||||||||||
FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); | ||||||||||
if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { | ||||||||||
Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) | ||||||||||
<< D.MostDerivedType << TargetQT; | ||||||||||
return false; | ||||||||||
} | ||||||||||
if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) | ||||||||||
return InvalidCast(); | ||||||||||
|
||||||||||
// Truncate the lvalue to the appropriate derived class. | ||||||||||
return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); | ||||||||||
|
@@ -6104,12 +6113,15 @@ static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, | |||||||||
} else if (Polymorphic) { | ||||||||||
// Conservatively refuse to perform a polymorphic operation if we would | ||||||||||
// not be able to read a notional 'vptr' value. | ||||||||||
APValue Val; | ||||||||||
This.moveInto(Val); | ||||||||||
QualType StarThisType = | ||||||||||
Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx)); | ||||||||||
Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) | ||||||||||
<< AK << Val.getAsString(Info.Ctx, StarThisType); | ||||||||||
if (!Info.checkingPotentialConstantExpression() || | ||||||||||
!This.AllowConstexprUnknown) { | ||||||||||
APValue Val; | ||||||||||
This.moveInto(Val); | ||||||||||
QualType StarThisType = | ||||||||||
Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx)); | ||||||||||
Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) | ||||||||||
<< AK << Val.getAsString(Info.Ctx, StarThisType); | ||||||||||
} | ||||||||||
return false; | ||||||||||
} | ||||||||||
return true; | ||||||||||
|
@@ -14554,6 +14566,11 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, | |||||||||
// Reject differing bases from the normal codepath; we special-case | ||||||||||
// comparisons to null. | ||||||||||
if (!HasSameBase(LHSValue, RHSValue)) { | ||||||||||
// Bail out early if we're checking potential constant expression. | ||||||||||
// Otherwise, prefer to diagnose other issues. | ||||||||||
if (Info.checkingPotentialConstantExpression() && | ||||||||||
(LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)) | ||||||||||
return false; | ||||||||||
auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) { | ||||||||||
std::string LHS = LHSValue.toString(Info.Ctx, E->getLHS()->getType()); | ||||||||||
std::string RHS = RHSValue.toString(Info.Ctx, E->getRHS()->getType()); | ||||||||||
|
@@ -14874,6 +14891,10 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { | |||||||||
// Reject differing bases from the normal codepath; we special-case | ||||||||||
// comparisons to null. | ||||||||||
if (!HasSameBase(LHSValue, RHSValue)) { | ||||||||||
if (Info.checkingPotentialConstantExpression() && | ||||||||||
(LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)) | ||||||||||
return false; | ||||||||||
|
||||||||||
const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); | ||||||||||
const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); | ||||||||||
|
||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to try to explain this... but then I realized the getSource() check isn't actually doing anything, so I just deleted this whole block of code.