Commit 18eaf017 authored by Clemens Backes's avatar Clemens Backes Committed by V8 LUCI CQ

[base] Fix compiler warning on empty FormattedString

GCC complains about empty format strings, and also clang already
required special-handling for this case.
We could either drop it, since statically empty strings are not that
useful anyway, but for completeness I fix it via "if constexpr" instead.

R=tebbi@chromium.org

Bug: chromium:1323177
Change-Id: I4d59e1b361afd1edcd552e8a9ce395759646e67f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3644433Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80540}
parent b24ac6a2
......@@ -88,14 +88,17 @@ std::array<char, kMaxLen> PrintFormattedStringToArray(Parts... parts) {
"Don't generate overly large strings; this limit can be increased, but "
"consider that the array lives on the stack of the caller.");
// This special case is needed because clang does not consider the empty
// string_view a valid format string (but "" is fine).
constexpr const char* kFormatString =
kFormat.size() == 0 ? "" : kFormat.data();
int characters = base::OS::SNPrintF(message.data(), kMaxLen, kFormatString,
parts.value...);
CHECK(characters >= 0 && characters < kMaxLen);
DCHECK_EQ('\0', message[characters]);
// Add a special case for empty strings, because compilers complain about
// empty format strings.
static_assert((kFormat.size() == 0) == (sizeof...(Parts) == 0));
if constexpr (kFormat.size() == 0) {
message[0] = '\0';
} else {
int characters = base::OS::SNPrintF(message.data(), kMaxLen, kFormat.data(),
parts.value...);
CHECK(characters >= 0 && characters < kMaxLen);
DCHECK_EQ('\0', message[characters]);
}
return message;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment