Chromium Code Reviews| Index: tools/clang/plugins/ChromeClassTester.cpp |
| diff --git a/tools/clang/plugins/ChromeClassTester.cpp b/tools/clang/plugins/ChromeClassTester.cpp |
| index cd508068e111399c0319dbebc49e707bd94fcea4..080cb96514a75c2afb7e93c4eacfbcd1b1af275b 100644 |
| --- a/tools/clang/plugins/ChromeClassTester.cpp |
| +++ b/tools/clang/plugins/ChromeClassTester.cpp |
| @@ -16,6 +16,9 @@ |
| #ifdef LLVM_ON_UNIX |
| #include <sys/param.h> |
| #endif |
| +#if defined(LLVM_ON_WIN32) |
| +#include <windows.h> |
| +#endif |
| using namespace clang; |
| using chrome_checker::Options; |
| @@ -119,27 +122,46 @@ bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
| #if defined(LLVM_ON_UNIX) |
| // Resolve the symlinktastic relative path and make it absolute. |
| - char resolvedPath[MAXPATHLEN]; |
| if (options_.no_realpath) { |
| // Same reason as windows below, but we don't need to do |
| // the '\\' manipulation on linux. |
| filename.insert(filename.begin(), '/'); |
| } else if (realpath(filename.c_str(), resolvedPath)) { |
| + char resolvedPath[MAXPATHLEN]; |
| filename = resolvedPath; |
| } |
| #endif |
| #if defined(LLVM_ON_WIN32) |
| - std::replace(filename.begin(), filename.end(), '\\', '/'); |
| + // Make path absolute. |
| + if (options_.no_realpath) { |
| + // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the |
| + // "/gen/" banned_dir work. |
| + filename.insert(filename.begin(), '/'); |
| + } else { |
| + // The Windows dance: Convert to UTF-16, call GetFullPathNameW, convert back |
| + // Use vector instead of string because string doesn't guarantee continuous |
|
dcheng
2016/09/09 17:20:57
According to my thorough scientific research of Go
Nico
2016/09/09 17:52:14
Done. (Why didn't the make data() return non-const
|
| + // storage in theory. |
| + DWORD size_needed = |
| + MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, nullptr, 0); |
| + std::vector<WCHAR> utf16(size_needed); |
| + MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, |
| + utf16.data(), size_needed); |
| + |
| + size_needed = GetFullPathName(utf16.data(), 0, nullptr, nullptr) + 1; // nul |
| + std::vector<WCHAR> full_utf16(size_needed, 0); |
| + GetFullPathNameW(utf16.data(), full_utf16.size(), full_utf16.data(), |
| + nullptr); |
| + |
| + size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, |
| + nullptr, 0, nullptr, nullptr); |
| + std::vector<char> utf8(size_needed); |
| + WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, utf8.data(), |
| + size_needed, nullptr, nullptr); |
| + filename.assign(utf8.begin(), utf8.end()); |
| + } |
| - // On Posix, realpath() has made the path absolute. On Windows, this isn't |
| - // necessarily true, so prepend a '/' to the path to make sure the |
| - // banned_directories_ loop below works correctly. |
| - // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the |
| - // "/gen/" banned_dir work. |
| - // This seems simpler than converting to utf16, calling GetFullPathNameW(), |
| - // and converting back to utf8. |
| - filename.insert(filename.begin(), '/'); |
| + std::replace(filename.begin(), filename.end(), '\\', '/'); |
| #endif |
| for (const std::string& allowed_dir : allowed_directories_) { |