Skip to content

Commit ecdd660

Browse files
authored
[clang] Report Diagnostic when builtin vector has negative size (#166055)
Report a diagnostic in case vector_size or ext_vector_type attributes are used with a negative size. The same evaluation result can be used for other checks, for example, the too big a size. Issue #165463
1 parent 425fe33 commit ecdd660

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ Improvements to Clang's diagnostics
395395
that were previously incorrectly accepted in case of other irrelevant
396396
conditions are now consistently diagnosed, identical to C++ mode.
397397

398+
- Clang now emits a diagnostic in case `vector_size` or `ext_vector_type`
399+
attributes are used with a negative size (#GH165463).
400+
398401
Improvements to Clang's time-trace
399402
----------------------------------
400403

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,8 @@ def err_init_method_bad_return_type : Error<
35103510
"init methods must return an object pointer type, not %0">;
35113511
def err_attribute_invalid_size : Error<
35123512
"vector size not an integral multiple of component size">;
3513+
def err_attribute_vec_negative_size
3514+
: Error<"vector must have non-negative size">;
35133515
def err_attribute_zero_size : Error<"zero %0 size">;
35143516
def err_attribute_size_too_large : Error<"%0 size too large">;
35153517
def err_typecheck_sve_rvv_ambiguous : Error<

clang/lib/Sema/SemaType.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,6 +2358,11 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr,
23582358
return QualType();
23592359
}
23602360

2361+
if (VecSize->isNegative()) {
2362+
Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
2363+
return QualType();
2364+
}
2365+
23612366
if (CurType->isDependentType())
23622367
return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc,
23632368
VectorKind::Generic);
@@ -2427,6 +2432,11 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
24272432
return QualType();
24282433
}
24292434

2435+
if (vecSize->isNegative()) {
2436+
Diag(ArraySize->getExprLoc(), diag::err_attribute_vec_negative_size);
2437+
return QualType();
2438+
}
2439+
24302440
if (!vecSize->isIntN(32)) {
24312441
Diag(AttrLoc, diag::err_attribute_size_too_large)
24322442
<< ArraySize->getSourceRange() << "vector";

clang/test/SemaCXX/vector.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,16 @@ const long long e = *0; // expected-error {{indirection requires pointer operand
786786
double f = a - e; // expected-error {{cannot initialize a variable of type 'double' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(double)))) double' (vector of 1 'double' value)}}
787787
int h = c - e; // expected-error {{cannot initialize a variable of type 'int' with an rvalue of type '__attribute__((__vector_size__(1 * sizeof(long)))) long' (vector of 1 'long' value)}}
788788
}
789+
790+
typedef int v_neg_size __attribute__((vector_size(-8))); // expected-error{{vector must have non-negative size}}
791+
typedef int v_neg_size_2 __attribute__((vector_size(-1 * 8))); // expected-error{{vector must have non-negative size}}
792+
typedef int v_ext_neg_size __attribute__((ext_vector_type(-8))); // expected-error{{vector must have non-negative size}}
793+
typedef int v_ext_neg_size2 __attribute__((ext_vector_type(-1 * 8))); // expected-error{{vector must have non-negative size}}
794+
795+
796+
#if __cplusplus >= 201103L
797+
798+
template <int N> using templated_v_size = int __attribute__((vector_size(N))); // expected-error{{vector must have non-negative size}}
799+
templated_v_size<-8> templated_v_neg_size; //expected-note{{in instantiation of template type alias 'templated_v_size' requested here}}
800+
801+
#endif

0 commit comments

Comments
 (0)