Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ Improvements to Clang's diagnostics
that were previously incorrectly accepted in case of other irrelevant
conditions are now consistently diagnosed, identical to C++ mode.

- Clang now emits a diagnostic in case `vector_size` or `ext_vector_type`
attributes are used with a negative size (#GH165463).

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3510,6 +3510,8 @@ def err_init_method_bad_return_type : Error<
"init methods must return an object pointer type, not %0">;
def err_attribute_invalid_size : Error<
"vector size not an integral multiple of component size">;
def err_attribute_vec_negative_size
: Error<"vector must have non-negative size">;
def err_attribute_zero_size : Error<"zero %0 size">;
def err_attribute_size_too_large : Error<"%0 size too large">;
def err_typecheck_sve_rvv_ambiguous : Error<
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,11 @@ QualType Sema::BuildVectorType(QualType CurType, Expr *SizeExpr,
return QualType();
}

if (VecSize->isNegative()) {
Diag(SizeExpr->getExprLoc(), diag::err_attribute_vec_negative_size);
return QualType();
}

if (CurType->isDependentType())
return Context.getDependentVectorType(CurType, SizeExpr, AttrLoc,
VectorKind::Generic);
Expand Down Expand Up @@ -2427,6 +2432,11 @@ QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize,
return QualType();
}

if (vecSize->isNegative()) {
Diag(ArraySize->getExprLoc(), diag::err_attribute_vec_negative_size);
return QualType();
}

if (!vecSize->isIntN(32)) {
Diag(AttrLoc, diag::err_attribute_size_too_large)
<< ArraySize->getSourceRange() << "vector";
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaCXX/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,3 +786,16 @@ const long long e = *0; // expected-error {{indirection requires pointer operand
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)}}
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)}}
}

typedef int v_neg_size __attribute__((vector_size(-8))); // expected-error{{vector must have non-negative size}}
typedef int v_neg_size_2 __attribute__((vector_size(-1 * 8))); // expected-error{{vector must have non-negative size}}
typedef int v_ext_neg_size __attribute__((ext_vector_type(-8))); // expected-error{{vector must have non-negative size}}
typedef int v_ext_neg_size2 __attribute__((ext_vector_type(-1 * 8))); // expected-error{{vector must have non-negative size}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another test case to add which I think won't be handled currently but will after moving into the Build functions is: https://godbolt.org/z/avobhW3Yx

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it works. I just need to write this test in another file that allows C++11 extension to be able to use using

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot i can use ifdef to make it for specific version :D

Copy link
Member Author

@AmrDeveloper AmrDeveloper Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why the parameter name is ArraySize, not VectorSize or Size 🤔 ?

QualType Sema::BuildExtVectorType(QualType T, Expr *ArraySize, <---- SourceLocation AttrLoc)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's just a think-o through refactoring, probably. Would be fine to rename in an NFC commit, IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will create NFC for that soon @AaronBallman

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done #166208



#if __cplusplus >= 201103L

template <int N> using templated_v_size = int __attribute__((vector_size(N))); // expected-error{{vector must have non-negative size}}
templated_v_size<-8> templated_v_neg_size; //expected-note{{in instantiation of template type alias 'templated_v_size' requested here}}

#endif