| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 // Changes Blink-style names to Chrome-style names. Currently transforms: | 5 // Changes Blink-style names to Chrome-style names. Currently transforms: |
| 6 // fields: | 6 // fields: |
| 7 // int m_operationCount => int operation_count_ | 7 // int m_operationCount => int operation_count_ |
| 8 // variables (including parameters): | 8 // variables (including parameters): |
| 9 // int mySuperVariable => int my_super_variable | 9 // int mySuperVariable => int my_super_variable |
| 10 // constants: | 10 // constants: |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 124 } |
| 125 | 125 |
| 126 class MethodBlocklist { | 126 class MethodBlocklist { |
| 127 public: | 127 public: |
| 128 explicit MethodBlocklist(const std::string& filepath) { | 128 explicit MethodBlocklist(const std::string& filepath) { |
| 129 if (!filepath.empty()) | 129 if (!filepath.empty()) |
| 130 ParseInputFile(filepath); | 130 ParseInputFile(filepath); |
| 131 } | 131 } |
| 132 | 132 |
| 133 bool Contains(const clang::FunctionDecl& method) const { | 133 bool Contains(const clang::FunctionDecl& method) const { |
| 134 auto it = method_to_class_to_args_.find(method.getName()); | 134 auto it = method_to_classes_.find(method.getName()); |
| 135 if (it == method_to_class_to_args_.end()) | 135 if (it == method_to_classes_.end()) |
| 136 return false; | 136 return false; |
| 137 | 137 |
| 138 // |method_context| is either | 138 // |method_context| is either |
| 139 // 1) a CXXRecordDecl (i.e. blink::Document) or | 139 // 1) a CXXRecordDecl (i.e. blink::Document) or |
| 140 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers). | 140 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers). |
| 141 const clang::NamedDecl* method_context = | 141 const clang::NamedDecl* method_context = |
| 142 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext()); | 142 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext()); |
| 143 if (!method_context) | 143 if (!method_context) |
| 144 return false; | 144 return false; |
| 145 | 145 |
| 146 const llvm::StringMap<std::set<unsigned>>& class_to_args = it->second; | 146 const llvm::StringSet<>& classes = it->second; |
| 147 auto it2 = class_to_args.find(method_context->getName()); | 147 auto it2 = classes.find(method_context->getName()); |
| 148 if (it2 == class_to_args.end()) | 148 if (it2 == classes.end()) |
| 149 return false; | 149 return false; |
| 150 | 150 |
| 151 const std::set<unsigned>& arg_counts = it2->second; | |
| 152 unsigned method_param_count = method.param_size(); | |
| 153 unsigned method_non_optional_param_count = method_param_count; | |
| 154 for (const clang::ParmVarDecl* param : method.parameters()) { | |
| 155 if (param->hasInit()) | |
| 156 method_non_optional_param_count--; | |
| 157 } | |
| 158 bool found_matching_arg_count = | |
| 159 std::any_of(arg_counts.begin(), arg_counts.end(), | |
| 160 [method_param_count, | |
| 161 method_non_optional_param_count](unsigned arg_count) { | |
| 162 return (method_non_optional_param_count <= arg_count) && | |
| 163 (arg_count <= method_param_count); | |
| 164 }); | |
| 165 | |
| 166 // No need to verify here that |actual_class| is in the |blink| namespace - | 151 // No need to verify here that |actual_class| is in the |blink| namespace - |
| 167 // this will be done by other matchers elsewhere. | 152 // this will be done by other matchers elsewhere. |
| 168 | 153 |
| 169 // TODO(lukasza): Do we need to consider return type and/or param types? | 154 // TODO(lukasza): Do we need to consider return type and/or param types? |
| 170 | 155 |
| 171 return found_matching_arg_count; | 156 // TODO(lukasza): Do we need to consider param count? |
| 157 |
| 158 return true; |
| 172 } | 159 } |
| 173 | 160 |
| 174 private: | 161 private: |
| 175 // Each line is expected to have the following format: | 162 // Each line is expected to have the following format: |
| 176 // <class name>:::<method name>:::<number of arguments> | 163 // <class name>:::<method name>:::<number of arguments> |
| 177 void ParseInputFile(const std::string& filepath) { | 164 void ParseInputFile(const std::string& filepath) { |
| 178 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file_or_err = | 165 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file_or_err = |
| 179 llvm::MemoryBuffer::getFile(filepath); | 166 llvm::MemoryBuffer::getFile(filepath); |
| 180 if (std::error_code err = file_or_err.getError()) { | 167 if (std::error_code err = file_or_err.getError()) { |
| 181 llvm::errs() << "ERROR: Cannot open the file specified in --" | 168 llvm::errs() << "ERROR: Cannot open the file specified in --" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 200 << kExpectedNumberOfParts | 187 << kExpectedNumberOfParts |
| 201 << " ':::'-delimited parts: " << filepath << ":" | 188 << " ':::'-delimited parts: " << filepath << ":" |
| 202 << it.line_number() << ": " << line << "\n"; | 189 << it.line_number() << ": " << line << "\n"; |
| 203 assert(false); | 190 assert(false); |
| 204 continue; | 191 continue; |
| 205 } | 192 } |
| 206 | 193 |
| 207 // Parse individual parts. | 194 // Parse individual parts. |
| 208 llvm::StringRef class_name = parts[0]; | 195 llvm::StringRef class_name = parts[0]; |
| 209 llvm::StringRef method_name = parts[1]; | 196 llvm::StringRef method_name = parts[1]; |
| 210 unsigned number_of_method_args; | 197 // ignoring parts[2] - the (not so trustworthy) number of parameters. |
| 211 if (parts[2].getAsInteger(0, number_of_method_args)) { | |
| 212 llvm::errs() << "ERROR: Parsing error - '" << parts[2] << "' " | |
| 213 << "is not an unsigned integer: " << filepath << ":" | |
| 214 << it.line_number() << ": " << line << "\n"; | |
| 215 assert(false); | |
| 216 continue; | |
| 217 } | |
| 218 | 198 |
| 219 // Store the new entry. | 199 // Store the new entry. |
| 220 method_to_class_to_args_[method_name][class_name].insert( | 200 method_to_classes_[method_name].insert(class_name); |
| 221 number_of_method_args); | |
| 222 } | 201 } |
| 223 } | 202 } |
| 224 | 203 |
| 225 // Stores methods to blacklist in a map: | 204 // Stores methods to blacklist in a map: |
| 226 // method name -> class name -> set of all allowed numbers of arguments. | 205 // method name -> class name -> set of all allowed numbers of arguments. |
| 227 llvm::StringMap<llvm::StringMap<std::set<unsigned>>> method_to_class_to_args_; | 206 llvm::StringMap<llvm::StringSet<>> method_to_classes_; |
| 228 }; | 207 }; |
| 229 | 208 |
| 230 AST_MATCHER_P(clang::FunctionDecl, | 209 AST_MATCHER_P(clang::FunctionDecl, |
| 231 isBlocklistedMethod, | 210 isBlocklistedMethod, |
| 232 MethodBlocklist, | 211 MethodBlocklist, |
| 233 Blocklist) { | 212 Blocklist) { |
| 234 return Blocklist.Contains(Node); | 213 return Blocklist.Contains(Node); |
| 235 } | 214 } |
| 236 | 215 |
| 237 // If |InnerMatcher| matches |top|, then the returned matcher will match: | 216 // If |InnerMatcher| matches |top|, then the returned matcher will match: |
| (...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1805 for (const auto& r : replacements) { | 1784 for (const auto& r : replacements) { |
| 1806 std::string replacement_text = r.getReplacementText().str(); | 1785 std::string replacement_text = r.getReplacementText().str(); |
| 1807 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1786 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 1808 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1787 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 1809 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1788 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 1810 } | 1789 } |
| 1811 llvm::outs() << "==== END EDITS ====\n"; | 1790 llvm::outs() << "==== END EDITS ====\n"; |
| 1812 | 1791 |
| 1813 return 0; | 1792 return 0; |
| 1814 } | 1793 } |
| OLD | NEW |