[clang-tidy] Fix identifier naming for initializer list member initializers.

Summary:
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
};
```

Reviewers: alexfh, hokein, aaron.ballman

Subscribers: cfe-commits

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

llvm-svn: 287153
This commit is contained in:
Eric Fiselier 2016-11-16 21:15:58 +00:00
parent c156427ded
commit 732a3e0dd1
2 changed files with 42 additions and 0 deletions

View File

@ -677,6 +677,15 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) {
addUsage(NamingCheckFailures, Decl->getParent(),
Decl->getNameInfo().getSourceRange());
for (const 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;
}

View File

@ -152,15 +152,20 @@ constexpr int ConstExpr_variable = MyConstant;
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 my_derived_class : public virtual my_class {};
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>{{$}}