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

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

Issue 2276983002: Prepend "Get" to method name if method name conflicts with return type name. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blink-style-partition-allocator-new-array-delete-array
Patch Set: Created 4 years, 3 months 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 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 // isEvaluable(). 330 // isEvaluable().
331 if (initializer->isInstantiationDependent()) 331 if (initializer->isInstantiationDependent())
332 return false; 332 return false;
333 333
334 // If the expression can be evaluated at compile time, then it should have a 334 // If the expression can be evaluated at compile time, then it should have a
335 // kFoo style name. Otherwise, not. 335 // kFoo style name. Otherwise, not.
336 return initializer->isEvaluatable(context); 336 return initializer->isEvaluatable(context);
337 } 337 }
338 338
339 bool GetNameForDecl(const clang::FunctionDecl& decl, 339 bool GetNameForDecl(const clang::FunctionDecl& decl,
340 const clang::ASTContext& context, 340 clang::ASTContext& context,
Łukasz Anforowicz 2016/08/24 18:09:49 |match| function call below takes a non-const ASTC
341 std::string& name) { 341 std::string& name) {
342 name = decl.getName().str(); 342 name = decl.getName().str();
343 name[0] = clang::toUppercase(name[0]); 343 name[0] = clang::toUppercase(name[0]);
344
345 // https://crbug.com/582312: Prepend "Get" if method name conflicts with type.
346 auto base_matcher =
347 hasCanonicalType(hasDeclaration(namedDecl(hasName(name))));
dcheng 2016/08/25 17:36:00 Can we just pull the return type out of the Functi
Łukasz Anforowicz 2016/08/25 18:30:40 Thanks - done. I didn't know about getBaseTypeIde
348 auto conflict_matcher = qualType(
349 anyOf(pointsTo(base_matcher), references(base_matcher), base_matcher));
350 if (!match(conflict_matcher, decl.getReturnType(), context).empty())
351 name = "Get" + name;
352
344 return true; 353 return true;
345 } 354 }
346 355
347 bool GetNameForDecl(const clang::EnumConstantDecl& decl, 356 bool GetNameForDecl(const clang::EnumConstantDecl& decl,
348 const clang::ASTContext& context, 357 clang::ASTContext& context,
Łukasz Anforowicz 2016/08/24 18:09:49 Best to be consistent here (i.e. use non-const in
349 std::string& name) { 358 std::string& name) {
350 StringRef original_name = decl.getName(); 359 StringRef original_name = decl.getName();
351 360
352 // If it's already correct leave it alone. 361 // If it's already correct leave it alone.
353 if (original_name.size() >= 2 && original_name[0] == 'k' && 362 if (original_name.size() >= 2 && original_name[0] == 'k' &&
354 clang::isUppercase(original_name[1])) 363 clang::isUppercase(original_name[1]))
355 return false; 364 return false;
356 365
357 bool is_shouty = true; 366 bool is_shouty = true;
358 for (char c : original_name) { 367 for (char c : original_name) {
359 if (!clang::isUppercase(c) && !clang::isDigit(c) && c != '_') { 368 if (!clang::isUppercase(c) && !clang::isDigit(c) && c != '_') {
360 is_shouty = false; 369 is_shouty = false;
361 break; 370 break;
362 } 371 }
363 } 372 }
364 373
365 if (is_shouty) 374 if (is_shouty)
366 return false; 375 return false;
367 376
368 name = 'k'; // k prefix on enum values. 377 name = 'k'; // k prefix on enum values.
369 name += original_name; 378 name += original_name;
370 name[1] = clang::toUppercase(name[1]); 379 name[1] = clang::toUppercase(name[1]);
371 return true; 380 return true;
372 } 381 }
373 382
374 bool GetNameForDecl(const clang::FieldDecl& decl, 383 bool GetNameForDecl(const clang::FieldDecl& decl,
375 const clang::ASTContext& context, 384 clang::ASTContext& context,
376 std::string& name) { 385 std::string& name) {
377 StringRef original_name = decl.getName(); 386 StringRef original_name = decl.getName();
378 bool member_prefix = original_name.startswith(kBlinkFieldPrefix); 387 bool member_prefix = original_name.startswith(kBlinkFieldPrefix);
379 388
380 StringRef rename_part = !member_prefix 389 StringRef rename_part = !member_prefix
381 ? original_name 390 ? original_name
382 : original_name.substr(strlen(kBlinkFieldPrefix)); 391 : original_name.substr(strlen(kBlinkFieldPrefix));
383 name = CamelCaseToUnderscoreCase(rename_part); 392 name = CamelCaseToUnderscoreCase(rename_part);
384 393
385 // Assume that prefix of m_ was intentional and always replace it with a 394 // Assume that prefix of m_ was intentional and always replace it with a
(...skipping 10 matching lines...) Expand all
396 clang::isUppercase(original_name[1])) 405 clang::isUppercase(original_name[1]))
397 return false; 406 return false;
398 407
399 name = 'k'; 408 name = 'k';
400 name.append(original_name.data(), original_name.size()); 409 name.append(original_name.data(), original_name.size());
401 name[1] = clang::toUppercase(name[1]); 410 name[1] = clang::toUppercase(name[1]);
402 return true; 411 return true;
403 } 412 }
404 413
405 bool GetNameForDecl(const clang::VarDecl& decl, 414 bool GetNameForDecl(const clang::VarDecl& decl,
406 const clang::ASTContext& context, 415 clang::ASTContext& context,
407 std::string& name) { 416 std::string& name) {
408 StringRef original_name = decl.getName(); 417 StringRef original_name = decl.getName();
409 418
410 // Nothing to do for unnamed parameters. 419 // Nothing to do for unnamed parameters.
411 if (clang::isa<clang::ParmVarDecl>(decl)) { 420 if (clang::isa<clang::ParmVarDecl>(decl)) {
412 if (original_name.empty()) 421 if (original_name.empty())
413 return false; 422 return false;
414 423
415 // Check if |decl| really covers text of |original_name|. This might not be 424 // Check if |decl| really covers text of |original_name|. This might not be
416 // true when specializing a templatized class that had an unnamed parameter 425 // true when specializing a templatized class that had an unnamed parameter
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 // Static members end with _ just like other members, but constants should 468 // Static members end with _ just like other members, but constants should
460 // not. 469 // not.
461 if (!is_const && decl.isStaticDataMember()) { 470 if (!is_const && decl.isStaticDataMember()) {
462 name += '_'; 471 name += '_';
463 } 472 }
464 473
465 return true; 474 return true;
466 } 475 }
467 476
468 bool GetNameForDecl(const clang::FunctionTemplateDecl& decl, 477 bool GetNameForDecl(const clang::FunctionTemplateDecl& decl,
469 const clang::ASTContext& context, 478 clang::ASTContext& context,
470 std::string& name) { 479 std::string& name) {
471 clang::FunctionDecl* templated_function = decl.getTemplatedDecl(); 480 clang::FunctionDecl* templated_function = decl.getTemplatedDecl();
472 return GetNameForDecl(*templated_function, context, name); 481 return GetNameForDecl(*templated_function, context, name);
473 } 482 }
474 483
475 bool GetNameForDecl(const clang::NamedDecl& decl, 484 bool GetNameForDecl(const clang::NamedDecl& decl,
476 const clang::ASTContext& context, 485 clang::ASTContext& context,
477 std::string& name) { 486 std::string& name) {
478 if (auto* function = clang::dyn_cast<clang::FunctionDecl>(&decl)) 487 if (auto* function = clang::dyn_cast<clang::FunctionDecl>(&decl))
479 return GetNameForDecl(*function, context, name); 488 return GetNameForDecl(*function, context, name);
480 if (auto* var = clang::dyn_cast<clang::VarDecl>(&decl)) 489 if (auto* var = clang::dyn_cast<clang::VarDecl>(&decl))
481 return GetNameForDecl(*var, context, name); 490 return GetNameForDecl(*var, context, name);
482 if (auto* field = clang::dyn_cast<clang::FieldDecl>(&decl)) 491 if (auto* field = clang::dyn_cast<clang::FieldDecl>(&decl))
483 return GetNameForDecl(*field, context, name); 492 return GetNameForDecl(*field, context, name);
484 if (auto* function_template = 493 if (auto* function_template =
485 clang::dyn_cast<clang::FunctionTemplateDecl>(&decl)) 494 clang::dyn_cast<clang::FunctionTemplateDecl>(&decl))
486 return GetNameForDecl(*function_template, context, name); 495 return GetNameForDecl(*function_template, context, name);
487 if (auto* enumc = clang::dyn_cast<clang::EnumConstantDecl>(&decl)) 496 if (auto* enumc = clang::dyn_cast<clang::EnumConstantDecl>(&decl))
488 return GetNameForDecl(*enumc, context, name); 497 return GetNameForDecl(*enumc, context, name);
489 498
490 return false; 499 return false;
491 } 500 }
492 501
493 bool GetNameForDecl(const clang::UsingDecl& decl, 502 bool GetNameForDecl(const clang::UsingDecl& decl,
494 const clang::ASTContext& context, 503 clang::ASTContext& context,
495 std::string& name) { 504 std::string& name) {
496 assert(decl.shadow_size() > 0); 505 assert(decl.shadow_size() > 0);
497 506
498 // If a using declaration's targeted declaration is a set of overloaded 507 // If a using declaration's targeted declaration is a set of overloaded
499 // functions, it can introduce multiple shadowed declarations. Just using the 508 // functions, it can introduce multiple shadowed declarations. Just using the
500 // first one is OK, since overloaded functions have the same name, by 509 // first one is OK, since overloaded functions have the same name, by
501 // definition. 510 // definition.
502 return GetNameForDecl(*decl.shadow_begin()->getTargetDecl(), context, name); 511 return GetNameForDecl(*decl.shadow_begin()->getTargetDecl(), context, name);
503 } 512 }
504 513
(...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 for (const auto& r : replacements) { 1181 for (const auto& r : replacements) {
1173 std::string replacement_text = r.getReplacementText().str(); 1182 std::string replacement_text = r.getReplacementText().str();
1174 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); 1183 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
1175 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() 1184 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
1176 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; 1185 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
1177 } 1186 }
1178 llvm::outs() << "==== END EDITS ====\n"; 1187 llvm::outs() << "==== END EDITS ====\n";
1179 1188
1180 return 0; 1189 return 0;
1181 } 1190 }
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