| 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 public: | 133 public: |
| 134 explicit MethodBlocklist(const std::string& filepath) { | 134 explicit MethodBlocklist(const std::string& filepath) { |
| 135 if (!filepath.empty()) | 135 if (!filepath.empty()) |
| 136 ParseInputFile(filepath); | 136 ParseInputFile(filepath); |
| 137 } | 137 } |
| 138 | 138 |
| 139 bool Contains(const clang::FunctionDecl& method) const { | 139 bool Contains(const clang::FunctionDecl& method) const { |
| 140 if (!method.getDeclName().isIdentifier()) | 140 if (!method.getDeclName().isIdentifier()) |
| 141 return false; | 141 return false; |
| 142 | 142 |
| 143 auto it = method_to_class_to_args_.find(method.getName()); | 143 auto it = method_to_classes_.find(method.getName()); |
| 144 if (it == method_to_class_to_args_.end()) | 144 if (it == method_to_classes_.end()) |
| 145 return false; | 145 return false; |
| 146 | 146 |
| 147 // |method_context| is either | 147 // |method_context| is either |
| 148 // 1) a CXXRecordDecl (i.e. blink::Document) or | 148 // 1) a CXXRecordDecl (i.e. blink::Document) or |
| 149 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers). | 149 // 2) a NamespaceDecl (i.e. blink::DOMWindowTimers). |
| 150 const clang::NamedDecl* method_context = | 150 const clang::NamedDecl* method_context = |
| 151 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext()); | 151 clang::dyn_cast<clang::NamedDecl>(method.getDeclContext()); |
| 152 if (!method_context) | 152 if (!method_context) |
| 153 return false; | 153 return false; |
| 154 if (!method_context->getDeclName().isIdentifier()) | 154 if (!method_context->getDeclName().isIdentifier()) |
| 155 return false; | 155 return false; |
| 156 | 156 |
| 157 const llvm::StringMap<std::set<unsigned>>& class_to_args = it->second; | 157 const llvm::StringSet<>& classes = it->second; |
| 158 auto it2 = class_to_args.find(method_context->getName()); | 158 auto it2 = classes.find(method_context->getName()); |
| 159 if (it2 == class_to_args.end()) | 159 if (it2 == classes.end()) |
| 160 return false; | 160 return false; |
| 161 | 161 |
| 162 const std::set<unsigned>& arg_counts = it2->second; | |
| 163 unsigned method_param_count = method.param_size(); | |
| 164 unsigned method_non_optional_param_count = method_param_count; | |
| 165 for (const clang::ParmVarDecl* param : method.parameters()) { | |
| 166 if (param->hasInit()) | |
| 167 method_non_optional_param_count--; | |
| 168 } | |
| 169 bool found_matching_arg_count = | |
| 170 std::any_of(arg_counts.begin(), arg_counts.end(), | |
| 171 [method_param_count, | |
| 172 method_non_optional_param_count](unsigned arg_count) { | |
| 173 return (method_non_optional_param_count <= arg_count) && | |
| 174 (arg_count <= method_param_count); | |
| 175 }); | |
| 176 | |
| 177 // No need to verify here that |actual_class| is in the |blink| namespace - | 162 // No need to verify here that |actual_class| is in the |blink| namespace - |
| 178 // this will be done by other matchers elsewhere. | 163 // this will be done by other matchers elsewhere. |
| 179 | 164 |
| 180 // TODO(lukasza): Do we need to consider return type and/or param types? | 165 // TODO(lukasza): Do we need to consider return type and/or param types? |
| 181 | 166 |
| 182 return found_matching_arg_count; | 167 // TODO(lukasza): Do we need to consider param count? |
| 168 |
| 169 return true; |
| 183 } | 170 } |
| 184 | 171 |
| 185 private: | 172 private: |
| 186 // Each line is expected to have the following format: | 173 // Each line is expected to have the following format: |
| 187 // <class name>:::<method name>:::<number of arguments> | 174 // <class name>:::<method name>:::<number of arguments> |
| 188 void ParseInputFile(const std::string& filepath) { | 175 void ParseInputFile(const std::string& filepath) { |
| 189 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file_or_err = | 176 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> file_or_err = |
| 190 llvm::MemoryBuffer::getFile(filepath); | 177 llvm::MemoryBuffer::getFile(filepath); |
| 191 if (std::error_code err = file_or_err.getError()) { | 178 if (std::error_code err = file_or_err.getError()) { |
| 192 llvm::errs() << "ERROR: Cannot open the file specified in --" | 179 llvm::errs() << "ERROR: Cannot open the file specified in --" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 211 << kExpectedNumberOfParts | 198 << kExpectedNumberOfParts |
| 212 << " ':::'-delimited parts: " << filepath << ":" | 199 << " ':::'-delimited parts: " << filepath << ":" |
| 213 << it.line_number() << ": " << line << "\n"; | 200 << it.line_number() << ": " << line << "\n"; |
| 214 assert(false); | 201 assert(false); |
| 215 continue; | 202 continue; |
| 216 } | 203 } |
| 217 | 204 |
| 218 // Parse individual parts. | 205 // Parse individual parts. |
| 219 llvm::StringRef class_name = parts[0]; | 206 llvm::StringRef class_name = parts[0]; |
| 220 llvm::StringRef method_name = parts[1]; | 207 llvm::StringRef method_name = parts[1]; |
| 221 unsigned number_of_method_args; | 208 // ignoring parts[2] - the (not so trustworthy) number of parameters. |
| 222 if (parts[2].getAsInteger(0, number_of_method_args)) { | |
| 223 llvm::errs() << "ERROR: Parsing error - '" << parts[2] << "' " | |
| 224 << "is not an unsigned integer: " << filepath << ":" | |
| 225 << it.line_number() << ": " << line << "\n"; | |
| 226 assert(false); | |
| 227 continue; | |
| 228 } | |
| 229 | 209 |
| 230 // Store the new entry. | 210 // Store the new entry. |
| 231 method_to_class_to_args_[method_name][class_name].insert( | 211 method_to_classes_[method_name].insert(class_name); |
| 232 number_of_method_args); | |
| 233 } | 212 } |
| 234 } | 213 } |
| 235 | 214 |
| 236 // Stores methods to blacklist in a map: | 215 // Stores methods to blacklist in a map: |
| 237 // method name -> class name -> set of all allowed numbers of arguments. | 216 // method name -> class name -> set of all allowed numbers of arguments. |
| 238 llvm::StringMap<llvm::StringMap<std::set<unsigned>>> method_to_class_to_args_; | 217 llvm::StringMap<llvm::StringSet<>> method_to_classes_; |
| 239 }; | 218 }; |
| 240 | 219 |
| 241 AST_MATCHER_P(clang::FunctionDecl, | 220 AST_MATCHER_P(clang::FunctionDecl, |
| 242 isBlocklistedMethod, | 221 isBlocklistedMethod, |
| 243 MethodBlocklist, | 222 MethodBlocklist, |
| 244 Blocklist) { | 223 Blocklist) { |
| 245 return Blocklist.Contains(Node); | 224 return Blocklist.Contains(Node); |
| 246 } | 225 } |
| 247 | 226 |
| 248 // If |InnerMatcher| matches |top|, then the returned matcher will match: | 227 // If |InnerMatcher| matches |top|, then the returned matcher will match: |
| (...skipping 1697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1946 for (const auto& r : replacements) { | 1925 for (const auto& r : replacements) { |
| 1947 std::string replacement_text = r.getReplacementText().str(); | 1926 std::string replacement_text = r.getReplacementText().str(); |
| 1948 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1927 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 1949 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1928 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 1950 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1929 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 1951 } | 1930 } |
| 1952 llvm::outs() << "==== END EDITS ====\n"; | 1931 llvm::outs() << "==== END EDITS ====\n"; |
| 1953 | 1932 |
| 1954 return 0; | 1933 return 0; |
| 1955 } | 1934 } |
| OLD | NEW |