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 // Emits a simple warning; this shouldn't be used if you require printf-style 34 // printing. 35 void emitWarning(clang::SourceLocation loc, const char* error); 36 37 // Utility method for subclasses to check if this class is in a banned 38 // namespace. 39 bool InBannedNamespace(const clang::Decl* record); 40 41 // Utility method for subclasses to check if the source location is in a 42 // directory the plugin should ignore. 43 bool InBannedDirectory(clang::SourceLocation loc); 44 45 // Utility method for subclasses to determine the namespace of the 46 // specified record, if any. Unnamed namespaces will be identified as 47 // "<anonymous namespace>". 48 std::string GetNamespace(const clang::Decl* record); 49 50 // Utility method to check whether the given record has any of the ignored 51 // base classes. 52 bool HasIgnoredBases(const clang::CXXRecordDecl* record); 53 54 // Utility method for subclasses to check if this class is within an 55 // implementation (.cc, .cpp, .mm) file. 56 bool InImplementationFile(clang::SourceLocation location); 57 58 // Options. 59 const chrome_checker::Options options_; 60 61 private: 62 void BuildBannedLists(); 63 64 // Filtered versions of tags that are only called with things defined in 65 // chrome header files. 66 virtual void CheckChromeClass(clang::SourceLocation record_location, 67 clang::CXXRecordDecl* record) = 0; 68 69 // Filtered versions of enum type that are only called with things defined 70 // in chrome header files. CheckChromeEnum(clang::SourceLocation enum_location,clang::EnumDecl * enum_decl)71 virtual void CheckChromeEnum(clang::SourceLocation enum_location, 72 clang::EnumDecl* enum_decl) { 73 } 74 75 // Utility methods used for filtering out non-chrome classes (and ones we 76 // deliberately ignore) in HandleTagDeclDefinition(). 77 std::string GetNamespaceImpl(const clang::DeclContext* context, 78 const std::string& candidate); 79 bool IsIgnoredType(const std::string& base_name); 80 81 // Attempts to determine the filename for the given SourceLocation. 82 // Returns false if the filename could not be determined. 83 bool GetFilename(clang::SourceLocation loc, std::string* filename); 84 85 clang::CompilerInstance& instance_; 86 clang::DiagnosticsEngine& diagnostic_; 87 88 // List of banned namespaces. 89 std::set<std::string> banned_namespaces_; 90 91 // List of directories allowed even though their parent directories are in 92 // |banned_directories_|, below. 93 std::set<std::string> allowed_directories_; 94 95 // List of banned directories. 96 std::set<std::string> banned_directories_; 97 98 // List of types that we don't check. 99 std::set<std::string> ignored_record_names_; 100 101 // List of base classes that we skip when checking complex class ctors/dtors. 102 std::set<std::string> ignored_base_classes_; 103 }; 104 105 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ 106