| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <sys/param.h> | 10 #include <sys/param.h> |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 139 |
| 140 // We ignore all classes that end with "Matcher" because they're probably | 140 // We ignore all classes that end with "Matcher" because they're probably |
| 141 // GMock artifacts. | 141 // GMock artifacts. |
| 142 if (ends_with(base_name, "Matcher")) | 142 if (ends_with(base_name, "Matcher")) |
| 143 return; | 143 return; |
| 144 | 144 |
| 145 CheckChromeClass(record_location, record); | 145 CheckChromeClass(record_location, record); |
| 146 } | 146 } |
| 147 } | 147 } |
| 148 | 148 |
| 149 void ChromeClassTester::emitWarning(SourceLocation loc, const char* raw_error) { | 149 void ChromeClassTester::emitWarning(SourceLocation loc, |
| 150 const char* raw_error) { |
| 150 FullSourceLoc full(loc, instance().getSourceManager()); | 151 FullSourceLoc full(loc, instance().getSourceManager()); |
| 151 std::string err; | 152 std::string err; |
| 152 err = "[chromium-style] "; | 153 err = "[chromium-style] "; |
| 153 err += raw_error; | 154 err += raw_error; |
| 154 DiagnosticsEngine::Level level = | 155 DiagnosticsEngine::Level level = |
| 155 diagnostic().getWarningsAsErrors() ? | 156 diagnostic().getWarningsAsErrors() ? |
| 156 DiagnosticsEngine::Error : | 157 DiagnosticsEngine::Error : |
| 157 DiagnosticsEngine::Warning; | 158 DiagnosticsEngine::Warning; |
| 158 unsigned id = diagnostic().getCustomDiagID(level, err); | 159 unsigned id = diagnostic().getCustomDiagID(level, err); |
| 159 DiagnosticBuilder B = diagnostic().Report(full, id); | 160 DiagnosticBuilder B = diagnostic().Report(full, id); |
| 160 } | 161 } |
| 161 | 162 |
| 162 bool ChromeClassTester::InTestingNamespace(const Decl* record) { | |
| 163 return GetNamespace(record).find("testing") != std::string::npos; | |
| 164 } | |
| 165 | |
| 166 bool ChromeClassTester::InBannedNamespace(const Decl* record) { | 163 bool ChromeClassTester::InBannedNamespace(const Decl* record) { |
| 167 std::string n = GetNamespace(record); | 164 std::string n = GetNamespace(record); |
| 168 if (n != "") { | 165 if (n != "") { |
| 169 return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n) | 166 return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n) |
| 170 != banned_namespaces_.end(); | 167 != banned_namespaces_.end(); |
| 171 } | 168 } |
| 172 | 169 |
| 173 return false; | 170 return false; |
| 174 } | 171 } |
| 175 | 172 |
| 173 bool ChromeClassTester::InImplementationFile(SourceLocation record_location) { |
| 174 std::string filename; |
| 175 if (!GetFilename(record_location, &filename)) { |
| 176 return false; |
| 177 } |
| 178 |
| 179 if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") || |
| 180 ends_with(filename, ".mm")) { |
| 181 return true; |
| 182 } |
| 183 |
| 184 return false; |
| 185 } |
| 186 |
| 176 std::string ChromeClassTester::GetNamespace(const Decl* record) { | 187 std::string ChromeClassTester::GetNamespace(const Decl* record) { |
| 177 return GetNamespaceImpl(record->getDeclContext(), ""); | 188 return GetNamespaceImpl(record->getDeclContext(), ""); |
| 178 } | 189 } |
| 179 | 190 |
| 180 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, | 191 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, |
| 181 std::string candidate) { | 192 const std::string& candidate) { |
| 182 switch (context->getDeclKind()) { | 193 switch (context->getDeclKind()) { |
| 183 case Decl::TranslationUnit: { | 194 case Decl::TranslationUnit: { |
| 184 return candidate; | 195 return candidate; |
| 185 } | 196 } |
| 186 case Decl::Namespace: { | 197 case Decl::Namespace: { |
| 187 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); | 198 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); |
| 188 std::string name_str; | 199 std::string name_str; |
| 189 llvm::raw_string_ostream OS(name_str); | 200 llvm::raw_string_ostream OS(name_str); |
| 190 if (decl->isAnonymousNamespace()) | 201 if (decl->isAnonymousNamespace()) |
| 191 OS << "<anonymous namespace>"; | 202 OS << "<anonymous namespace>"; |
| 192 else | 203 else |
| 193 OS << *decl; | 204 OS << *decl; |
| 194 return GetNamespaceImpl(context->getParent(), | 205 return GetNamespaceImpl(context->getParent(), |
| 195 OS.str()); | 206 OS.str()); |
| 196 } | 207 } |
| 197 default: { | 208 default: { |
| 198 return GetNamespaceImpl(context->getParent(), candidate); | 209 return GetNamespaceImpl(context->getParent(), candidate); |
| 199 } | 210 } |
| 200 } | 211 } |
| 201 } | 212 } |
| 202 | 213 |
| 203 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { | 214 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
| 204 const SourceManager &SM = instance_.getSourceManager(); | 215 std::string filename; |
| 205 SourceLocation spelling_location = SM.getSpellingLoc(loc); | 216 if (!GetFilename(loc, &filename)) { |
| 206 PresumedLoc ploc = SM.getPresumedLoc(spelling_location); | 217 // If the filename cannot be determined, simply treat this as a banned |
| 207 std::string buffer_name; | 218 // location, instead of going through the full lookup process. |
| 208 if (ploc.isInvalid()) { | |
| 209 // If we're in an invalid location, we're looking at things that aren't | |
| 210 // actually stated in the source; treat this as a banned location instead | |
| 211 // of going through our full lookup process. | |
| 212 return true; | 219 return true; |
| 213 } else { | 220 } |
| 214 std::string b = ploc.getFilename(); | |
| 215 | 221 |
| 216 // We need to special case scratch space; which is where clang does its | 222 // We need to special case scratch space; which is where clang does its |
| 217 // macro expansion. We explicitly want to allow people to do otherwise bad | 223 // macro expansion. We explicitly want to allow people to do otherwise bad |
| 218 // things through macros that were defined due to third party libraries. | 224 // things through macros that were defined due to third party libraries. |
| 219 if (b == "<scratch space>") | 225 if (filename == "<scratch space>") |
| 220 return true; | 226 return true; |
| 221 | 227 |
| 222 // Don't complain about these things in implementation files. | 228 // Don't complain about autogenerated protobuf files. |
| 223 if (ends_with(b, ".cc") || ends_with(b, ".cpp") || ends_with(b, ".mm")) { | 229 if (ends_with(filename, ".pb.h")) { |
| 224 return true; | 230 return true; |
| 225 } | 231 } |
| 226 | 232 |
| 227 // Don't complain about autogenerated protobuf files. | 233 // We need to munge the paths so that they are relative to the repository |
| 228 if (ends_with(b, ".pb.h")) { | 234 // srcroot. We first resolve the symlinktastic relative path and then |
| 229 return true; | 235 // remove our known srcroot from it if needed. |
| 230 } | 236 char resolvedPath[MAXPATHLEN]; |
| 237 if (realpath(filename.c_str(), resolvedPath)) { |
| 238 filename = resolvedPath; |
| 239 } |
| 231 | 240 |
| 232 // We need to munge the paths so that they are relative to the repository | 241 // On linux, chrome is often checked out to /usr/local/google. Due to the |
| 233 // srcroot. We first resolve the symlinktastic relative path and then | 242 // "usr" rule in banned_directories_, all diagnostics would be suppressed |
| 234 // remove our known srcroot from it if needed. | 243 // in that case. As a workaround, strip that prefix. |
| 235 char resolvedPath[MAXPATHLEN]; | 244 filename = lstrip(filename, "/usr/local/google"); |
| 236 if (realpath(b.c_str(), resolvedPath)) { | |
| 237 b = resolvedPath; | |
| 238 } | |
| 239 | 245 |
| 240 // On linux, chrome is often checked out to /usr/local/google. Due to the | 246 for (std::vector<std::string>::const_iterator it = |
| 241 // "usr" rule in banned_directories_, all diagnostics would be suppressed | 247 banned_directories_.begin(); |
| 242 // in that case. As a workaround, strip that prefix. | 248 it != banned_directories_.end(); ++it) { |
| 243 b = lstrip(b, "/usr/local/google"); | 249 // If we can find any of the banned path components in this path, then |
| 244 | 250 // this file is rejected. |
| 245 for (std::vector<std::string>::const_iterator it = | 251 size_t index = filename.find(*it); |
| 246 banned_directories_.begin(); | 252 if (index != std::string::npos) { |
| 247 it != banned_directories_.end(); ++it) { | 253 bool matches_full_dir_name = index == 0 || filename[index - 1] == '/'; |
| 248 // If we can find any of the banned path components in this path, then | 254 if ((*it)[0] == '/') |
| 249 // this file is rejected. | 255 matches_full_dir_name = true; |
| 250 size_t index = b.find(*it); | 256 if (matches_full_dir_name) |
| 251 if (index != std::string::npos) { | 257 return true; |
| 252 bool matches_full_dir_name = index == 0 || b[index - 1] == '/'; | |
| 253 if ((*it)[0] == '/') | |
| 254 matches_full_dir_name = true; | |
| 255 if (matches_full_dir_name) | |
| 256 return true; | |
| 257 } | |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 return false; | 261 return false; |
| 262 } | 262 } |
| 263 | 263 |
| 264 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { | 264 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { |
| 265 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); | 265 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); |
| 266 } | 266 } |
| 267 |
| 268 bool ChromeClassTester::GetFilename(SourceLocation loc, |
| 269 std::string* filename) { |
| 270 const SourceManager &SM = instance_.getSourceManager(); |
| 271 SourceLocation spelling_location = SM.getSpellingLoc(loc); |
| 272 PresumedLoc ploc = SM.getPresumedLoc(spelling_location); |
| 273 if (ploc.isInvalid()) { |
| 274 // If we're in an invalid location, we're looking at things that aren't |
| 275 // actually stated in the source. |
| 276 return false; |
| 277 } |
| 278 |
| 279 *filename = ploc.getFilename(); |
| 280 return true; |
| 281 } |
| OLD | NEW |