Skip to content

Commit 3e94fc0

Browse files
authored
[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.
1 parent 1435c8e commit 3e94fc0

File tree

6 files changed

+70
-19
lines changed

6 files changed

+70
-19
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ C Language Changes
9393

9494
- Clang now allows an ``inline`` specifier on a typedef declaration of a
9595
function type in Microsoft compatibility mode. #GH124869
96+
- Clang now allows ``restrict`` qualifier for array types with pointer elements (#GH92847).
9697

9798
C2y Feature Support
9899
^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7413,6 +7413,11 @@ def warn_c23_compat_utf8_string : Warning<
74137413
def note_cxx20_c23_compat_utf8_string_remove_u8 : Note<
74147414
"remove 'u8' prefix to avoid a change of behavior; "
74157415
"Clang encodes unprefixed narrow string literals as UTF-8">;
7416+
def warn_c23_compat_restrict_on_array_of_pointers : Warning<
7417+
"'restrict' qualifier on an array of pointers is incompatible with C standards before C23">,
7418+
InGroup<CPre23Compat>, DefaultIgnore;
7419+
def ext_restrict_on_array_of_pointers_c23 : Extension<
7420+
"'restrict' qualifier on an array of pointers is a C23 extension">, InGroup<C23>;
74167421
def err_array_init_different_type : Error<
74177422
"cannot initialize array %diff{of type $ with array of type $|"
74187423
"with different type of array}0,1">;

clang/lib/Sema/SemaType.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,35 +1593,38 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
15931593
// object or incomplete types shall not be restrict-qualified."
15941594
if (Qs.hasRestrict()) {
15951595
unsigned DiagID = 0;
1596-
QualType ProblemTy;
1597-
1598-
if (T->isAnyPointerType() || T->isReferenceType() ||
1599-
T->isMemberPointerType()) {
1600-
QualType EltTy;
1601-
if (T->isObjCObjectPointerType())
1602-
EltTy = T;
1603-
else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
1596+
QualType EltTy = Context.getBaseElementType(T);
1597+
1598+
if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
1599+
EltTy->isMemberPointerType()) {
1600+
1601+
if (const auto *PTy = EltTy->getAs<MemberPointerType>())
16041602
EltTy = PTy->getPointeeType();
16051603
else
1606-
EltTy = T->getPointeeType();
1604+
EltTy = EltTy->getPointeeType();
16071605

16081606
// If we have a pointer or reference, the pointee must have an object
16091607
// incomplete type.
1610-
if (!EltTy->isIncompleteOrObjectType()) {
1608+
if (!EltTy->isIncompleteOrObjectType())
16111609
DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
1612-
ProblemTy = EltTy;
1613-
}
1610+
16141611
} else if (!isDependentOrGNUAutoType(T)) {
16151612
// For an __auto_type variable, we may not have seen the initializer yet
16161613
// and so have no idea whether the underlying type is a pointer type or
16171614
// not.
16181615
DiagID = diag::err_typecheck_invalid_restrict_not_pointer;
1619-
ProblemTy = T;
1616+
EltTy = T;
16201617
}
16211618

1619+
Loc = DS ? DS->getRestrictSpecLoc() : Loc;
16221620
if (DiagID) {
1623-
Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
1621+
Diag(Loc, DiagID) << EltTy;
16241622
Qs.removeRestrict();
1623+
} else {
1624+
if (T->isArrayType())
1625+
Diag(Loc, getLangOpts().C23
1626+
? diag::warn_c23_compat_restrict_on_array_of_pointers
1627+
: diag::ext_restrict_on_array_of_pointers_c23);
16251628
}
16261629
}
16271630

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -verify=pedantic,expected %s
3+
// RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=c2x-compat,expected %s
4+
5+
typedef int (*T1)[2];
6+
restrict T1 t1;
7+
8+
typedef int *T2[2];
9+
restrict T2 t2; // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
10+
// c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
11+
12+
typedef int *T3[2][2];
13+
restrict T3 t3; // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
14+
// c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
15+
16+
typedef int (*t4)(); // pedantic-warning {{a function declaration without a prototype is deprecated in all versions of C}}
17+
typedef t4 t5[2];
18+
typedef t5 restrict t6; // // expected-error-re {{pointer to function type 'int {{\((void)?\)}}' may not be 'restrict' qualified}}
19+
20+
typedef int t7[2];
21+
typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}}

clang/test/Sema/restrict-qualifier.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -std=c2y -fsyntax-only -verify -pedantic %s
2+
3+
typedef int (*T1)[2];
4+
restrict T1 t1;
5+
static_assert(_Generic(typeof (t1), int (*restrict)[2] : 1, default : 0));
6+
7+
typedef int *T2[2];
8+
restrict T2 t2;
9+
static_assert(_Generic(typeof (t2), int *restrict[2] : 1, default : 0));
10+
11+
typedef int *T3[2][2];
12+
restrict T3 t3;
13+
static_assert(_Generic(typeof (t3), int *restrict[2][2] : 1, default : 0));
14+
static_assert(_Generic(void(T3 restrict), void(int *restrict (*)[2]): 1, default: 0));
15+
16+
typedef int (*t4)();
17+
typedef t4 t5[2];
18+
typedef t5 restrict t6; // expected-error {{pointer to function type 'int (void)' may not be 'restrict' qualified}}
19+
20+
typedef int t7[2];
21+
typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]')}}

clang/test/Sema/types.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ typedef int (*T)[2];
99
restrict T x;
1010

1111
typedef int *S[2];
12-
restrict S y; // expected-error {{restrict requires a pointer or reference ('S' (aka 'int *[2]') is invalid)}}
13-
14-
12+
restrict S y; // expected-warning {{'restrict' qualifier on an array of pointers is a C23 extension}}
1513

1614
// int128_t is available.
17-
int a(void) {
15+
void a(void) {
1816
__int128_t s;
1917
__uint128_t t;
2018
}
19+
2120
// but not a keyword
22-
int b(void) {
21+
void b(void) {
2322
int __int128_t;
2423
int __uint128_t;
2524
}
25+
2626
// __int128 is a keyword
2727
int c(void) {
2828
__int128 i;

0 commit comments

Comments
 (0)