Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp

Issue 2580773003: Blacklist method names without considering static-vs-instance. (Closed)
Patch Set: Stop blacklisting the |trace| method instead. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/methods-expected.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 return true; 211 return true;
212 } 212 }
213 return false; 213 return false;
214 } 214 }
215 215
216 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) { 216 bool IsBlacklistedFunction(const clang::FunctionDecl& decl) {
217 // swap() functions should match the signature of std::swap for ADL tricks. 217 // swap() functions should match the signature of std::swap for ADL tricks.
218 return decl.getName() == "swap"; 218 return decl.getName() == "swap";
219 } 219 }
220 220
221 bool IsBlacklistedMethodName(llvm::StringRef name) {
222 static const char* kBlacklistedNames[] = {
223 "lock", "unlock", "try_lock",
224 "begin", "end", "rbegin", "rend",
225 };
226 for (const auto& b : kBlacklistedNames) {
227 if (name == b)
228 return true;
229 }
230 return false;
231 }
232
221 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) { 233 bool IsBlacklistedMethod(const clang::CXXMethodDecl& decl) {
222 if (decl.isStatic()) 234 if (decl.isStatic())
223 return false; 235 return false;
224 236
225 clang::StringRef name = decl.getName(); 237 clang::StringRef name = decl.getName();
226 238 if (IsBlacklistedMethodName(name))
227 // These methods should never be renamed.
228 static const char* kBlacklistMethods[] = {"trace", "traceImpl", "lock",
229 "unlock", "try_lock", "begin",
230 "end", "rbegin", "rend"};
231 for (const auto& b : kBlacklistMethods) {
232 if (name == b)
233 return true; 239 return true;
234 }
235 240
236 // Subclasses of InspectorAgent will subclass "disable()" from both blink and 241 // Subclasses of InspectorAgent will subclass "disable()" from both blink and
237 // from gen/, which is problematic, but DevTools folks don't want to rename 242 // 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. 243 // it or split this up. So don't rename it at all.
239 if (name.equals("disable") && 244 if (name.equals("disable") &&
240 IsMethodOverrideOf(decl, "blink::InspectorAgent")) 245 IsMethodOverrideOf(decl, "blink::InspectorAgent"))
241 return true; 246 return true;
242 247
243 return false; 248 return false;
244 } 249 }
245 250
246 bool IsBlacklistedFunctionOrMethodName(llvm::StringRef name) {
247 static const char* kBlacklistedNames[] = {
248 // From IsBlacklistedFunction:
249 "swap",
250 // From IsBlacklistedMethod:
251 "trace", "traceImpl", "lock", "unlock", "try_lock", "begin", "end",
252 "rbegin", "rend", "disable",
253 };
254 for (const auto& b : kBlacklistedNames) {
255 if (name == b)
256 return true;
257 }
258 return false;
259 }
260
261 AST_MATCHER(clang::FunctionDecl, isBlacklistedFunction) { 251 AST_MATCHER(clang::FunctionDecl, isBlacklistedFunction) {
262 return IsBlacklistedFunction(Node); 252 return IsBlacklistedFunction(Node);
263 } 253 }
264 254
265 AST_MATCHER(clang::CXXMethodDecl, isBlacklistedMethod) { 255 AST_MATCHER(clang::CXXMethodDecl, isBlacklistedMethod) {
266 return IsBlacklistedMethod(Node); 256 return IsBlacklistedMethod(Node);
267 } 257 }
268 258
269 // Helper to convert from a camelCaseName to camel_case_name. It uses some 259 // Helper to convert from a camelCaseName to camel_case_name. It uses some
270 // heuristics to try to handle acronyms in camel case names correctly. 260 // heuristics to try to handle acronyms in camel case names correctly.
(...skipping 548 matching lines...) Expand 10 before | Expand all | Expand 10 after
819 if (old_name.startswith(kBlinkFieldPrefix)) { 809 if (old_name.startswith(kBlinkFieldPrefix)) {
820 std::string field_name = old_name.substr(strlen(kBlinkFieldPrefix)); 810 std::string field_name = old_name.substr(strlen(kBlinkFieldPrefix));
821 if (field_name.find('_') == std::string::npos) { 811 if (field_name.find('_') == std::string::npos) {
822 new_name = CamelCaseToUnderscoreCase(field_name) + "_"; 812 new_name = CamelCaseToUnderscoreCase(field_name) + "_";
823 return true; 813 return true;
824 } 814 }
825 } 815 }
826 816
827 // |T::myMethod(...)| -> |T::MyMethod(...)|. 817 // |T::myMethod(...)| -> |T::MyMethod(...)|.
828 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) && 818 if ((old_name.find('_') == std::string::npos) && IsCallee(node, context) &&
829 !IsBlacklistedFunctionOrMethodName(old_name)) { 819 !IsBlacklistedMethodName(old_name)) {
830 new_name = old_name; 820 new_name = old_name;
831 new_name[0] = clang::toUppercase(old_name[0]); 821 new_name[0] = clang::toUppercase(old_name[0]);
832 return true; 822 return true;
833 } 823 }
834 824
835 // In the future we can consider more heuristics: 825 // In the future we can consider more heuristics:
836 // - "s_" and "g_" prefixes 826 // - "s_" and "g_" prefixes
837 // - "ALL_CAPS" 827 // - "ALL_CAPS"
838 // - |T::myStaticField| -> |T::kMyStaticField| 828 // - |T::myStaticField| -> |T::kMyStaticField|
839 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?) 829 // (but have to be careful not to rename |value| in WTF/TypeTraits.h?)
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
1265 for (const auto& r : replacements) { 1255 for (const auto& r : replacements) {
1266 std::string replacement_text = r.getReplacementText().str(); 1256 std::string replacement_text = r.getReplacementText().str();
1267 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1257 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1268 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1258 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1269 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1259 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1270 } 1260 }
1271 llvm::outs() << "==== END EDITS ====\n"; 1261 llvm::outs() << "==== END EDITS ====\n";
1272 1262
1273 return 0; 1263 return 0;
1274 } 1264 }
OLDNEW
« no previous file with comments | « no previous file | tools/clang/rewrite_to_chrome_style/tests/methods-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698