Skip to content

Commit 8292aac

Browse files
committed
Address Shafik's feedback
1 parent 62248be commit 8292aac

File tree

3 files changed

+62
-68
lines changed

3 files changed

+62
-68
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3933,9 +3933,10 @@ class Sema final {
39333933
APValue &Value, CCEKind CCE,
39343934
NamedDecl *Dest = nullptr);
39353935

3936-
ExprResult EvaluateConvertedConstantExpression(Expr *E, QualType T,
3937-
APValue &Value, CCEKind CCE,
3938-
bool RequireInt);
3936+
ExprResult
3937+
EvaluateConvertedConstantExpression(Expr *E, QualType T, APValue &Value,
3938+
CCEKind CCE, bool RequireInt,
3939+
const APValue &PreNarrowingValue);
39393940

39403941
/// Abstract base class used to perform a contextual implicit
39413942
/// conversion from an expression to any type passing a filter.

clang/lib/Sema/SemaOverload.cpp

Lines changed: 56 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6123,61 +6123,6 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
61236123
return Result;
61246124
}
61256125

6126-
/// EvaluateConvertedConstantExpression - Evaluate an Expression
6127-
/// That is a converted constant expression
6128-
/// (which was built with BuildConvertedConstantExpression)
6129-
static ExprResult EvaluateConvertedConstantExpression(
6130-
Sema &S, Expr *E, QualType T, APValue &Value, Sema::CCEKind CCE,
6131-
bool RequireInt, const APValue &PreNarrowingValue) {
6132-
ExprResult Result = E;
6133-
// Check the expression is a constant expression.
6134-
SmallVector<PartialDiagnosticAt, 8> Notes;
6135-
Expr::EvalResult Eval;
6136-
Eval.Diag = &Notes;
6137-
6138-
ConstantExprKind Kind;
6139-
if (CCE == Sema::CCEK_TemplateArg && T->isRecordType())
6140-
Kind = ConstantExprKind::ClassTemplateArgument;
6141-
else if (CCE == Sema::CCEK_TemplateArg)
6142-
Kind = ConstantExprKind::NonClassTemplateArgument;
6143-
else
6144-
Kind = ConstantExprKind::Normal;
6145-
6146-
if (!E->EvaluateAsConstantExpr(Eval, S.Context, Kind) ||
6147-
(RequireInt && !Eval.Val.isInt())) {
6148-
// The expression can't be folded, so we can't keep it at this position in
6149-
// the AST.
6150-
Result = ExprError();
6151-
} else {
6152-
Value = Eval.Val;
6153-
6154-
if (Notes.empty()) {
6155-
// It's a constant expression.
6156-
Expr *E = ConstantExpr::Create(S.Context, Result.get(), Value);
6157-
if (!PreNarrowingValue.isAbsent())
6158-
Value = std::move(PreNarrowingValue);
6159-
return E;
6160-
}
6161-
}
6162-
6163-
// It's not a constant expression. Produce an appropriate diagnostic.
6164-
if (Notes.size() == 1 &&
6165-
Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) {
6166-
S.Diag(Notes[0].first, diag::err_expr_not_cce) << CCE;
6167-
} else if (!Notes.empty() && Notes[0].second.getDiagID() ==
6168-
diag::note_constexpr_invalid_template_arg) {
6169-
Notes[0].second.setDiagID(diag::err_constexpr_invalid_template_arg);
6170-
for (unsigned I = 0; I < Notes.size(); ++I)
6171-
S.Diag(Notes[I].first, Notes[I].second);
6172-
} else {
6173-
S.Diag(E->getBeginLoc(), diag::err_expr_not_cce)
6174-
<< CCE << E->getSourceRange();
6175-
for (unsigned I = 0; I < Notes.size(); ++I)
6176-
S.Diag(Notes[I].first, Notes[I].second);
6177-
}
6178-
return ExprError();
6179-
}
6180-
61816126
/// CheckConvertedConstantExpression - Check that the expression From is a
61826127
/// converted constant expression of type T, perform the conversion and produce
61836128
/// the converted expression, per C++11 [expr.const]p3.
@@ -6194,8 +6139,8 @@ static ExprResult CheckConvertedConstantExpression(Sema &S, Expr *From,
61946139
Value = APValue();
61956140
return Result;
61966141
}
6197-
return EvaluateConvertedConstantExpression(S, Result.get(), T, Value, CCE,
6198-
RequireInt, PreNarrowingValue);
6142+
return S.EvaluateConvertedConstantExpression(Result.get(), T, Value, CCE,
6143+
RequireInt, PreNarrowingValue);
61996144
}
62006145

62016146
ExprResult Sema::BuildConvertedConstantExpression(Expr *From, QualType T,
@@ -6226,14 +6171,61 @@ ExprResult Sema::CheckConvertedConstantExpression(Expr *From, QualType T,
62266171
return R;
62276172
}
62286173

6229-
ExprResult Sema::EvaluateConvertedConstantExpression(Expr *E, QualType T,
6230-
APValue &Value,
6231-
Sema::CCEKind CCE,
6232-
bool RequireInt) {
6174+
/// EvaluateConvertedConstantExpression - Evaluate an Expression
6175+
/// That is a converted constant expression
6176+
/// (which was built with BuildConvertedConstantExpression)
6177+
ExprResult
6178+
Sema::EvaluateConvertedConstantExpression(Expr *E, QualType T, APValue &Value,
6179+
Sema::CCEKind CCE, bool RequireInt,
6180+
const APValue &PreNarrowingValue) {
62336181

6234-
APValue PreNarrowingValue;
6235-
return ::EvaluateConvertedConstantExpression(*this, E, T, Value, CCE,
6236-
RequireInt, PreNarrowingValue);
6182+
ExprResult Result = E;
6183+
// Check the expression is a constant expression.
6184+
SmallVector<PartialDiagnosticAt, 8> Notes;
6185+
Expr::EvalResult Eval;
6186+
Eval.Diag = &Notes;
6187+
6188+
ConstantExprKind Kind;
6189+
if (CCE == Sema::CCEK_TemplateArg && T->isRecordType())
6190+
Kind = ConstantExprKind::ClassTemplateArgument;
6191+
else if (CCE == Sema::CCEK_TemplateArg)
6192+
Kind = ConstantExprKind::NonClassTemplateArgument;
6193+
else
6194+
Kind = ConstantExprKind::Normal;
6195+
6196+
if (!E->EvaluateAsConstantExpr(Eval, Context, Kind) ||
6197+
(RequireInt && !Eval.Val.isInt())) {
6198+
// The expression can't be folded, so we can't keep it at this position in
6199+
// the AST.
6200+
Result = ExprError();
6201+
} else {
6202+
Value = Eval.Val;
6203+
6204+
if (Notes.empty()) {
6205+
// It's a constant expression.
6206+
Expr *E = ConstantExpr::Create(Context, Result.get(), Value);
6207+
if (!PreNarrowingValue.isAbsent())
6208+
Value = std::move(PreNarrowingValue);
6209+
return E;
6210+
}
6211+
}
6212+
6213+
// It's not a constant expression. Produce an appropriate diagnostic.
6214+
if (Notes.size() == 1 &&
6215+
Notes[0].second.getDiagID() == diag::note_invalid_subexpr_in_const_expr) {
6216+
Diag(Notes[0].first, diag::err_expr_not_cce) << CCE;
6217+
} else if (!Notes.empty() && Notes[0].second.getDiagID() ==
6218+
diag::note_constexpr_invalid_template_arg) {
6219+
Notes[0].second.setDiagID(diag::err_constexpr_invalid_template_arg);
6220+
for (unsigned I = 0; I < Notes.size(); ++I)
6221+
Diag(Notes[I].first, Notes[I].second);
6222+
} else {
6223+
Diag(E->getBeginLoc(), diag::err_expr_not_cce)
6224+
<< CCE << E->getSourceRange();
6225+
for (unsigned I = 0; I < Notes.size(); ++I)
6226+
Diag(Notes[I].first, Notes[I].second);
6227+
}
6228+
return ExprError();
62376229
}
62386230

62396231
/// dropPointerConversions - If the given standard conversion sequence

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7391,9 +7391,10 @@ ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
73917391
return ArgResult;
73927392
}
73937393

7394+
APValue PreNarrowingValue;
73947395
ArgResult = EvaluateConvertedConstantExpression(
73957396
ArgResult.get(), ParamType, Value, CCEK_TemplateArg, /*RequireInt=*/
7396-
false);
7397+
false, PreNarrowingValue);
73977398
if (ArgResult.isInvalid())
73987399
return ExprError();
73997400

0 commit comments

Comments
 (0)