[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 7 07:48:46 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/7] [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 6b9e1109f3906ea..52daea9b8eb2b6d 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 83464c50b4b238c..e84daeee679a57a 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 e0a6ba4f0691b95..4c90634b7ce2842 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/7] 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 e84daeee679a57a..ab85d6f51d22f75 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 000000000000000..a68e55b47b58084
--- /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/7] 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 da6352248239082..3dd4952a829e09b 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/7] 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 3dd4952a829e09b..3d38df8c3d859bc 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 5/7] 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 3d38df8c3d859bc..c9aa7c8488bc211 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 a68e55b47b58084..1360c54d3f767f8 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 6/7] 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 c9aa7c8488bc211..b447607d2d90545 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 7/7] 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 1360c54d3f767f8..f96961c9c3314d2 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];
More information about the cfe-commits
mailing list