Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: tools/clang/plugins/ChromeClassTester.cpp

Issue 2318733002: clang plugin: Compute absolute paths on Windows as well. (Closed)
Patch Set: relan Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/clang/plugins/ChromeClassTester.cpp
diff --git a/tools/clang/plugins/ChromeClassTester.cpp b/tools/clang/plugins/ChromeClassTester.cpp
index cd508068e111399c0319dbebc49e707bd94fcea4..b0cc3db5aed56da1435b1ec19dff3c48fa3ff423 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;
@@ -130,16 +133,31 @@ bool ChromeClassTester::InBannedDirectory(SourceLocation loc) {
#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
+ DWORD size_needed =
+ MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, nullptr, 0);
+ std::wstring utf16(size_needed, L'\0');
+ MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1,
+ &utf16[0], size_needed);
+
+ size_needed = GetFullPathNameW(utf16.data(), 0, nullptr, nullptr);
+ std::wstring full_utf16(size_needed, L'\0');
+ GetFullPathNameW(utf16.data(), full_utf16.size(), &full_utf16[0], nullptr);
+
+ size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1,
+ nullptr, 0, nullptr, nullptr);
+ filename.resize(size_needed);
+ WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, &filename[0],
+ size_needed, nullptr, nullptr);
+ }
- // 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_) {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698