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.
5 #ifndef TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_
6 #define TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/TypeLoc.h"
13 #include "clang/Frontend/CompilerInstance.h"
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 {
19 explicit ChromeClassTester(clang::CompilerInstance& instance,
20 bool check_url_directory);
21 virtual ~ChromeClassTester();
23 // clang::ASTConsumer:
24 virtual void HandleTagDeclDefinition(clang::TagDecl* tag);
25 virtual bool HandleTopLevelDecl(clang::DeclGroupRef group_ref);
28 clang::CompilerInstance& instance() { return instance_; }
29 clang::DiagnosticsEngine& diagnostic() { return diagnostic_; }
31 // Emits a simple warning; this shouldn't be used if you require printf-style
33 void emitWarning(clang::SourceLocation loc, const char* error);
35 // Utility method for subclasses to check if this class is in a banned
37 bool InBannedNamespace(const clang::Decl* record);
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);
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);
49 void BuildBannedLists();
51 void CheckTag(clang::TagDecl*);
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;
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);
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);
69 clang::CompilerInstance& instance_;
70 clang::DiagnosticsEngine& diagnostic_;
72 // List of banned namespaces.
73 std::vector<std::string> banned_namespaces_;
75 // List of banned directories.
76 std::vector<std::string> banned_directories_;
78 // List of types that we don't check.
79 std::set<std::string> ignored_record_names_;
81 // List of decls to check once the current top-level decl is parsed.
82 std::vector<clang::TagDecl*> pending_class_decls_;
84 // TODO(tfarina): Remove once url/ directory compiles without warnings.
85 bool check_url_directory_;
88 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_