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

Side by Side Diff: tools/clang/plugins/ChromeClassTester.cpp

Issue 2318733002: clang plugin: Compute absolute paths on Windows as well. (Closed)
Patch Set: compile 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // A general interface for filtering and only acting on classes in Chromium C++ 5 // A general interface for filtering and only acting on classes in Chromium C++
6 // code. 6 // code.
7 7
8 #include "ChromeClassTester.h" 8 #include "ChromeClassTester.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
11 11
12 #include "clang/AST/AST.h" 12 #include "clang/AST/AST.h"
13 #include "clang/Basic/FileManager.h" 13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/SourceManager.h" 14 #include "clang/Basic/SourceManager.h"
15 15
16 #ifdef LLVM_ON_UNIX 16 #ifdef LLVM_ON_UNIX
17 #include <sys/param.h> 17 #include <sys/param.h>
18 #endif 18 #endif
19 #if defined(LLVM_ON_WIN32)
20 #include <windows.h>
21 #endif
19 22
20 using namespace clang; 23 using namespace clang;
21 using chrome_checker::Options; 24 using chrome_checker::Options;
22 25
23 namespace { 26 namespace {
24 27
25 bool ends_with(const std::string& one, const std::string& two) { 28 bool ends_with(const std::string& one, const std::string& two) {
26 if (two.size() > one.size()) 29 if (two.size() > one.size())
27 return false; 30 return false;
28 31
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 if (filename == "<scratch space>") 115 if (filename == "<scratch space>")
113 return true; 116 return true;
114 117
115 // Don't complain about autogenerated protobuf files. 118 // Don't complain about autogenerated protobuf files.
116 if (ends_with(filename, ".pb.h")) { 119 if (ends_with(filename, ".pb.h")) {
117 return true; 120 return true;
118 } 121 }
119 122
120 #if defined(LLVM_ON_UNIX) 123 #if defined(LLVM_ON_UNIX)
121 // Resolve the symlinktastic relative path and make it absolute. 124 // Resolve the symlinktastic relative path and make it absolute.
122 char resolvedPath[MAXPATHLEN];
123 if (options_.no_realpath) { 125 if (options_.no_realpath) {
124 // Same reason as windows below, but we don't need to do 126 // Same reason as windows below, but we don't need to do
125 // the '\\' manipulation on linux. 127 // the '\\' manipulation on linux.
126 filename.insert(filename.begin(), '/'); 128 filename.insert(filename.begin(), '/');
127 } else if (realpath(filename.c_str(), resolvedPath)) { 129 } else if (realpath(filename.c_str(), resolvedPath)) {
130 char resolvedPath[MAXPATHLEN];
128 filename = resolvedPath; 131 filename = resolvedPath;
129 } 132 }
130 #endif 133 #endif
131 134
132 #if defined(LLVM_ON_WIN32) 135 #if defined(LLVM_ON_WIN32)
136 // Make path absolute.
137 if (options_.no_realpath) {
138 // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the
139 // "/gen/" banned_dir work.
140 filename.insert(filename.begin(), '/');
141 } else {
142 // The Windows dance: Convert to UTF-16, call GetFullPathNameW, convert back
143 // 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
144 // storage in theory.
145 DWORD size_needed =
146 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, nullptr, 0);
147 std::vector<WCHAR> utf16(size_needed);
148 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1,
149 utf16.data(), size_needed);
150
151 size_needed = GetFullPathName(utf16.data(), 0, nullptr, nullptr) + 1; // nul
152 std::vector<WCHAR> full_utf16(size_needed, 0);
153 GetFullPathNameW(utf16.data(), full_utf16.size(), full_utf16.data(),
154 nullptr);
155
156 size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1,
157 nullptr, 0, nullptr, nullptr);
158 std::vector<char> utf8(size_needed);
159 WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, utf8.data(),
160 size_needed, nullptr, nullptr);
161 filename.assign(utf8.begin(), utf8.end());
162 }
163
133 std::replace(filename.begin(), filename.end(), '\\', '/'); 164 std::replace(filename.begin(), filename.end(), '\\', '/');
134
135 // On Posix, realpath() has made the path absolute. On Windows, this isn't
136 // necessarily true, so prepend a '/' to the path to make sure the
137 // banned_directories_ loop below works correctly.
138 // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the
139 // "/gen/" banned_dir work.
140 // This seems simpler than converting to utf16, calling GetFullPathNameW(),
141 // and converting back to utf8.
142 filename.insert(filename.begin(), '/');
143 #endif 165 #endif
144 166
145 for (const std::string& allowed_dir : allowed_directories_) { 167 for (const std::string& allowed_dir : allowed_directories_) {
146 // If any of the allowed directories occur as a component in filename, 168 // If any of the allowed directories occur as a component in filename,
147 // this file is allowed. 169 // this file is allowed.
148 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'"); 170 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'");
149 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'"); 171 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'");
150 172
151 if (filename.find(allowed_dir) != std::string::npos) 173 if (filename.find(allowed_dir) != std::string::npos)
152 return false; 174 return false;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 345 }
324 346
325 *filename = ploc.getFilename(); 347 *filename = ploc.getFilename();
326 return true; 348 return true;
327 } 349 }
328 350
329 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { 351 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() {
330 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error 352 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error
331 : DiagnosticsEngine::Warning; 353 : DiagnosticsEngine::Warning;
332 } 354 }
OLDNEW
« 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