Skip to content

Commit e781627

Browse files
committed
address tex
1 parent a9282fd commit e781627

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

tools/clang/unittests/HLSL/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ add_clang_library(ClangHLSLTests SHARED
4848
PixTestUtils.cpp
4949
RewriterTest.cpp
5050
SystemValueTest.cpp
51+
${CMAKE_SOURCE_DIR}/lib/dxcsupport/Unicode.cpp
5152
ValidationTest.cpp
5253
VerifierTest.cpp
5354
)
@@ -78,6 +79,7 @@ add_clang_unittest(ClangHLSLTests
7879
PixTestUtils.cpp
7980
SystemValueTest.cpp
8081
TestMain.cpp
82+
${CMAKE_SOURCE_DIR}/lib/dxcsupport/Unicode.cpp
8183
ValidationTest.cpp
8284
VerifierTest.cpp
8385
)

tools/clang/unittests/HLSL/ValidationTest.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "dxc/DxilContainer/DxilContainerAssembler.h"
2121
#include "dxc/DxilContainer/DxilPipelineStateValidation.h"
2222
#include "dxc/DxilHash/DxilHash.h"
23+
#include "dxc/Support/Unicode.h" // for wstring conversions like WideToUtf8String
2324
#include "dxc/Support/WinIncludes.h"
2425
#include "llvm/ADT/ArrayRef.h"
2526
#include "llvm/ADT/StringRef.h"
@@ -4210,39 +4211,15 @@ TEST_F(ValidationTest, ValidateWithHash) {
42104211
VERIFY_ARE_EQUAL(memcmp(Result, pHeader->Hash.Digest, sizeof(Result)), 0);
42114212
}
42124213

4213-
std::wstring string_to_wstring(const std::string &str) {
4214-
if (str.empty())
4215-
return L"";
4216-
4217-
int size_needed =
4218-
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), nullptr, 0);
4219-
std::wstring wstrTo(size_needed, 0);
4220-
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size(), &wstrTo[0],
4221-
size_needed);
4222-
return wstrTo;
4223-
}
4224-
4225-
std::string wstring_to_utf8(const std::wstring &wstr) {
4226-
if (wstr.empty())
4227-
return "";
4228-
4229-
int size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, nullptr, 0,
4230-
nullptr, nullptr);
4231-
std::string result(size - 1, 0); // -1 to exclude null terminator
4232-
WideCharToMultiByte(CP_UTF8, 0, wstr.data(), -1, &result[0], size, nullptr,
4233-
nullptr);
4234-
return result;
4235-
}
4236-
4237-
std::string GetEnvVarA(const std::string &varName) {
4214+
std::wstring GetEnvVarW(const std::wstring &varName) {
42384215
#ifdef _WIN32
4239-
DWORD size = GetEnvironmentVariableA(varName.c_str(), nullptr, 0);
4216+
DWORD size = GetEnvironmentVariableW(varName.c_str(), nullptr, 0);
42404217
if (size == 0) {
4241-
return ""; // Not found or empty
4218+
return L""; // Not found or empty
42424219
}
42434220

4244-
std::string buffer(size - 1, '\0'); // size includes null terminator
4245-
GetEnvironmentVariableA(varName.c_str(), &buffer[0], size);
4221+
std::wstring buffer(size - 1, '\0'); // size includes null terminator
4222+
GetEnvironmentVariableW(varName.c_str(), &buffer[0], size);
42464223
return buffer;
42474224
#else
42484225
const char *result = std::getenv(varName.c_str());
@@ -4254,15 +4231,29 @@ void SetEnvVarW(const std::wstring &varName, const std::wstring &varValue) {
42544231
#ifdef _WIN32
42554232
VERIFY_IS_TRUE(SetEnvironmentVariableW(varName.c_str(), varValue.c_str()));
42564233
// also update the CRT environment
4257-
_putenv_s(wstring_to_utf8(varName).c_str(),
4258-
wstring_to_utf8(varValue).c_str());
4234+
std::string varNameStr;
4235+
std::string varValueStr;
4236+
Unicode::WideToUTF8String(varName.c_str(), &varNameStr);
4237+
Unicode::WideToUTF8String(varValue.c_str(), &varValueStr);
4238+
_putenv_s(varNameStr.c_str(), varValueStr.c_str());
42594239
#else
42604240
std::string name_utf8 = wstring_to_utf8(varName);
42614241
std::string value_utf8 = wstring_to_utf8(varValue);
42624242
setenv(name_utf8.c_str(), value_utf8.c_str(), 1);
42634243
#endif
42644244
}
42654245

4246+
void ClearEnvVarW(const std::wstring &varName) {
4247+
std::string varNameStr;
4248+
Unicode::WideToUTF8String(varName.c_str(), &varNameStr);
4249+
#ifdef _WIN32
4250+
SetEnvironmentVariableW(varName.c_str(), nullptr);
4251+
_putenv_s(varNameStr.c_str(), "");
4252+
#else
4253+
unsetenv(varNameStr.c_str());
4254+
#endif
4255+
}
4256+
42664257
// For now, 3 things are tested:
42674258
// 1. The environment variable is not set. GetDxilDllPath() is empty and
42684259
// DxilDllFailedToLoad() returns false
@@ -4280,7 +4271,7 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
42804271

42814272
// capture any existing value in the environment variable,
42824273
// so that it can be restored after the test
4283-
std::string oldEnvVal = GetEnvVarA("DXC_DXIL_DLL_PATH");
4274+
std::wstring oldEnvVal = GetEnvVarW(L"DXC_DXIL_DLL_PATH");
42844275

42854276
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
42864277

@@ -4335,7 +4326,9 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
43354326

43364327
// reset the environment variable to its previous value, if it had one.
43374328
if (!oldEnvVal.empty()) {
4338-
SetEnvVarW(L"DXC_DXIL_DLL_PATH", string_to_wstring(oldEnvVal));
4329+
SetEnvVarW(L"DXC_DXIL_DLL_PATH", oldEnvVal);
4330+
} else {
4331+
ClearEnvVarW(L"DXC_DXIL_DLL_PATH");
43394332
}
43404333
}
43414334

0 commit comments

Comments
 (0)