Skip to content

Commit 3d4f979

Browse files
authored
[C] Allow __attribute__((nonstring)) on multidimensional arrays (#138133)
This addresses post-commit review feedback from the Linux kernel folks and improves compatibility with the same feature in GCC
1 parent 7f5c34b commit 3d4f979

File tree

2 files changed

+207
-3
lines changed

2 files changed

+207
-3
lines changed

clang/lib/Sema/SemaInit.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,22 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
261261
<< Str->getSourceRange();
262262
else if (StrLength - 1 == ArrayLen) {
263263
// If the entity being initialized has the nonstring attribute, then
264-
// silence the "missing nonstring" diagnostic.
265-
if (const ValueDecl *D = Entity.getDecl();
264+
// silence the "missing nonstring" diagnostic. If there's no entity,
265+
// check whether we're initializing an array of arrays; if so, walk the
266+
// parents to find an entity.
267+
auto FindCorrectEntity =
268+
[](const InitializedEntity *Entity) -> const ValueDecl * {
269+
while (Entity) {
270+
if (const ValueDecl *VD = Entity->getDecl())
271+
return VD;
272+
if (!Entity->getType()->isArrayType())
273+
return nullptr;
274+
Entity = Entity->getParent();
275+
}
276+
277+
return nullptr;
278+
};
279+
if (const ValueDecl *D = FindCorrectEntity(&Entity);
266280
!D || !D->hasAttr<NonStringAttr>())
267281
S.Diag(
268282
Str->getBeginLoc(),

0 commit comments

Comments
 (0)