Skip to content

Commit eec01ad

Browse files
committedApr 26, 2017
[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 llvm-svn: 301431
1 parent a84ae0b commit eec01ad

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ static bool matchesStyle(StringRef Name,
262262
else
263263
Matches = false;
264264

265+
// Ensure the name doesn't have any extra underscores beyond those specified
266+
// in the prefix and suffix.
267+
if (Name.startswith("_") || Name.endswith("_"))
268+
Matches = false;
269+
265270
if (Style.Case && !Matchers[static_cast<size_t>(*Style.Case)].match(Name))
266271
Matches = false;
267272

@@ -367,10 +372,12 @@ static std::string fixupWithCase(StringRef Name,
367372
static std::string
368373
fixupWithStyle(StringRef Name,
369374
const IdentifierNamingCheck::NamingStyle &Style) {
370-
return Style.Prefix +
371-
fixupWithCase(Name, Style.Case.getValueOr(
372-
IdentifierNamingCheck::CaseType::CT_AnyCase)) +
373-
Style.Suffix;
375+
const std::string Fixed = fixupWithCase(
376+
Name, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase));
377+
StringRef Mid = StringRef(Fixed).trim("_");
378+
if (Mid.empty())
379+
Mid = "_";
380+
return (Style.Prefix + Mid + Style.Suffix).str();
374381
}
375382

376383
static StyleKind findStyleKind(

‎clang-tools-extra/test/clang-tidy/readability-identifier-naming.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ class my_class {
175175
int member2 = 2;
176176
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'member2'
177177
// CHECK-FIXES: {{^}} int __member2 = 2;{{$}}
178+
int _memberWithExtraUnderscores_ = 42;
179+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member '_memberWithExtraUnderscores_'
180+
// CHECK-FIXES: {{^}} int __memberWithExtraUnderscores = 42;{{$}}
178181

179182
private:
180183
int private_member = 3;

0 commit comments

Comments
 (0)
Please sign in to comment.