Index: clang-tidy/readability/IdentifierNamingCheck.cpp =================================================================== --- clang-tidy/readability/IdentifierNamingCheck.cpp +++ clang-tidy/readability/IdentifierNamingCheck.cpp @@ -142,7 +142,12 @@ // enclosing context (namespace, class, etc). Finder->addMatcher(namedDecl().bind("decl"), this); - Finder->addMatcher(declRefExpr().bind("declref"), 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(typeLoc().bind("typeLoc"), this); + Finder->addMatcher(nestedNameSpecifierLoc().bind("nestedNameLoc"), this); } static bool matchesStyle(StringRef Name, @@ -515,7 +520,101 @@ } void IdentifierNamingCheck::check(const MatchFinder::MatchResult &Result) { - if (const auto *DeclRef = Result.Nodes.getNodeAs("declref")) { + if (const auto *Decl = + Result.Nodes.getNodeAs("classRef")) { + if (Decl->isImplicit()) + return; + + return addUsage(NamingCheckFailures, Decl->getParent(), + Decl->getNameInfo().getSourceRange(), Result.SourceManager); + } + + if (const auto *Decl = + Result.Nodes.getNodeAs("classRef")) { + if (Decl->isImplicit()) + return; + + SourceRange Range = Decl->getNameInfo().getSourceRange(); + Range.setBegin(Range.getBegin().getLocWithOffset(1)); + return addUsage(NamingCheckFailures, Decl->getParent(), Range, + Result.SourceManager); + } + + if (const auto *Loc = Result.Nodes.getNodeAs("typeLoc")) { + if (const auto &Ref = Loc->getAs()) { + return addUsage(NamingCheckFailures, Ref.getDecl(), Loc->getSourceRange(), + Result.SourceManager); + } + + if (const auto &Ref = Loc->getAs()) { + return addUsage(NamingCheckFailures, Ref.getDecl(), Loc->getSourceRange(), + Result.SourceManager); + } + + if (const auto &Ref = Loc->getAs()) { + return addUsage(NamingCheckFailures, Ref.getDecl(), Loc->getSourceRange(), + Result.SourceManager); + } + + if (const auto &Ref = Loc->getAs()) { + return addUsage(NamingCheckFailures, Ref.getDecl(), Loc->getSourceRange(), + Result.SourceManager); + } + + if (const auto &Ref = Loc->getAs()) { + const auto *Decl = + Ref.getTypePtr()->getTemplateName().getAsTemplateDecl(); + SourceRange Range = SourceRange(Ref.getTemplateNameLoc(), + Ref.getLAngleLoc().getLocWithOffset(-1)); + + if (const auto *ClassDecl = dyn_cast(Decl)) { + return addUsage(NamingCheckFailures, ClassDecl->getTemplatedDecl(), + Range, Result.SourceManager); + } + + if (const auto *ClassDecl = dyn_cast(Decl)) { + return addUsage(NamingCheckFailures, ClassDecl->getTemplatedDecl(), + Range, Result.SourceManager); + } + + if (const auto *ClassDecl = dyn_cast(Decl)) { + return addUsage(NamingCheckFailures, ClassDecl->getTemplatedDecl(), + Range, Result.SourceManager); + } + + if (const auto *ClassDecl = dyn_cast(Decl)) { + return addUsage(NamingCheckFailures, ClassDecl->getTemplatedDecl(), + Range, Result.SourceManager); + } + } + + if (const auto &Ref = + Loc->getAs()) { + return addUsage(NamingCheckFailures, Ref.getTypePtr()->getAsTagDecl(), + Loc->getSourceRange(), Result.SourceManager); + } + } + + if (const auto *Loc = + Result.Nodes.getNodeAs("nestedNameLoc")) { + if (NestedNameSpecifier *Spec = Loc->getNestedNameSpecifier()) { + if (NamespaceDecl *Decl = Spec->getAsNamespace()) { + SourceRange Range = Loc->getLocalSourceRange(); + Range.setEnd(Range.getEnd().getLocWithOffset(-1)); + return addUsage(NamingCheckFailures, Decl, Range, Result.SourceManager); + } + } + } + + if (const auto *Decl = Result.Nodes.getNodeAs("using")) { + for (const auto &Shadow : Decl->shadows()) { + addUsage(NamingCheckFailures, Shadow->getTargetDecl(), + Decl->getNameInfo().getSourceRange(), Result.SourceManager); + } + return; + } + + if (const auto *DeclRef = Result.Nodes.getNodeAs("declRef")) { SourceRange Range = DeclRef->getNameInfo().getSourceRange(); return addUsage(NamingCheckFailures, DeclRef->getDecl(), Range, Result.SourceManager); @@ -525,6 +624,11 @@ if (!Decl->getIdentifier() || Decl->getName().empty() || Decl->isImplicit()) return; + // Ignore ClassTemplateSpecializationDecl which are creating duplicate + // replacements with CXXRecordDecl + if (isa(Decl)) + return; + StyleKind SK = findStyleKind(Decl, NamingStyles); if (SK == SK_Invalid) return; Index: test/clang-tidy/readability-identifier-naming.cpp =================================================================== --- test/clang-tidy/readability-identifier-naming.cpp +++ test/clang-tidy/readability-identifier-naming.cpp @@ -104,6 +104,9 @@ // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class 'my_class' // CHECK-FIXES: {{^}}class CMyClass {{{$}} my_class(); +// CHECK-FIXES: {{^}} CMyClass();{{$}} + ~my_class(); +// CHECK-FIXES: {{^}} ~CMyClass();{{$}} const int MEMBER_one_1 = ConstExpr_variable; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: invalid case style for constant member 'MEMBER_one_1' @@ -137,15 +140,36 @@ const int my_class::classConstant = 4; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class constant 'classConstant' -// CHECK-FIXES: {{^}}const int my_class::kClassConstant = 4;{{$}} -// FIXME: The fixup should reflect class name fixups as well: -// FIXME: {{^}}const int CMyClass::kClassConstant = 4;{{$}} +// CHECK-FIXES: {{^}}const int CMyClass::kClassConstant = 4;{{$}} int my_class::ClassMember_2 = 5; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class member 'ClassMember_2' -// CHECK-FIXES: {{^}}int my_class::ClassMember2 = 5;{{$}} -// FIXME: The fixup should reflect class name fixups as well: -// FIXME: {{^}}int CMyClass::ClassMember2 = 5;{{$}} +// CHECK-FIXES: {{^}}int CMyClass::ClassMember2 = 5;{{$}} + +class my_derived_class : public virtual my_class {}; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class 'my_derived_class' +// CHECK-FIXES: {{^}}class CMyDerivedClass : public virtual CMyClass {};{{$}} + +class CMyWellNamedClass {}; +// No warning expected as this class is well named. + +template +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for type template parameter 'T' +// CHECK-FIXES: {{^}}template{{$}} +class my_templated_class : CMyWellNamedClass {}; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class 'my_templated_class' +// CHECK-FIXES: {{^}}class CMyTemplatedClass : CMyWellNamedClass {};{{$}} + +template +// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for type template parameter 'T' +// CHECK-FIXES: {{^}}template{{$}} +class my_other_templated_class : my_templated_class, private my_derived_class {}; +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for class 'my_other_templated_class' +// CHECK-FIXES: {{^}}class CMyOtherTemplatedClass : CMyTemplatedClass, private CMyDerivedClass {};{{$}} + +template +using MYSUPER_TPL = my_other_templated_class<::FOO_NS::my_class>; +// CHECK-FIXES: {{^}}using MYSUPER_TPL = CMyOtherTemplatedClass<::foo_ns::CMyClass>;{{$}} const int global_Constant = 6; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for global constant 'global_Constant' @@ -186,7 +210,7 @@ void Global_Fun(TYPE_parameters... PARAMETER_PACK) { // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for global function 'Global_Fun' // CHECK-MESSAGES: :[[@LINE-2]]:17: warning: invalid case style for parameter pack 'PARAMETER_PACK' -// CHECK-FIXES: {{^}}void GlobalFun(TYPE_parameters... parameterPack) {{{$}} +// CHECK-FIXES: {{^}}void GlobalFun(typeParameters_t... parameterPack) {{{$}} global_function(1, 2); // CHECK-FIXES: {{^}} GlobalFunction(1, 2);{{$}} FOO_bar = Global_variable; @@ -214,6 +238,8 @@ void non_Virtual_METHOD() {} // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for private method 'non_Virtual_METHOD' // CHECK-FIXES: {{^}} void __non_Virtual_METHOD() {}{{$}} + +public: static void CLASS_METHOD() {} // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: invalid case style for class method 'CLASS_METHOD' // CHECK-FIXES: {{^}} static void classMethod() {}{{$}} @@ -244,8 +270,7 @@ // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for struct 'THIS___Structure' // CHECK-FIXES: {{^}}struct this_structure {{{$}} THIS___Structure(); -// FIXME: There should be a fixup for the constructor as well -// FIXME: {{^}} this_structure();{{$}} +// CHECK-FIXES: {{^}} this_structure();{{$}} union __MyUnion_is_wonderful__ {}; // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: invalid case style for union '__MyUnion_is_wonderful__' @@ -254,13 +279,19 @@ typedef THIS___Structure struct_type; // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for typedef 'struct_type' -// CHECK-FIXES: {{^}}typedef THIS___Structure struct_type_t;{{$}} -// FIXME: The fixup should reflect structure name fixups as well: -// FIXME: {{^}}typedef this_structure struct_type_t;{{$}} +// CHECK-FIXES: {{^}}typedef this_structure struct_type_t;{{$}} static void static_Function() { // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: invalid case style for function 'static_Function' // CHECK-FIXES: {{^}}static void staticFunction() {{{$}} + + ::FOO_NS::InlineNamespace::abstract_class::CLASS_METHOD(); +// CHECK-FIXES: {{^}} ::foo_ns::inline_namespace::AAbstractClass::classMethod();{{$}} + ::FOO_NS::InlineNamespace::static_Function(); +// CHECK-FIXES: {{^}} ::foo_ns::inline_namespace::staticFunction();{{$}} + + using ::FOO_NS::InlineNamespace::CE_function; +// CHECK-FIXES: {{^}} using ::foo_ns::inline_namespace::ce_function;{{$}} } }