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(
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(
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits