20
20
#include " dxc/DxilContainer/DxilContainerAssembler.h"
21
21
#include " dxc/DxilContainer/DxilPipelineStateValidation.h"
22
22
#include " dxc/DxilHash/DxilHash.h"
23
+ #include " dxc/Support/Unicode.h" // for wstring conversions like WideToUtf8String
23
24
#include " dxc/Support/WinIncludes.h"
24
25
#include " llvm/ADT/ArrayRef.h"
25
26
#include " llvm/ADT/StringRef.h"
@@ -4210,39 +4211,15 @@ TEST_F(ValidationTest, ValidateWithHash) {
4210
4211
VERIFY_ARE_EQUAL (memcmp (Result, pHeader->Hash .Digest , sizeof (Result)), 0 );
4211
4212
}
4212
4213
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) {
4238
4215
#ifdef _WIN32
4239
- DWORD size = GetEnvironmentVariableA (varName.c_str (), nullptr , 0 );
4216
+ DWORD size = GetEnvironmentVariableW (varName.c_str (), nullptr , 0 );
4240
4217
if (size == 0 ) {
4241
- return " " ; // Not found or empty
4218
+ return L "" ; // Not found or empty
4242
4219
}
4243
4220
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);
4246
4223
return buffer;
4247
4224
#else
4248
4225
const char *result = std::getenv (varName.c_str ());
@@ -4254,15 +4231,29 @@ void SetEnvVarW(const std::wstring &varName, const std::wstring &varValue) {
4254
4231
#ifdef _WIN32
4255
4232
VERIFY_IS_TRUE (SetEnvironmentVariableW (varName.c_str (), varValue.c_str ()));
4256
4233
// 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 ());
4259
4239
#else
4260
4240
std::string name_utf8 = wstring_to_utf8 (varName);
4261
4241
std::string value_utf8 = wstring_to_utf8 (varValue);
4262
4242
setenv (name_utf8.c_str (), value_utf8.c_str (), 1 );
4263
4243
#endif
4264
4244
}
4265
4245
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
+
4266
4257
// For now, 3 things are tested:
4267
4258
// 1. The environment variable is not set. GetDxilDllPath() is empty and
4268
4259
// DxilDllFailedToLoad() returns false
@@ -4280,7 +4271,7 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
4280
4271
4281
4272
// capture any existing value in the environment variable,
4282
4273
// 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" );
4284
4275
4285
4276
// 1. with no env var set, test GetDxilDllPath() and DxilDllFailedToLoad()
4286
4277
@@ -4335,7 +4326,9 @@ TEST_F(ValidationTest, UnitTestExtValidationSupport) {
4335
4326
4336
4327
// reset the environment variable to its previous value, if it had one.
4337
4328
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" );
4339
4332
}
4340
4333
}
4341
4334
0 commit comments