[Clang] allow restrict qualifier for array types with pointer types as element types (#120896)

Fixes #92847

---

> Types other than pointer types whose referenced type is an object type
and (possibly multi-dimensional) array types with such pointer types as
element type shall not be restrict-qualified.
This commit is contained in:
Oleksandr T.
2025-02-14 19:06:30 +02:00
committed by GitHub
parent 1435c8ed95
commit 3e94fc0682
6 changed files with 69 additions and 18 deletions

View File

@@ -93,6 +93,7 @@ C Language Changes
- Clang now allows an ``inline`` specifier on a typedef declaration of a
function type in Microsoft compatibility mode. #GH124869
- Clang now allows ``restrict`` qualifier for array types with pointer elements (#GH92847).
C2y Feature Support
^^^^^^^^^^^^^^^^^^^

View File

@@ -7413,6 +7413,11 @@ def warn_c23_compat_utf8_string : Warning<
def note_cxx20_c23_compat_utf8_string_remove_u8 : Note<
"remove 'u8' prefix to avoid a change of behavior; "
"Clang encodes unprefixed narrow string literals as UTF-8">;
def warn_c23_compat_restrict_on_array_of_pointers : Warning<
"'restrict' qualifier on an array of pointers is incompatible with C standards before C23">,
InGroup<CPre23Compat>, DefaultIgnore;
def ext_restrict_on_array_of_pointers_c23 : Extension<
"'restrict' qualifier on an array of pointers is a C23 extension">, InGroup<C23>;
def err_array_init_different_type : Error<
"cannot initialize array %diff{of type $ with array of type $|"
"with different type of array}0,1">;

View File

@@ -1593,35 +1593,38 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
// object or incomplete types shall not be restrict-qualified."
if (Qs.hasRestrict()) {
unsigned DiagID = 0;
QualType ProblemTy;
QualType EltTy = Context.getBaseElementType(T);
if (T->isAnyPointerType() || T->isReferenceType() ||
T->isMemberPointerType()) {
QualType EltTy;
if (T->isObjCObjectPointerType())
EltTy = T;
else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
EltTy->isMemberPointerType()) {
if (const auto *PTy = EltTy->getAs<MemberPointerType>())
EltTy = PTy->getPointeeType();
else
EltTy = T->getPointeeType();
EltTy = EltTy->getPointeeType();
// If we have a pointer or reference, the pointee must have an object
// incomplete type.
if (!EltTy->isIncompleteOrObjectType()) {
if (!EltTy->isIncompleteOrObjectType())
DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
ProblemTy = EltTy;
}
} else if (!isDependentOrGNUAutoType(T)) {
// For an __auto_type variable, we may not have seen the initializer yet
// and so have no idea whether the underlying type is a pointer type or
// not.
DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
ProblemTy = T;
EltTy = T;
}
Loc = DS ? DS->getRestrictSpecLoc() : Loc;
if (DiagID) {
Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
Diag(Loc, DiagID) << EltTy;
Qs.removeRestrict();
} else {
if (T->isArrayType())
Diag(Loc, getLangOpts().C23
? diag::warn_c23_compat_restrict_on_array_of_pointers
: diag::ext_restrict_on_array_of_pointers_c23);
}
}

View File

@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -verify=pedantic,expected %s
// RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=c2x-compat,expected %s
typedef int (*T1)[2];
restrict T1 t1;
typedef int *T2[2];
restrict T2 t2; // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
// c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
typedef int *T3[2][2];
restrict T3 t3; // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
// c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
typedef int (*t4)(); // pedantic-warning {{a function declaration without a prototype is deprecated in all versions of C}}
typedef t4 t5[2];
typedef t5 restrict t6; // // expected-error-re {{pointer to function type 'int {{\((void)?\)}}' may not be 'restrict' qualified}}
typedef int t7[2];
typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}}

View File

@@ -0,0 +1,21 @@
// RUN: %clang_cc1 -std=c2y -fsyntax-only -verify -pedantic %s
typedef int (*T1)[2];
restrict T1 t1;
static_assert(_Generic(typeof (t1), int (*restrict)[2] : 1, default : 0));
typedef int *T2[2];
restrict T2 t2;
static_assert(_Generic(typeof (t2), int *restrict[2] : 1, default : 0));
typedef int *T3[2][2];
restrict T3 t3;
static_assert(_Generic(typeof (t3), int *restrict[2][2] : 1, default : 0));
static_assert(_Generic(void(T3 restrict), void(int *restrict (*)[2]): 1, default: 0));
typedef int (*t4)();
typedef t4 t5[2];
typedef t5 restrict t6; // expected-error {{pointer to function type 'int (void)' may not be 'restrict' qualified}}
typedef int t7[2];
typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]')}}

View File

@@ -9,20 +9,20 @@ typedef int (*T)[2];
restrict T x;
typedef int *S[2];
restrict S y; // expected-error {{restrict requires a pointer or reference ('S' (aka 'int *[2]') is invalid)}}
restrict S y; // expected-warning {{'restrict' qualifier on an array of pointers is a C23 extension}}
// int128_t is available.
int a(void) {
void a(void) {
__int128_t s;
__uint128_t t;
}
// but not a keyword
int b(void) {
void b(void) {
int __int128_t;
int __uint128_t;
}
// __int128 is a keyword
int c(void) {
__int128 i;