[PATCH] D26744: [clang-tidy] Fix identifier naming for initializer list member initializers.
Eric Fiselier via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 16 05:52:21 PST 2016
EricWF created this revision.
EricWF added reviewers: alexfh, hokein, aaron.ballman.
EricWF added a subscriber: cfe-commits.
This patch adds handling for member initializers in a constructors initializer list. Previously we only handled base-class and delegating initializers, which are transformed by the `TypeLoc` matcher. For Example:
// Style options: All identifiers should start with an upper case letter.
struct base { ... };
struct der : base {
int field; // FIXES: int Field;
der() : der(42) {} // FIXES: Der() : Der(42) {}
der(int X) : base(), field(X) {} // FIXES: Der(int X) : Base(), field(X)
// Note that `field` doesn't get replaced
};
https://reviews.llvm.org/D26744
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
@@ -152,15 +152,20 @@
class my_class {
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for class 'my_class'
// CHECK-FIXES: {{^}}class CMyClass {{{$}}
+public:
my_class();
// CHECK-FIXES: {{^}} CMyClass();{{$}}
+ my_class(void*) : my_class() {}
+// CHECK-FIXES: {{^}} CMyClass(void*) : CMyClass() {}{{$}}
+
~
my_class();
// (space in destructor token test, we could check trigraph but they will be deprecated)
// CHECK-FIXES: {{^}} ~{{$}}
// CHECK-FIXES: {{^}} CMyClass();{{$}}
+private:
const int MEMBER_one_1 = ConstExpr_variable;
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: invalid case style for constant member 'MEMBER_one_1'
// CHECK-FIXES: {{^}} const int member_one_1 = const_expr_variable;{{$}}
@@ -211,6 +216,34 @@
class CMyWellNamedClass {};
// No warning expected as this class is well named.
+template <typename t_t>
+class CMyWellNamedClass2 : public my_class {
+ // CHECK-FIXES: {{^}}class CMyWellNamedClass2 : public CMyClass {{{$}}
+ t_t my_Bad_Member;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'my_Bad_Member'
+ // CHECK-FIXES: {{^}} t_t __my_Bad_Member;{{$}}
+ int my_Other_Bad_Member = 42;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for private member 'my_Other_Bad_Member'
+ // CHECK-FIXES: {{^}} int __my_Other_Bad_Member = 42;{{$}}
+public:
+ CMyWellNamedClass2() = default;
+ CMyWellNamedClass2(CMyWellNamedClass2 const&) = default;
+ CMyWellNamedClass2(CMyWellNamedClass2 &&) = default;
+ CMyWellNamedClass2(t_t a_v, void *a_p) : my_class(a_p), my_Bad_Member(a_v) {}
+ // CHECK-FIXES: {{^}} CMyWellNamedClass2(t_t a_v, void *a_p) : CMyClass(a_p), __my_Bad_Member(a_v) {}{{$}}
+
+ CMyWellNamedClass2(t_t a_v) : my_class(), my_Bad_Member(a_v), my_Other_Bad_Member(11) {}
+ // CHECK-FIXES: {{^}} CMyWellNamedClass2(t_t a_v) : CMyClass(), __my_Bad_Member(a_v), __my_Other_Bad_Member(11) {}{{$}}
+};
+void InstantiateClassMethods() {
+ // Ensure we trigger the instantiation of each constructor
+ CMyWellNamedClass2<int> x;
+ CMyWellNamedClass2<int> x2 = x;
+ CMyWellNamedClass2<int> x3 = static_cast<CMyWellNamedClass2<int>&&>(x2);
+ CMyWellNamedClass2<int> x4(42);
+ CMyWellNamedClass2<int> x5(42, nullptr);
+}
+
template<typename T>
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: invalid case style for type template parameter 'T'
// CHECK-FIXES: {{^}}template<typename t_t>{{$}}
Index: clang-tidy/readability/IdentifierNamingCheck.cpp
===================================================================
--- clang-tidy/readability/IdentifierNamingCheck.cpp
+++ clang-tidy/readability/IdentifierNamingCheck.cpp
@@ -677,6 +677,15 @@
addUsage(NamingCheckFailures, Decl->getParent(),
Decl->getNameInfo().getSourceRange());
+
+ for (auto Init : Decl->inits()) {
+ if (!Init->isWritten() || Init->isInClassMemberInitializer())
+ continue;
+ if (const auto *FD = Init->getAnyMember())
+ addUsage(NamingCheckFailures, FD, SourceRange(Init->getMemberLocation()));
+ // Note: delegating constructors and base class initializers are handled
+ // via the "typeLoc" matcher.
+ }
return;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26744.78175.patch
Type: text/x-patch
Size: 3463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161116/b68a9ad6/attachment-0001.bin>
More information about the cfe-commits
mailing list