michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ michael@0: #define TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "clang/AST/ASTConsumer.h" michael@0: #include "clang/AST/TypeLoc.h" michael@0: #include "clang/Frontend/CompilerInstance.h" michael@0: michael@0: // A class on top of ASTConsumer that forwards classes defined in Chromium michael@0: // headers to subclasses which implement CheckChromeClass(). michael@0: class ChromeClassTester : public clang::ASTConsumer { michael@0: public: michael@0: explicit ChromeClassTester(clang::CompilerInstance& instance); michael@0: virtual ~ChromeClassTester(); michael@0: michael@0: // clang::ASTConsumer: michael@0: virtual void HandleTagDeclDefinition(clang::TagDecl* tag); michael@0: virtual bool HandleTopLevelDecl(clang::DeclGroupRef group_ref); michael@0: michael@0: protected: michael@0: clang::CompilerInstance& instance() { return instance_; } michael@0: clang::DiagnosticsEngine& diagnostic() { return diagnostic_; } michael@0: michael@0: // Emits a simple warning; this shouldn't be used if you require printf-style michael@0: // printing. michael@0: void emitWarning(clang::SourceLocation loc, const char* error); michael@0: michael@0: // Utility method for subclasses to check if this class is in a banned michael@0: // namespace. michael@0: bool InBannedNamespace(const clang::Decl* record); michael@0: michael@0: // Utility method for subclasses to determine the namespace of the michael@0: // specified record, if any. Unnamed namespaces will be identified as michael@0: // "". michael@0: std::string GetNamespace(const clang::Decl* record); michael@0: michael@0: // Utility method for subclasses to check if this class is within an michael@0: // implementation (.cc, .cpp, .mm) file. michael@0: bool InImplementationFile(clang::SourceLocation location); michael@0: michael@0: private: michael@0: void BuildBannedLists(); michael@0: michael@0: void CheckTag(clang::TagDecl*); michael@0: michael@0: // Filtered versions of tags that are only called with things defined in michael@0: // chrome header files. michael@0: virtual void CheckChromeClass(clang::SourceLocation record_location, michael@0: clang::CXXRecordDecl* record) = 0; michael@0: michael@0: // Utility methods used for filtering out non-chrome classes (and ones we michael@0: // deliberately ignore) in HandleTagDeclDefinition(). michael@0: std::string GetNamespaceImpl(const clang::DeclContext* context, michael@0: const std::string& candidate); michael@0: bool InBannedDirectory(clang::SourceLocation loc); michael@0: bool IsIgnoredType(const std::string& base_name); michael@0: michael@0: // Attempts to determine the filename for the given SourceLocation. michael@0: // Returns false if the filename could not be determined. michael@0: bool GetFilename(clang::SourceLocation loc, std::string* filename); michael@0: michael@0: clang::CompilerInstance& instance_; michael@0: clang::DiagnosticsEngine& diagnostic_; michael@0: michael@0: // List of banned namespaces. michael@0: std::vector banned_namespaces_; michael@0: michael@0: // List of banned directories. michael@0: std::vector banned_directories_; michael@0: michael@0: // List of types that we don't check. michael@0: std::set ignored_record_names_; michael@0: michael@0: // List of decls to check once the current top-level decl is parsed. michael@0: std::vector pending_class_decls_; michael@0: }; michael@0: michael@0: #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_