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

Oleksandr T. via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 14 01:05:23 PST 2025


https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/120896

>From 295df258043ef5a87ae603eedd308b863bad7b59 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sun, 22 Dec 2024 15:14:30 +0200
Subject: [PATCH 01/13] [Clang] allow restrict qualifier for array types with
 pointer types as element types

---
 clang/docs/ReleaseNotes.rst |  1 +
 clang/lib/Sema/SemaType.cpp |  4 +++-
 clang/test/Sema/types.c     | 10 +++++-----
 3 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6b9e1109f3906..52daea9b8eb2b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -332,6 +332,7 @@ C Language Changes
 ------------------
 
 - Extend clang's ``<limits.h>`` to define ``LONG_LONG_*`` macros for Android's bionic.
+- Clang now allows ``restrict`` qualifier for array types with pointer elements (#GH92847).
 
 C2y Feature Support
 ^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 83464c50b4b23..e84daeee679a5 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1595,12 +1595,14 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
     QualType ProblemTy;
 
     if (T->isAnyPointerType() || T->isReferenceType() ||
-        T->isMemberPointerType()) {
+        T->isMemberPointerType() || T->isArrayType()) {
       QualType EltTy;
       if (T->isObjCObjectPointerType())
         EltTy = T;
       else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();
+      else if (T->isArrayType())
+        EltTy = Context.getBaseElementType(T);
       else
         EltTy = T->getPointeeType();
 
diff --git a/clang/test/Sema/types.c b/clang/test/Sema/types.c
index e0a6ba4f0691b..4c90634b7ce28 100644
--- a/clang/test/Sema/types.c
+++ b/clang/test/Sema/types.c
@@ -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;
 
 // int128_t is available.
 int a(void) {
   __int128_t s;
   __uint128_t t;
-}
+} // expected-warning {{non-void function does not return a value}}
+
 // but not a keyword
 int b(void) {
   int __int128_t;
   int __uint128_t;
-}
+} // expected-warning {{non-void function does not return a value}}
+
 // __int128 is a keyword
 int c(void) {
   __int128 i;

>From 9e8dcdb20a3dd1ea9b49d501c2593d1ac1c7e424 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Fri, 31 Jan 2025 23:54:03 +0200
Subject: [PATCH 02/13] add additional tests

---
 clang/lib/Sema/SemaType.cpp          |  2 +-
 clang/test/Sema/restrict-qualifier.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/restrict-qualifier.c

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e84daeee679a5..ab85d6f51d22f 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1602,7 +1602,7 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();
       else if (T->isArrayType())
-        EltTy = Context.getBaseElementType(T);
+        EltTy = BuildQualifiedType(Context.getBaseElementType(T), Loc, Qs);
       else
         EltTy = T->getPointeeType();
 
diff --git a/clang/test/Sema/restrict-qualifier.c b/clang/test/Sema/restrict-qualifier.c
new file mode 100644
index 0000000000000..a68e55b47b580
--- /dev/null
+++ b/clang/test/Sema/restrict-qualifier.c
@@ -0,0 +1,20 @@
+// 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));
+
+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 ('int' is invalid)}}

>From 7c85906d8a35abfa84ae20f7af952e2a15b6fa0f Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Sat, 1 Feb 2025 15:00:19 +0200
Subject: [PATCH 03/13] adjust array handling for clearer inner type check

---
 clang/lib/Sema/SemaType.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index da63522482390..3dd4952a829e0 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1598,12 +1598,14 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
     if (T->isAnyPointerType() || T->isReferenceType() ||
         T->isMemberPointerType() || T->isArrayType()) {
       QualType EltTy;
+      ProblemTy = Context.getBaseElementType(T);
+
       if (T->isObjCObjectPointerType())
         EltTy = T;
       else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();
       else if (T->isArrayType())
-        EltTy = BuildQualifiedType(Context.getBaseElementType(T), Loc, Qs);
+        EltTy = BuildQualifiedType(ProblemTy, Loc, Qs);
       else
         EltTy = T->getPointeeType();
 

>From 2dd3d224555ad569cbf84e6cde7b6b97ec436d9d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 4 Feb 2025 11:22:55 +0200
Subject: [PATCH 04/13] eliminate recursion in type qualification handling

---
 clang/lib/Sema/SemaType.cpp | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 3dd4952a829e0..3d38df8c3d859 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1593,38 +1593,30 @@ 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() || T->isArrayType()) {
-      QualType EltTy;
-      ProblemTy = Context.getBaseElementType(T);
+    if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
+        EltTy->isMemberPointerType() || EltTy->isArrayType()) {
 
-      if (T->isObjCObjectPointerType())
-        EltTy = T;
-      else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
+      if (const MemberPointerType *PTy = EltTy->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();
-      else if (T->isArrayType())
-        EltTy = BuildQualifiedType(ProblemTy, Loc, Qs);
       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)) {
+
+    } else if (!isDependentOrGNUAutoType(EltTy)) {
       // 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;
     }
 
     if (DiagID) {
-      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
+      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << EltTy;
       Qs.removeRestrict();
     }
   }

>From 0f097e7026ebe0a8a7e249da5c6b792b8a8ed08d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 4 Feb 2025 12:10:59 +0200
Subject: [PATCH 05/13] cleanup

---
 clang/lib/Sema/SemaType.cpp          | 3 ++-
 clang/test/Sema/restrict-qualifier.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 3d38df8c3d859..c9aa7c8488bc2 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1608,11 +1608,12 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       if (!EltTy->isIncompleteOrObjectType())
         DiagID = diag::err_typecheck_invalid_restrict_invalid_pointee;
 
-    } else if (!isDependentOrGNUAutoType(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;
+      EltTy = T;
     }
 
     if (DiagID) {
diff --git a/clang/test/Sema/restrict-qualifier.c b/clang/test/Sema/restrict-qualifier.c
index a68e55b47b580..1360c54d3f767 100644
--- a/clang/test/Sema/restrict-qualifier.c
+++ b/clang/test/Sema/restrict-qualifier.c
@@ -17,4 +17,4 @@ 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 ('int' is invalid)}}
+typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]')}}

>From 10ed4c2b0ec4e149b69fe9d213c0ecbbfddd863a Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 5 Feb 2025 11:15:50 +0200
Subject: [PATCH 06/13] cleanup

---
 clang/lib/Sema/SemaType.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index c9aa7c8488bc2..b447607d2d905 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1596,7 +1596,7 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
     QualType EltTy = Context.getBaseElementType(T);
 
     if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
-        EltTy->isMemberPointerType() || EltTy->isArrayType()) {
+        EltTy->isMemberPointerType()) {
 
       if (const MemberPointerType *PTy = EltTy->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();

>From 62f4c33fc353cbc74a8fc413b6b0a22364905ad4 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 5 Feb 2025 11:16:04 +0200
Subject: [PATCH 07/13] add additional test case

---
 clang/test/Sema/restrict-qualifier.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/test/Sema/restrict-qualifier.c b/clang/test/Sema/restrict-qualifier.c
index 1360c54d3f767..f96961c9c3314 100644
--- a/clang/test/Sema/restrict-qualifier.c
+++ b/clang/test/Sema/restrict-qualifier.c
@@ -11,6 +11,7 @@ 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];

>From f66627bc86448b1e02f2ca00d2d96165a430b582 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 11 Feb 2025 02:40:22 +0200
Subject: [PATCH 08/13] add additional diagnostics

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  5 ++++
 clang/lib/Sema/SemaType.cpp                   |  8 +++++-
 clang/test/Sema/pre-c2x-restrict-qualifier.c  | 26 +++++++++++++++++++
 clang/test/Sema/types.c                       |  4 +--
 4 files changed, 40 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/pre-c2x-restrict-qualifier.c

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index bcae9e9f30093..2d06bd84dca74 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7409,6 +7409,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_pointers_to_array : Warning<
+  "'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier">,
+  InGroup<CPre23Compat>, DefaultIgnore;
+def ext_restrict_pointers_to_array_c23 : Extension<
+  "'restrict' qualifier on pointers to arrays 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">;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 67064c59bc36e..f24ca6e02e9b7 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1616,9 +1616,15 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       EltTy = T;
     }
 
+    Loc = DS ? DS->getRestrictSpecLoc() : Loc;
     if (DiagID) {
-      Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << EltTy;
+      Diag(Loc, DiagID) << EltTy;
       Qs.removeRestrict();
+    } else {
+      if (!getLangOpts().C23 && (T->isArrayType() || EltTy->isArrayType())) {
+        Diag(Loc, diag::ext_restrict_pointers_to_array_c23);
+        Diag(Loc, diag::warn_c23_compat_restrict_pointers_to_array);
+      }
     }
   }
 
diff --git a/clang/test/Sema/pre-c2x-restrict-qualifier.c b/clang/test/Sema/pre-c2x-restrict-qualifier.c
new file mode 100644
index 0000000000000..c1126506268ac
--- /dev/null
+++ b/clang/test/Sema/pre-c2x-restrict-qualifier.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -verify=pedantic %s
+// RUN: %clang_cc1 -std=c17 -fsyntax-only -Wpre-c2x-compat -verify=pre-c2x-compat %s
+
+typedef int (*T1)[2];
+restrict T1 t1;         // pedantic-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}} \
+                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+
+typedef int *T2[2];
+restrict T2 t2;         // pedantic-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}} \
+                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+
+typedef int *T3[2][2];
+restrict T3 t3;         // pedantic-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}} \
+                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+
+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 {{pointer to function type 'int (void)' may not be 'restrict' qualified}} \
+                        // pedantic-error {{pointer to function type 'int ()' may not be 'restrict' qualified}} \
+                        // pre-c2x-compat-error {{pointer to function type 'int ()' 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)}} \
+                        // pedantic-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}} \
+                        // pre-c2x-compat-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid}}
diff --git a/clang/test/Sema/types.c b/clang/test/Sema/types.c
index 4c90634b7ce28..3a874e770cbd9 100644
--- a/clang/test/Sema/types.c
+++ b/clang/test/Sema/types.c
@@ -6,10 +6,10 @@
 // RUN: %clang_cc1 %s -fblocks -pedantic -verify -triple=powerpc64-ibm-aix-xcoff
 
 typedef int (*T)[2];
-restrict T x;
+restrict T x; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
 
 typedef int *S[2];
-restrict S y;
+restrict S y; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
 
 // int128_t is available.
 int a(void) {

>From b3fbbbd150cab2f54765c7647ddaea46685e642c Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Tue, 11 Feb 2025 22:46:36 +0200
Subject: [PATCH 09/13] update diagnostic messages

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  8 ++++----
 clang/lib/Sema/SemaType.cpp                      |  6 +++---
 clang/test/Sema/pre-c2x-restrict-qualifier.c     | 11 +++++------
 clang/test/Sema/types.c                          |  4 ++--
 4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9e610670e775a..8803b60169b71 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7409,11 +7409,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_pointers_to_array : Warning<
-  "'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier">,
+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_pointers_to_array_c23 : Extension<
-  "'restrict' qualifier on pointers to arrays is a C23 extension">, InGroup<C23>;
+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">;
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index f24ca6e02e9b7..5c7361d9244e4 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1621,9 +1621,9 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       Diag(Loc, DiagID) << EltTy;
       Qs.removeRestrict();
     } else {
-      if (!getLangOpts().C23 && (T->isArrayType() || EltTy->isArrayType())) {
-        Diag(Loc, diag::ext_restrict_pointers_to_array_c23);
-        Diag(Loc, diag::warn_c23_compat_restrict_pointers_to_array);
+      if (!getLangOpts().C23 && T->isArrayType()) {
+        Diag(Loc, diag::ext_restrict_on_array_of_pointers_c23);
+        Diag(Loc, diag::warn_c23_compat_restrict_on_array_of_pointers);
       }
     }
   }
diff --git a/clang/test/Sema/pre-c2x-restrict-qualifier.c b/clang/test/Sema/pre-c2x-restrict-qualifier.c
index c1126506268ac..ac00bffec6637 100644
--- a/clang/test/Sema/pre-c2x-restrict-qualifier.c
+++ b/clang/test/Sema/pre-c2x-restrict-qualifier.c
@@ -3,16 +3,15 @@
 // RUN: %clang_cc1 -std=c17 -fsyntax-only -Wpre-c2x-compat -verify=pre-c2x-compat %s
 
 typedef int (*T1)[2];
-restrict T1 t1;         // pedantic-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}} \
-                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+restrict T1 t1;
 
 typedef int *T2[2];
-restrict T2 t2;         // pedantic-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}} \
-                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+restrict T2 t2;         // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
+                        // pre-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 pointers to arrays is a C23 extension}} \
-                        // pre-c2x-compat-warning {{'restrict' qualifier on pointers to arrays is incompatible with C17 and earlier}}
+restrict T3 t3;         // pedantic-warning {{'restrict' qualifier on an array of pointers is a C23 extension}} \
+                        // pre-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];
diff --git a/clang/test/Sema/types.c b/clang/test/Sema/types.c
index 3a874e770cbd9..cd8bea477d998 100644
--- a/clang/test/Sema/types.c
+++ b/clang/test/Sema/types.c
@@ -6,10 +6,10 @@
 // RUN: %clang_cc1 %s -fblocks -pedantic -verify -triple=powerpc64-ibm-aix-xcoff
 
 typedef int (*T)[2];
-restrict T x; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
+restrict T x;
 
 typedef int *S[2];
-restrict S y; // expected-warning {{'restrict' qualifier on pointers to arrays is a C23 extension}}
+restrict S y; // expected-warning {{'restrict' qualifier on an array of pointers is a C23 extension}}
 
 // int128_t is available.
 int a(void) {

>From 1bd2f015d2ab892bc307e11ff169c9f95a51f85d Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 12 Feb 2025 00:28:44 +0200
Subject: [PATCH 10/13] update C2x diagnostic handling logic

---
 clang/lib/Sema/SemaType.cpp                  |  8 +++++---
 clang/test/Sema/pre-c2x-restrict-qualifier.c | 10 +++++-----
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 89adab02a69ae..418df24c14c63 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1621,9 +1621,11 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       Diag(Loc, DiagID) << EltTy;
       Qs.removeRestrict();
     } else {
-      if (!getLangOpts().C23 && T->isArrayType()) {
-        Diag(Loc, diag::ext_restrict_on_array_of_pointers_c23);
-        Diag(Loc, diag::warn_c23_compat_restrict_on_array_of_pointers);
+      if (T->isArrayType()) {
+        if (getLangOpts().C23)
+          Diag(Loc, diag::warn_c23_compat_restrict_on_array_of_pointers);
+        else
+          Diag(Loc, diag::ext_restrict_on_array_of_pointers_c23);
       }
     }
   }
diff --git a/clang/test/Sema/pre-c2x-restrict-qualifier.c b/clang/test/Sema/pre-c2x-restrict-qualifier.c
index ac00bffec6637..05406afaf0929 100644
--- a/clang/test/Sema/pre-c2x-restrict-qualifier.c
+++ b/clang/test/Sema/pre-c2x-restrict-qualifier.c
@@ -1,25 +1,25 @@
 // RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
 // RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -verify=pedantic %s
-// RUN: %clang_cc1 -std=c17 -fsyntax-only -Wpre-c2x-compat -verify=pre-c2x-compat %s
+// RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=c2x-compat %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}} \
-                        // pre-c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
+                        // 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}} \
-                        // pre-c2x-compat-warning {{'restrict' qualifier on an array of pointers is incompatible with C standards before C23}}
+                        // 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 {{pointer to function type 'int (void)' may not be 'restrict' qualified}} \
                         // pedantic-error {{pointer to function type 'int ()' may not be 'restrict' qualified}} \
-                        // pre-c2x-compat-error {{pointer to function type 'int ()' may not be 'restrict' qualified}}
+                        // c2x-compat-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]') is invalid)}} \
                         // pedantic-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}} \
-                        // pre-c2x-compat-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid}}
+                        // c2x-compat-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid}}

>From 88b2b07ec02e942afaa0526895e397c3978ca365 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 12 Feb 2025 22:35:59 +0200
Subject: [PATCH 11/13] cleanup duplicate error messages

---
 clang/test/Sema/pre-c2x-restrict-qualifier.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/clang/test/Sema/pre-c2x-restrict-qualifier.c b/clang/test/Sema/pre-c2x-restrict-qualifier.c
index 05406afaf0929..5395180ee66b0 100644
--- a/clang/test/Sema/pre-c2x-restrict-qualifier.c
+++ b/clang/test/Sema/pre-c2x-restrict-qualifier.c
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -std=c23 -fsyntax-only -verify %s
-// RUN: %clang_cc1 -std=c17 -fsyntax-only -pedantic -verify=pedantic %s
-// RUN: %clang_cc1 -std=c2x -fsyntax-only -Wpre-c2x-compat -verify=c2x-compat %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;
@@ -15,11 +15,7 @@ restrict T3 t3;         // pedantic-warning {{'restrict' qualifier on an array o
 
 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 {{pointer to function type 'int (void)' may not be 'restrict' qualified}} \
-                        // pedantic-error {{pointer to function type 'int ()' may not be 'restrict' qualified}} \
-                        // c2x-compat-error {{pointer to function type 'int (void)' may not be 'restrict' qualified}}
+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)}} \
-                        // pedantic-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}} \
-                        // c2x-compat-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid}}
+typedef t7 restrict t8; // expected-error {{restrict requires a pointer or reference ('t7' (aka 'int[2]') is invalid)}}

>From edaaeb1711c2588f64ab438bf7d5f5428675324b Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 12 Feb 2025 22:36:22 +0200
Subject: [PATCH 12/13] update test to remove useless errors

---
 clang/test/Sema/types.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/Sema/types.c b/clang/test/Sema/types.c
index cd8bea477d998..2a5f530740e9a 100644
--- a/clang/test/Sema/types.c
+++ b/clang/test/Sema/types.c
@@ -12,16 +12,16 @@ typedef int *S[2];
 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;
-} // expected-warning {{non-void function does not return a value}}
+}
 
 // but not a keyword
-int b(void) {
+void b(void) {
   int __int128_t;
   int __uint128_t;
-} // expected-warning {{non-void function does not return a value}}
+}
 
 // __int128 is a keyword
 int c(void) {

>From bc9e0de01bbeb8e078bec9e0357d3c7c7e9d6e03 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.tarasiuk at outlook.com>
Date: Wed, 12 Feb 2025 22:36:46 +0200
Subject: [PATCH 13/13] change code styles

---
 clang/lib/Sema/SemaType.cpp | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 418df24c14c63..db0177f9750e0 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1598,7 +1598,7 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
     if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
         EltTy->isMemberPointerType()) {
 
-      if (const MemberPointerType *PTy = EltTy->getAs<MemberPointerType>())
+      if (const auto *PTy = EltTy->getAs<MemberPointerType>())
         EltTy = PTy->getPointeeType();
       else
         EltTy = EltTy->getPointeeType();
@@ -1621,12 +1621,10 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
       Diag(Loc, DiagID) << EltTy;
       Qs.removeRestrict();
     } else {
-      if (T->isArrayType()) {
-        if (getLangOpts().C23)
-          Diag(Loc, diag::warn_c23_compat_restrict_on_array_of_pointers);
-        else
-          Diag(Loc, diag::ext_restrict_on_array_of_pointers_c23);
-      }
+      if (T->isArrayType())
+        Diag(Loc, getLangOpts().C23
+                      ? diag::warn_c23_compat_restrict_on_array_of_pointers
+                      : diag::ext_restrict_on_array_of_pointers_c23);
     }
   }
 



More information about the cfe-commits mailing list