Skip to content

Commit 37c74df

Browse files
committed
Revert "[c++17] Implement P0145R3 during constant evaluation."
This reverts commit ded79be. It causes a crash (I sent the crash reproducer directly to the author).
1 parent 17b9a91 commit 37c74df

File tree

3 files changed

+36
-169
lines changed

3 files changed

+36
-169
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,12 +1856,8 @@ void CallStackFrame::describe(raw_ostream &Out) {
18561856
Out << ", ";
18571857

18581858
const ParmVarDecl *Param = *I;
1859-
if (Arguments) {
1860-
const APValue &Arg = Arguments[ArgIndex];
1861-
Arg.printPretty(Out, Info.Ctx, Param->getType());
1862-
} else {
1863-
Out << "<...>";
1864-
}
1859+
const APValue &Arg = Arguments[ArgIndex];
1860+
Arg.printPretty(Out, Info.Ctx, Param->getType());
18651861

18661862
if (ArgIndex == 0 && IsMemberCall)
18671863
Out << "->" << *Callee << '(';
@@ -5796,8 +5792,6 @@ typedef SmallVector<APValue, 8> ArgVector;
57965792
/// EvaluateArgs - Evaluate the arguments to a function call.
57975793
static bool EvaluateArgs(ArrayRef<const Expr *> Args, ArgVector &ArgValues,
57985794
EvalInfo &Info, const FunctionDecl *Callee) {
5799-
ArgValues.resize(Args.size());
5800-
58015795
bool Success = true;
58025796
llvm::SmallBitVector ForbiddenNullArgs;
58035797
if (Callee->hasAttr<NonNullAttr>()) {
@@ -5815,6 +5809,8 @@ static bool EvaluateArgs(ArrayRef<const Expr *> Args, ArgVector &ArgValues,
58155809
}
58165810
}
58175811
}
5812+
// FIXME: This is the wrong evaluation order for an assignment operator
5813+
// called via operator syntax.
58185814
for (unsigned Idx = 0; Idx < Args.size(); Idx++) {
58195815
if (!Evaluate(ArgValues[Idx], Info, Args[Idx])) {
58205816
// If we're checking for a potential constant expression, evaluate all
@@ -5838,13 +5834,17 @@ static bool EvaluateArgs(ArrayRef<const Expr *> Args, ArgVector &ArgValues,
58385834
/// Evaluate a function call.
58395835
static bool HandleFunctionCall(SourceLocation CallLoc,
58405836
const FunctionDecl *Callee, const LValue *This,
5841-
ArrayRef<const Expr *> Args, APValue *ArgValues,
5842-
const Stmt *Body, EvalInfo &Info,
5843-
APValue &Result, const LValue *ResultSlot) {
5837+
ArrayRef<const Expr*> Args, const Stmt *Body,
5838+
EvalInfo &Info, APValue &Result,
5839+
const LValue *ResultSlot) {
5840+
ArgVector ArgValues(Args.size());
5841+
if (!EvaluateArgs(Args, ArgValues, Info, Callee))
5842+
return false;
5843+
58445844
if (!Info.CheckCallLimit(CallLoc))
58455845
return false;
58465846

5847-
CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues);
5847+
CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data());
58485848

58495849
// For a trivial copy or move assignment, perform an APValue copy. This is
58505850
// essential for unions, where the operations performed by the assignment
@@ -7293,8 +7293,6 @@ class ExprEvaluatorBase
72937293
auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs());
72947294
bool HasQualifier = false;
72957295

7296-
ArgVector ArgValues;
7297-
72987296
// Extract function decl and 'this' pointer from the callee.
72997297
if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) {
73007298
const CXXMethodDecl *Member = nullptr;
@@ -7343,22 +7341,6 @@ class ExprEvaluatorBase
73437341
return Error(E);
73447342
}
73457343

7346-
// For an (overloaded) assignment expression, evaluate the RHS before the
7347-
// LHS.
7348-
auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
7349-
if (OCE && OCE->isAssignmentOp()) {
7350-
assert(Args.size() == 2 && "wrong number of arguments in assignment");
7351-
if (isa<CXXMethodDecl>(FD)) {
7352-
// Args[0] is the object argument.
7353-
if (!EvaluateArgs({Args[1]}, ArgValues, Info, FD))
7354-
return false;
7355-
} else {
7356-
if (!EvaluateArgs({Args[1], Args[0]}, ArgValues, Info, FD))
7357-
return false;
7358-
std::swap(ArgValues[0], ArgValues[1]);
7359-
}
7360-
}
7361-
73627344
// Overloaded operator calls to member functions are represented as normal
73637345
// calls with '*this' as the first argument.
73647346
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD);
@@ -7421,11 +7403,6 @@ class ExprEvaluatorBase
74217403
} else
74227404
return Error(E);
74237405

7424-
// Evaluate the arguments now if we've not already done so.
7425-
if (ArgValues.empty() && !Args.empty() &&
7426-
!EvaluateArgs(Args, ArgValues, Info, FD))
7427-
return false;
7428-
74297406
SmallVector<QualType, 4> CovariantAdjustmentPath;
74307407
if (This) {
74317408
auto *NamedMember = dyn_cast<CXXMethodDecl>(FD);
@@ -7447,7 +7424,6 @@ class ExprEvaluatorBase
74477424
// Destructor calls are different enough that they have their own codepath.
74487425
if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) {
74497426
assert(This && "no 'this' pointer for destructor call");
7450-
assert(ArgValues.empty() && "unexpected destructor arguments");
74517427
return HandleDestruction(Info, E, *This,
74527428
Info.Ctx.getRecordType(DD->getParent()));
74537429
}
@@ -7456,8 +7432,8 @@ class ExprEvaluatorBase
74567432
Stmt *Body = FD->getBody(Definition);
74577433

74587434
if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) ||
7459-
!HandleFunctionCall(E->getExprLoc(), Definition, This, Args,
7460-
ArgValues.data(), Body, Info, Result, ResultSlot))
7435+
!HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info,
7436+
Result, ResultSlot))
74617437
return false;
74627438

74637439
if (!CovariantAdjustmentPath.empty() &&
@@ -8095,20 +8071,17 @@ bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
80958071
if (E->getBase()->getType()->isVectorType())
80968072
return Error(E);
80978073

8098-
APSInt Index;
80998074
bool Success = true;
8100-
8101-
// C++17's rules require us to evaluate the LHS first, regardless of which
8102-
// side is the base.
8103-
for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
8104-
if (SubExpr == E->getBase() ? !evaluatePointer(SubExpr, Result)
8105-
: !EvaluateInteger(SubExpr, Index, Info)) {
8106-
if (!Info.noteFailure())
8107-
return false;
8108-
Success = false;
8109-
}
8075+
if (!evaluatePointer(E->getBase(), Result)) {
8076+
if (!Info.noteFailure())
8077+
return false;
8078+
Success = false;
81108079
}
81118080

8081+
APSInt Index;
8082+
if (!EvaluateInteger(E->getIdx(), Index, Info))
8083+
return false;
8084+
81128085
return Success &&
81138086
HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index);
81148087
}
@@ -8152,10 +8125,7 @@ bool LValueExprEvaluator::VisitCompoundAssignOperator(
81528125
if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
81538126
return Error(CAO);
81548127

8155-
// C++17 onwards require that we evaluate the RHS first.
81568128
APValue RHS;
8157-
if (!Evaluate(RHS, this->Info, CAO->getRHS()))
8158-
return false;
81598129

81608130
// The overall lvalue result is the result of evaluating the LHS.
81618131
if (!this->Visit(CAO->getLHS())) {
@@ -8164,6 +8134,9 @@ bool LValueExprEvaluator::VisitCompoundAssignOperator(
81648134
return false;
81658135
}
81668136

8137+
if (!Evaluate(RHS, this->Info, CAO->getRHS()))
8138+
return false;
8139+
81678140
return handleCompoundAssignment(
81688141
this->Info, CAO,
81698142
Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(),
@@ -8174,17 +8147,17 @@ bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
81748147
if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
81758148
return Error(E);
81768149

8177-
// C++17 onwards require that we evaluate the RHS first.
81788150
APValue NewVal;
8179-
if (!Evaluate(NewVal, this->Info, E->getRHS()))
8180-
return false;
81818151

81828152
if (!this->Visit(E->getLHS())) {
81838153
if (Info.noteFailure())
81848154
Evaluate(NewVal, this->Info, E->getRHS());
81858155
return false;
81868156
}
81878157

8158+
if (!Evaluate(NewVal, this->Info, E->getRHS()))
8159+
return false;
8160+
81888161
if (Info.getLangOpts().CPlusPlus20 &&
81898162
!HandleUnionActiveMemberChange(Info, E->getLHS(), Result))
81908163
return false;
@@ -15297,8 +15270,7 @@ bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
1529715270
} else {
1529815271
SourceLocation Loc = FD->getLocation();
1529915272
HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr,
15300-
Args, /*ArgValues*/ nullptr, FD->getBody(), Info,
15301-
Scratch, nullptr);
15273+
Args, FD->getBody(), Info, Scratch, nullptr);
1530215274
}
1530315275

1530415276
return Diags.empty();
@@ -15320,8 +15292,13 @@ bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
1532015292
Info.CheckingPotentialConstantExpression = true;
1532115293

1532215294
// Fabricate a call stack frame to give the arguments a plausible cover story.
15323-
CallStackFrame Frame(Info, SourceLocation(), FD, /*This*/ nullptr,
15324-
/*ArgValues*/ nullptr);
15295+
ArrayRef<const Expr*> Args;
15296+
ArgVector ArgValues(0);
15297+
bool Success = EvaluateArgs(Args, ArgValues, Info, FD);
15298+
(void)Success;
15299+
assert(Success &&
15300+
"Failed to set up arguments for potential constant evaluation");
15301+
CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data());
1532515302

1532615303
APValue ResultScratch;
1532715304
Evaluate(ResultScratch, Info, E);

clang/test/SemaCXX/constant-expression-cxx1z.cpp

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -59,112 +59,3 @@ void test() {
5959
else if constexpr (v<int>) {}
6060
}
6161
}
62-
63-
// Check that assignment operators evaluate their operands right-to-left.
64-
namespace EvalOrder {
65-
template<typename T> struct lvalue {
66-
T t;
67-
constexpr T &get() { return t; }
68-
};
69-
70-
struct UserDefined {
71-
int n = 0;
72-
constexpr UserDefined &operator=(const UserDefined&) { return *this; }
73-
constexpr UserDefined &operator+=(const UserDefined&) { return *this; }
74-
constexpr void operator<<(const UserDefined&) const {}
75-
constexpr void operator>>(const UserDefined&) const {}
76-
constexpr void operator+(const UserDefined&) const {}
77-
constexpr void operator[](int) const {}
78-
};
79-
constexpr UserDefined ud;
80-
81-
struct NonMember {};
82-
constexpr void operator+=(NonMember, NonMember) {}
83-
constexpr void operator<<(NonMember, NonMember) {}
84-
constexpr void operator>>(NonMember, NonMember) {}
85-
constexpr void operator+(NonMember, NonMember) {}
86-
constexpr NonMember nm;
87-
88-
constexpr void f(...) {}
89-
90-
// Helper to ensure that 'a' is evaluated before 'b'.
91-
struct seq_checker {
92-
bool done_a = false;
93-
bool done_b = false;
94-
95-
template <typename T> constexpr T &&a(T &&v) {
96-
done_a = true;
97-
return (T &&)v;
98-
}
99-
template <typename T> constexpr T &&b(T &&v) {
100-
if (!done_a)
101-
throw "wrong";
102-
done_b = true;
103-
return (T &&)v;
104-
}
105-
106-
constexpr bool ok() { return done_a && done_b; }
107-
};
108-
109-
// SEQ(expr), where part of the expression is tagged A(...) and part is
110-
// tagged B(...), checks that A is evaluated before B.
111-
#define A sc.a
112-
#define B sc.b
113-
#define SEQ(...) static_assert([](seq_checker sc) { void(__VA_ARGS__); return sc.ok(); }({}))
114-
115-
// Longstanding sequencing rules.
116-
SEQ((A(1), B(2)));
117-
SEQ((A(true) ? B(2) : throw "huh?"));
118-
SEQ((A(false) ? throw "huh?" : B(2)));
119-
SEQ(A(true) && B(true));
120-
SEQ(A(false) || B(true));
121-
122-
// From P0145R3:
123-
124-
// Rules 1 and 2 have no effect ('b' is not an expression).
125-
126-
// Rule 3: a->*b
127-
SEQ(A(ud).*B(&UserDefined::n));
128-
SEQ(A(&ud)->*B(&UserDefined::n));
129-
130-
// Rule 4: a(b1, b2, b3)
131-
SEQ(A(f)(B(1), B(2), B(3)));
132-
133-
// Rule 5: b = a, b @= a
134-
SEQ(B(lvalue<int>().get()) = A(0));
135-
SEQ(B(lvalue<UserDefined>().get()) = A(ud));
136-
SEQ(B(lvalue<int>().get()) += A(0));
137-
SEQ(B(lvalue<UserDefined>().get()) += A(ud));
138-
SEQ(B(lvalue<NonMember>().get()) += A(nm));
139-
140-
// Rule 6: a[b]
141-
constexpr int arr[3] = {};
142-
SEQ(A(arr)[B(0)]);
143-
SEQ(A(+arr)[B(0)]);
144-
SEQ(A(0)[B(arr)]);
145-
SEQ(A(0)[B(+arr)]);
146-
SEQ(A(ud)[B(0)]);
147-
148-
// Rule 7: a << b
149-
SEQ(A(1) << B(2));
150-
SEQ(A(ud) << B(ud));
151-
SEQ(A(nm) << B(nm));
152-
153-
// Rule 8: a >> b
154-
SEQ(A(1) >> B(2));
155-
SEQ(A(ud) >> B(ud));
156-
SEQ(A(nm) >> B(nm));
157-
158-
// No particular order of evaluation is specified in other cases, but we in
159-
// practice evaluate left-to-right.
160-
// FIXME: Technically we're expected to check for undefined behavior due to
161-
// unsequenced read and modification and treat it as non-constant due to UB.
162-
SEQ(A(1) + B(2));
163-
SEQ(A(ud) + B(ud));
164-
SEQ(A(nm) + B(nm));
165-
SEQ(f(A(1), B(2)));
166-
167-
#undef SEQ
168-
#undef A
169-
#undef B
170-
}

clang/www/cxx_status.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@ <h2 id="cxx17">C++17 implementation status</h2>
807807
<tt>operator&amp;&amp;</tt>, <tt>operator||</tt>, and <tt>operator,</tt>
808808
functions using expression syntax are no longer guaranteed to be destroyed in
809809
reverse construction order in that ABI.
810-
This is not fully supported during constant expression evaluation until Clang 12.
811810
</span><br>
812811
<span id="p0522">(10): Despite being the resolution to a Defect Report, this
813812
feature is disabled by default in all language versions, and can be enabled

0 commit comments

Comments
 (0)