| 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> |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 ChromeClassTester::~ChromeClassTester() {} | 45 ChromeClassTester::~ChromeClassTester() {} |
| 46 | 46 |
| 47 void ChromeClassTester::CheckTag(TagDecl* tag) { | 47 void ChromeClassTester::CheckTag(TagDecl* tag) { |
| 48 // We handle class types here where we have semantic information. We can only | 48 // We handle class types here where we have semantic information. We can only |
| 49 // check structs/classes/enums here, but we get a bunch of nice semantic | 49 // check structs/classes/enums here, but we get a bunch of nice semantic |
| 50 // information instead of just parsing information. | 50 // information instead of just parsing information. |
| 51 if (InBannedNamespace(tag)) | 51 if (InBannedNamespace(tag)) |
| 52 return; | 52 return; |
| 53 | 53 |
| 54 SourceLocation location = tag->getInnerLocStart(); | 54 SourceLocation location = tag->getInnerLocStart(); |
| 55 LocationType location_type = ClassifyLocation(location); | 55 LocationType location_type = ClassifyLocation(location, tag); |
| 56 if (location_type == LocationType::kThirdParty) | 56 if (location_type == LocationType::kThirdParty) |
| 57 return; | 57 return; |
| 58 | 58 |
| 59 if (CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)) { | 59 if (CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)) { |
| 60 // We sadly need to maintain a blacklist of types that violate these | 60 // We sadly need to maintain a blacklist of types that violate these |
| 61 // rules, but do so for good reason or due to limitations of this | 61 // rules, but do so for good reason or due to limitations of this |
| 62 // checker (i.e., we don't handle extern templates very well). | 62 // checker (i.e., we don't handle extern templates very well). |
| 63 std::string base_name = record->getNameAsString(); | 63 std::string base_name = record->getNameAsString(); |
| 64 if (IsIgnoredType(base_name)) | 64 if (IsIgnoredType(base_name)) |
| 65 return; | 65 return; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 89 | 89 |
| 90 DiagnosticIDs::Level level = getErrorLevel() == DiagnosticsEngine::Error | 90 DiagnosticIDs::Level level = getErrorLevel() == DiagnosticsEngine::Error |
| 91 ? DiagnosticIDs::Error : DiagnosticIDs::Warning; | 91 ? DiagnosticIDs::Error : DiagnosticIDs::Warning; |
| 92 | 92 |
| 93 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); | 93 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); |
| 94 DiagnosticBuilder builder = diagnostic().Report(full, id); | 94 DiagnosticBuilder builder = diagnostic().Report(full, id); |
| 95 | 95 |
| 96 } | 96 } |
| 97 | 97 |
| 98 ChromeClassTester::LocationType ChromeClassTester::ClassifyLocation( | 98 ChromeClassTester::LocationType ChromeClassTester::ClassifyLocation( |
| 99 SourceLocation loc) { | 99 SourceLocation loc, |
| 100 const Decl* record) { |
| 101 std::string ns = GetNamespace(record); |
| 102 if (ns == "blink" || ns == "WTF") |
| 103 return LocationType::kBlink; |
| 104 |
| 100 if (instance().getSourceManager().isInSystemHeader(loc)) | 105 if (instance().getSourceManager().isInSystemHeader(loc)) |
| 101 return LocationType::kThirdParty; | 106 return LocationType::kThirdParty; |
| 102 | 107 |
| 103 std::string filename; | 108 std::string filename; |
| 104 if (!GetFilename(loc, &filename)) { | 109 if (!GetFilename(loc, &filename)) { |
| 105 // If the filename cannot be determined, simply treat this as a banned | 110 // If the filename cannot be determined, simply treat this as a banned |
| 106 // location, instead of going through the full lookup process. | 111 // location, instead of going through the full lookup process. |
| 107 return LocationType::kThirdParty; | 112 return LocationType::kThirdParty; |
| 108 } | 113 } |
| 109 | 114 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, | 155 size_needed = WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, |
| 151 nullptr, 0, nullptr, nullptr); | 156 nullptr, 0, nullptr, nullptr); |
| 152 filename.resize(size_needed); | 157 filename.resize(size_needed); |
| 153 WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, &filename[0], | 158 WideCharToMultiByte(CP_UTF8, 0, full_utf16.data(), -1, &filename[0], |
| 154 size_needed, nullptr, nullptr); | 159 size_needed, nullptr, nullptr); |
| 155 } | 160 } |
| 156 | 161 |
| 157 std::replace(filename.begin(), filename.end(), '\\', '/'); | 162 std::replace(filename.begin(), filename.end(), '\\', '/'); |
| 158 #endif | 163 #endif |
| 159 | 164 |
| 160 for (const std::string& blink_dir : blink_directories_) { | |
| 161 // If any of the allowed directories occur as a component in filename, | |
| 162 // this file is allowed. | |
| 163 assert(blink_dir.front() == '/' && "Allowed dir must start with '/'"); | |
| 164 assert(blink_dir.back() == '/' && "Allowed dir must end with '/'"); | |
| 165 | |
| 166 if (filename.find(blink_dir) != std::string::npos) | |
| 167 return LocationType::kBlink; | |
| 168 } | |
| 169 | |
| 170 for (const std::string& banned_dir : banned_directories_) { | 165 for (const std::string& banned_dir : banned_directories_) { |
| 171 // If any of the banned directories occur as a component in filename, | 166 // If any of the banned directories occur as a component in filename, |
| 172 // this file is rejected. | 167 // this file is rejected. |
| 173 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); | 168 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); |
| 174 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); | 169 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); |
| 175 | 170 |
| 176 if (filename.find(banned_dir) != std::string::npos) | 171 if (filename.find(banned_dir) != std::string::npos) |
| 177 return LocationType::kThirdParty; | 172 return LocationType::kThirdParty; |
| 178 } | 173 } |
| 179 | 174 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 source_manager.getImmediateExpansionRange(record_location).first; | 221 source_manager.getImmediateExpansionRange(record_location).first; |
| 227 } | 222 } |
| 228 | 223 |
| 229 return false; | 224 return false; |
| 230 } | 225 } |
| 231 | 226 |
| 232 void ChromeClassTester::BuildBannedLists() { | 227 void ChromeClassTester::BuildBannedLists() { |
| 233 banned_namespaces_.emplace("std"); | 228 banned_namespaces_.emplace("std"); |
| 234 banned_namespaces_.emplace("__gnu_cxx"); | 229 banned_namespaces_.emplace("__gnu_cxx"); |
| 235 | 230 |
| 236 blink_directories_.emplace("/third_party/WebKit/"); | |
| 237 | |
| 238 banned_directories_.emplace("/third_party/"); | 231 banned_directories_.emplace("/third_party/"); |
| 239 banned_directories_.emplace("/native_client/"); | 232 banned_directories_.emplace("/native_client/"); |
| 240 banned_directories_.emplace("/breakpad/"); | 233 banned_directories_.emplace("/breakpad/"); |
| 241 banned_directories_.emplace("/courgette/"); | 234 banned_directories_.emplace("/courgette/"); |
| 242 banned_directories_.emplace("/ppapi/"); | 235 banned_directories_.emplace("/ppapi/"); |
| 243 banned_directories_.emplace("/testing/"); | 236 banned_directories_.emplace("/testing/"); |
| 244 banned_directories_.emplace("/v8/"); | 237 banned_directories_.emplace("/v8/"); |
| 245 banned_directories_.emplace("/sdch/"); | 238 banned_directories_.emplace("/sdch/"); |
| 246 banned_directories_.emplace("/frameworks/"); | 239 banned_directories_.emplace("/frameworks/"); |
| 247 | 240 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 327 } |
| 335 | 328 |
| 336 *filename = ploc.getFilename(); | 329 *filename = ploc.getFilename(); |
| 337 return true; | 330 return true; |
| 338 } | 331 } |
| 339 | 332 |
| 340 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { | 333 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { |
| 341 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error | 334 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error |
| 342 : DiagnosticsEngine::Warning; | 335 : DiagnosticsEngine::Warning; |
| 343 } | 336 } |
| OLD | NEW |