|
| 1 | +// Probe for mcpp#970: what cl.exe and link.exe write, and in which encoding. |
| 2 | +// Prints facts only; asserts nothing. |
| 3 | +#include <windows.h> |
| 4 | +#include <cstdio> |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +static HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); |
| 9 | + |
| 10 | +static void say(const std::string& s) { |
| 11 | + DWORD n = 0; |
| 12 | + WriteFile(out, s.data(), static_cast<DWORD>(s.size()), &n, nullptr); |
| 13 | +} |
| 14 | + |
| 15 | +static std::string utf8(const std::wstring& w) { |
| 16 | + if (w.empty()) return {}; |
| 17 | + int n = WideCharToMultiByte(CP_UTF8, 0, w.data(), (int)w.size(), nullptr, 0, nullptr, nullptr); |
| 18 | + std::string s(n, '\0'); |
| 19 | + WideCharToMultiByte(CP_UTF8, 0, w.data(), (int)w.size(), s.data(), n, nullptr, nullptr); |
| 20 | + return s; |
| 21 | +} |
| 22 | + |
| 23 | +static std::wstring wide(const std::string& s, UINT cp, bool strict, bool* ok = nullptr) { |
| 24 | + if (s.empty()) { if (ok) *ok = true; return {}; } |
| 25 | + int n = MultiByteToWideChar(cp, strict ? MB_ERR_INVALID_CHARS : 0, s.data(), (int)s.size(), nullptr, 0); |
| 26 | + if (ok) *ok = n > 0; |
| 27 | + if (n <= 0) return {}; |
| 28 | + std::wstring w(n, L'\0'); |
| 29 | + MultiByteToWideChar(cp, strict ? MB_ERR_INVALID_CHARS : 0, s.data(), (int)s.size(), w.data(), n); |
| 30 | + return w; |
| 31 | +} |
| 32 | + |
| 33 | +static std::string hex(const std::string& s) { |
| 34 | + static const char* d = "0123456789abcdef"; |
| 35 | + std::string h; |
| 36 | + for (unsigned char c : s) { |
| 37 | + if (c >= 0x20 && c < 0x7f) { h += (char)c; continue; } |
| 38 | + h += "\\x"; h += d[c >> 4]; h += d[c & 15]; |
| 39 | + } |
| 40 | + return h; |
| 41 | +} |
| 42 | + |
| 43 | +static void write_file(const std::wstring& path, const std::string& content) { |
| 44 | + HANDLE h = CreateFileW(path.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 45 | + DWORD n = 0; |
| 46 | + WriteFile(h, content.data(), (DWORD)content.size(), &n, nullptr); |
| 47 | + CloseHandle(h); |
| 48 | +} |
| 49 | + |
| 50 | +static bool read_file(const std::wstring& path, std::string& content) { |
| 51 | + HANDLE h = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); |
| 52 | + if (h == INVALID_HANDLE_VALUE) return false; |
| 53 | + char buf[65536]; |
| 54 | + DWORD n = 0; |
| 55 | + content.clear(); |
| 56 | + while (ReadFile(h, buf, sizeof buf, &n, nullptr) && n) content.append(buf, n); |
| 57 | + CloseHandle(h); |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +struct Ran { DWORD exit = 0; std::string bytes; bool started = false; DWORD error = 0; }; |
| 62 | + |
| 63 | +static Ran run(const std::wstring& cmd, DWORD flags, const std::wstring& cwd) { |
| 64 | + Ran r; |
| 65 | + SECURITY_ATTRIBUTES sa{sizeof sa, nullptr, TRUE}; |
| 66 | + HANDLE rd = nullptr, wr = nullptr; |
| 67 | + CreatePipe(&rd, &wr, &sa, 0); |
| 68 | + SetHandleInformation(rd, HANDLE_FLAG_INHERIT, 0); |
| 69 | + HANDLE nul = CreateFileW(L"NUL", GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, OPEN_EXISTING, 0, nullptr); |
| 70 | + STARTUPINFOW si{}; |
| 71 | + si.cb = sizeof si; |
| 72 | + si.dwFlags = STARTF_USESTDHANDLES; |
| 73 | + si.hStdInput = nul; |
| 74 | + si.hStdOutput = wr; |
| 75 | + si.hStdError = wr; |
| 76 | + PROCESS_INFORMATION pi{}; |
| 77 | + std::wstring line = cmd; |
| 78 | + r.started = CreateProcessW(nullptr, line.data(), nullptr, nullptr, TRUE, flags | CREATE_UNICODE_ENVIRONMENT, |
| 79 | + nullptr, cwd.c_str(), &si, &pi) != 0; |
| 80 | + if (!r.started) r.error = GetLastError(); |
| 81 | + CloseHandle(wr); |
| 82 | + CloseHandle(nul); |
| 83 | + char buf[4096]; |
| 84 | + DWORD n = 0; |
| 85 | + while (ReadFile(rd, buf, sizeof buf, &n, nullptr) && n) r.bytes.append(buf, n); |
| 86 | + CloseHandle(rd); |
| 87 | + if (r.started) { |
| 88 | + WaitForSingleObject(pi.hProcess, INFINITE); |
| 89 | + GetExitCodeProcess(pi.hProcess, &r.exit); |
| 90 | + CloseHandle(pi.hProcess); |
| 91 | + CloseHandle(pi.hThread); |
| 92 | + } |
| 93 | + return r; |
| 94 | +} |
| 95 | + |
| 96 | +static const char* const kExpected[] = { |
| 97 | + "\xe4\xb8\xad\xe6\x96\x87\xc3\xa9", // 中文é (the directory) |
| 98 | + "\xe6\x9c\xaa\xe5\xae\x9a\xe4\xb9\x89_\xc3\xa9", // 未定义_é (the identifier) |
| 99 | + "\xe7\xbc\xba\xe5\xa4\xb1", // 缺失 (a missing name) |
| 100 | + "H\xc3\xa9" "ader", // Héader |
| 101 | +}; |
| 102 | + |
| 103 | +static void report(const char* what, const Ran& r) { |
| 104 | + say(std::string("---- ") + what + ": started=" + (r.started ? "yes" : "no") + " error=" + std::to_string(r.error) |
| 105 | + + " exit=" + std::to_string(r.exit) + " bytes=" + std::to_string(r.bytes.size()) + "\n"); |
| 106 | + size_t from = 0; |
| 107 | + while (from < r.bytes.size()) { |
| 108 | + size_t nl = r.bytes.find('\n', from); |
| 109 | + std::string ln = r.bytes.substr(from, nl == std::string::npos ? std::string::npos : nl - from); |
| 110 | + from = nl == std::string::npos ? r.bytes.size() : nl + 1; |
| 111 | + if (!ln.empty() && ln.back() == '\r') ln.pop_back(); |
| 112 | + bool high = false; |
| 113 | + for (unsigned char c : ln) high |= c >= 0x80; |
| 114 | + say(" | " + hex(ln) + "\n"); |
| 115 | + if (!high) continue; |
| 116 | + const UINT cps[] = {437, 850, 1252, 936, 65001, GetACP(), GetOEMCP()}; |
| 117 | + for (UINT cp : cps) { |
| 118 | + bool ok = false; |
| 119 | + std::string text = utf8(wide(ln, cp, true, &ok)); |
| 120 | + std::string hits; |
| 121 | + for (auto* e : kExpected) if (text.find(e) != std::string::npos) hits += std::string(" has[") + e + "]"; |
| 122 | + say(" as " + std::to_string(cp) + (ok ? "" : " (invalid)") + ": " + text + hits + "\n"); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +int wmain(int argc, wchar_t** argv) { |
| 128 | + const std::wstring label = argc > 1 ? argv[1] : L"default"; |
| 129 | + say("==== probe970 " + utf8(label) + "\n"); |
| 130 | + say("GetACP=" + std::to_string(GetACP()) + " GetOEMCP=" + std::to_string(GetOEMCP()) |
| 131 | + + " console window=" + (GetConsoleWindow() ? "yes" : "no") |
| 132 | + + " GetConsoleOutputCP=" + std::to_string(GetConsoleOutputCP()) |
| 133 | + + " GetConsoleCP=" + std::to_string(GetConsoleCP()) + "\n"); |
| 134 | + wchar_t vslang[64] = L""; |
| 135 | + GetEnvironmentVariableW(L"VSLANG", vslang, 64); |
| 136 | + say("VSLANG=" + utf8(vslang) + "\n"); |
| 137 | + if (GetConsoleOutputCP() == 0) { |
| 138 | + BOOL made = AllocConsole(); |
| 139 | + say(std::string("no console: AllocConsole=") + (made ? "yes" : "no") + " now GetConsoleOutputCP=" |
| 140 | + + std::to_string(GetConsoleOutputCP()) + "\n"); |
| 141 | + } |
| 142 | + const UINT original = GetConsoleOutputCP(); |
| 143 | + |
| 144 | + const std::wstring root = L"C:\\p970\\\u4e2d\u6587\u00e9"; |
| 145 | + CreateDirectoryW(L"C:\\p970", nullptr); |
| 146 | + CreateDirectoryW(root.c_str(), nullptr); |
| 147 | + CreateDirectoryW((root + L"\\Inc Dir").c_str(), nullptr); |
| 148 | + write_file(root + L"\\Inc Dir\\H\u00e9ader.h", "#pragma once\ninline int header_value() { return 1; }\n"); |
| 149 | + write_file(root + L"\\a.cpp", |
| 150 | + "#include \"H\xc3\xa9" "ader.h\"\n#include <vector>\n" |
| 151 | + "int main() { std::vector<int> v; return header_value() + (int)v.size(); }\n"); |
| 152 | + write_file(root + L"\\b.cpp", |
| 153 | + "#include \"H\xc3\xa9" "ader.h\"\nint f() { return \xe6\x9c\xaa\xe5\xae\x9a\xe4\xb9\x89_\xc3\xa9; }\n"); |
| 154 | + write_file(root + L"\\c.cpp", "#include \"\xe7\xbc\xba\xe5\xa4\xb1.h\"\n"); |
| 155 | + write_file(root + L"\\k.cpp", |
| 156 | + "void h(int); void h(long);\nvoid k() { int unused_\xc3\xa9 = 0; h(1u); }\n"); |
| 157 | + write_file(root + L"\\m.ixx", |
| 158 | + "module;\n#include \"H\xc3\xa9" "ader.h\"\nexport module m;\nexport int g() { return header_value(); }\n"); |
| 159 | + |
| 160 | + const std::wstring common = L" /nologo /c /EHsc /std:c++latest /utf-8 /W4 /I \"Inc Dir\" "; |
| 161 | + // The record, and the dependency text, for a compile that succeeds, one that fails, and a module. |
| 162 | + for (const wchar_t* unit : {L"a.cpp", L"b.cpp", L"m.ixx"}) { |
| 163 | + std::wstring base(unit); |
| 164 | + base = base.substr(0, base.find(L'.')); |
| 165 | + DeleteFileW((root + L"\\" + base + L".json").c_str()); |
| 166 | + Ran r = run(L"cl.exe" + common + L"/sourceDependencies " + base + L".json /showIncludes " + unit, 0, root); |
| 167 | + report(("showIncludes+sourceDependencies " + utf8(unit)).c_str(), r); |
| 168 | + std::string json; |
| 169 | + if (read_file(root + L"\\" + base + L".json", json)) { |
| 170 | + say(" record " + utf8(base) + ".json (" + std::to_string(json.size()) + " bytes):\n"); |
| 171 | + size_t from = 0; |
| 172 | + int shown = 0; |
| 173 | + while (from < json.size() && shown < 40) { |
| 174 | + size_t nl = json.find('\n', from); |
| 175 | + std::string ln = json.substr(from, nl == std::string::npos ? std::string::npos : nl - from); |
| 176 | + from = nl == std::string::npos ? json.size() : nl + 1; |
| 177 | + say(" " + hex(ln) + "\n"); |
| 178 | + ++shown; |
| 179 | + } |
| 180 | + bool ok = false; |
| 181 | + wide(json, 65001, true, &ok); |
| 182 | + say(std::string(" record is valid UTF-8: ") + (ok ? "yes" : "no") + "\n"); |
| 183 | + } else { |
| 184 | + say(" no record " + utf8(base) + ".json\n"); |
| 185 | + } |
| 186 | + } |
| 187 | + // The same compile with the record only: is the output then free of include lines? |
| 188 | + { |
| 189 | + Ran r = run(L"cl.exe" + common + L"/sourceDependencies a2.json a.cpp", 0, root); |
| 190 | + report("sourceDependencies only, a.cpp", r); |
| 191 | + } |
| 192 | + |
| 193 | + // The encoding of the text: the same failing compile, link and notes under each console code page. |
| 194 | + const UINT pages[] = {437, 1252, 936, 65001}; |
| 195 | + for (UINT cp : pages) { |
| 196 | + BOOL set = SetConsoleOutputCP(cp); |
| 197 | + say("\n#### console output code page " + std::to_string(cp) + " set=" + (set ? "yes" : "no") |
| 198 | + + " now=" + std::to_string(GetConsoleOutputCP()) + "\n"); |
| 199 | + report("cl b.cpp (inherited console)", run(L"cl.exe" + common + L"b.cpp", 0, root)); |
| 200 | + report("cl c.cpp (inherited console)", run(L"cl.exe" + common + L"c.cpp", 0, root)); |
| 201 | + report("cl k.cpp (inherited console)", run(L"cl.exe" + common + L"k.cpp", 0, root)); |
| 202 | + report("link (inherited console)", run(L"link.exe /nologo \u7f3a\u5931.obj", 0, root)); |
| 203 | + report("cl b.cpp DETACHED_PROCESS", run(L"cl.exe" + common + L"b.cpp", DETACHED_PROCESS, root)); |
| 204 | + report("cl b.cpp CREATE_NO_WINDOW", run(L"cl.exe" + common + L"b.cpp", CREATE_NO_WINDOW, root)); |
| 205 | + report("link DETACHED_PROCESS", run(L"link.exe /nologo \u7f3a\u5931.obj", DETACHED_PROCESS, root)); |
| 206 | + } |
| 207 | + SetConsoleOutputCP(original); |
| 208 | + say("\nrestored console output code page " + std::to_string(GetConsoleOutputCP()) + "\n"); |
| 209 | + return 0; |
| 210 | +} |
0 commit comments