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 "clang/AST/ASTConsumer.h" 12 #include "clang/AST/TypeLoc.h" 13 #include "clang/Frontend/CompilerInstance.h" 14 15 // A class on top of ASTConsumer that forwards classes defined in Chromium 16 // headers to subclasses which implement CheckChromeClass(). 17 class ChromeClassTester : public clang::ASTConsumer { 18 public: 19 explicit ChromeClassTester(clang::CompilerInstance& instance, 20 bool check_url_directory); 21 virtual ~ChromeClassTester(); 22 23 // clang::ASTConsumer: 24 virtual void HandleTagDeclDefinition(clang::TagDecl* tag); 25 virtual bool HandleTopLevelDecl(clang::DeclGroupRef group_ref); 26 27 protected: instance()28 clang::CompilerInstance& instance() { return instance_; } diagnostic()29 clang::DiagnosticsEngine& diagnostic() { return diagnostic_; } 30 31 // Emits a simple warning; this shouldn't be used if you require printf-style 32 // printing. 33 void emitWarning(clang::SourceLocation loc, const char* error); 34 35 // Utility method for subclasses to check if this class is in a banned 36 // namespace. 37 bool InBannedNamespace(const clang::Decl* record); 38 39 // Utility method for subclasses to determine the namespace of the 40 // specified record, if any. Unnamed namespaces will be identified as 41 // "<anonymous namespace>". 42 std::string GetNamespace(const clang::Decl* record); 43 44 // Utility method for subclasses to check if this class is within an 45 // implementation (.cc, .cpp, .mm) file. 46 bool InImplementationFile(clang::SourceLocation location); 47 48 private: 49 void BuildBannedLists(); 50 51 void CheckTag(clang::TagDecl*); 52 53 // Filtered versions of tags that are only called with things defined in 54 // chrome header files. 55 virtual void CheckChromeClass(clang::SourceLocation record_location, 56 clang::CXXRecordDecl* record) = 0; 57 58 // Utility methods used for filtering out non-chrome classes (and ones we 59 // deliberately ignore) in HandleTagDeclDefinition(). 60 std::string GetNamespaceImpl(const clang::DeclContext* context, 61 const std::string& candidate); 62 bool InBannedDirectory(clang::SourceLocation loc); 63 bool IsIgnoredType(const std::string& base_name); 64 65 // Attempts to determine the filename for the given SourceLocation. 66 // Returns false if the filename could not be determined. 67 bool GetFilename(clang::SourceLocation loc, std::string* filename); 68 69 clang::CompilerInstance& instance_; 70 clang::DiagnosticsEngine& diagnostic_; 71 72 // List of banned namespaces. 73 std::vector<std::string> banned_namespaces_; 74 75 // List of banned directories. 76 std::vector<std::string> banned_directories_; 77 78 // List of types that we don't check. 79 std::set<std::string> ignored_record_names_; 80 81 // List of decls to check once the current top-level decl is parsed. 82 std::vector<clang::TagDecl*> pending_class_decls_; 83 84 // TODO(tfarina): Remove once url/ directory compiles without warnings. 85 bool check_url_directory_; 86 }; 87 88 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ 89