Index: clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h =================================================================== --- clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h +++ clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.h @@ -41,7 +41,7 @@ private: llvm::Optional - GetDeclFailureInfo(const StringRef &TypeName, const NamedDecl *Decl, + getDeclFailureInfo(const StringRef &TypeName, const NamedDecl *Decl, const SourceManager &SM) const override; llvm::Optional GetMacroFailureInfo(const Token &MacroNameTok, Index: clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp +++ clang-tools-extra/clang-tidy/bugprone/ReservedIdentifierCheck.cpp @@ -152,7 +152,7 @@ } Optional -ReservedIdentifierCheck::GetDeclFailureInfo(const StringRef &Type, +ReservedIdentifierCheck::getDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, const SourceManager &) const { assert(Decl && Decl->getIdentifier() && !Decl->getName().empty() && Index: clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h =================================================================== --- clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h +++ clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @@ -66,7 +66,7 @@ private: llvm::Optional - GetDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, + getDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, const SourceManager &SM) const override; llvm::Optional GetMacroFailureInfo(const Token &MacroNameTok, Index: clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp +++ clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.cpp @@ -223,7 +223,7 @@ {"char*", "sz"}, {"wchar_t*", "wsz"}}; // clang-format on - for (auto &Type : NullString) { + for (const auto &Type : NullString) { const auto &Key = Type.getKey(); if (ClonedTypeName.find(Key.str()) == 0) { PrefixStr = Type.getValue().str(); @@ -238,7 +238,7 @@ {"char", "sz"}, {"wchar_t", "wsz"}}; // clang-format on - for (auto &Type : NullString) { + for (const auto &Type : NullString) { const auto &Key = Type.getKey(); if (ClonedTypeName.find(Key.str()) == 0) { PrefixStr = Type.getValue().str(); @@ -251,28 +251,28 @@ } // Handle pointers - size_t nPtrCount = [&](std::string TypeName) -> size_t { - size_t nPos = TypeName.find('*'); - size_t nCnt = 0; - for (; nPos < TypeName.length(); nPos++, nCnt++) { - if ('*' != TypeName[nPos]) + 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 nCnt; + return Count; }(ClonedTypeName); - if (nPtrCount > 0) { - ClonedTypeName = [&](std::string str, const std::string &from, - const std::string &to) { - size_t start_pos = 0; - while ((start_pos = str.find(from, start_pos)) != std::string::npos) { - str.replace(start_pos, from.length(), to); - start_pos += to.length(); + if (PtrCount > 0) { + ClonedTypeName = [&](std::string Str, const std::string &From, + const std::string &To) { + size_t StartPos = 0; + while ((StartPos = Str.find(From, StartPos)) != std::string::npos) { + Str.replace(StartPos, From.length(), To); + StartPos += To.length(); } - return str; + return Str; }(ClonedTypeName, "*", ""); } - for (auto &Type : HungarianNotationTable) { + for (const auto &Type : HungarianNotationTable) { const auto &Key = Type.getKey(); if (ClonedTypeName == Key) { PrefixStr = Type.getValue().str(); @@ -280,8 +280,8 @@ } } - if (nPtrCount > 0) { - for (size_t nIdx = 0; nIdx < nPtrCount; nIdx++) { + if (PtrCount > 0) { + for (size_t Idx = 0; Idx < PtrCount; Idx++) { PrefixStr.insert(PrefixStr.begin(), 'p'); } } @@ -314,7 +314,8 @@ return false; if (Style.Case == IdentifierNamingCheck::CaseType::CT_HungarianNotation) { - const auto TypePrefix = getHungarianNotationTypePrefix(Type.str(), Decl); + const std::string TypePrefix = + getHungarianNotationTypePrefix(Type.str(), Decl); if (TypePrefix.length() > 0) { if (!Name.startswith(TypePrefix)) return false; @@ -322,10 +323,7 @@ } } - size_t MatcherIndex = static_cast(*Style.Case); - auto MatcherResult = Matchers[MatcherIndex].match(Name); - - if (Style.Case && !MatcherResult) + if (Style.Case && !Matchers[static_cast(*Style.Case)].match(Name)) return false; return true; @@ -427,19 +425,19 @@ case IdentifierNamingCheck::CT_HungarianNotation: { const NamedDecl *pNamedDecl = dyn_cast(pDecl); - const auto TypePrefix = + const std::string TypePrefix = getHungarianNotationTypePrefix(Type.str(), pNamedDecl); Fixup = TypePrefix; - for (size_t nIdx = 0; nIdx < Words.size(); nIdx++) { + for (size_t Idx = 0; Idx < Words.size(); Idx++) { // Skip first part if it's a lowercase string - if (nIdx == 0) { - const bool bLowerAlnum = - std::all_of(Words[nIdx].begin(), Words[nIdx].end(), + if (Idx == 0) { + const bool LowerAlnum = + std::all_of(Words[Idx].begin(), Words[Idx].end(), [](const char c) { return isdigit(c) || islower(c); }); - if (bLowerAlnum) + if (LowerAlnum) continue; } - Fixup += Words[nIdx]; + Fixup += Words[Idx]; } break; } @@ -511,9 +509,9 @@ static std::string fixupWithStyle(const StringRef &Type, const StringRef &Name, const IdentifierNamingCheck::NamingStyle &Style, - const Decl *pDecl) { + const Decl *Decl) { const std::string Fixed = fixupWithCase( - Type, Name, pDecl, + Type, Name, Decl, Style.Case.getValueOr(IdentifierNamingCheck::CaseType::CT_AnyCase)); StringRef Mid = StringRef(Fixed).trim("_"); if (Mid.empty()) @@ -837,7 +835,7 @@ } llvm::Optional -IdentifierNamingCheck::GetDeclFailureInfo(const StringRef &Type, +IdentifierNamingCheck::getDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, const SourceManager &SM) const { SourceLocation Loc = Decl->getLocation(); Index: clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h =================================================================== --- clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h +++ clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.h @@ -32,7 +32,7 @@ /// Derived classes should not implement any matching logic themselves; this /// class will do the matching and call the derived class' - /// GetDeclFailureInfo() and GetMacroFailureInfo() for determining whether a + /// getDeclFailureInfo() and GetMacroFailureInfo() for determining whether a /// given identifier passes or fails the check. void registerMatchers(ast_matchers::MatchFinder *Finder) override final; void @@ -124,7 +124,7 @@ /// Overridden by derived classes, returns information about if and how a Decl /// failed the check. A 'None' result means the Decl did not fail the check. virtual llvm::Optional - GetDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, + getDeclFailureInfo(const StringRef &Type, const NamedDecl *Decl, const SourceManager &SM) const = 0; /// Overridden by derived classes, returns information about if and how a Index: clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp +++ clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp @@ -400,11 +400,11 @@ // Get type text of variable declarations. const auto &SrcMgr = Decl->getASTContext().getSourceManager(); - const char *szBegin = SrcMgr.getCharacterData(Decl->getBeginLoc()); - const char *szCurr = SrcMgr.getCharacterData(Decl->getLocation()); - const intptr_t iPtrLen = szCurr - szBegin; - if (iPtrLen > 0) { - std::string Type(szBegin, iPtrLen); + const char *Begin = SrcMgr.getCharacterData(Decl->getBeginLoc()); + const char *Curr = SrcMgr.getCharacterData(Decl->getLocation()); + const intptr_t StrLen = Curr - Begin; + if (StrLen > 0) { + std::string Type(Begin, StrLen); const static std::list Keywords = { // Qualifier @@ -415,23 +415,23 @@ "constexpr", "constinit", "const_cast", "consteval"}; // Remove keywords - for (const auto &kw : Keywords) { - for (size_t pos = 0; - (pos = Type.find(kw, pos)) != std::string::npos;) { - Type.replace(pos, kw.length(), ""); + for (const auto &Kw : Keywords) { + for (size_t Pos = 0; + (Pos = Type.find(Kw, Pos)) != std::string::npos;) { + Type.replace(Pos, Kw.length(), ""); } } // Replace spaces with single space - for (size_t pos = 0; (pos = Type.find(" ", pos)) != std::string::npos; - pos += strlen(" ")) { - Type.replace(pos, strlen(" "), " "); + for (size_t Pos = 0; (Pos = Type.find(" ", Pos)) != std::string::npos; + Pos += strlen(" ")) { + Type.replace(Pos, strlen(" "), " "); } // Replace " *" with "*" - for (size_t pos = 0; (pos = Type.find(" *", pos)) != std::string::npos; - pos += strlen("*")) { - Type.replace(pos, strlen(" *"), "*"); + for (size_t Pos = 0; (Pos = Type.find(" *", Pos)) != std::string::npos; + Pos += strlen("*")) { + Type.replace(Pos, strlen(" *"), "*"); } Type = Type.erase(Type.find_last_not_of(" ") + 1); @@ -460,7 +460,7 @@ return; Optional MaybeFailure = - GetDeclFailureInfo(TypeName, Decl, *Result.SourceManager); + getDeclFailureInfo(TypeName, Decl, *Result.SourceManager); if (!MaybeFailure) return; FailureInfo &Info = *MaybeFailure; Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -67,10 +67,13 @@ Improvements to clang-tidy -------------------------- +* Added: Add IdentifierNamingCheck::CaseType, CT_HungarianNotation, supporting naming check with Hungarian notation. + + Changes in existing checks ^^^^^^^^^^^^^^^^^^^^^^^^^^ -- Improved :doc:`readability-identifier-naming +- Improved: doc:`readability-identifier-naming ` check. Added an option `GetConfigPerFile` to support including files which use