Skip to content

Commit

Permalink
Tweak usage of LLVM Unicode conversion functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed Aug 8, 2019
1 parent 231d7c1 commit ae23a47
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
12 changes: 8 additions & 4 deletions driver/args.cpp
Expand Up @@ -10,11 +10,14 @@
#include "args.h"

#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/StringSaver.h"

#include <cstdlib>

#ifdef _WIN32
#include "llvm/Support/ConvertUTF.h"
#endif

#if LDC_WINDOWS_WMAIN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Expand Down Expand Up @@ -84,9 +87,10 @@ bool isRunArg(const char *arg) {
namespace env {
#ifdef _WIN32
static wchar_t *wget(const char *name) {
std::wstring wname;
llvm::ConvertUTF8toWide(name, wname);
return _wgetenv(wname.c_str());
llvm::SmallVector<wchar_t, 32> wname;
llvm::sys::windows::UTF8ToUTF16(name, wname);
wname.push_back(0);
return _wgetenv(wname.data());
}
#endif

Expand Down
5 changes: 1 addition & 4 deletions driver/configfile.cpp
Expand Up @@ -77,10 +77,7 @@ static bool ReadPathFromRegistry(llvm::SmallString<128> &p) {
const auto data = buffer.data();
if (RegGetValueW(hkey, nullptr, L"Path", RRF_RT_REG_SZ, nullptr, data,
&length) == ERROR_SUCCESS) {
std::string out;
res = llvm::convertUTF16ToUTF8String(
{reinterpret_cast<UTF16 *>(data), length}, out);
p = out;
res = !llvm::sys::windows::UTF16ToUTF8(data, length, p);
}
}
RegCloseKey(hkey);
Expand Down
31 changes: 18 additions & 13 deletions driver/tool.cpp
Expand Up @@ -277,12 +277,12 @@ int executeAndWait(const char *commandLine) {

DWORD exitCode;

std::wstring wcommandLine;
if (!llvm::ConvertUTF8toWide(commandLine, wcommandLine))
llvm::SmallVector<wchar_t, 512> wcommandLine;
if (llvm::sys::windows::UTF8ToUTF16(commandLine, wcommandLine))
return -3;
auto wcmdline = const_cast<wchar_t *>(wcommandLine.c_str());
if (!CreateProcessW(nullptr, wcmdline, nullptr, nullptr, TRUE, 0, nullptr,
nullptr, &si, &pi)) {
wcommandLine.push_back(0);
if (!CreateProcessW(nullptr, wcommandLine.data(), nullptr, nullptr, TRUE, 0,
nullptr, nullptr, &si, &pi)) {
exitCode = -1;
} else {
if (WaitForSingleObject(pi.hProcess, INFINITE) != 0 ||
Expand Down Expand Up @@ -382,17 +382,22 @@ bool setupMsvcEnvironmentImpl() {
bool haveVsInstallDir = false;

for (const auto &pair : env) {
const std::string key = pair.first.str();
const std::string value = pair.second.str();
const llvm::StringRef key = pair.first;
const llvm::StringRef value = pair.second;

if (global.params.verbose)
message(" %s=%s", key.c_str(), value.c_str());
if (global.params.verbose) {
message(" %.*s=%.*s", (int)key.size(), key.data(), (int)value.size(),
value.data());
}

std::wstring wkey, wvalue;
llvm::ConvertUTF8toWide(key, wkey);
llvm::ConvertUTF8toWide(value, wvalue);
llvm::SmallVector<wchar_t, 32> wkey;
llvm::SmallVector<wchar_t, 512> wvalue;
llvm::sys::windows::UTF8ToUTF16(key, wkey);
llvm::sys::windows::UTF8ToUTF16(value, wvalue);
wkey.push_back(0);
wvalue.push_back(0);

SetEnvironmentVariableW(wkey.c_str(), wvalue.c_str());
SetEnvironmentVariableW(wkey.data(), wvalue.data());

if (key == "VSINSTALLDIR")
haveVsInstallDir = true;
Expand Down

0 comments on commit ae23a47

Please sign in to comment.