OLD | NEW |
1 // Copyright (c) 2011 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 void BuildBannedLists(); | |
25 | |
26 // ASTConsumer: | 24 // ASTConsumer: |
27 virtual void HandleTagDeclDefinition(clang::TagDecl* tag); | 25 virtual void HandleTagDeclDefinition(clang::TagDecl* tag); |
28 | 26 |
29 protected: | 27 protected: |
30 clang::CompilerInstance& instance() { return instance_; } | 28 clang::CompilerInstance& instance() { return instance_; } |
31 clang::DiagnosticsEngine& diagnostic() { return diagnostic_; } | 29 clang::DiagnosticsEngine& diagnostic() { return diagnostic_; } |
32 | 30 |
33 // Emits a simple warning; this shouldn't be used if you require printf-style | 31 // Emits a simple warning; this shouldn't be used if you require printf-style |
34 // printing. | 32 // printing. |
35 void emitWarning(clang::SourceLocation loc, const char* error); | 33 void emitWarning(clang::SourceLocation loc, const char* error); |
36 | 34 |
37 // Utility method for subclasses to check if testing details are in this | |
38 // class. Some tests won't care if a class has a ::testing member and others | |
39 // will. | |
40 bool InTestingNamespace(const clang::Decl* record); | |
41 | |
42 // Utility method for subclasses to check if this class is in a banned | 35 // Utility method for subclasses to check if this class is in a banned |
43 // namespace. | 36 // namespace. |
44 bool InBannedNamespace(const clang::Decl* record); | 37 bool InBannedNamespace(const clang::Decl* record); |
45 | 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 |
46 private: | 48 private: |
| 49 void BuildBannedLists(); |
| 50 |
47 // Filtered versions of tags that are only called with things defined in | 51 // Filtered versions of tags that are only called with things defined in |
48 // chrome header files. | 52 // chrome header files. |
49 virtual void CheckChromeClass(const clang::SourceLocation& record_location, | 53 virtual void CheckChromeClass(clang::SourceLocation record_location, |
50 clang::CXXRecordDecl* record) = 0; | 54 clang::CXXRecordDecl* record) = 0; |
51 | 55 |
52 // Utility methods used for filtering out non-chrome classes (and ones we | 56 // Utility methods used for filtering out non-chrome classes (and ones we |
53 // deliberately ignore) in HandleTagDeclDefinition(). | 57 // deliberately ignore) in HandleTagDeclDefinition(). |
54 std::string GetNamespace(const clang::Decl* record); | |
55 std::string GetNamespaceImpl(const clang::DeclContext* context, | 58 std::string GetNamespaceImpl(const clang::DeclContext* context, |
56 std::string candidate); | 59 const std::string& candidate); |
57 bool InBannedDirectory(clang::SourceLocation loc); | 60 bool InBannedDirectory(clang::SourceLocation loc); |
58 bool IsIgnoredType(const std::string& base_name); | 61 bool IsIgnoredType(const std::string& base_name); |
59 | 62 |
| 63 // Attempts to determine the filename for the given SourceLocation. |
| 64 // Returns false if the filename could not be determined. |
| 65 bool GetFilename(clang::SourceLocation loc, std::string* filename); |
| 66 |
60 clang::CompilerInstance& instance_; | 67 clang::CompilerInstance& instance_; |
61 clang::DiagnosticsEngine& diagnostic_; | 68 clang::DiagnosticsEngine& diagnostic_; |
62 | 69 |
63 // List of banned namespaces. | 70 // List of banned namespaces. |
64 std::vector<std::string> banned_namespaces_; | 71 std::vector<std::string> banned_namespaces_; |
65 | 72 |
66 // List of banned directories. | 73 // List of banned directories. |
67 std::vector<std::string> banned_directories_; | 74 std::vector<std::string> banned_directories_; |
68 | 75 |
69 // List of types that we don't check. | 76 // List of types that we don't check. |
70 std::set<std::string> ignored_record_names_; | 77 std::set<std::string> ignored_record_names_; |
71 }; | 78 }; |
72 | 79 |
73 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ | 80 #endif // TOOLS_CLANG_PLUGINS_CHROMECLASSTESTER_H_ |
OLD | NEW |