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