Chromium Code Reviews| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { | 52 AST_MATCHER(clang::FunctionDecl, isOverloadedOperator) { |
| 53 return Node.isOverloadedOperator(); | 53 return Node.isOverloadedOperator(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 constexpr char kBlinkFieldPrefix[] = "m_"; | 56 constexpr char kBlinkFieldPrefix[] = "m_"; |
| 57 constexpr char kBlinkStaticMemberPrefix[] = "s_"; | 57 constexpr char kBlinkStaticMemberPrefix[] = "s_"; |
| 58 | 58 |
| 59 bool GetNameForDecl(const clang::FunctionDecl& decl, | 59 bool GetNameForDecl(const clang::FunctionDecl& decl, |
| 60 const clang::ASTContext& context, | 60 const clang::ASTContext& context, |
| 61 std::string& name) { | 61 std::string& name) { |
| 62 name = decl.getNameAsString(); | 62 // If there's no name to change, return. |
| 63 if (!decl.getIdentifier()) | |
| 64 return false; | |
| 65 StringRef original_name = decl.getName(); | |
| 63 | 66 |
| 64 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) { | 67 if (const auto* method = clang::dyn_cast<const clang::CXXMethodDecl>(&decl)) { |
| 65 if (!method->isStatic()) { | 68 if (!method->isStatic()) { |
| 66 // Some methods shouldn't be renamed because reasons. | 69 // Some methods shouldn't be renamed because reasons. |
| 67 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend", | 70 static const char* kBlacklist[] = {"begin", "end", "rbegin", "rend", |
| 68 "trace"}; | 71 "trace"}; |
| 69 for (const auto& b : kBlacklist) { | 72 for (const auto& b : kBlacklist) { |
| 70 if (name == b) | 73 if (original_name == b) |
| 71 return false; | 74 return false; |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 } | 77 } |
| 75 | 78 |
| 79 name = original_name.str(); | |
| 76 name[0] = clang::toUppercase(name[0]); | 80 name[0] = clang::toUppercase(name[0]); |
| 77 return true; | 81 return true; |
| 78 } | 82 } |
| 79 | 83 |
| 80 // Helper to convert from a camelCaseName to camel_case_name. It uses some | 84 // Helper to convert from a camelCaseName to camel_case_name. It uses some |
| 81 // heuristics to try to handle acronyms in camel case names correctly. | 85 // heuristics to try to handle acronyms in camel case names correctly. |
| 82 std::string CamelCaseToUnderscoreCase(StringRef input) { | 86 std::string CamelCaseToUnderscoreCase(StringRef input) { |
| 83 std::string output; | 87 std::string output; |
| 84 bool needs_underscore = false; | 88 bool needs_underscore = false; |
| 85 bool was_lowercase = false; | 89 bool was_lowercase = false; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 107 was_lowercase = is_lowercase; | 111 was_lowercase = is_lowercase; |
| 108 was_uppercase = is_uppercase; | 112 was_uppercase = is_uppercase; |
| 109 } | 113 } |
| 110 std::reverse(output.begin(), output.end()); | 114 std::reverse(output.begin(), output.end()); |
| 111 return output; | 115 return output; |
| 112 } | 116 } |
| 113 | 117 |
| 114 bool GetNameForDecl(const clang::FieldDecl& decl, | 118 bool GetNameForDecl(const clang::FieldDecl& decl, |
| 115 const clang::ASTContext& context, | 119 const clang::ASTContext& context, |
| 116 std::string& name) { | 120 std::string& name) { |
| 121 // If there's no name to change, return. | |
| 122 if (!decl.getIdentifier()) | |
| 123 return false; | |
| 117 StringRef original_name = decl.getName(); | 124 StringRef original_name = decl.getName(); |
| 118 // Blink style field names are prefixed with `m_`. If this prefix isn't | 125 // Blink style field names are prefixed with `m_`. If this prefix isn't |
| 119 // present, assume it's already been converted to Google style. | 126 // present, assume it's already been converted to Google style. |
| 120 if (original_name.size() < strlen(kBlinkFieldPrefix) || | 127 if (original_name.size() < strlen(kBlinkFieldPrefix) || |
| 121 !original_name.startswith(kBlinkFieldPrefix)) | 128 !original_name.startswith(kBlinkFieldPrefix)) |
| 122 return false; | 129 return false; |
| 123 name = CamelCaseToUnderscoreCase( | 130 name = CamelCaseToUnderscoreCase( |
| 124 original_name.substr(strlen(kBlinkFieldPrefix))); | 131 original_name.substr(strlen(kBlinkFieldPrefix))); |
| 125 // The few examples I could find used struct-style naming with no `_` suffix | 132 // The few examples I could find used struct-style naming with no `_` suffix |
| 126 // for unions. | 133 // for unions. |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 151 return false; | 158 return false; |
| 152 | 159 |
| 153 // If the expression can be evaluated at compile time, then it should have a | 160 // If the expression can be evaluated at compile time, then it should have a |
| 154 // kFoo style name. Otherwise, not. | 161 // kFoo style name. Otherwise, not. |
| 155 return initializer->isEvaluatable(context); | 162 return initializer->isEvaluatable(context); |
| 156 } | 163 } |
| 157 | 164 |
| 158 bool GetNameForDecl(const clang::VarDecl& decl, | 165 bool GetNameForDecl(const clang::VarDecl& decl, |
| 159 const clang::ASTContext& context, | 166 const clang::ASTContext& context, |
| 160 std::string& name) { | 167 std::string& name) { |
| 168 // If there's no name to change, return. | |
| 169 if (!decl.getIdentifier()) | |
|
dcheng
2016/01/29 01:13:05
Does it makes sense to move this into RewriteBase:
danakj
2016/01/29 01:15:05
Hm ya sure.
| |
| 170 return false; | |
| 161 StringRef original_name = decl.getName(); | 171 StringRef original_name = decl.getName(); |
| 162 | 172 |
| 163 // Nothing to do for unnamed parameters. | 173 // Nothing to do for unnamed parameters. |
| 164 if (clang::isa<clang::ParmVarDecl>(decl) && original_name.empty()) | 174 if (clang::isa<clang::ParmVarDecl>(decl) && original_name.empty()) |
| 165 return false; | 175 return false; |
| 166 | 176 |
| 167 // static class members match against VarDecls. Blink style dictates that | 177 // static class members match against VarDecls. Blink style dictates that |
| 168 // these should be prefixed with `s_`, so strip that off. Also check for `m_` | 178 // these should be prefixed with `s_`, so strip that off. Also check for `m_` |
| 169 // and strip that off too, for code that accidentally uses the wrong prefix. | 179 // and strip that off too, for code that accidentally uses the wrong prefix. |
| 170 if (original_name.startswith(kBlinkStaticMemberPrefix)) | 180 if (original_name.startswith(kBlinkStaticMemberPrefix)) |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 for (const auto& r : replacements) { | 584 for (const auto& r : replacements) { |
| 575 std::string replacement_text = r.getReplacementText().str(); | 585 std::string replacement_text = r.getReplacementText().str(); |
| 576 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 586 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 577 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 587 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 578 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 588 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 579 } | 589 } |
| 580 llvm::outs() << "==== END EDITS ====\n"; | 590 llvm::outs() << "==== END EDITS ====\n"; |
| 581 | 591 |
| 582 return 0; | 592 return 0; |
| 583 } | 593 } |
| OLD | NEW |