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 // A general interface for filtering and only acting on classes in Chromium C++ | 5 // A general interface for filtering and only acting on classes in Chromium C++ |
6 // code. | 6 // code. |
7 | 7 |
8 #include "ChromeClassTester.h" | 8 #include "ChromeClassTester.h" |
9 | 9 |
10 #include <sys/param.h> | 10 #include <algorithm> |
11 | 11 |
12 #include "clang/AST/AST.h" | 12 #include "clang/AST/AST.h" |
13 #include "clang/Basic/FileManager.h" | 13 #include "clang/Basic/FileManager.h" |
14 #include "clang/Basic/SourceManager.h" | 14 #include "clang/Basic/SourceManager.h" |
15 | 15 |
| 16 #ifdef LLVM_ON_UNIX |
| 17 #include <sys/param.h> |
| 18 #endif |
| 19 |
16 using namespace clang; | 20 using namespace clang; |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 bool starts_with(const std::string& one, const std::string& two) { | |
21 return one.compare(0, two.size(), two) == 0; | |
22 } | |
23 | |
24 std::string lstrip(const std::string& one, const std::string& two) { | |
25 if (starts_with(one, two)) | |
26 return one.substr(two.size()); | |
27 return one; | |
28 } | |
29 | |
30 bool ends_with(const std::string& one, const std::string& two) { | 24 bool ends_with(const std::string& one, const std::string& two) { |
31 if (two.size() > one.size()) | 25 if (two.size() > one.size()) |
32 return false; | 26 return false; |
33 | 27 |
34 return one.compare(one.size() - two.size(), two.size(), two) == 0; | 28 return one.compare(one.size() - two.size(), two.size(), two) == 0; |
35 } | 29 } |
36 | 30 |
37 } // namespace | 31 } // namespace |
38 | 32 |
39 ChromeClassTester::ChromeClassTester(CompilerInstance& instance) | 33 ChromeClassTester::ChromeClassTester(CompilerInstance& instance) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 CheckChromeEnum(enum_location, enum_decl); | 88 CheckChromeEnum(enum_location, enum_decl); |
95 } | 89 } |
96 } | 90 } |
97 | 91 |
98 void ChromeClassTester::emitWarning(SourceLocation loc, | 92 void ChromeClassTester::emitWarning(SourceLocation loc, |
99 const char* raw_error) { | 93 const char* raw_error) { |
100 FullSourceLoc full(loc, instance().getSourceManager()); | 94 FullSourceLoc full(loc, instance().getSourceManager()); |
101 std::string err; | 95 std::string err; |
102 err = "[chromium-style] "; | 96 err = "[chromium-style] "; |
103 err += raw_error; | 97 err += raw_error; |
| 98 // TODO(dcheng): Re-enable -Werror for these diagnostics on Windows once all |
| 99 // the pre-existing warnings are cleaned up. https://crbug.com/467287 |
104 DiagnosticIDs::Level level = | 100 DiagnosticIDs::Level level = |
| 101 #if !defined(LLVM_ON_WIN32) |
105 diagnostic().getWarningsAsErrors() ? | 102 diagnostic().getWarningsAsErrors() ? |
106 DiagnosticIDs::Error : | 103 DiagnosticIDs::Error : |
| 104 #endif |
107 DiagnosticIDs::Warning; | 105 DiagnosticIDs::Warning; |
108 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); | 106 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); |
109 DiagnosticBuilder builder = diagnostic().Report(full, id); | 107 DiagnosticBuilder builder = diagnostic().Report(full, id); |
110 } | 108 } |
111 | 109 |
| 110 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
| 111 if (instance().getSourceManager().isInSystemHeader(loc)) |
| 112 return true; |
| 113 |
| 114 std::string filename; |
| 115 if (!GetFilename(loc, &filename)) { |
| 116 // If the filename cannot be determined, simply treat this as a banned |
| 117 // location, instead of going through the full lookup process. |
| 118 return true; |
| 119 } |
| 120 |
| 121 // We need to special case scratch space; which is where clang does its |
| 122 // macro expansion. We explicitly want to allow people to do otherwise bad |
| 123 // things through macros that were defined due to third party libraries. |
| 124 if (filename == "<scratch space>") |
| 125 return true; |
| 126 |
| 127 // Don't complain about autogenerated protobuf files. |
| 128 if (ends_with(filename, ".pb.h")) { |
| 129 return true; |
| 130 } |
| 131 |
| 132 #if defined(LLVM_ON_UNIX) |
| 133 // We need to munge the paths so that they are relative to the repository |
| 134 // srcroot. We first resolve the symlinktastic relative path and then |
| 135 // remove our known srcroot from it if needed. |
| 136 char resolvedPath[MAXPATHLEN]; |
| 137 if (realpath(filename.c_str(), resolvedPath)) { |
| 138 filename = resolvedPath; |
| 139 } |
| 140 #endif |
| 141 |
| 142 #if defined(LLVM_ON_WIN32) |
| 143 std::replace(filename.begin(), filename.end(), '\\', '/'); |
| 144 #endif |
| 145 |
| 146 for (const std::string& banned_dir : banned_directories_) { |
| 147 // If any of the banned directories occur as a component in filename, |
| 148 // this file is rejected. |
| 149 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); |
| 150 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); |
| 151 |
| 152 if (filename.find(banned_dir) != std::string::npos) |
| 153 return true; |
| 154 } |
| 155 |
| 156 return false; |
| 157 } |
| 158 |
112 bool ChromeClassTester::InBannedNamespace(const Decl* record) { | 159 bool ChromeClassTester::InBannedNamespace(const Decl* record) { |
113 std::string n = GetNamespace(record); | 160 std::string n = GetNamespace(record); |
114 if (!n.empty()) { | 161 if (!n.empty()) { |
115 return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n) | 162 return std::find(banned_namespaces_.begin(), banned_namespaces_.end(), n) |
116 != banned_namespaces_.end(); | 163 != banned_namespaces_.end(); |
117 } | 164 } |
118 | 165 |
119 return false; | 166 return false; |
120 } | 167 } |
121 | 168 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 OS << *decl; | 279 OS << *decl; |
233 return GetNamespaceImpl(context->getParent(), | 280 return GetNamespaceImpl(context->getParent(), |
234 OS.str()); | 281 OS.str()); |
235 } | 282 } |
236 default: { | 283 default: { |
237 return GetNamespaceImpl(context->getParent(), candidate); | 284 return GetNamespaceImpl(context->getParent(), candidate); |
238 } | 285 } |
239 } | 286 } |
240 } | 287 } |
241 | 288 |
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. | |
247 return true; | |
248 } | |
249 | |
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>") | |
254 return true; | |
255 | |
256 // Don't complain about autogenerated protobuf files. | |
257 if (ends_with(filename, ".pb.h")) { | |
258 return true; | |
259 } | |
260 | |
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; | |
267 } | |
268 | |
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 '/'"); | |
275 | |
276 if (filename.find(banned_dir) != std::string::npos) | |
277 return true; | |
278 } | |
279 | |
280 return false; | |
281 } | |
282 | |
283 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { | 289 bool ChromeClassTester::IsIgnoredType(const std::string& base_name) { |
284 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); | 290 return ignored_record_names_.find(base_name) != ignored_record_names_.end(); |
285 } | 291 } |
286 | 292 |
287 bool ChromeClassTester::GetFilename(SourceLocation loc, | 293 bool ChromeClassTester::GetFilename(SourceLocation loc, |
288 std::string* filename) { | 294 std::string* filename) { |
289 const SourceManager& source_manager = instance_.getSourceManager(); | 295 const SourceManager& source_manager = instance_.getSourceManager(); |
290 SourceLocation spelling_location = source_manager.getSpellingLoc(loc); | 296 SourceLocation spelling_location = source_manager.getSpellingLoc(loc); |
291 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); | 297 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); |
292 if (ploc.isInvalid()) { | 298 if (ploc.isInvalid()) { |
293 // If we're in an invalid location, we're looking at things that aren't | 299 // If we're in an invalid location, we're looking at things that aren't |
294 // actually stated in the source. | 300 // actually stated in the source. |
295 return false; | 301 return false; |
296 } | 302 } |
297 | 303 |
298 *filename = ploc.getFilename(); | 304 *filename = ploc.getFilename(); |
299 return true; | 305 return true; |
300 } | 306 } |
OLD | NEW |