[clang] [Clang] allow restrict qualifier for array types with pointer types as element types (PR #120896)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 11 13:53:57 PST 2025
================
@@ -1593,35 +1593,38 @@ 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 (!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);
+ }
----------------
efriedma-quic wrote:
```suggestion
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);
}
```
https://github.com/llvm/llvm-project/pull/120896
More information about the cfe-commits
mailing list