Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 DWORD size_needed = | |
| 144 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, nullptr, 0); | |
| 145 std::wstring utf16(size_needed, 0); | |
|
dcheng
2016/09/09 18:57:29
Nit: '\0' maybe, for slightly better clarity?
Nico
2016/09/09 19:06:33
meh, it's overwritten below anyways. And wouldn't
dcheng
2016/09/09 19:09:48
I think '\0' should work fine here: the L prefix i
| |
| 146 MultiByteToWideChar(CP_UTF8, 0, filename.data(), -1, | |
| 147 &utf16[0], size_needed); | |
| 148 | |
| 149 size_needed = GetFullPathName(utf16.data(), 0, nullptr, nullptr) + 1; // nul | |
|
dcheng
2016/09/09 18:57:29
Nit: let's be consistent about calling the W versi
dcheng
2016/09/09 18:57:29
Also:
If the lpBuffer buffer is too small to cont
Nico
2016/09/09 19:06:33
Oh oops, done, thanks!
Nico
2016/09/09 19:06:33
You're right, I misread the docs. Done.
| |
| 150 std::wstring full_utf16(size_needed, 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 Loading... | |
| 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 } |
| OLD | NEW |