[clang] [Clang] allow restrict qualifier for array types with pointer types as element types (PR #120896)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 12 12:06:23 PST 2025
================
@@ -1593,35 +1593,40 @@ QualType Sema::BuildQualifiedType(QualType T, SourceLocation Loc,
// object or incomplete types shall not be restrict-qualified."
if (Qs.hasRestrict()) {
unsigned DiagID = 0;
- QualType ProblemTy;
-
- if (T->isAnyPointerType() || T->isReferenceType() ||
- T->isMemberPointerType()) {
- QualType EltTy;
- if (T->isObjCObjectPointerType())
- EltTy = T;
- else if (const MemberPointerType *PTy = T->getAs<MemberPointerType>())
+ QualType EltTy = Context.getBaseElementType(T);
+
+ if (EltTy->isAnyPointerType() || EltTy->isReferenceType() ||
+ EltTy->isMemberPointerType()) {
+
+ if (const MemberPointerType *PTy = EltTy->getAs<MemberPointerType>())
EltTy = PTy->getPointeeType();
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)) {
// 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;
+ EltTy = T;
}
+ Loc = DS ? DS->getRestrictSpecLoc() : Loc;
if (DiagID) {
- Diag(DS ? DS->getRestrictSpecLoc() : Loc, DiagID) << ProblemTy;
+ 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);
----------------
AaronBallman wrote:
```suggestion
Diag(Loc, getLangOpts().C23 ? diag::warn_c23_compat_restrict_on_array_of_pointers : diag::ext_restrict_on_array_of_pointers_c23);
```
And wrapped properly for clang-format.
https://github.com/llvm/llvm-project/pull/120896
More information about the cfe-commits
mailing list