[LLVMdev] [cfe-dev] Unicode path handling on Windows

Aaron Ballman aaron at aaronballman.com
Tue Sep 20 10:42:43 PDT 2011


On Tue, Sep 20, 2011 at 11:52 AM, Nikola Smiljanic <popizdeh at gmail.com> wrote:
> I spent some more time on this. My idea was to use functionality from
> llvm::sys::fs like file_status instead of stat struct, but as it turns out
> this is not really possible. file_status structure is not a replacement for
> stat, nor are there functions inside llvm::sys::fs that can replace calls to
> ::stat and ::open.
> The only solution I can see is to create wrappers around ::stat, ::open,
> etc. and use them in the codebase instead of direct calls. Unix
> implementation would only forward calls to the right function. Windows
> implementation would convert path from utf8 to utf16 and call the
> appropriate function (::_wopen instead of ::open and so on). Is this
> solution acceptable?
> Here's a patch where I tried this (it has a number of issues but I'd like to
> hear from someone that this solution makes sense before I try to address
> them).

General nitpick: make sure you're using spaces instead of tabs, and
that your lines are less than 80 characters wide.

+	wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);

You may want to test the consistency of this approach from the
original with regards to backslashes and quotation marks, if for no
other reason than documenting it.  CommandLineToArgvW calls them out
in the documentation.

+	for (int i = 0; i != argc; ++i)
+	{
+		int len = ::WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0,
NULL, NULL);
+		argv[i] = new char[len];

You should check the return value of WideCharToMultiByte.  I don't
expect it to fail, but better safe than sorry.  I would guess an
assert would be acceptable.

I couldn't see it from the patch, but are all the calls to Open on
Windows only, or is there an Open implementation for the Mac that
calls open instead of _wopen?

I agree with the general gist of the patch though.  Thanks!

~Aaron




More information about the llvm-dev mailing list