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> | |
| 11 | |
| 10 #include "clang/AST/AST.h" | 12 #include "clang/AST/AST.h" |
| 11 #include "clang/Basic/FileManager.h" | 13 #include "clang/Basic/FileManager.h" |
| 12 #include "clang/Basic/SourceManager.h" | 14 #include "clang/Basic/SourceManager.h" |
| 13 | 15 |
| 14 #ifdef LLVM_ON_UNIX | 16 #ifdef LLVM_ON_UNIX |
| 15 #include <sys/param.h> | 17 #include <sys/param.h> |
| 16 #endif | 18 #endif |
| 17 | 19 |
| 18 using namespace clang; | 20 using namespace clang; |
| 19 | 21 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 | 124 |
| 123 if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") || | 125 if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") || |
| 124 ends_with(filename, ".mm")) { | 126 ends_with(filename, ".mm")) { |
| 125 return true; | 127 return true; |
| 126 } | 128 } |
| 127 | 129 |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 130 | 132 |
| 131 void ChromeClassTester::BuildBannedLists() { | 133 void ChromeClassTester::BuildBannedLists() { |
| 132 banned_namespaces_.push_back("std"); | 134 banned_namespaces_.emplace_back("std"); |
|
Nico
2015/04/12 04:19:14
nit: I'd keep push_back since everybody knows what
dcheng
2015/04/12 06:29:30
But but but C++11!
I'll probably revisit this in
| |
| 133 banned_namespaces_.push_back("__gnu_cxx"); | 135 banned_namespaces_.emplace_back("__gnu_cxx"); |
| 134 | 136 |
| 135 banned_namespaces_.push_back("blink"); | 137 banned_namespaces_.emplace_back("blink"); |
| 136 banned_namespaces_.push_back("WTF"); | 138 banned_namespaces_.emplace_back("WTF"); |
| 137 | 139 |
| 138 banned_directories_.push_back("/third_party/"); | 140 // System headers: |
| 139 banned_directories_.push_back("/native_client/"); | 141 #if defined(LLVM_ON_UNIX) |
| 140 banned_directories_.push_back("/breakpad/"); | 142 banned_directories_.emplace_back("/usr/include/"); |
| 141 banned_directories_.push_back("/courgette/"); | 143 banned_directories_.emplace_back("/usr/lib/"); |
| 142 banned_directories_.push_back("/pdf/"); | 144 banned_directories_.emplace_back("/usr/local/include/"); |
| 143 banned_directories_.push_back("/ppapi/"); | 145 banned_directories_.emplace_back("/usr/local/lib/"); |
| 144 banned_directories_.push_back("/usr/include/"); | 146 #endif |
| 145 banned_directories_.push_back("/usr/lib/"); | 147 |
| 146 banned_directories_.push_back("/usr/local/include/"); | 148 banned_directories_.emplace_back("/third_party/"); |
| 147 banned_directories_.push_back("/usr/local/lib/"); | 149 banned_directories_.emplace_back("/native_client/"); |
| 148 banned_directories_.push_back("/testing/"); | 150 banned_directories_.emplace_back("/breakpad/"); |
| 149 banned_directories_.push_back("/v8/"); | 151 banned_directories_.emplace_back("/courgette/"); |
| 150 banned_directories_.push_back("/dart/"); | 152 banned_directories_.emplace_back("/pdf/"); |
| 151 banned_directories_.push_back("/sdch/"); | 153 banned_directories_.emplace_back("/ppapi/"); |
| 152 banned_directories_.push_back("/icu4c/"); | 154 banned_directories_.emplace_back("/testing/"); |
| 153 banned_directories_.push_back("/frameworks/"); | 155 banned_directories_.emplace_back("/v8/"); |
| 156 banned_directories_.emplace_back("/dart/"); | |
| 157 banned_directories_.emplace_back("/sdch/"); | |
| 158 banned_directories_.emplace_back("/icu4c/"); | |
| 159 banned_directories_.emplace_back("/frameworks/"); | |
| 154 | 160 |
| 155 // Don't check autogenerated headers. | 161 // Don't check autogenerated headers. |
| 156 // Make puts them below $(builddir_name)/.../gen and geni. | 162 // Make puts them below $(builddir_name)/.../gen and geni. |
| 157 // Ninja puts them below OUTPUT_DIR/.../gen | 163 // Ninja puts them below OUTPUT_DIR/.../gen |
| 158 // Xcode has a fixed output directory for everything. | 164 // Xcode has a fixed output directory for everything. |
| 159 banned_directories_.push_back("/gen/"); | 165 banned_directories_.emplace_back("/gen/"); |
| 160 banned_directories_.push_back("/geni/"); | 166 banned_directories_.emplace_back("/geni/"); |
| 161 banned_directories_.push_back("/xcodebuild/"); | 167 banned_directories_.emplace_back("/xcodebuild/"); |
| 162 | 168 |
| 163 // You are standing in a mazy of twisty dependencies, all resolved by | 169 // You are standing in a mazy of twisty dependencies, all resolved by |
| 164 // putting everything in the header. | 170 // putting everything in the header. |
| 165 banned_directories_.push_back("/automation/"); | 171 banned_directories_.emplace_back("/automation/"); |
| 166 | 172 |
| 167 // Don't check system headers. | 173 // Don't check system headers. |
| 168 banned_directories_.push_back("/Developer/"); | 174 banned_directories_.emplace_back("/Developer/"); |
| 169 | 175 |
| 170 // Used in really low level threading code that probably shouldn't be out of | 176 // Used in really low level threading code that probably shouldn't be out of |
| 171 // lined. | 177 // lined. |
| 172 ignored_record_names_.insert("ThreadLocalBoolean"); | 178 ignored_record_names_.emplace("ThreadLocalBoolean"); |
| 173 | 179 |
| 174 // A complicated pickle derived struct that is all packed integers. | 180 // A complicated pickle derived struct that is all packed integers. |
| 175 ignored_record_names_.insert("Header"); | 181 ignored_record_names_.emplace("Header"); |
| 176 | 182 |
| 177 // Part of the GPU system that uses multiple included header | 183 // Part of the GPU system that uses multiple included header |
| 178 // weirdness. Never getting this right. | 184 // weirdness. Never getting this right. |
| 179 ignored_record_names_.insert("Validators"); | 185 ignored_record_names_.emplace("Validators"); |
| 180 | 186 |
| 181 // Has a UNIT_TEST only constructor. Isn't *terribly* complex... | 187 // Has a UNIT_TEST only constructor. Isn't *terribly* complex... |
| 182 ignored_record_names_.insert("AutocompleteController"); | 188 ignored_record_names_.emplace("AutocompleteController"); |
| 183 ignored_record_names_.insert("HistoryURLProvider"); | 189 ignored_record_names_.emplace("HistoryURLProvider"); |
| 184 | 190 |
| 185 // Because of chrome frame | 191 // Because of chrome frame |
| 186 ignored_record_names_.insert("ReliabilityTestSuite"); | 192 ignored_record_names_.emplace("ReliabilityTestSuite"); |
| 187 | 193 |
| 188 // Used over in the net unittests. A large enough bundle of integers with 1 | 194 // Used over in the net unittests. A large enough bundle of integers with 1 |
| 189 // non-pod class member. Probably harmless. | 195 // non-pod class member. Probably harmless. |
| 190 ignored_record_names_.insert("MockTransaction"); | 196 ignored_record_names_.emplace("MockTransaction"); |
| 191 | 197 |
| 192 // Enum type with _LAST members where _LAST doesn't mean last enum value. | 198 // Enum type with _LAST members where _LAST doesn't mean last enum value. |
| 193 ignored_record_names_.insert("ServerFieldType"); | 199 ignored_record_names_.emplace("ServerFieldType"); |
| 194 | 200 |
| 195 // Used heavily in ui_base_unittests and once in views_unittests. Fixing this | 201 // Used heavily in ui_base_unittests and once in views_unittests. Fixing this |
| 196 // isn't worth the overhead of an additional library. | 202 // isn't worth the overhead of an additional library. |
| 197 ignored_record_names_.insert("TestAnimationDelegate"); | 203 ignored_record_names_.emplace("TestAnimationDelegate"); |
| 198 | 204 |
| 199 // Part of our public interface that nacl and friends use. (Arguably, this | 205 // Part of our public interface that nacl and friends use. (Arguably, this |
| 200 // should mean that this is a higher priority but fixing this looks hard.) | 206 // should mean that this is a higher priority but fixing this looks hard.) |
| 201 ignored_record_names_.insert("PluginVersionInfo"); | 207 ignored_record_names_.emplace("PluginVersionInfo"); |
| 202 | 208 |
| 203 // Measured performance improvement on cc_perftests. See | 209 // Measured performance improvement on cc_perftests. See |
| 204 // https://codereview.chromium.org/11299290/ | 210 // https://codereview.chromium.org/11299290/ |
| 205 ignored_record_names_.insert("QuadF"); | 211 ignored_record_names_.emplace("QuadF"); |
| 206 | 212 |
| 207 // Enum type with _LAST members where _LAST doesn't mean last enum value. | 213 // Enum type with _LAST members where _LAST doesn't mean last enum value. |
| 208 ignored_record_names_.insert("ViewID"); | 214 ignored_record_names_.emplace("ViewID"); |
| 209 } | 215 } |
| 210 | 216 |
| 211 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, | 217 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, |
| 212 const std::string& candidate) { | 218 const std::string& candidate) { |
| 213 switch (context->getDeclKind()) { | 219 switch (context->getDeclKind()) { |
| 214 case Decl::TranslationUnit: { | 220 case Decl::TranslationUnit: { |
| 215 return candidate; | 221 return candidate; |
| 216 } | 222 } |
| 217 case Decl::Namespace: { | 223 case Decl::Namespace: { |
| 218 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); | 224 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); |
| 219 std::string name_str; | 225 std::string name_str; |
| 220 llvm::raw_string_ostream OS(name_str); | 226 llvm::raw_string_ostream OS(name_str); |
| 221 if (decl->isAnonymousNamespace()) | 227 if (decl->isAnonymousNamespace()) |
| 222 OS << "<anonymous namespace>"; | 228 OS << "<anonymous namespace>"; |
| 223 else | 229 else |
| 224 OS << *decl; | 230 OS << *decl; |
| 225 return GetNamespaceImpl(context->getParent(), | 231 return GetNamespaceImpl(context->getParent(), |
| 226 OS.str()); | 232 OS.str()); |
| 227 } | 233 } |
| 228 default: { | 234 default: { |
| 229 return GetNamespaceImpl(context->getParent(), candidate); | 235 return GetNamespaceImpl(context->getParent(), candidate); |
| 230 } | 236 } |
| 231 } | 237 } |
| 232 } | 238 } |
| 233 | 239 |
| 234 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { | 240 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
| 241 if (instance().getSourceManager().isInSystemHeader(loc)) | |
| 242 return true; | |
| 243 | |
| 235 std::string filename; | 244 std::string filename; |
| 236 if (!GetFilename(loc, &filename)) { | 245 if (!GetFilename(loc, &filename)) { |
| 237 // If the filename cannot be determined, simply treat this as a banned | 246 // If the filename cannot be determined, simply treat this as a banned |
| 238 // location, instead of going through the full lookup process. | 247 // location, instead of going through the full lookup process. |
| 239 return true; | 248 return true; |
| 240 } | 249 } |
| 241 | 250 |
| 242 // We need to special case scratch space; which is where clang does its | 251 // We need to special case scratch space; which is where clang does its |
| 243 // macro expansion. We explicitly want to allow people to do otherwise bad | 252 // macro expansion. We explicitly want to allow people to do otherwise bad |
| 244 // things through macros that were defined due to third party libraries. | 253 // things through macros that were defined due to third party libraries. |
| 245 if (filename == "<scratch space>") | 254 if (filename == "<scratch space>") |
| 246 return true; | 255 return true; |
| 247 | 256 |
| 248 // Don't complain about autogenerated protobuf files. | 257 // Don't complain about autogenerated protobuf files. |
| 249 if (ends_with(filename, ".pb.h")) { | 258 if (ends_with(filename, ".pb.h")) { |
| 250 return true; | 259 return true; |
| 251 } | 260 } |
| 252 | 261 |
| 253 #ifdef LLVM_ON_UNIX | 262 #if defined(LLVM_ON_UNIX) |
| 254 // We need to munge the paths so that they are relative to the repository | 263 // We need to munge the paths so that they are relative to the repository |
| 255 // srcroot. We first resolve the symlinktastic relative path and then | 264 // srcroot. We first resolve the symlinktastic relative path and then |
| 256 // remove our known srcroot from it if needed. | 265 // remove our known srcroot from it if needed. |
| 257 char resolvedPath[MAXPATHLEN]; | 266 char resolvedPath[MAXPATHLEN]; |
| 258 if (realpath(filename.c_str(), resolvedPath)) { | 267 if (realpath(filename.c_str(), resolvedPath)) { |
| 259 filename = resolvedPath; | 268 filename = resolvedPath; |
| 260 } | 269 } |
| 261 #endif | 270 #endif |
| 262 | 271 |
| 272 #if defined(LLVM_ON_WIN32) | |
| 273 std::replace(filename.begin(), filename.end(), '\\', '/'); | |
| 274 #endif | |
| 275 | |
| 263 for (const std::string& banned_dir : banned_directories_) { | 276 for (const std::string& banned_dir : banned_directories_) { |
| 264 // If any of the banned directories occur as a component in filename, | 277 // If any of the banned directories occur as a component in filename, |
| 265 // this file is rejected. | 278 // this file is rejected. |
| 266 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); | 279 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); |
| 267 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); | 280 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); |
| 268 | 281 |
| 269 if (filename.find(banned_dir) != std::string::npos) | 282 if (filename.find(banned_dir) != std::string::npos) |
| 270 return true; | 283 return true; |
| 271 } | 284 } |
| 272 | 285 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 284 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); | 297 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); |
| 285 if (ploc.isInvalid()) { | 298 if (ploc.isInvalid()) { |
| 286 // If we're in an invalid location, we're looking at things that aren't | 299 // If we're in an invalid location, we're looking at things that aren't |
| 287 // actually stated in the source. | 300 // actually stated in the source. |
| 288 return false; | 301 return false; |
| 289 } | 302 } |
| 290 | 303 |
| 291 *filename = ploc.getFilename(); | 304 *filename = ploc.getFilename(); |
| 292 return true; | 305 return true; |
| 293 } | 306 } |
| OLD | NEW |