| 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 | |
| 22 | 19 |
| 23 using namespace clang; | 20 using namespace clang; |
| 24 using chrome_checker::Options; | 21 using chrome_checker::Options; |
| 25 | 22 |
| 26 namespace { | 23 namespace { |
| 27 | 24 |
| 28 bool ends_with(const std::string& one, const std::string& two) { | 25 bool ends_with(const std::string& one, const std::string& two) { |
| 29 if (two.size() > one.size()) | 26 if (two.size() > one.size()) |
| 30 return false; | 27 return false; |
| 31 | 28 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 if (filename == "<scratch space>") | 112 if (filename == "<scratch space>") |
| 116 return true; | 113 return true; |
| 117 | 114 |
| 118 // Don't complain about autogenerated protobuf files. | 115 // Don't complain about autogenerated protobuf files. |
| 119 if (ends_with(filename, ".pb.h")) { | 116 if (ends_with(filename, ".pb.h")) { |
| 120 return true; | 117 return true; |
| 121 } | 118 } |
| 122 | 119 |
| 123 #if defined(LLVM_ON_UNIX) | 120 #if defined(LLVM_ON_UNIX) |
| 124 // Resolve the symlinktastic relative path and make it absolute. | 121 // Resolve the symlinktastic relative path and make it absolute. |
| 122 char resolvedPath[MAXPATHLEN]; |
| 125 if (options_.no_realpath) { | 123 if (options_.no_realpath) { |
| 126 // Same reason as windows below, but we don't need to do | 124 // Same reason as windows below, but we don't need to do |
| 127 // the '\\' manipulation on linux. | 125 // the '\\' manipulation on linux. |
| 128 filename.insert(filename.begin(), '/'); | 126 filename.insert(filename.begin(), '/'); |
| 129 } else if (realpath(filename.c_str(), resolvedPath)) { | 127 } else if (realpath(filename.c_str(), resolvedPath)) { |
| 130 char resolvedPath[MAXPATHLEN]; | |
| 131 filename = resolvedPath; | 128 filename = resolvedPath; |
| 132 } | 129 } |
| 133 #endif | 130 #endif |
| 134 | 131 |
| 135 #if defined(LLVM_ON_WIN32) | 132 #if defined(LLVM_ON_WIN32) |
| 136 // Make path absolute. | 133 std::replace(filename.begin(), filename.end(), '\\', '/'); |
| 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 | 134 |
| 149 size_needed = GetFullPathNameW(utf16.data(), 0, nullptr, nullptr); | 135 // On Posix, realpath() has made the path absolute. On Windows, this isn't |
| 150 std::wstring full_utf16(size_needed, L'\0'); | 136 // necessarily true, so prepend a '/' to the path to make sure the |
| 151 GetFullPathNameW(utf16.data(), full_utf16.size(), &full_utf16[0], nullptr); | 137 // banned_directories_ loop below works correctly. |
| 152 | 138 // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the |
| 153 size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, | 139 // "/gen/" banned_dir work. |
| 154 nullptr, 0, nullptr, nullptr); | 140 // This seems simpler than converting to utf16, calling GetFullPathNameW(), |
| 155 filename.resize(size_needed); | 141 // and converting back to utf8. |
| 156 WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, &filename[0], | 142 filename.insert(filename.begin(), '/'); |
| 157 size_needed, nullptr, nullptr); | |
| 158 } | |
| 159 | |
| 160 std::replace(filename.begin(), filename.end(), '\\', '/'); | |
| 161 #endif | 143 #endif |
| 162 | 144 |
| 163 for (const std::string& allowed_dir : allowed_directories_) { | 145 for (const std::string& allowed_dir : allowed_directories_) { |
| 164 // If any of the allowed directories occur as a component in filename, | 146 // If any of the allowed directories occur as a component in filename, |
| 165 // this file is allowed. | 147 // this file is allowed. |
| 166 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'"); | 148 assert(allowed_dir.front() == '/' && "Allowed dir must start with '/'"); |
| 167 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'"); | 149 assert(allowed_dir.back() == '/' && "Allowed dir must end with '/'"); |
| 168 | 150 |
| 169 if (filename.find(allowed_dir) != std::string::npos) | 151 if (filename.find(allowed_dir) != std::string::npos) |
| 170 return false; | 152 return false; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 } | 323 } |
| 342 | 324 |
| 343 *filename = ploc.getFilename(); | 325 *filename = ploc.getFilename(); |
| 344 return true; | 326 return true; |
| 345 } | 327 } |
| 346 | 328 |
| 347 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { | 329 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { |
| 348 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error | 330 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error |
| 349 : DiagnosticsEngine::Warning; | 331 : DiagnosticsEngine::Warning; |
| 350 } | 332 } |
| OLD | NEW |