diff options
Diffstat (limited to 'clang/lib/Sema/SemaInit.cpp')
-rw-r--r-- | clang/lib/Sema/SemaInit.cpp | 52 |
1 files changed, 22 insertions, 30 deletions
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp index 0844cb4..da56225 100644 --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -260,37 +260,29 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, diag::ext_initializer_string_for_char_array_too_long) << Str->getSourceRange(); else if (StrLength - 1 == ArrayLen) { - // In C, if the string literal is null-terminated explicitly, e.g., `char - // a[4] = "ABC\0"`, there should be no warning: - const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens()); - bool IsSLSafe = SL && SL->getLength() > 0 && - SL->getCodeUnit(SL->getLength() - 1) == 0; - - if (!IsSLSafe) { - // If the entity being initialized has the nonstring attribute, then - // silence the "missing nonstring" diagnostic. If there's no entity, - // check whether we're initializing an array of arrays; if so, walk the - // parents to find an entity. - auto FindCorrectEntity = - [](const InitializedEntity *Entity) -> const ValueDecl * { - while (Entity) { - if (const ValueDecl *VD = Entity->getDecl()) - return VD; - if (!Entity->getType()->isArrayType()) - return nullptr; - Entity = Entity->getParent(); - } + // If the entity being initialized has the nonstring attribute, then + // silence the "missing nonstring" diagnostic. If there's no entity, + // check whether we're initializing an array of arrays; if so, walk the + // parents to find an entity. + auto FindCorrectEntity = + [](const InitializedEntity *Entity) -> const ValueDecl * { + while (Entity) { + if (const ValueDecl *VD = Entity->getDecl()) + return VD; + if (!Entity->getType()->isArrayType()) + return nullptr; + Entity = Entity->getParent(); + } + + return nullptr; + }; + if (const ValueDecl *D = FindCorrectEntity(&Entity); + !D || !D->hasAttr<NonStringAttr>()) + S.Diag( + Str->getBeginLoc(), + diag::warn_initializer_string_for_char_array_too_long_no_nonstring) + << ArrayLen << StrLength << Str->getSourceRange(); - return nullptr; - }; - if (const ValueDecl *D = FindCorrectEntity(&Entity); - !D || !D->hasAttr<NonStringAttr>()) - S.Diag( - Str->getBeginLoc(), - diag:: - warn_initializer_string_for_char_array_too_long_no_nonstring) - << ArrayLen << StrLength << Str->getSourceRange(); - } // Always emit the C++ compatibility diagnostic. S.Diag(Str->getBeginLoc(), diag::warn_initializer_string_for_char_array_too_long_for_cpp) |