Chromium Code Reviews| 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(const SourceLocation& loc, |
|
Nico
2012/04/11 22:27:26
ditto
| |
| 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( | |
| 174 const SourceLocation& record_location) { | |
| 175 std::string filename; | |
| 176 if (!GetFilename(record_location, &filename)) { | |
| 177 return false; | |
| 178 } | |
| 179 | |
| 180 if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") || | |
| 181 ends_with(filename, ".mm")) { | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 176 std::string ChromeClassTester::GetNamespace(const Decl* record) { | 188 std::string ChromeClassTester::GetNamespace(const Decl* record) { |
| 177 return GetNamespaceImpl(record->getDeclContext(), ""); | 189 return GetNamespaceImpl(record->getDeclContext(), ""); |
| 178 } | 190 } |
| 179 | 191 |
| 180 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, | 192 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, |
| 181 std::string candidate) { | 193 const std::string& candidate) { |
| 182 switch (context->getDeclKind()) { | 194 switch (context->getDeclKind()) { |
| 183 case Decl::TranslationUnit: { | 195 case Decl::TranslationUnit: { |
| 184 return candidate; | 196 return candidate; |
| 185 } | 197 } |
| 186 case Decl::Namespace: { | 198 case Decl::Namespace: { |
| 187 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); | 199 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); |
| 188 std::string name_str; | 200 std::string name_str; |
| 189 llvm::raw_string_ostream OS(name_str); | 201 llvm::raw_string_ostream OS(name_str); |
| 190 if (decl->isAnonymousNamespace()) | 202 if (decl->isAnonymousNamespace()) |
| 191 OS << "<anonymous namespace>"; | 203 OS << "<anonymous namespace>"; |
| 192 else | 204 else |
| 193 OS << *decl; | 205 OS << *decl; |
| 194 return GetNamespaceImpl(context->getParent(), | 206 return GetNamespaceImpl(context->getParent(), |
| 195 OS.str()); | 207 OS.str()); |
| 196 } | 208 } |
| 197 default: { | 209 default: { |
| 198 return GetNamespaceImpl(context->getParent(), candidate); | 210 return GetNamespaceImpl(context->getParent(), candidate); |
| 199 } | 211 } |
| 200 } | 212 } |
| 201 } | 213 } |
| 202 | 214 |
| 203 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { | 215 bool ChromeClassTester::InBannedDirectory(const SourceLocation& loc) { |
|
Nico
2012/04/11 22:27:26
ditto
| |
| 204 const SourceManager &SM = instance_.getSourceManager(); | 216 std::string filename; |
| 205 SourceLocation spelling_location = SM.getSpellingLoc(loc); | 217 if (!GetFilename(loc, &filename)) { |
| 206 PresumedLoc ploc = SM.getPresumedLoc(spelling_location); | 218 // If the filename cannot be determined, simply treat this as a banned |
| 207 std::string buffer_name; | 219 // 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; | 220 return true; |
| 213 } else { | 221 } |
| 214 std::string b = ploc.getFilename(); | |
| 215 | 222 |
| 216 // We need to special case scratch space; which is where clang does its | 223 // 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 | 224 // macro expansion. We explicitly want to allow people to do otherwise bad |
| 218 // things through macros that were defined due to third party libraries. | 225 // things through macros that were defined due to third party libraries. |
| 219 if (b == "<scratch space>") | 226 if (filename == "<scratch space>") |
| 220 return true; | 227 return true; |
| 221 | 228 |
| 222 // Don't complain about these things in implementation files. | 229 // Don't complain about autogenerated protobuf files. |
| 223 if (ends_with(b, ".cc") || ends_with(b, ".cpp") || ends_with(b, ".mm")) { | 230 if (ends_with(filename, ".pb.h")) { |
| 224 return true; | 231 return true; |
| 225 } | 232 } |
| 226 | 233 |
| 227 // Don't complain about autogenerated protobuf files. | 234 // We need to munge the paths so that they are relative to the repository |
| 228 if (ends_with(b, ".pb.h")) { | 235 // srcroot. We first resolve the symlinktastic relative path and then |
| 229 return true; | 236 // remove our known srcroot from it if needed. |
| 230 } | 237 char resolvedPath[MAXPATHLEN]; |
| 238 if (realpath(filename.c_str(), resolvedPath)) { | |
| 239 filename = resolvedPath; | |
| 240 } | |
| 231 | 241 |
| 232 // We need to munge the paths so that they are relative to the repository | 242 // 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 | 243 // "usr" rule in banned_directories_, all diagnostics would be suppressed |
| 234 // remove our known srcroot from it if needed. | 244 // in that case. As a workaround, strip that prefix. |
| 235 char resolvedPath[MAXPATHLEN]; | 245 filename = lstrip(filename, "/usr/local/google"); |
| 236 if (realpath(b.c_str(), resolvedPath)) { | |
| 237 b = resolvedPath; | |
| 238 } | |
| 239 | 246 |
| 240 // On linux, chrome is often checked out to /usr/local/google. Due to the | 247 for (std::vector<std::string>::const_iterator it = |
| 241 // "usr" rule in banned_directories_, all diagnostics would be suppressed | 248 banned_directories_.begin(); |
| 242 // in that case. As a workaround, strip that prefix. | 249 it != banned_directories_.end(); ++it) { |
| 243 b = lstrip(b, "/usr/local/google"); | 250 // If we can find any of the banned path components in this path, then |
| 244 | 251 // this file is rejected. |
| 245 for (std::vector<std::string>::const_iterator it = | 252 size_t index = filename.find(*it); |
| 246 banned_directories_.begin(); | 253 if (index != std::string::npos) { |
| 247 it != banned_directories_.end(); ++it) { | 254 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 | 255 if ((*it)[0] == '/') |
| 249 // this file is rejected. | 256 matches_full_dir_name = true; |
| 250 size_t index = b.find(*it); | 257 if (matches_full_dir_name) |
| 251 if (index != std::string::npos) { | 258 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 } | 259 } |
| 259 } | 260 } |
| 260 | 261 |
| 261 return false; | 262 return false; |
| 262 } | 263 } |
| 263 | 264 |
| 264 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { | 265 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { |
| 265 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); | 266 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); |
| 266 } | 267 } |
| 268 | |
| 269 bool ChromeClassTester::GetFilename(const clang::SourceLocation& loc, | |
| 270 std::string* filename) { | |
| 271 const SourceManager& SM = instance_.getSourceManager(); | |
|
Nico
2012/04/11 22:27:26
This file tries to follow clang style (it's copied
| |
| 272 SourceLocation spelling_location = SM.getSpellingLoc(loc); | |
| 273 PresumedLoc ploc = SM.getPresumedLoc(spelling_location); | |
| 274 if (ploc.isInvalid()) { | |
| 275 // If we're in an invalid location, we're looking at things that aren't | |
| 276 // actually stated in the source. | |
| 277 return false; | |
| 278 } | |
| 279 | |
| 280 *filename = ploc.getFilename(); | |
| 281 return true; | |
| 282 } | |
| OLD | NEW |