diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -237,17 +237,23 @@ Finder->addMatcher(namedDecl().bind("decl"), this); Finder->addMatcher(usingDecl().bind("using"), this); Finder->addMatcher(declRefExpr().bind("declRef"), this); - Finder->addMatcher(cxxConstructorDecl().bind("classRef"), this); - Finder->addMatcher(cxxDestructorDecl().bind("classRef"), this); + Finder->addMatcher(cxxConstructorDecl(unless(isImplicit())).bind("classRef"), + this); + Finder->addMatcher(cxxDestructorDecl(unless(isImplicit())).bind("classRef"), + this); Finder->addMatcher(typeLoc().bind("typeLoc"), this); Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this); + Finder->addMatcher( + functionDecl(unless(cxxMethodDecl(isImplicit())), + hasBody(forEachDescendant(memberExpr().bind("memberExpr")))), + this); } void IdentifierNamingCheck::registerPPCallbacks( const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { ModuleExpanderPP->addPPCallbacks( std::make_unique(ModuleExpanderPP, - this)); + this)); } static bool matchesStyle(StringRef Name, @@ -399,7 +405,7 @@ if (isa(D) && NamingStyles[SK_ObjcIvar]) return SK_ObjcIvar; - + if (isa(D) && NamingStyles[SK_Typedef]) return SK_Typedef; @@ -495,7 +501,8 @@ return SK_ConstexprVariable; if (!Type.isNull() && Type.isConstQualified()) { - if (Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_ConstantPointerParameter]) + if (Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_ConstantPointerParameter]) return SK_ConstantPointerParameter; if (NamingStyles[SK_ConstantParameter]) @@ -508,8 +515,9 @@ if (Decl->isParameterPack() && NamingStyles[SK_ParameterPack]) return SK_ParameterPack; - if (!Type.isNull() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_PointerParameter]) - return SK_PointerParameter; + if (!Type.isNull() && Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_PointerParameter]) + return SK_PointerParameter; if (NamingStyles[SK_Parameter]) return SK_Parameter; @@ -527,7 +535,8 @@ if (Decl->isStaticDataMember() && NamingStyles[SK_ClassConstant]) return SK_ClassConstant; - if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_GlobalConstantPointer]) + if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_GlobalConstantPointer]) return SK_GlobalConstantPointer; if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalConstant]) @@ -536,7 +545,8 @@ if (Decl->isStaticLocal() && NamingStyles[SK_StaticConstant]) return SK_StaticConstant; - if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_LocalConstantPointer]) + if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_LocalConstantPointer]) return SK_LocalConstantPointer; if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalConstant]) @@ -552,7 +562,8 @@ if (Decl->isStaticDataMember() && NamingStyles[SK_ClassMember]) return SK_ClassMember; - if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_GlobalPointer]) + if (Decl->isFileVarDecl() && Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_GlobalPointer]) return SK_GlobalPointer; if (Decl->isFileVarDecl() && NamingStyles[SK_GlobalVariable]) @@ -560,8 +571,9 @@ if (Decl->isStaticLocal() && NamingStyles[SK_StaticVariable]) return SK_StaticVariable; - - if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && NamingStyles[SK_LocalPointer]) + + if (Decl->isLocalVarDecl() && Type.getTypePtr()->isAnyPointerType() && + NamingStyles[SK_LocalPointer]) return SK_LocalPointer; if (Decl->isLocalVarDecl() && NamingStyles[SK_LocalVariable]) @@ -710,8 +722,6 @@ void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *Decl = Result.Nodes.getNodeAs("classRef")) { - if (Decl->isImplicit()) - return; addUsage(NamingCheckFailures, Decl->getParent(), Decl->getNameInfo().getSourceRange()); @@ -719,9 +729,10 @@ for (const auto *Init : Decl->inits()) { if (!Init->isWritten() || Init->isInClassMemberInitializer()) continue; - if (const auto *FD = Init->getAnyMember()) + 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. } @@ -730,8 +741,6 @@ if (const auto *Decl = Result.Nodes.getNodeAs("classRef")) { - if (Decl->isImplicit()) - return; SourceRange Range = Decl->getNameInfo().getSourceRange(); if (Range.getBegin().isInvalid()) @@ -806,6 +815,13 @@ return; } + if (const auto MemberRef = Result.Nodes.getNodeAs("memberExpr")) { + SourceRange Range = MemberRef->getMemberNameInfo().getSourceRange(); + addUsage(NamingCheckFailures, MemberRef->getMemberDecl(), Range, + Result.SourceManager); + return; + } + if (const auto *Decl = Result.Nodes.getNodeAs("decl")) { if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit()) return; diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-member-decl-usage.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-member-decl-usage.cpp new file mode 100644 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/readability-identifier-naming-member-decl-usage.cpp @@ -0,0 +1,123 @@ +// RUN: %check_clang_tidy %s readability-identifier-naming %t -- \ +// RUN: -config='{CheckOptions: [ \ +// RUN: {key: readability-identifier-naming.MemberCase, value: CamelCase}, \ +// RUN: {key: readability-identifier-naming.ParameterCase, value: CamelCase} \ +// RUN: ]}' + +int set_up(int); +int clear(int); + +class Foo { +public: + const int bar_baz; // comment-0 + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: invalid case style for member 'bar_baz' + // CHECK-FIXES: {{^}} const int BarBaz; // comment-0 + + Foo(int Val) : bar_baz(Val) { // comment-1 + // CHECK-FIXES: {{^}} Foo(int Val) : BarBaz(Val) { // comment-1 + set_up(bar_baz); // comment-2 + // CHECK-FIXES: {{^}} set_up(BarBaz); // comment-2 + } + + Foo() : Foo(0) {} + + ~Foo() { + clear(bar_baz); // comment-3 + // CHECK-FIXES: {{^}} clear(BarBaz); // comment-3 + } + + int getBar() const { return bar_baz; } // comment-4 + // CHECK-FIXES: {{^}} int getBar() const { return BarBaz; } // comment-4 +}; + +class FooBar : public Foo { +public: + int getFancyBar() const { + return this->bar_baz; // comment-5 + // CHECK-FIXES: {{^}} return this->BarBaz; // comment-5 + } +}; + +int getBar(const Foo &Foo) { + return Foo.bar_baz; // comment-6 + // CHECK-FIXES: {{^}} return Foo.BarBaz; // comment-6 +} + +int getBar(const FooBar &Foobar) { + return Foobar.bar_baz; // comment-7 + // CHECK-FIXES: {{^}} return Foobar.BarBaz; // comment-7 +} + +int getFancyBar(const FooBar &Foobar) { + return Foobar.getFancyBar(); +} + +template +class TempTest : public Foo { +public: + TempTest() = default; + TempTest(int Val) : Foo(Val) {} + int getBar() const { return Foo::bar_baz; } // comment-8 + // CHECK-FIXES: {{^}} int getBar() const { return Foo::BarBaz; } // comment-8 + int getBar2() const { return this->bar_baz; } // comment-9 + // CHECK-FIXES: {{^}} int getBar2() const { return this->BarBaz; } // comment-9 +}; + +TempTest x; //force an instantiation + +int blah() { + return x.getBar2(); // gotta have a reference to the getBar2 so that the + // compiler will generate the function and resolve + // the DependantScopeMemberExpr +} + +namespace Bug41122 { +namespace std { + +// for this example we aren't bothered about how std::vector is treated +template //NOLINT +class vector { //NOLINT +public: + void push_back(bool); //NOLINT + void pop_back(); //NOLINT +}; //NOLINT +}; // namespace std + +class Foo { + + // bug report has Stack swapped to stack, this is stack swapped to Stack + // probably not going to cause any issue its just how + // people prefer their style + + std::vector &stack; + // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: invalid case style for member 'stack' [readability-identifier-naming] +public: + Foo(std::vector &stack) + // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: invalid case style for parameter 'stack' [readability-identifier-naming] + // CHECK-FIXES: {{^}} Foo(std::vector &Stack) + : stack(stack) { + // CHECK-FIXES: {{^}} : Stack(Stack) { + stack.push_back(true); + // CHECK-FIXES: {{^}} Stack.push_back(true); + } + ~Foo() { + stack.pop_back(); + // CHECK-FIXES: {{^}} Stack.pop_back(); + } +}; +}; // namespace Bug41122 + +namespace Bug29005 { +class Foo { +public: + int a_member_of_foo = 0; + // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: invalid case style for member 'a_member_of_foo' + // CHECK-FIXES: {{^}} int AMemberOfFoo = 0; +}; + +int main() { + Foo foo; + return foo.a_member_of_foo; + // CHECK-FIXES: {{^}} return foo.AMemberOfFoo; +} +}; // namespace Bug29005