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 // A general interface for filtering and only acting on classes in Chromium C++
8 #include "ChromeClassTester.h"
10 #include <sys/param.h>
12 #include "clang/AST/AST.h"
13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/SourceManager.h"
16 using namespace clang
;
20 bool starts_with(const std::string
& one
, const std::string
& two
) {
21 return one
.compare(0, two
.size(), two
) == 0;
24 std::string
lstrip(const std::string
& one
, const std::string
& two
) {
25 if (starts_with(one
, two
))
26 return one
.substr(two
.size());
30 bool ends_with(const std::string
& one
, const std::string
& two
) {
31 if (two
.size() > one
.size())
34 return one
.compare(one
.size() - two
.size(), two
.size(), two
) == 0;
39 ChromeClassTester::ChromeClassTester(CompilerInstance
& instance
)
40 : instance_(instance
),
41 diagnostic_(instance
.getDiagnostics()) {
45 ChromeClassTester::~ChromeClassTester() {}
47 void ChromeClassTester::HandleTagDeclDefinition(TagDecl
* tag
) {
48 pending_class_decls_
.push_back(tag
);
51 bool ChromeClassTester::HandleTopLevelDecl(DeclGroupRef group_ref
) {
52 for (size_t i
= 0; i
< pending_class_decls_
.size(); ++i
)
53 CheckTag(pending_class_decls_
[i
]);
54 pending_class_decls_
.clear();
56 return true; // true means continue parsing.
59 void ChromeClassTester::CheckTag(TagDecl
* tag
) {
60 // We handle class types here where we have semantic information. We can only
61 // check structs/classes/enums here, but we get a bunch of nice semantic
62 // information instead of just parsing information.
64 if (CXXRecordDecl
* record
= dyn_cast
<CXXRecordDecl
>(tag
)) {
65 if (InBannedNamespace(record
))
68 SourceLocation record_location
= record
->getInnerLocStart();
69 if (InBannedDirectory(record_location
))
72 // We sadly need to maintain a blacklist of types that violate these
73 // rules, but do so for good reason or due to limitations of this
74 // checker (i.e., we don't handle extern templates very well).
75 std::string base_name
= record
->getNameAsString();
76 if (IsIgnoredType(base_name
))
79 // We ignore all classes that end with "Matcher" because they're probably
81 if (ends_with(base_name
, "Matcher"))
84 CheckChromeClass(record_location
, record
);
85 } else if (EnumDecl
* enum_decl
= dyn_cast
<EnumDecl
>(tag
)) {
86 SourceLocation enum_location
= enum_decl
->getInnerLocStart();
87 if (InBannedDirectory(enum_location
))
90 std::string base_name
= enum_decl
->getNameAsString();
91 if (IsIgnoredType(base_name
))
94 CheckChromeEnum(enum_location
, enum_decl
);
98 void ChromeClassTester::emitWarning(SourceLocation loc
,
99 const char* raw_error
) {
100 FullSourceLoc
full(loc
, instance().getSourceManager());
102 err
= "[chromium-style] ";
104 DiagnosticIDs::Level level
=
105 diagnostic().getWarningsAsErrors() ?
106 DiagnosticIDs::Error
:
107 DiagnosticIDs::Warning
;
108 unsigned id
= diagnostic().getDiagnosticIDs()->getCustomDiagID(level
, err
);
109 DiagnosticBuilder builder
= diagnostic().Report(full
, id
);
112 bool ChromeClassTester::InBannedNamespace(const Decl
* record
) {
113 std::string n
= GetNamespace(record
);
115 return std::find(banned_namespaces_
.begin(), banned_namespaces_
.end(), n
)
116 != banned_namespaces_
.end();
122 std::string
ChromeClassTester::GetNamespace(const Decl
* record
) {
123 return GetNamespaceImpl(record
->getDeclContext(), "");
126 bool ChromeClassTester::InImplementationFile(SourceLocation record_location
) {
127 std::string filename
;
128 if (!GetFilename(record_location
, &filename
))
131 if (ends_with(filename
, ".cc") || ends_with(filename
, ".cpp") ||
132 ends_with(filename
, ".mm")) {
139 void ChromeClassTester::BuildBannedLists() {
140 banned_namespaces_
.push_back("std");
141 banned_namespaces_
.push_back("__gnu_cxx");
143 banned_namespaces_
.push_back("blink");
144 banned_namespaces_
.push_back("WTF");
146 banned_directories_
.push_back("/third_party/");
147 banned_directories_
.push_back("/native_client/");
148 banned_directories_
.push_back("/breakpad/");
149 banned_directories_
.push_back("/courgette/");
150 banned_directories_
.push_back("/pdf/");
151 banned_directories_
.push_back("/ppapi/");
152 banned_directories_
.push_back("/usr/include/");
153 banned_directories_
.push_back("/usr/lib/");
154 banned_directories_
.push_back("/usr/local/include/");
155 banned_directories_
.push_back("/usr/local/lib/");
156 banned_directories_
.push_back("/testing/");
157 banned_directories_
.push_back("/v8/");
158 banned_directories_
.push_back("/dart/");
159 banned_directories_
.push_back("/sdch/");
160 banned_directories_
.push_back("/icu4c/");
161 banned_directories_
.push_back("/frameworks/");
163 // Don't check autogenerated headers.
164 // Make puts them below $(builddir_name)/.../gen and geni.
165 // Ninja puts them below OUTPUT_DIR/.../gen
166 // Xcode has a fixed output directory for everything.
167 banned_directories_
.push_back("/gen/");
168 banned_directories_
.push_back("/geni/");
169 banned_directories_
.push_back("/xcodebuild/");
171 // You are standing in a mazy of twisty dependencies, all resolved by
172 // putting everything in the header.
173 banned_directories_
.push_back("/automation/");
175 // Don't check system headers.
176 banned_directories_
.push_back("/Developer/");
178 // Used in really low level threading code that probably shouldn't be out of
180 ignored_record_names_
.insert("ThreadLocalBoolean");
182 // A complicated pickle derived struct that is all packed integers.
183 ignored_record_names_
.insert("Header");
185 // Part of the GPU system that uses multiple included header
186 // weirdness. Never getting this right.
187 ignored_record_names_
.insert("Validators");
189 // Has a UNIT_TEST only constructor. Isn't *terribly* complex...
190 ignored_record_names_
.insert("AutocompleteController");
191 ignored_record_names_
.insert("HistoryURLProvider");
193 // Because of chrome frame
194 ignored_record_names_
.insert("ReliabilityTestSuite");
196 // Used over in the net unittests. A large enough bundle of integers with 1
197 // non-pod class member. Probably harmless.
198 ignored_record_names_
.insert("MockTransaction");
200 // Enum type with _LAST members where _LAST doesn't mean last enum value.
201 ignored_record_names_
.insert("ServerFieldType");
203 // Used heavily in ui_base_unittests and once in views_unittests. Fixing this
204 // isn't worth the overhead of an additional library.
205 ignored_record_names_
.insert("TestAnimationDelegate");
207 // Part of our public interface that nacl and friends use. (Arguably, this
208 // should mean that this is a higher priority but fixing this looks hard.)
209 ignored_record_names_
.insert("PluginVersionInfo");
211 // Measured performance improvement on cc_perftests. See
212 // https://codereview.chromium.org/11299290/
213 ignored_record_names_
.insert("QuadF");
215 // Enum type with _LAST members where _LAST doesn't mean last enum value.
216 ignored_record_names_
.insert("ViewID");
219 std::string
ChromeClassTester::GetNamespaceImpl(const DeclContext
* context
,
220 const std::string
& candidate
) {
221 switch (context
->getDeclKind()) {
222 case Decl::TranslationUnit
: {
225 case Decl::Namespace
: {
226 const NamespaceDecl
* decl
= dyn_cast
<NamespaceDecl
>(context
);
227 std::string name_str
;
228 llvm::raw_string_ostream
OS(name_str
);
229 if (decl
->isAnonymousNamespace())
230 OS
<< "<anonymous namespace>";
233 return GetNamespaceImpl(context
->getParent(),
237 return GetNamespaceImpl(context
->getParent(), candidate
);
242 bool ChromeClassTester::InBannedDirectory(SourceLocation loc
) {
243 std::string filename
;
244 if (!GetFilename(loc
, &filename
)) {
245 // If the filename cannot be determined, simply treat this as a banned
246 // location, instead of going through the full lookup process.
250 // We need to special case scratch space; which is where clang does its
251 // macro expansion. We explicitly want to allow people to do otherwise bad
252 // things through macros that were defined due to third party libraries.
253 if (filename
== "<scratch space>")
256 // Don't complain about autogenerated protobuf files.
257 if (ends_with(filename
, ".pb.h")) {
261 // We need to munge the paths so that they are relative to the repository
262 // srcroot. We first resolve the symlinktastic relative path and then
263 // remove our known srcroot from it if needed.
264 char resolvedPath
[MAXPATHLEN
];
265 if (realpath(filename
.c_str(), resolvedPath
)) {
266 filename
= resolvedPath
;
269 for (size_t i
= 0; i
< banned_directories_
.size(); ++i
) {
270 // If any of the banned directories occur as a component in filename,
271 // this file is rejected.
272 const std::string
& banned_dir
= banned_directories_
[i
];
273 assert(banned_dir
.front() == '/' && "Banned dir must start with '/'");
274 assert(banned_dir
.back() == '/' && "Banned dir must end with '/'");
276 if (filename
.find(banned_dir
) != std::string::npos
)
283 bool ChromeClassTester::IsIgnoredType(const std::string
& base_name
) {
284 return ignored_record_names_
.find(base_name
) != ignored_record_names_
.end();
287 bool ChromeClassTester::GetFilename(SourceLocation loc
,
288 std::string
* filename
) {
289 const SourceManager
& source_manager
= instance_
.getSourceManager();
290 SourceLocation spelling_location
= source_manager
.getSpellingLoc(loc
);
291 PresumedLoc ploc
= source_manager
.getPresumedLoc(spelling_location
);
292 if (ploc
.isInvalid()) {
293 // If we're in an invalid location, we're looking at things that aren't
294 // actually stated in the source.
298 *filename
= ploc
.getFilename();