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 #ifndef TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ 6 #define TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ 7 8 #include <set> 9 #include <vector> 10 11 #include "Options.h" 12 #include "clang/AST/ASTConsumer.h" 13 #include "clang/AST/TypeLoc.h" 14 #include "clang/Frontend/CompilerInstance.h" 15 16 // A class on top of ASTConsumer that forwards classes defined in Chromium 17 // headers to subclasses which implement CheckChromeClass(). 18 // TODO(vmpstr): Fold this class into FindBadConstructsConsumer. 19 class ChromeClassTester { 20 public: 21 ChromeClassTester(clang::CompilerInstance& instance, 22 const chrome_checker::Options& options); 23 virtual ~ChromeClassTester(); 24 25 void CheckTag(clang::TagDecl*); 26 27 clang::DiagnosticsEngine::Level getErrorLevel(); 28 29 protected: instance()30 clang::CompilerInstance& instance() { return instance_; } diagnostic()31 clang::DiagnosticsEngine& diagnostic() { return diagnostic_; } 32 33 // Utility method for subclasses to check how a certain SourceLocation should 34 // be handled. The main criteria for classification is the SourceLocation's 35 // path (e.g. whether it's in //third_party). 36 enum class LocationType { 37 // Enforce all default checks. 38 kChrome, 39 // Enforces a subset of checks for Blink code. This is hopefully a 40 // transitional stage, as more plugin checks are gradually enabled in Blink. 41 kBlink, 42 // Skip all checks. Typically, this is third-party or generated code where 43 // it doesn't make sense to enforce Chrome's custom diagnostics. 44 kThirdParty, 45 }; 46 LocationType ClassifyLocation(clang::SourceLocation loc); 47 48 // Utility method to check whether the given record has any of the ignored 49 // base classes. 50 bool HasIgnoredBases(const clang::CXXRecordDecl* record); 51 52 // Utility method for subclasses to check if this class is within an 53 // implementation (.cc, .cpp, .mm) file. 54 bool InImplementationFile(clang::SourceLocation location); 55 56 // Options. 57 const chrome_checker::Options options_; 58 59 private: 60 void BuildBannedLists(); 61 62 // Filtered versions of tags that are only called with things defined in 63 // chrome header files. 64 virtual void CheckChromeClass(LocationType location_type, 65 clang::SourceLocation record_location, 66 clang::CXXRecordDecl* record) = 0; 67 68 // Utility methods used for filtering out non-chrome classes (and ones we 69 // deliberately ignore) in HandleTagDeclDefinition(). 70 bool IsIgnoredType(const std::string& base_name); 71 72 // Attempts to determine the filename for the given SourceLocation. 73 // Returns false if the filename could not be determined. 74 bool GetFilename(clang::SourceLocation loc, std::string* filename); 75 76 clang::CompilerInstance& instance_; 77 clang::DiagnosticsEngine& diagnostic_; 78 79 // List of banned directories. 80 std::set<std::string> banned_directories_; 81 82 // List of types that we don't check. 83 std::set<std::string> ignored_record_names_; 84 85 // List of base classes that we skip when checking complex class ctors/dtors. 86 std::set<std::string> ignored_base_classes_; 87 }; 88 89 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ 90