Skip to content

Commit f66627b

Browse files
committed
add additional diagnostics
1 parent 31c0d88 commit f66627b

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7409,6 +7409,11 @@ def warn_c23_compat_utf8_string : Warning<
74097409
def note_cxx20_c23_compat_utf8_string_remove_u8 : Note<
74107410
"remove 'u8' prefix to avoid a change of behavior; "
74117411
"Clang encodes unprefixed narrow string literals as UTF-8">;
7412+
def warn_c23_compat_restrict_pointers_to_array : Warning<
7413+
"'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier">,
7414+
InGroup<CPre23Compat>, DefaultIgnore;
7415+
def ext_restrict_pointers_to_array_c23 : Extension<
7416+
"'restrict' qualifier on pointers to arrays is a C23 extension">, InGroup<C23>;
74127417
def err_array_init_different_type : Error<
74137418
"cannot initialize array %diff{of type $ with array of type $|"
74147419
"with different type of array}0,1">;

clang/lib/Sema/SemaType.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,9 +1616,15 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
16161616
EltTy = T;
16171617
}
16181618

1619+
Loc = DS ? DS->getRestrictSpecLoc() : Loc;
16191620
if (DiagID) {
1620-
Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << EltTy;
1621+
Diag(Loc, DiagID) << EltTy;
16211622
Qs.removeRestrict();
1623+
} else {
1624+
if (!getLangOpts().C23 && (T->isArrayType() || EltTy->isArrayType())) {
1625+
Diag(Loc, diag::ext_restrict_pointers_to_array_c23);
1626+
Diag(Loc, diag::warn_c23_compat_restrict_pointers_to_array);
1627+
}
16221628
}
16231629
}
16241630

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

clang/test/Sema/types.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
// RUN: %clang_cc1 %s -fblocks -pedantic -verify -triple=powerpc64-ibm-aix-xcoff
77

88
typedef int (*T)[2];
9-
restrict T x;
9+
restrict T x; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
1010

1111
typedef int *S[2];
12-
restrict S y;
12+
restrict S y; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
1313

1414
// int128_t is available.
1515
int a(void) {

0 commit comments

Comments
 (0)