[clang] [-Wunterminated-string-initialization] Handle C string literals ending with explicit '\0' (PR #143487)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 19 14:07:54 PDT 2025
================
@@ -260,6 +260,12 @@ 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) {
+ // If the string literal is null-terminated explicitly, e.g., `char a[4] =
+ // "ABC\0"`, there should be no warning:
+ if (const auto *SL = dyn_cast<StringLiteral>(Str->IgnoreParens());
+ SL && SL->getLength() > 0 &&
+ SL->getCodeUnit(SL->getLength() - 1) == 0)
+ return;
----------------
efriedma-quic wrote:
I think the early return suppresses warn_initializer_string_for_char_array_too_long_for_cpp warnings?
https://github.com/llvm/llvm-project/pull/143487
More information about the cfe-commits
mailing list