[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 Jan 31 13:58:21 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/2] [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/2] 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)}}
More information about the cfe-commits
mailing list