| 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 <algorithm> | 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 | 16 #ifdef LLVM_ON_UNIX |
| 17 #include <sys/param.h> | 17 #include <sys/param.h> |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 using namespace clang; | 20 using namespace clang; |
| 21 using chrome_checker::Options; |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 bool ends_with(const std::string& one, const std::string& two) { | 25 bool ends_with(const std::string& one, const std::string& two) { |
| 25 if (two.size() > one.size()) | 26 if (two.size() > one.size()) |
| 26 return false; | 27 return false; |
| 27 | 28 |
| 28 return one.compare(one.size() - two.size(), two.size(), two) == 0; | 29 return one.compare(one.size() - two.size(), two.size(), two) == 0; |
| 29 } | 30 } |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 ChromeClassTester::ChromeClassTester(CompilerInstance& instance) | 34 ChromeClassTester::ChromeClassTester(CompilerInstance& instance, |
| 34 : instance_(instance), | 35 const Options& options) |
| 36 : options_(options), |
| 37 instance_(instance), |
| 35 diagnostic_(instance.getDiagnostics()) { | 38 diagnostic_(instance.getDiagnostics()) { |
| 36 BuildBannedLists(); | 39 BuildBannedLists(); |
| 37 } | 40 } |
| 38 | 41 |
| 39 ChromeClassTester::~ChromeClassTester() {} | 42 ChromeClassTester::~ChromeClassTester() {} |
| 40 | 43 |
| 41 void ChromeClassTester::HandleTagDeclDefinition(TagDecl* tag) { | 44 void ChromeClassTester::HandleTagDeclDefinition(TagDecl* tag) { |
| 42 pending_class_decls_.push_back(tag); | 45 pending_class_decls_.push_back(tag); |
| 43 } | 46 } |
| 44 | 47 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 CheckChromeEnum(enum_location, enum_decl); | 91 CheckChromeEnum(enum_location, enum_decl); |
| 89 } | 92 } |
| 90 } | 93 } |
| 91 | 94 |
| 92 void ChromeClassTester::emitWarning(SourceLocation loc, | 95 void ChromeClassTester::emitWarning(SourceLocation loc, |
| 93 const char* raw_error) { | 96 const char* raw_error) { |
| 94 FullSourceLoc full(loc, instance().getSourceManager()); | 97 FullSourceLoc full(loc, instance().getSourceManager()); |
| 95 std::string err; | 98 std::string err; |
| 96 err = "[chromium-style] "; | 99 err = "[chromium-style] "; |
| 97 err += raw_error; | 100 err += raw_error; |
| 98 // TODO(dcheng): Re-enable -Werror for these diagnostics on Windows once all | 101 |
| 99 // the pre-existing warnings are cleaned up. https://crbug.com/467287 | 102 DiagnosticIDs::Level level = getErrorLevel() == DiagnosticsEngine::Error |
| 100 DiagnosticIDs::Level level = | 103 ? DiagnosticIDs::Error : DiagnosticIDs::Warning; |
| 101 #if !defined(LLVM_ON_WIN32) | 104 |
| 102 diagnostic().getWarningsAsErrors() ? | |
| 103 DiagnosticIDs::Error : | |
| 104 #endif | |
| 105 DiagnosticIDs::Warning; | |
| 106 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); | 105 unsigned id = diagnostic().getDiagnosticIDs()->getCustomDiagID(level, err); |
| 107 DiagnosticBuilder builder = diagnostic().Report(full, id); | 106 DiagnosticBuilder builder = diagnostic().Report(full, id); |
| 107 |
| 108 } | 108 } |
| 109 | 109 |
| 110 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { | 110 bool ChromeClassTester::InBannedDirectory(SourceLocation loc) { |
| 111 if (instance().getSourceManager().isInSystemHeader(loc)) | 111 if (instance().getSourceManager().isInSystemHeader(loc)) |
| 112 return true; | 112 return true; |
| 113 | 113 |
| 114 std::string filename; | 114 std::string filename; |
| 115 if (!GetFilename(loc, &filename)) { | 115 if (!GetFilename(loc, &filename)) { |
| 116 // If the filename cannot be determined, simply treat this as a banned | 116 // If the filename cannot be determined, simply treat this as a banned |
| 117 // location, instead of going through the full lookup process. | 117 // location, instead of going through the full lookup process. |
| 118 return true; | 118 return true; |
| 119 } | 119 } |
| 120 | 120 |
| 121 // We need to special case scratch space; which is where clang does its | 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 | 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. | 123 // things through macros that were defined due to third party libraries. |
| 124 if (filename == "<scratch space>") | 124 if (filename == "<scratch space>") |
| 125 return true; | 125 return true; |
| 126 | 126 |
| 127 // Don't complain about autogenerated protobuf files. | 127 // Don't complain about autogenerated protobuf files. |
| 128 if (ends_with(filename, ".pb.h")) { | 128 if (ends_with(filename, ".pb.h")) { |
| 129 return true; | 129 return true; |
| 130 } | 130 } |
| 131 | 131 |
| 132 #if defined(LLVM_ON_UNIX) | 132 #if defined(LLVM_ON_UNIX) |
| 133 // We need to munge the paths so that they are relative to the repository | 133 // Resolve the symlinktastic relative path and make it absolute. |
| 134 // srcroot. We first resolve the symlinktastic relative path and then | |
| 135 // remove our known srcroot from it if needed. | |
| 136 char resolvedPath[MAXPATHLEN]; | 134 char resolvedPath[MAXPATHLEN]; |
| 137 if (realpath(filename.c_str(), resolvedPath)) { | 135 if (realpath(filename.c_str(), resolvedPath)) { |
| 138 filename = resolvedPath; | 136 filename = resolvedPath; |
| 139 } | 137 } |
| 140 #endif | 138 #endif |
| 141 | 139 |
| 142 #if defined(LLVM_ON_WIN32) | 140 #if defined(LLVM_ON_WIN32) |
| 143 std::replace(filename.begin(), filename.end(), '\\', '/'); | 141 std::replace(filename.begin(), filename.end(), '\\', '/'); |
| 142 |
| 143 // On Posix, realpath() has made the path absolute. On Windows, this isn't |
| 144 // necessarily true, so prepend a '/' to the path to make sure the |
| 145 // banned_directories_ loop below works correctly. |
| 146 // This turns e.g. "gen/dir/file.cc" to "/gen/dir/file.cc" which lets the |
| 147 // "/gen/" banned_dir work. |
| 148 // This seems simpler than converting to utf16, calling GetFullPathNameW(), |
| 149 // and converting back to utf8. |
| 150 filename.insert(filename.begin(), '/'); |
| 144 #endif | 151 #endif |
| 145 | 152 |
| 146 for (const std::string& banned_dir : banned_directories_) { | 153 for (const std::string& banned_dir : banned_directories_) { |
| 147 // If any of the banned directories occur as a component in filename, | 154 // If any of the banned directories occur as a component in filename, |
| 148 // this file is rejected. | 155 // this file is rejected. |
| 149 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); | 156 assert(banned_dir.front() == '/' && "Banned dir must start with '/'"); |
| 150 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); | 157 assert(banned_dir.back() == '/' && "Banned dir must end with '/'"); |
| 151 | 158 |
| 152 if (filename.find(banned_dir) != std::string::npos) | 159 if (filename.find(banned_dir) != std::string::npos) |
| 153 return true; | 160 return true; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); | 304 PresumedLoc ploc = source_manager.getPresumedLoc(spelling_location); |
| 298 if (ploc.isInvalid()) { | 305 if (ploc.isInvalid()) { |
| 299 // If we're in an invalid location, we're looking at things that aren't | 306 // If we're in an invalid location, we're looking at things that aren't |
| 300 // actually stated in the source. | 307 // actually stated in the source. |
| 301 return false; | 308 return false; |
| 302 } | 309 } |
| 303 | 310 |
| 304 *filename = ploc.getFilename(); | 311 *filename = ploc.getFilename(); |
| 305 return true; | 312 return true; |
| 306 } | 313 } |
| 314 |
| 315 DiagnosticsEngine::Level ChromeClassTester::getErrorLevel() { |
| 316 if (options_.warn_only) |
| 317 return DiagnosticsEngine::Warning; |
| 318 |
| 319 return diagnostic().getWarningsAsErrors() ? DiagnosticsEngine::Error |
| 320 : DiagnosticsEngine::Warning; |
| 321 } |
| OLD | NEW |