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 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 if (decl.getParent()->getQualifiedNameAsString() == class_name) | 200 if (decl.getParent()->getQualifiedNameAsString() == class_name) |
201 return true; | 201 return true; |
202 for (auto it = decl.begin_overridden_methods(); | 202 for (auto it = decl.begin_overridden_methods(); |
203 it != decl.end_overridden_methods(); ++it) { | 203 it != decl.end_overridden_methods(); ++it) { |
204 if (IsMethodOverrideOf(**it, class_name)) | 204 if (IsMethodOverrideOf(**it, class_name)) |
205 return true; | 205 return true; |
206 } | 206 } |
207 return false; | 207 return false; |
208 } | 208 } |
209 | 209 |
210 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) { | 210 bool IsBlacklistedFunctionName(llvm::StringRef name) { |
211 // swap() functions should match the signature of std::swap for ADL tricks. | 211 // https://crbug.com/677166: Have to avoid renaming |hash| -> |Hash| to avoid |
212 return decl.getName() == "swap"; | 212 // colliding with a struct already named |Hash|. |
213 return name == "hash"; | |
213 } | 214 } |
214 | 215 |
215 bool IsBlacklistedMethodName(llvm::StringRef name) { | 216 bool IsBlacklistedFreeFunctionName(llvm::StringRef name) { |
217 // swap() functions should match the signature of std::swap for ADL tricks. | |
218 return name == "swap"; | |
219 } | |
220 | |
221 bool IsBlacklistedInstanceMethodName(llvm::StringRef name) { | |
216 static const char* kBlacklistedNames[] = { | 222 static const char* kBlacklistedNames[] = { |
217 "hash", | 223 // We should avoid renaming the method names listed below, because |
218 "lock", "unlock", "try_lock", | 224 // 1. They are used in templated code (e.g. in <algorithms>) |
219 "begin", "end", "rbegin", "rend", | 225 // 2. They (begin+end) are used in range-based for syntax sugar |
226 // - for (auto x : foo) { ... } // <- foo.begin() will be called. | |
227 "begin", "end", "rbegin", "rend", "lock", "unlock", "try_lock", | |
220 }; | 228 }; |
221 for (const auto& b : kBlacklistedNames) { | 229 for (const auto& b : kBlacklistedNames) { |
222 if (name == b) | 230 if (name == b) |
223 return true; | 231 return true; |
224 } | 232 } |
225 return false; | 233 return false; |
226 } | 234 } |
227 | 235 |
236 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) { | |
237 clang::StringRef name = decl.getName(); | |
238 return IsBlacklistedFunctionName(name) || IsBlacklistedFreeFunctionName(name); | |
239 } | |
240 | |
228 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { | 241 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { |
229 if (decl.isStatic()) | 242 clang::StringRef name = decl.getName(); |
230 return false; | 243 if (IsBlacklistedFunctionName(name)) |
244 return true; | |
231 | 245 |
232 clang::StringRef name = decl.getName(); | 246 if (decl.isInstance() && IsBlacklistedInstanceMethodName(name)) |
233 if (IsBlacklistedMethodName(name)) | 247 return true; |
dcheng
2016/12/28 18:48:49
I think we can just early return here if it's stat
Łukasz Anforowicz
2016/12/28 18:56:50
Done.
| |
234 return true; | |
235 | 248 |
236 // Subclasses of InspectorAgent will subclass "disable()" from both blink and | 249 // Subclasses of InspectorAgent will subclass "disable()" from both blink and |
237 // from gen/, which is problematic, but DevTools folks don't want to rename | 250 // from gen/, which is problematic, but DevTools folks don't want to rename |
238 // it or split this up. So don't rename it at all. | 251 // it or split this up. So don't rename it at all. |
239 if (name.equals("disable") && | 252 if (decl.isInstance() && name.equals("disable") && |
240 IsMethodOverrideOf(decl, "blink::InspectorAgent")) | 253 IsMethodOverrideOf(decl, "blink::InspectorAgent")) |
241 return true; | 254 return true; |
242 | 255 |
243 return false; | 256 return false; |
244 } | 257 } |
245 | 258 |
246 AST_MATCHER(clang::FunctionDecl, isBlacklistedFunction) { | 259 AST_MATCHER(clang::FunctionDecl, isBlacklistedFunction) { |
247 return IsBlacklistedFunction(Node); | 260 return IsBlacklistedFunction(Node); |
248 } | 261 } |
249 | 262 |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
845 llvm::StringRef old_name = info->getName(); | 858 llvm::StringRef old_name = info->getName(); |
846 | 859 |
847 // Try to guess a new name. | 860 // Try to guess a new name. |
848 std::string new_name; | 861 std::string new_name; |
849 if (GuessNameForUnresolvedDependentNode(node, *result.Context, old_name, | 862 if (GuessNameForUnresolvedDependentNode(node, *result.Context, old_name, |
850 new_name)) | 863 new_name)) |
851 Base::AddReplacement(result, old_name, std::move(new_name)); | 864 Base::AddReplacement(result, old_name, std::move(new_name)); |
852 } | 865 } |
853 | 866 |
854 private: | 867 private: |
868 static bool IsBlacklistedMethodName(llvm::StringRef name) { | |
dcheng
2016/12/28 18:48:49
How come this is a static method now?
Łukasz Anforowicz
2016/12/28 18:56:50
This was a free function, but the only caller was/
dcheng
2016/12/28 19:03:38
I think it's just confusing to not have all the si
Łukasz Anforowicz
2016/12/28 19:08:07
Done.
| |
869 return IsBlacklistedFunctionName(name) || | |
870 IsBlacklistedInstanceMethodName(name); | |
871 } | |
872 | |
855 // This method calculates a new name for nodes that depend on template | 873 // This method calculates a new name for nodes that depend on template |
856 // parameters (http://en.cppreference.com/w/cpp/language/dependent_name). The | 874 // parameters (http://en.cppreference.com/w/cpp/language/dependent_name). The |
857 // renaming is based on crude heuristics, because such nodes are not bound to | 875 // renaming is based on crude heuristics, because such nodes are not bound to |
858 // a specific decl until template instantiation - at the point of rename, one | 876 // a specific decl until template instantiation - at the point of rename, one |
859 // cannot tell whether the node will eventually resolve to a field / method / | 877 // cannot tell whether the node will eventually resolve to a field / method / |
860 // constant / etc. | 878 // constant / etc. |
861 // | 879 // |
862 // The method returns false if no renaming should be done. | 880 // The method returns false if no renaming should be done. |
863 // Otherwise the method returns true and sets |new_name|. | 881 // Otherwise the method returns true and sets |new_name|. |
864 bool GuessNameForUnresolvedDependentNode(const TargetNode& node, | 882 bool GuessNameForUnresolvedDependentNode(const TargetNode& node, |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1294 for (const auto& r : replacements) { | 1312 for (const auto& r : replacements) { |
1295 std::string replacement_text = r.getReplacementText().str(); | 1313 std::string replacement_text = r.getReplacementText().str(); |
1296 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1314 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
1297 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1315 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
1298 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1316 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
1299 } | 1317 } |
1300 llvm::outs() << "==== END EDITS ====\n"; | 1318 llvm::outs() << "==== END EDITS ====\n"; |
1301 | 1319 |
1302 return 0; | 1320 return 0; |
1303 } | 1321 } |
OLD | NEW |