Skip to content

Commit 30de98c

Browse files
WeaverWeaver
authored andcommitted
Revert "[-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' (#143487)"
This reverts commit 9903c19. Caused the following buildbot failure: https://lab.llvm.org/buildbot/#/builders/144/builds/28591 Please fix before recommitting.
1 parent 0cc1038 commit 30de98c

File tree

3 files changed

+22
-81
lines changed

3 files changed

+22
-81
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ C Language Changes
222222
223223
char buf1[3] = "foo"; // -Wunterminated-string-initialization
224224
char buf2[3] = "flarp"; // -Wexcess-initializers
225-
char buf3[3] = "fo\0"; // This is fine, no warning.
226225
227226
This diagnostic can be suppressed by adding the new ``nonstring`` attribute
228227
to the field or variable being initialized. #GH137705

clang/lib/Sema/SemaInit.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -260,37 +260,29 @@ static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
260260
diag::ext_initializer_string_for_char_array_too_long)
261261
<< Str->getSourceRange();
262262
else if (StrLength - 1 == ArrayLen) {
263-
// In C, if the string literal is null-terminated explicitly, e.g., `char
264-
// a[4] = "ABC\0"`, there should be no warning:
265-
const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens());
266-
bool IsSLSafe = SL && SL->getLength() > 0 &&
267-
SL->getCodeUnit(SL->getLength() - 1) == 0;
268-
269-
if (!IsSLSafe) {
270-
// If the entity being initialized has the nonstring attribute, then
271-
// silence the "missing nonstring" diagnostic. If there's no entity,
272-
// check whether we're initializing an array of arrays; if so, walk the
273-
// parents to find an entity.
274-
auto FindCorrectEntity =
275-
[](const InitializedEntity *Entity) -> const ValueDecl * {
276-
while (Entity) {
277-
if (const ValueDecl *VD = Entity->getDecl())
278-
return VD;
279-
if (!Entity->getType()->isArrayType())
280-
return nullptr;
281-
Entity = Entity->getParent();
282-
}
263+
// If the entity being initialized has the nonstring attribute, then
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);
280+
!D || !D->hasAttr<NonStringAttr>())
281+
S.Diag(
282+
Str->getBeginLoc(),
283+
diag::warn_initializer_string_for_char_array_too_long_no_nonstring)
284+
<< ArrayLen << StrLength << Str->getSourceRange();
283285

284-
return nullptr;
285-
};
286-
if (const ValueDecl *D = FindCorrectEntity(&Entity);
287-
!D || !D->hasAttr<NonStringAttr>())
288-
S.Diag(
289-
Str->getBeginLoc(),
290-
diag::
291-
warn_initializer_string_for_char_array_too_long_no_nonstring)
292-
<< ArrayLen << StrLength << Str->getSourceRange();
293-
}
294286
// Always emit the C++ compatibility diagnostic.
295287
S.Diag(Str->getBeginLoc(),
296288
diag::warn_initializer_string_for_char_array_too_long_for_cpp)

clang/test/Sema/attr-nonstring_safe.c

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)