[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
Tue Feb 4 01:29:15 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 1/4] [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 6b9e1109f3906e..52daea9b8eb2b6 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 83464c50b4b238..e84daeee679a57 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 e0a6ba4f0691b9..4c90634b7ce284 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 2/4] 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 e84daeee679a57..ab85d6f51d22f7 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 00000000000000..a68e55b47b5808
--- /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 3/4] 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 da635224823908..3dd4952a829e09 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 4/4] 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 3dd4952a829e09..3d38df8c3d859b 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();
     }
   }



More information about the cfe-commits mailing list