Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | // A general interface for filtering and only acting on classes in Chromium C++ |
michael@0 | 6 | // code. |
michael@0 | 7 | |
michael@0 | 8 | #include "ChromeClassTester.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <sys/param.h> |
michael@0 | 11 | |
michael@0 | 12 | #include "clang/AST/AST.h" |
michael@0 | 13 | #include "clang/Basic/FileManager.h" |
michael@0 | 14 | #include "clang/Basic/SourceManager.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace clang; |
michael@0 | 17 | |
michael@0 | 18 | namespace { |
michael@0 | 19 | |
michael@0 | 20 | bool starts_with(const std::string& one, const std::string& two) { |
michael@0 | 21 | return one.compare(0, two.size(), two) == 0; |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | std::string lstrip(const std::string& one, const std::string& two) { |
michael@0 | 25 | if (starts_with(one, two)) |
michael@0 | 26 | return one.substr(two.size()); |
michael@0 | 27 | return one; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | bool ends_with(const std::string& one, const std::string& two) { |
michael@0 | 31 | if (two.size() > one.size()) |
michael@0 | 32 | return false; |
michael@0 | 33 | |
michael@0 | 34 | return one.compare(one.size() - two.size(), two.size(), two) == 0; |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | } // namespace |
michael@0 | 38 | |
michael@0 | 39 | ChromeClassTester::ChromeClassTester(CompilerInstance& instance) |
michael@0 | 40 | : instance_(instance), |
michael@0 | 41 | diagnostic_(instance.getDiagnostics()) { |
michael@0 | 42 | BuildBannedLists(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | ChromeClassTester::~ChromeClassTester() {} |
michael@0 | 46 | |
michael@0 | 47 | void ChromeClassTester::HandleTagDeclDefinition(TagDecl* tag) { |
michael@0 | 48 | pending_class_decls_.push_back(tag); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | bool ChromeClassTester::HandleTopLevelDecl(DeclGroupRef group_ref) { |
michael@0 | 52 | for (size_t i = 0; i < pending_class_decls_.size(); ++i) |
michael@0 | 53 | CheckTag(pending_class_decls_[i]); |
michael@0 | 54 | pending_class_decls_.clear(); |
michael@0 | 55 | |
michael@0 | 56 | return true; // true means continue parsing. |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | void ChromeClassTester::CheckTag(TagDecl* tag) { |
michael@0 | 60 | // We handle class types here where we have semantic information. We can only |
michael@0 | 61 | // check structs/classes/enums here, but we get a bunch of nice semantic |
michael@0 | 62 | // information instead of just parsing information. |
michael@0 | 63 | |
michael@0 | 64 | if (CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)) { |
michael@0 | 65 | // If this is a POD or a class template or a type dependent on a |
michael@0 | 66 | // templated class, assume there's no ctor/dtor/virtual method |
michael@0 | 67 | // optimization that we can do. |
michael@0 | 68 | if (record->isPOD() || |
michael@0 | 69 | record->getDescribedClassTemplate() || |
michael@0 | 70 | record->getTemplateSpecializationKind() || |
michael@0 | 71 | record->isDependentType()) |
michael@0 | 72 | return; |
michael@0 | 73 | |
michael@0 | 74 | if (InBannedNamespace(record)) |
michael@0 | 75 | return; |
michael@0 | 76 | |
michael@0 | 77 | SourceLocation record_location = record->getInnerLocStart(); |
michael@0 | 78 | if (InBannedDirectory(record_location)) |
michael@0 | 79 | return; |
michael@0 | 80 | |
michael@0 | 81 | // We sadly need to maintain a blacklist of types that violate these |
michael@0 | 82 | // rules, but do so for good reason or due to limitations of this |
michael@0 | 83 | // checker (i.e., we don't handle extern templates very well). |
michael@0 | 84 | std::string base_name = record->getNameAsString(); |
michael@0 | 85 | if (IsIgnoredType(base_name)) |
michael@0 | 86 | return; |
michael@0 | 87 | |
michael@0 | 88 | // We ignore all classes that end with "Matcher" because they're probably |
michael@0 | 89 | // GMock artifacts. |
michael@0 | 90 | if (ends_with(base_name, "Matcher")) |
michael@0 | 91 | return; |
michael@0 | 92 | |
michael@0 | 93 | CheckChromeClass(record_location, record); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void ChromeClassTester::emitWarning(SourceLocation loc, |
michael@0 | 98 | const char* raw_error) { |
michael@0 | 99 | FullSourceLoc full(loc, instance().getSourceManager()); |
michael@0 | 100 | std::string err; |
michael@0 | 101 | err = "[chromium-style] "; |
michael@0 | 102 | err += raw_error; |
michael@0 | 103 | DiagnosticsEngine::Level level = |
michael@0 | 104 | diagnostic().getWarningsAsErrors() ? |
michael@0 | 105 | DiagnosticsEngine::Error : |
michael@0 | 106 | DiagnosticsEngine::Warning; |
michael@0 | 107 | unsigned id = diagnostic().getCustomDiagID(level, err); |
michael@0 | 108 | DiagnosticBuilder builder = diagnostic().Report(full, id); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | bool ChromeClassTester::InBannedNamespace(const Decl* record) { |
michael@0 | 112 | std::string n = GetNamespace(record); |
michael@0 | 113 | if (!n.empty()) { |
michael@0 | 114 | return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n) |
michael@0 | 115 | != banned_namespaces_.end(); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | return false; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | std::string ChromeClassTester::GetNamespace(const Decl* record) { |
michael@0 | 122 | return GetNamespaceImpl(record->getDeclContext(), ""); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | bool ChromeClassTester::InImplementationFile(SourceLocation record_location) { |
michael@0 | 126 | std::string filename; |
michael@0 | 127 | if (!GetFilename(record_location, &filename)) |
michael@0 | 128 | return false; |
michael@0 | 129 | |
michael@0 | 130 | if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") || |
michael@0 | 131 | ends_with(filename, ".mm")) { |
michael@0 | 132 | return true; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | return false; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void ChromeClassTester::BuildBannedLists() { |
michael@0 | 139 | banned_namespaces_.push_back("std"); |
michael@0 | 140 | banned_namespaces_.push_back("__gnu_cxx"); |
michael@0 | 141 | banned_namespaces_.push_back("WebKit"); |
michael@0 | 142 | |
michael@0 | 143 | banned_directories_.push_back("third_party/"); |
michael@0 | 144 | banned_directories_.push_back("native_client/"); |
michael@0 | 145 | banned_directories_.push_back("breakpad/"); |
michael@0 | 146 | banned_directories_.push_back("courgette/"); |
michael@0 | 147 | banned_directories_.push_back("pdf/"); |
michael@0 | 148 | banned_directories_.push_back("ppapi/"); |
michael@0 | 149 | banned_directories_.push_back("usr/"); |
michael@0 | 150 | banned_directories_.push_back("testing/"); |
michael@0 | 151 | banned_directories_.push_back("googleurl/"); |
michael@0 | 152 | banned_directories_.push_back("v8/"); |
michael@0 | 153 | banned_directories_.push_back("dart/"); |
michael@0 | 154 | banned_directories_.push_back("sdch/"); |
michael@0 | 155 | banned_directories_.push_back("icu4c/"); |
michael@0 | 156 | banned_directories_.push_back("frameworks/"); |
michael@0 | 157 | |
michael@0 | 158 | // Don't check autogenerated headers. |
michael@0 | 159 | // Make puts them below $(builddir_name)/.../gen and geni. |
michael@0 | 160 | // Ninja puts them below OUTPUT_DIR/.../gen |
michael@0 | 161 | // Xcode has a fixed output directory for everything. |
michael@0 | 162 | banned_directories_.push_back("gen/"); |
michael@0 | 163 | banned_directories_.push_back("geni/"); |
michael@0 | 164 | banned_directories_.push_back("xcodebuild/"); |
michael@0 | 165 | |
michael@0 | 166 | // You are standing in a mazy of twisty dependencies, all resolved by |
michael@0 | 167 | // putting everything in the header. |
michael@0 | 168 | banned_directories_.push_back("automation/"); |
michael@0 | 169 | |
michael@0 | 170 | // Don't check system headers. |
michael@0 | 171 | banned_directories_.push_back("/Developer/"); |
michael@0 | 172 | |
michael@0 | 173 | // Used in really low level threading code that probably shouldn't be out of |
michael@0 | 174 | // lined. |
michael@0 | 175 | ignored_record_names_.insert("ThreadLocalBoolean"); |
michael@0 | 176 | |
michael@0 | 177 | // A complicated pickle derived struct that is all packed integers. |
michael@0 | 178 | ignored_record_names_.insert("Header"); |
michael@0 | 179 | |
michael@0 | 180 | // Part of the GPU system that uses multiple included header |
michael@0 | 181 | // weirdness. Never getting this right. |
michael@0 | 182 | ignored_record_names_.insert("Validators"); |
michael@0 | 183 | |
michael@0 | 184 | // Has a UNIT_TEST only constructor. Isn't *terribly* complex... |
michael@0 | 185 | ignored_record_names_.insert("AutocompleteController"); |
michael@0 | 186 | ignored_record_names_.insert("HistoryURLProvider"); |
michael@0 | 187 | |
michael@0 | 188 | // Because of chrome frame |
michael@0 | 189 | ignored_record_names_.insert("ReliabilityTestSuite"); |
michael@0 | 190 | |
michael@0 | 191 | // Used over in the net unittests. A large enough bundle of integers with 1 |
michael@0 | 192 | // non-pod class member. Probably harmless. |
michael@0 | 193 | ignored_record_names_.insert("MockTransaction"); |
michael@0 | 194 | |
michael@0 | 195 | // Used heavily in ui_unittests and once in views_unittests. Fixing this |
michael@0 | 196 | // isn't worth the overhead of an additional library. |
michael@0 | 197 | ignored_record_names_.insert("TestAnimationDelegate"); |
michael@0 | 198 | |
michael@0 | 199 | // Part of our public interface that nacl and friends use. (Arguably, this |
michael@0 | 200 | // should mean that this is a higher priority but fixing this looks hard.) |
michael@0 | 201 | ignored_record_names_.insert("PluginVersionInfo"); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context, |
michael@0 | 205 | const std::string& candidate) { |
michael@0 | 206 | switch (context->getDeclKind()) { |
michael@0 | 207 | case Decl::TranslationUnit: { |
michael@0 | 208 | return candidate; |
michael@0 | 209 | } |
michael@0 | 210 | case Decl::Namespace: { |
michael@0 | 211 | const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context); |
michael@0 | 212 | std::string name_str; |
michael@0 | 213 | llvm::raw_string_ostream OS(name_str); |
michael@0 | 214 | if (decl->isAnonymousNamespace()) |
michael@0 | 215 | OS << "<anonymous namespace>"; |
michael@0 | 216 | else |
michael@0 | 217 | OS << *decl; |
michael@0 | 218 | return GetNamespaceImpl(context->getParent(), |
michael@0 | 219 | OS.str()); |
michael@0 | 220 | } |
michael@0 | 221 | default: { |
michael@0 | 222 | return GetNamespaceImpl(context->getParent(), candidate); |
michael@0 | 223 | } |
michael@0 | 224 | } |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
michael@0 | 228 | std::string filename; |
michael@0 | 229 | if (!GetFilename(loc, &filename)) { |
michael@0 | 230 | // If the filename cannot be determined, simply treat this as a banned |
michael@0 | 231 | // location, instead of going through the full lookup process. |
michael@0 | 232 | return true; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // We need to special case scratch space; which is where clang does its |
michael@0 | 236 | // macro expansion. We explicitly want to allow people to do otherwise bad |
michael@0 | 237 | // things through macros that were defined due to third party libraries. |
michael@0 | 238 | if (filename == "<scratch space>") |
michael@0 | 239 | return true; |
michael@0 | 240 | |
michael@0 | 241 | // Don't complain about autogenerated protobuf files. |
michael@0 | 242 | if (ends_with(filename, ".pb.h")) { |
michael@0 | 243 | return true; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | // We need to munge the paths so that they are relative to the repository |
michael@0 | 247 | // srcroot. We first resolve the symlinktastic relative path and then |
michael@0 | 248 | // remove our known srcroot from it if needed. |
michael@0 | 249 | char resolvedPath[MAXPATHLEN]; |
michael@0 | 250 | if (realpath(filename.c_str(), resolvedPath)) { |
michael@0 | 251 | filename = resolvedPath; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | // On linux, chrome is often checked out to /usr/local/google. Due to the |
michael@0 | 255 | // "usr" rule in banned_directories_, all diagnostics would be suppressed |
michael@0 | 256 | // in that case. As a workaround, strip that prefix. |
michael@0 | 257 | filename = lstrip(filename, "/usr/local/google"); |
michael@0 | 258 | |
michael@0 | 259 | for (std::vector<std::string>::const_iterator it = |
michael@0 | 260 | banned_directories_.begin(); |
michael@0 | 261 | it != banned_directories_.end(); ++it) { |
michael@0 | 262 | // If we can find any of the banned path components in this path, then |
michael@0 | 263 | // this file is rejected. |
michael@0 | 264 | size_t index = filename.find(*it); |
michael@0 | 265 | if (index != std::string::npos) { |
michael@0 | 266 | bool matches_full_dir_name = index == 0 || filename[index - 1] == '/'; |
michael@0 | 267 | if ((*it)[0] == '/') |
michael@0 | 268 | matches_full_dir_name = true; |
michael@0 | 269 | if (matches_full_dir_name) |
michael@0 | 270 | return true; |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | return false; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { |
michael@0 | 278 | return ignored_record_names_.find(base_name) != ignored_record_names_.end(); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | bool ChromeClassTester::GetFilename(SourceLocation loc, |
michael@0 | 282 | std::string* filename) { |
michael@0 | 283 | const SourceManager& source_manager = instance_.getSourceManager(); |
michael@0 | 284 | SourceLocation spelling_location = source_manager.getSpellingLoc(loc); |
michael@0 | 285 | PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); |
michael@0 | 286 | if (ploc.isInvalid()) { |
michael@0 | 287 | // If we're in an invalid location, we're looking at things that aren't |
michael@0 | 288 | // actually stated in the source. |
michael@0 | 289 | return false; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | *filename = ploc.getFilename(); |
michael@0 | 293 | return true; |
michael@0 | 294 | } |