[PATCH] D32333: [clang-tidy] Update IdentifierNamingCheck to remove extra leading/trailing underscores

Jacob Bandes-Storch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 20 19:32:01 PDT 2017


jtbandes created this revision.

The goal of this change is to fix the following suboptimal replacements currently suggested by clang-tidy:

  // with MemberPrefix == "_"
  int __foo;  // accepted without complaint

  // with MemberPrefix == "m_"
  int _foo;
      ^~~~~~
      m__foo

I fixed this by

- updating `matchesStyle()` to reject names which have a leading underscore after a prefix has already been stripped, or a trailing underscore if a suffix has already been stripped;
- updating `fixupWithStyle()` to strip leading & trailing underscores before adding the user-defined prefix and suffix.

The replacements are now:

  // MemberPrefix == "_"
  int __foo;
      ^~~~~~
      _foo

  // MemberPrefix == "m_"
  int _foo;
      ^~~~~
      m_foo

Future improvements might elect to add .clang-tidy flags to improve what is being stripped. For instance, stripping `m_` could allow `m_foo` to be automatically replaced with `_foo`.


https://reviews.llvm.org/D32333

Files:
  clang-tidy/readability/IdentifierNamingCheck.cpp
  test/clang-tidy/readability-identifier-naming.cpp


Index: test/clang-tidy/readability-identifier-naming.cpp
===================================================================
--- test/clang-tidy/readability-identifier-naming.cpp
+++ test/clang-tidy/readability-identifier-naming.cpp
@@ -175,6 +175,9 @@
   int member2 = 2;
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'member2'
 // CHECK-FIXES: {{^}}  int __member2 = 2;{{$}}
+  int _memberWithExtraUnderscores_ = 42;
+// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member '_memberWithExtraUnderscores_'
+// CHECK-FIXES: {{^}}  int __memberWithExtraUnderscores = 42;{{$}}
 
 private:
     int private_member = 3;
Index: clang-tidy/readability/IdentifierNamingCheck.cpp
===================================================================
--- clang-tidy/readability/IdentifierNamingCheck.cpp
+++ clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -262,6 +262,13 @@
   else
     Matches = false;
 
+  // Ensure the name doesn't have any extra underscores beyond those specified
+  // in the prefix and suffix.
+  if (!Style.Prefix.empty() && Name.startswith("_"))
+    Matches = false;
+  if (!Style.Suffix.empty() && Name.endswith("_"))
+    Matches = false;
+
   if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
     Matches = false;
 
@@ -367,10 +374,12 @@
 static std::string
 fixupWithStyle(StringRef Name,
                const IdentifierNamingCheck::NamingStyle &Style) {
-  return Style.Prefix +
-         fixupWithCase(Name, Style.Case.getValueOr(
-                                 IdentifierNamingCheck::CaseType::CT_AnyCase)) +
-         Style.Suffix;
+  const std::string Fixed = fixupWithCase(
+      Name, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase));
+  StringRef Mid = StringRef(Fixed).trim("_");
+  if (Mid.size() == 0)
+    Mid = "_";
+  return (Style.Prefix + Mid + Style.Suffix).str();
 }
 
 static StyleKind findStyleKind(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32333.96081.patch
Type: text/x-patch
Size: 1957 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170421/19cee0b4/attachment-0001.bin>


More information about the cfe-commits mailing list