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

Side by Side 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 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 if (options_.no_realpath) { 126 if (options_.no_realpath) {
124 // Same reason as windows below, but we don't need to do 127 // Same reason as windows below, but we don't need to do
125 // the '\\' manipulation on linux. 128 // the '\\' manipulation on linux.
126 filename.insert(filename.begin(), '/'); 129 filename.insert(filename.begin(), '/');
127 } else if (realpath(filename.c_str(), resolvedPath)) { 130 } else if (realpath(filename.c_str(), resolvedPath)) {
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 DWORD size_needed =
144 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, nullptr, 0);
145 std::wstring utf16(size_needed, L'\0');
146 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1,
147 &utf16[0], size_needed);
148
149 size_needed = GetFullPathNameW(utf16.data(), 0, nullptr, nullptr);
150 std::wstring full_utf16(size_needed, L'\0');
151 GetFullPathNameW(utf16.data(), full_utf16.size(), &full_utf16[0], nullptr);
152
153 size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1,
154 nullptr, 0, nullptr, nullptr);
155 filename.resize(size_needed);
156 WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, &filename[0],
157 size_needed, nullptr, nullptr);
158 }
159
133 std::replace(filename.begin(), filename.end(), '\\', '/'); 160 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 161 #endif
144 162
145 for (const std::string& allowed_dir : allowed_directories_) { 163 for (const std::string& allowed_dir : allowed_directories_) {
146 // If any of the allowed directories occur as a component in filename, 164 // If any of the allowed directories occur as a component in filename,
147 // this file is allowed. 165 // this file is allowed.
148 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'"); 166 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'");
149 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'"); 167 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'");
150 168
151 if (filename.find(allowed_dir) != std::string::npos) 169 if (filename.find(allowed_dir) != std::string::npos)
152 return false; 170 return false;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
323 } 341 }
324 342
325 *filename = ploc.getFilename(); 343 *filename = ploc.getFilename();
326 return true; 344 return true;
327 } 345 }
328 346
329 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { 347 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() {
330 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error 348 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error
331 : DiagnosticsEngine::Warning; 349 : DiagnosticsEngine::Warning;
332 } 350 }
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