diff --git a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h --- a/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ b/clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -91,6 +91,11 @@ bool checkOptionValid(int StyleKindIndex) const; bool isOptionEnabled(StringRef OptionKey, const llvm::StringMap &StrMap) const; + + size_t getAsteriskCount(const std::string &TypeName) const; + size_t getAsteriskCount(const std::string &TypeName, + const NamedDecl *ND) const; + void loadDefaultConfig( IdentifierNamingCheck::HungarianNotationOption &HNOption) const; void loadFileConfig( 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 @@ -339,6 +339,12 @@ } TypeName = Type.erase(0, Type.find_first_not_of(" ")); + // Remove template parameters + const size_t Pos = Type.find("<"); + if (Pos != std::string::npos) { + TypeName = Type.erase(Pos, Type.size() - Pos); + } + // Replace spaces with single space. for (size_t Pos = 0; (Pos = Type.find(" ", Pos)) != std::string::npos; Pos += strlen(" ")) { @@ -364,11 +370,13 @@ for (auto Kw : TailsOfMultiWordType) { size_t Pos = Type.rfind(Kw.data()); if (Pos != std::string::npos) { - Type = Type.substr(0, Pos + Kw.size()); + const size_t PtrCount = getAsteriskCount(Type, ND); + Type = Type.substr(0, Pos + Kw.size() + PtrCount); RedundantRemoved = true; break; } } + TypeName = Type.erase(0, Type.find_first_not_of(" ")); if (!RedundantRemoved) { std::size_t FoundSpace = Type.find(" "); @@ -585,15 +593,7 @@ } // Pointers - size_t PtrCount = [&](std::string TypeName) -> size_t { - size_t Pos = TypeName.find('*'); - size_t Count = 0; - for (; Pos < TypeName.length(); Pos++, Count++) { - if ('*' != TypeName[Pos]) - break; - } - return Count; - }(ModifiedTypeName); + size_t PtrCount = getAsteriskCount(ModifiedTypeName); if (PtrCount > 0) { ModifiedTypeName = [&](std::string Str, StringRef From, StringRef To) { size_t StartPos = 0; @@ -691,6 +691,28 @@ return Initial; } +size_t IdentifierNamingCheck::HungarianNotation::getAsteriskCount( + const std::string &TypeName) const { + size_t Pos = TypeName.find('*'); + size_t Count = 0; + for (; Pos < TypeName.length(); Pos++, Count++) { + if ('*' != TypeName[Pos]) + break; + } + return Count; +} + +size_t IdentifierNamingCheck::HungarianNotation::getAsteriskCount( + const std::string &TypeName, const NamedDecl *ND) const { + size_t PtrCount = 0; + if (const auto *TD = dyn_cast(ND)) { + QualType QT = TD->getType(); + if (QT->isPointerType()) + PtrCount = getAsteriskCount(TypeName); + } + return PtrCount; +} + void IdentifierNamingCheck::HungarianNotation::loadDefaultConfig( IdentifierNamingCheck::HungarianNotationOption &HNOption) const { diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -257,6 +257,10 @@ behavior of using `i` as the prefix for enum tags, set the `EnumConstantPrefix` option to `i` instead of using `EnumConstantHungarianPrefix`. +- Fixed a hungarian notation issue in :doc:`readability-identifier-naming + ` which failed to indicate + the number of asterisks. + - Fixed a false positive in :doc:`readability-implicit-bool-conversion ` check warning would be unnecessarily emitted for explicit cast using direct list initialization. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-c-language.c @@ -283,6 +283,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for global pointer 'ValueU8Ptr' [readability-identifier-naming] // CHECK-FIXES: {{^}}uint8_t *pu8ValueU8Ptr; +unsigned char *ValueUcPtr; +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for global pointer 'ValueUcPtr' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char *pucValueUcPtr; + +unsigned char **ValueUcPtr2; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: invalid case style for global pointer 'ValueUcPtr2' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char **ppucValueUcPtr2; + void MyFunc2(void* Val){} // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for pointer parameter 'Val' [readability-identifier-naming] // CHECK-FIXES: {{^}}void MyFunc2(void* pVal){} diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-cfgfile.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-cfgfile.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-cfgfile.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation-cfgfile.cpp @@ -355,6 +355,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for global pointer 'ValueU8Ptr' [readability-identifier-naming] // CHECK-FIXES: {{^}}uint8_t *custpcustu8ValueU8Ptr; +unsigned char *ValueUcPtr; +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for global pointer 'ValueUcPtr' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char *custpcustucValueUcPtr; + +unsigned char **ValueUcPtr2; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: invalid case style for global pointer 'ValueUcPtr2' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char **custpcustpcustucValueUcPtr2; + void MyFunc2(void* Val){} // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for pointer parameter 'Val' [readability-identifier-naming] // CHECK-FIXES: {{^}}void MyFunc2(void* custpcustvVal){} diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/identifier-naming-hungarian-notation.cpp @@ -355,6 +355,14 @@ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: invalid case style for global pointer 'ValueU8Ptr' [readability-identifier-naming] // CHECK-FIXES: {{^}}uint8_t *pu8ValueU8Ptr; +unsigned char *ValueUcPtr; +// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: invalid case style for global pointer 'ValueUcPtr' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char *pucValueUcPtr; + +unsigned char **ValueUcPtr2; +// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: invalid case style for global pointer 'ValueUcPtr2' [readability-identifier-naming] +// CHECK-FIXES: {{^}}unsigned char **ppucValueUcPtr2; + void MyFunc2(void* Val){} // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: invalid case style for pointer parameter 'Val' [readability-identifier-naming] // CHECK-FIXES: {{^}}void MyFunc2(void* pVal){}