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