[clang-tools-extra] r301431 - [clang-tidy] Update IdentifierNamingCheck to remove extra leading/trailing underscores

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 26 09:39:12 PDT 2017


Author: alexfh
Date: Wed Apr 26 11:39:11 2017
New Revision: 301431

URL: http://llvm.org/viewvc/llvm-project?rev=301431&view=rev
Log:
[clang-tidy] Update IdentifierNamingCheck to remove extra leading/trailing underscores

Summary:
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`.

Reviewers: alexfh

Reviewed By: alexfh

Subscribers: cfe-commits

Patch by Jacob Bandes-Storch!

Differential Revision: https://reviews.llvm.org/D32333

Modified:
    clang-tools-extra/trunk/clang-tidy/readability/IdentifierNamingCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/readability-identifier-naming.cpp

Modified: clang-tools-extra/trunk/clang-tidy/readability/IdentifierNamingCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/readability/IdentifierNamingCheck.cpp?rev=301431&r1=301430&r2=301431&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/readability/IdentifierNamingCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/readability/IdentifierNamingCheck.cpp Wed Apr 26 11:39:11 2017
@@ -262,6 +262,11 @@ static bool matchesStyle(StringRef Name,
   else
     Matches = false;
 
+  // Ensure the name doesn't have any extra underscores beyond those specified
+  // in the prefix and suffix.
+  if (Name.startswith("_") || Name.endswith("_"))
+    Matches = false;
+
   if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
     Matches = false;
 
@@ -367,10 +372,12 @@ static std::string fixupWithCase(StringR
 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.empty())
+    Mid = "_";
+  return (Style.Prefix + Mid + Style.Suffix).str();
 }
 
 static StyleKind findStyleKind(

Modified: clang-tools-extra/trunk/test/clang-tidy/readability-identifier-naming.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/readability-identifier-naming.cpp?rev=301431&r1=301430&r2=301431&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/readability-identifier-naming.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/readability-identifier-naming.cpp Wed Apr 26 11:39:11 2017
@@ -175,6 +175,9 @@ private:
   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;




More information about the cfe-commits mailing list