Skip to content

Commit e02f443

Browse files
committed
address review comments
1 parent b2f4d35 commit e02f443

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13533,10 +13533,14 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
1353313533
if (!EvaluateAsRValue(Info, E->getArg(0), Source))
1353413534
return false;
1353513535

13536-
auto SourceLen = Source.getVectorLength();
13536+
unsigned SourceLen = Source.getVectorLength();
1353713537
APSInt Reduced = Source.getVectorElt(0).getInt();
1353813538
for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
13539-
Reduced += Source.getVectorElt(EltNum).getInt();
13539+
if (!CheckedIntArithmetic(
13540+
Info, E, Reduced, Source.getVectorElt(EltNum).getInt(),
13541+
Reduced.getBitWidth() + 1, std::plus<APSInt>(), Reduced)) {
13542+
return false;
13543+
}
1354013544
}
1354113545

1354213546
return Success(Reduced, E);

clang/test/Sema/constant_builtins_vector.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,15 @@ static_assert(__builtin_reduce_add((vector4char){1, 2, 3, 4}) == 10);
729729
static_assert(__builtin_reduce_add((vector4short){10, 20, 30, 40}) == 100);
730730
static_assert(__builtin_reduce_add((vector4int){100, 200, 300, 400}) == 1000);
731731
static_assert(__builtin_reduce_add((vector4long){1000, 2000, 3000, 4000}) == 10000);
732+
constexpr int reduceAddInt1 = __builtin_reduce_add((vector4int){~(1 << 31), 0, 0, 1});
733+
// expected-error@-1 {{must be initialized by a constant expression}} \
734+
// expected-note@-1 {{outside the range of representable values of type 'int'}}
735+
constexpr long long reduceAddLong1 = __builtin_reduce_add((vector4long){~(1LL << 63), 0, 0, 1});
736+
// expected-error@-1 {{must be initialized by a constant expression}} \
737+
// expected-note@-1 {{outside the range of representable values of type 'long long'}}
738+
constexpr int reduceAddInt2 = __builtin_reduce_add((vector4int){(1 << 31), 0, 0, -1});
739+
// expected-error@-1 {{must be initialized by a constant expression}} \
740+
// expected-note@-1 {{outside the range of representable values of type 'int'}}
741+
constexpr long long reduceAddLong2 = __builtin_reduce_add((vector4long){(1LL << 63), 0, 0, -1});
742+
// expected-error@-1 {{must be initialized by a constant expression}} \
743+
// expected-note@-1 {{outside the range of representable values of type 'long long'}}

0 commit comments

Comments
 (0)