Skip to content

Commit fed8695

Browse files
authored
[clang][bytecode] Emit better diagnostic for invalid shufflevector index (#111643)
1 parent 275a2b0 commit fed8695

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3586,8 +3586,9 @@ bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
35863586
}
35873587
for (unsigned I = 0; I != NumOutputElems; ++I) {
35883588
APSInt ShuffleIndex = E->getShuffleMaskIdx(Ctx.getASTContext(), I);
3589+
assert(ShuffleIndex >= -1);
35893590
if (ShuffleIndex == -1)
3590-
return this->emitInvalid(E); // FIXME: Better diagnostic.
3591+
return this->emitInvalidShuffleVectorIndex(I, E);
35913592

35923593
assert(ShuffleIndex < (NumInputElems * 2));
35933594
if (!this->emitGetLocal(PT_Ptr,

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/AST/DeclObjC.h"
2222
#include "clang/AST/Expr.h"
2323
#include "clang/AST/ExprCXX.h"
24+
#include "clang/Basic/DiagnosticSema.h"
2425
#include "llvm/ADT/APSInt.h"
2526
#include "llvm/ADT/StringExtras.h"
2627
#include <limits>
@@ -1406,6 +1407,14 @@ bool handleFixedPointOverflow(InterpState &S, CodePtr OpPC,
14061407
return S.noteUndefinedBehavior();
14071408
}
14081409

1410+
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index) {
1411+
const SourceInfo &Loc = S.Current->getSource(OpPC);
1412+
S.FFDiag(Loc,
1413+
diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
1414+
<< Index;
1415+
return false;
1416+
}
1417+
14091418
// https://github.com/llvm/llvm-project/issues/102513
14101419
#if defined(_WIN32) && !defined(__clang__) && !defined(NDEBUG)
14111420
#pragma optimize("", off)

clang/lib/AST/ByteCode/Interp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ bool CallBI(InterpState &S, CodePtr OpPC, const Function *Func,
161161
bool CallPtr(InterpState &S, CodePtr OpPC, uint32_t ArgSize,
162162
const CallExpr *CE);
163163
bool CheckLiteralType(InterpState &S, CodePtr OpPC, const Type *T);
164+
bool InvalidShuffleVectorIndex(InterpState &S, CodePtr OpPC, uint32_t Index);
164165

165166
template <typename T>
166167
static bool handleOverflow(InterpState &S, CodePtr OpPC, const T &SrcValue) {

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,9 @@ def InvalidDeclRef : Opcode {
773773
}
774774

775775
def SizelessVectorElementSize : Opcode;
776+
def InvalidShuffleVectorIndex : Opcode {
777+
let Args = [ArgUint32];
778+
}
776779

777780
def Assume : Opcode;
778781

clang/test/AST/ByteCode/builtin-functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ namespace shufflevector {
956956
static_assert(vectorShuffle6[7] == 7, "");
957957

958958
constexpr vector4char vectorShuffleFail1 = __builtin_shufflevector( // both-error {{must be initialized by a constant expression}}\
959-
// ref-error {{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}}
959+
// both-error {{index for __builtin_shufflevector not within the bounds of the input vectors; index of -1 found at position 0 is not permitted in a constexpr context}}
960960
vector4charConst1,
961961
vector4charConst2, -1, -1, -1, -1);
962962
}

0 commit comments

Comments
 (0)