aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaInit.cpp
diff options
context:
space:
mode:
authorWeaver <Tom.Weaver@sony.com>2025-06-26 11:51:42 +0100
committerWeaver <Tom.Weaver@sony.com>2025-06-26 11:51:42 +0100
commit30de98c283f5795dc10cc1c754d87f46a61d15ac (patch)
treefbf45db7fbb404683b771943fbf03b361d442edc /clang/lib/Sema/SemaInit.cpp
parent0cc10383f4bc7043c13440f7438715703c69b939 (diff)
downloadllvm-30de98c283f5795dc10cc1c754d87f46a61d15ac.zip
llvm-30de98c283f5795dc10cc1c754d87f46a61d15ac.tar.gz
llvm-30de98c283f5795dc10cc1c754d87f46a61d15ac.tar.bz2
Revert "[-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' (#143487)"
This reverts commit 9903c1936a5d174cfc7d38f77f40ed460e344db6. Caused the following buildbot failure: https://lab.llvm.org/buildbot/#/builders/144/builds/28591 Please fix before recommitting.
Diffstat (limited to 'clang/lib/Sema/SemaInit.cpp')
-rw-r--r--clang/lib/Sema/SemaInit.cpp52
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)