Skip to content

Commit 33aac08

Browse files
authored
[NFC] Fix build issues with newer libcxx (#7926)
This replaces a few uses of `_countof` where we already have constant expressions for sizes with the constant expressions, uses `std::size` in other places, and adds a missing include to `<cstdlib>`. Chromium encountered a build failure due to these issues with the latest libc++.
1 parent 67c9849 commit 33aac08

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

include/dxc/Support/Global.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,15 @@ extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(
237237
inline void OutputDebugBytes(const void *ptr, size_t len) {
238238
const char digits[] = "0123456789abcdef";
239239
const unsigned char *pBytes = (const unsigned char *)ptr;
240-
const int bytesPerLine = 16;
241-
char buffer[bytesPerLine * 3 + 2 + 1];
242-
buffer[_countof(buffer) - 3] = '\r';
243-
buffer[_countof(buffer) - 2] = '\n';
244-
buffer[_countof(buffer) - 1] = '\0';
240+
constexpr size_t bytesPerLine = 16;
241+
constexpr size_t bufferSize = bytesPerLine * 3 + 2 + 1;
242+
char buffer[bufferSize];
243+
buffer[bufferSize - 3] = '\r';
244+
buffer[bufferSize - 2] = '\n';
245+
buffer[bufferSize - 1] = '\0';
245246

246247
char *pWrite = buffer;
247-
char *pEnd = buffer + _countof(buffer) - 3;
248+
char *pEnd = buffer + bufferSize - 3;
248249
while (len) {
249250
*pWrite++ = digits[(*pBytes & 0xF0) >> 4];
250251
*pWrite++ = digits[*pBytes & 0x0f];
@@ -264,11 +265,12 @@ inline void OutputDebugBytes(const void *ptr, size_t len) {
264265
}
265266

266267
inline void OutputDebugFormatA(const char *pszFormat, ...) {
267-
char buffer[1024];
268+
constexpr size_t bufferSize = 1024;
269+
char buffer[bufferSize];
268270

269271
va_list argList;
270272
va_start(argList, pszFormat);
271-
int count = vsnprintf_s(buffer, _countof(buffer), pszFormat, argList);
273+
int count = vsnprintf_s(buffer, bufferSize, pszFormat, argList);
272274
va_end(argList);
273275

274276
OutputDebugStringA(buffer);

lib/DXIL/DxilSemantic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "dxc/DXIL/DxilSignature.h"
1414
#include "dxc/Support/Global.h"
1515

16+
#include <cstdlib>
1617
#include <string>
1718

1819
using std::string;

lib/DXIL/DxilShaderModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ const char *ShaderModel::GetKindName() const { return GetKindName(m_Kind); }
417417

418418
const char *ShaderModel::GetKindName(Kind kind) {
419419
static_assert(static_cast<unsigned>(Kind::Invalid) ==
420-
_countof(ShaderModelKindNames) - 1,
420+
std::size(ShaderModelKindNames) - 1,
421421
"Invalid kinds or names");
422422
return ShaderModelKindNames[static_cast<unsigned int>(kind)];
423423
}
@@ -648,7 +648,7 @@ static const char *NodeLaunchTypeNames[] = {"invalid", "broadcasting",
648648

649649
const char *ShaderModel::GetNodeLaunchTypeName(DXIL::NodeLaunchType launchTy) {
650650
static_assert(static_cast<unsigned>(DXIL::NodeLaunchType::Thread) ==
651-
_countof(NodeLaunchTypeNames) - 1,
651+
std::size(NodeLaunchTypeNames) - 1,
652652
"Invalid launch type or names");
653653
return NodeLaunchTypeNames[static_cast<unsigned int>(launchTy)];
654654
}

0 commit comments

Comments
 (0)