Don't add extra app list launcher page webviews.
[chromium-blink-merge.git] / tools / clang / plugins / ChromeClassTester.cpp
bloba08ead4bd5d39f6c885d0ec2f7648451d14464c5
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++
6 // code.
8 #include "ChromeClassTester.h"
10 #include "clang/AST/AST.h"
11 #include "clang/Basic/FileManager.h"
12 #include "clang/Basic/SourceManager.h"
14 #ifdef LLVM_ON_UNIX
15 #include <sys/param.h>
16 #endif
18 using namespace clang;
20 namespace {
22 bool ends_with(const std::string& one, const std::string& two) {
23 if (two.size() > one.size())
24 return false;
26 return one.compare(one.size() - two.size(), two.size(), two) == 0;
29 } // namespace
31 ChromeClassTester::ChromeClassTester(CompilerInstance& instance)
32 : instance_(instance),
33 diagnostic_(instance.getDiagnostics()) {
34 BuildBannedLists();
37 ChromeClassTester::~ChromeClassTester() {}
39 void ChromeClassTester::HandleTagDeclDefinition(TagDecl* tag) {
40 pending_class_decls_.push_back(tag);
43 bool ChromeClassTester::HandleTopLevelDecl(DeclGroupRef group_ref) {
44 for (size_t i = 0; i < pending_class_decls_.size(); ++i)
45 CheckTag(pending_class_decls_[i]);
46 pending_class_decls_.clear();
48 return true; // true means continue parsing.
51 void ChromeClassTester::CheckTag(TagDecl* tag) {
52 // We handle class types here where we have semantic information. We can only
53 // check structs/classes/enums here, but we get a bunch of nice semantic
54 // information instead of just parsing information.
56 if (CXXRecordDecl* record = dyn_cast<CXXRecordDecl>(tag)) {
57 if (InBannedNamespace(record))
58 return;
60 SourceLocation record_location = record->getInnerLocStart();
61 if (InBannedDirectory(record_location))
62 return;
64 // We sadly need to maintain a blacklist of types that violate these
65 // rules, but do so for good reason or due to limitations of this
66 // checker (i.e., we don't handle extern templates very well).
67 std::string base_name = record->getNameAsString();
68 if (IsIgnoredType(base_name))
69 return;
71 // We ignore all classes that end with "Matcher" because they're probably
72 // GMock artifacts.
73 if (ends_with(base_name, "Matcher"))
74 return;
76 CheckChromeClass(record_location, record);
77 } else if (EnumDecl* enum_decl = dyn_cast<EnumDecl>(tag)) {
78 SourceLocation enum_location = enum_decl->getInnerLocStart();
79 if (InBannedDirectory(enum_location))
80 return;
82 std::string base_name = enum_decl->getNameAsString();
83 if (IsIgnoredType(base_name))
84 return;
86 CheckChromeEnum(enum_location, enum_decl);
90 void ChromeClassTester::emitWarning(SourceLocation loc,
91 const char* raw_error) {
92 FullSourceLoc full(loc, instance().getSourceManager());
93 std::string err;
94 err = "[chromium-style] ";
95 err += raw_error;
96 DiagnosticIDs::Level level =
97 diagnostic().getWarningsAsErrors() ?
98 DiagnosticIDs::Error :
99 DiagnosticIDs::Warning;
100 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err);
101 DiagnosticBuilder builder = diagnostic().Report(full, id);
104 bool ChromeClassTester::InBannedNamespace(const Decl* record) {
105 std::string n = GetNamespace(record);
106 if (!n.empty()) {
107 return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n)
108 != banned_namespaces_.end();
111 return false;
114 std::string ChromeClassTester::GetNamespace(const Decl* record) {
115 return GetNamespaceImpl(record->getDeclContext(), "");
118 bool ChromeClassTester::InImplementationFile(SourceLocation record_location) {
119 std::string filename;
120 if (!GetFilename(record_location, &filename))
121 return false;
123 if (ends_with(filename, ".cc") || ends_with(filename, ".cpp") ||
124 ends_with(filename, ".mm")) {
125 return true;
128 return false;
131 void ChromeClassTester::BuildBannedLists() {
132 banned_namespaces_.push_back("std");
133 banned_namespaces_.push_back("__gnu_cxx");
135 banned_namespaces_.push_back("blink");
136 banned_namespaces_.push_back("WTF");
138 banned_directories_.push_back("/third_party/");
139 banned_directories_.push_back("/native_client/");
140 banned_directories_.push_back("/breakpad/");
141 banned_directories_.push_back("/courgette/");
142 banned_directories_.push_back("/pdf/");
143 banned_directories_.push_back("/ppapi/");
144 banned_directories_.push_back("/usr/include/");
145 banned_directories_.push_back("/usr/lib/");
146 banned_directories_.push_back("/usr/local/include/");
147 banned_directories_.push_back("/usr/local/lib/");
148 banned_directories_.push_back("/testing/");
149 banned_directories_.push_back("/v8/");
150 banned_directories_.push_back("/dart/");
151 banned_directories_.push_back("/sdch/");
152 banned_directories_.push_back("/icu4c/");
153 banned_directories_.push_back("/frameworks/");
155 // Don't check autogenerated headers.
156 // Make puts them below $(builddir_name)/.../gen and geni.
157 // Ninja puts them below OUTPUT_DIR/.../gen
158 // Xcode has a fixed output directory for everything.
159 banned_directories_.push_back("/gen/");
160 banned_directories_.push_back("/geni/");
161 banned_directories_.push_back("/xcodebuild/");
163 // You are standing in a mazy of twisty dependencies, all resolved by
164 // putting everything in the header.
165 banned_directories_.push_back("/automation/");
167 // Don't check system headers.
168 banned_directories_.push_back("/Developer/");
170 // Used in really low level threading code that probably shouldn't be out of
171 // lined.
172 ignored_record_names_.insert("ThreadLocalBoolean");
174 // A complicated pickle derived struct that is all packed integers.
175 ignored_record_names_.insert("Header");
177 // Part of the GPU system that uses multiple included header
178 // weirdness. Never getting this right.
179 ignored_record_names_.insert("Validators");
181 // Has a UNIT_TEST only constructor. Isn't *terribly* complex...
182 ignored_record_names_.insert("AutocompleteController");
183 ignored_record_names_.insert("HistoryURLProvider");
185 // Because of chrome frame
186 ignored_record_names_.insert("ReliabilityTestSuite");
188 // Used over in the net unittests. A large enough bundle of integers with 1
189 // non-pod class member. Probably harmless.
190 ignored_record_names_.insert("MockTransaction");
192 // Enum type with _LAST members where _LAST doesn't mean last enum value.
193 ignored_record_names_.insert("ServerFieldType");
195 // Used heavily in ui_base_unittests and once in views_unittests. Fixing this
196 // isn't worth the overhead of an additional library.
197 ignored_record_names_.insert("TestAnimationDelegate");
199 // Part of our public interface that nacl and friends use. (Arguably, this
200 // should mean that this is a higher priority but fixing this looks hard.)
201 ignored_record_names_.insert("PluginVersionInfo");
203 // Measured performance improvement on cc_perftests. See
204 // https://codereview.chromium.org/11299290/
205 ignored_record_names_.insert("QuadF");
207 // Enum type with _LAST members where _LAST doesn't mean last enum value.
208 ignored_record_names_.insert("ViewID");
211 std::string ChromeClassTester::GetNamespaceImpl(const DeclContext* context,
212 const std::string& candidate) {
213 switch (context->getDeclKind()) {
214 case Decl::TranslationUnit: {
215 return candidate;
217 case Decl::Namespace: {
218 const NamespaceDecl* decl = dyn_cast<NamespaceDecl>(context);
219 std::string name_str;
220 llvm::raw_string_ostream OS(name_str);
221 if (decl->isAnonymousNamespace())
222 OS << "<anonymous namespace>";
223 else
224 OS << *decl;
225 return GetNamespaceImpl(context->getParent(),
226 OS.str());
228 default: {
229 return GetNamespaceImpl(context->getParent(), candidate);
234 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) {
235 std::string filename;
236 if (!GetFilename(loc, &filename)) {
237 // If the filename cannot be determined, simply treat this as a banned
238 // location, instead of going through the full lookup process.
239 return true;
242 // We need to special case scratch space; which is where clang does its
243 // macro expansion. We explicitly want to allow people to do otherwise bad
244 // things through macros that were defined due to third party libraries.
245 if (filename == "<scratch space>")
246 return true;
248 // Don't complain about autogenerated protobuf files.
249 if (ends_with(filename, ".pb.h")) {
250 return true;
253 #ifdef LLVM_ON_UNIX
254 // We need to munge the paths so that they are relative to the repository
255 // srcroot. We first resolve the symlinktastic relative path and then
256 // remove our known srcroot from it if needed.
257 char resolvedPath[MAXPATHLEN];
258 if (realpath(filename.c_str(), resolvedPath)) {
259 filename = resolvedPath;
261 #endif
263 for (const std::string& banned_dir : banned_directories_) {
264 // If any of the banned directories occur as a component in filename,
265 // this file is rejected.
266 assert(banned_dir.front() == '/' && "Banned dir must start with '/'");
267 assert(banned_dir.back() == '/' && "Banned dir must end with '/'");
269 if (filename.find(banned_dir) != std::string::npos)
270 return true;
273 return false;
276 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) {
277 return ignored_record_names_.find(base_name) != ignored_record_names_.end();
280 bool ChromeClassTester::GetFilename(SourceLocation loc,
281 std::string* filename) {
282 const SourceManager& source_manager = instance_.getSourceManager();
283 SourceLocation spelling_location = source_manager.getSpellingLoc(loc);
284 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location);
285 if (ploc.isInvalid()) {
286 // If we're in an invalid location, we're looking at things that aren't
287 // actually stated in the source.
288 return false;
291 *filename = ploc.getFilename();
292 return true;