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

Unified Diff: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp

Issue 1639663003: Clean up rewrite_to_chrome_style naming logic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More comprehensive fixes Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
diff --git a/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp b/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
index 9bcf3c120514c75c6b9e53b61888ff7065ee9855..333687fb6cfda3cdeffad20c2e4ce5ee729d13ed 100644
--- a/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
+++ b/tools/clang/rewrite_to_chrome_style/RewriteToChromeStyle.cpp
@@ -33,6 +33,7 @@
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"
+#include "tools/clang/rewrite_to_chrome_style/Renamer.h"
using namespace clang::ast_matchers;
using clang::tooling::CommonOptionsParser;
@@ -50,45 +51,10 @@ constexpr char kBlinkFieldPrefix[] = "m_";
constexpr char kBlinkStaticMemberPrefix[] = "s_";
bool GetNameForDecl(const clang::FunctionDecl& decl, std::string& name) {
- name = decl.getNameAsString();
- name[0] = clang::toUppercase(name[0]);
+ name = IdentifierToCamelCase(decl.getName());
return true;
}
-// Helper to convert from a camelCaseName to camel_case_name. It uses some
-// heuristics to try to handle acronyms in camel case names correctly.
-std::string CamelCaseToUnderscoreCase(StringRef input) {
- std::string output;
- bool needs_underscore = false;
- bool was_lowercase = false;
- bool was_uppercase = false;
- // Iterate in reverse to minimize the amount of backtracking.
- for (const unsigned char* i = input.bytes_end() - 1; i >= input.bytes_begin();
- --i) {
- char c = *i;
- bool is_lowercase = clang::isLowercase(c);
- bool is_uppercase = clang::isUppercase(c);
- c = clang::toLowercase(c);
- // Transitioning from upper to lower case requires an underscore. This is
- // needed to handle names with acronyms, e.g. handledHTTPRequest needs a '_'
- // in 'dH'. This is a complement to the non-acronym case further down.
- if (needs_underscore || (was_uppercase && is_lowercase)) {
- output += '_';
- needs_underscore = false;
- }
- output += c;
- // Handles the non-acronym case: transitioning from lower to upper case
- // requires an underscore when emitting the next character, e.g. didLoad
- // needs a '_' in 'dL'.
- if (i != input.bytes_end() - 1 && was_lowercase && is_uppercase)
- needs_underscore = true;
- was_lowercase = is_lowercase;
- was_uppercase = is_uppercase;
- }
- std::reverse(output.begin(), output.end());
- return output;
-}
-
bool GetNameForDecl(const clang::FieldDecl& decl, std::string& name) {
StringRef original_name = decl.getName();
// Blink style field names are prefixed with `m_`. If this prefix isn't
@@ -96,7 +62,7 @@ bool GetNameForDecl(const clang::FieldDecl& decl, std::string& name) {
if (original_name.size() < strlen(kBlinkFieldPrefix) ||
!original_name.startswith(kBlinkFieldPrefix))
return false;
- name = CamelCaseToUnderscoreCase(
+ name = IdentifierToUnderscoreCase(
original_name.substr(strlen(kBlinkFieldPrefix)));
// The few examples I could find used struct-style naming with no `_` suffix
// for unions.
@@ -162,10 +128,9 @@ bool GetNameForDecl(const clang::VarDecl& decl, std::string& name) {
clang::isUppercase(original_name[1]))
return false;
name = 'k';
- name.append(original_name.data(), original_name.size());
- name[1] = clang::toUppercase(name[1]);
+ name += IdentifierToCamelCase(original_name);
} else {
- name = CamelCaseToUnderscoreCase(original_name);
+ name = IdentifierToUnderscoreCase(original_name);
}
if (decl.isStaticDataMember()) {

Powered by Google App Engine
This is Rietveld 408576698