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 @@ -60,6 +60,15 @@ std::string Suffix; }; + struct FileStyle { + FileStyle(); + FileStyle(SmallVectorImpl> &&Styles, + bool IgnoreMainLike) + : Styles(std::move(Styles)), IgnoreMainLikeFunctions(IgnoreMainLike) {} + SmallVector, 0> Styles; + bool IgnoreMainLikeFunctions; + }; + private: llvm::Optional GetDeclFailureInfo(const NamedDecl *Decl, @@ -70,19 +79,16 @@ DiagInfo GetDiagInfo(const NamingCheckId &ID, const NamingCheckFailure &Failure) const override; - ArrayRef> - getStyleForFile(StringRef FileName) const; + const FileStyle &getStyleForFile(StringRef FileName) const; /// Stores the style options as a vector, indexed by the specified \ref /// StyleKind, for a given directory. - mutable llvm::StringMap>> - NamingStylesCache; - ArrayRef> MainFileStyle; + mutable llvm::StringMap NamingStylesCache; + FileStyle *MainFileStyle; ClangTidyContext *const Context; const std::string CheckName; const bool GetConfigPerFile; const bool IgnoreFailedSplit; - const bool IgnoreMainLikeFunctions; }; } // namespace readability 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 @@ -122,9 +122,9 @@ #undef NAMING_KEYS // clang-format on -static std::vector> -getNamingStyles(const ClangTidyCheck::OptionsView &Options) { - std::vector> Styles( +static IdentifierNamingCheck::FileStyle +getFileStyle(const ClangTidyCheck::OptionsView &Options) { + SmallVector, 0> Styles( SK_Count); SmallString<64> StyleString; for (unsigned I = 0; I < SK_Count; ++I) { @@ -145,50 +145,54 @@ Styles[I].emplace(std::move(CaseOptional), std::move(Prefix), std::move(Postfix)); } - return Styles; + bool IgnoreMainLike = Options.get("IgnoreMainLikeFunctions", false); + return {std::move(Styles), IgnoreMainLike}; } IdentifierNamingCheck::IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context) : RenamerClangTidyCheck(Name, Context), Context(Context), CheckName(Name), GetConfigPerFile(Options.get("GetConfigPerFile", true)), - IgnoreFailedSplit(Options.get("IgnoreFailedSplit", false)), - IgnoreMainLikeFunctions(Options.get("IgnoreMainLikeFunctions", false)) { + IgnoreFailedSplit(Options.get("IgnoreFailedSplit", false)) { auto IterAndInserted = NamingStylesCache.try_emplace( llvm::sys::path::parent_path(Context->getCurrentFile()), - getNamingStyles(Options)); + getFileStyle(Options)); assert(IterAndInserted.second && "Couldn't insert Style"); // Holding a reference to the data in the vector is safe as it should never // move. - MainFileStyle = IterAndInserted.first->getValue(); + MainFileStyle = &IterAndInserted.first->getValue(); } +IdentifierNamingCheck::FileStyle::FileStyle() + : Styles(SK_Count), IgnoreMainLikeFunctions(false) {} + IdentifierNamingCheck::~IdentifierNamingCheck() = default; void IdentifierNamingCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { RenamerClangTidyCheck::storeOptions(Opts); SmallString<64> StyleString; for (size_t I = 0; I < SK_Count; ++I) { - if (!MainFileStyle[I]) + if (!MainFileStyle->Styles[I]) continue; StyleString = StyleNames[I]; size_t StyleSize = StyleString.size(); StyleString.append("Prefix"); - Options.store(Opts, StyleString, MainFileStyle[I]->Prefix); + Options.store(Opts, StyleString, MainFileStyle->Styles[I]->Prefix); // Fast replacement of [Pre]fix -> [Suf]fix. memcpy(&StyleString[StyleSize], "Suf", 3); - Options.store(Opts, StyleString, MainFileStyle[I]->Suffix); - if (MainFileStyle[I]->Case) { + Options.store(Opts, StyleString, MainFileStyle->Styles[I]->Suffix); + if (MainFileStyle->Styles[I]->Case) { memcpy(&StyleString[StyleSize], "Case", 4); StyleString.pop_back(); StyleString.pop_back(); - Options.store(Opts, StyleString, *MainFileStyle[I]->Case); + Options.store(Opts, StyleString, *MainFileStyle->Styles[I]->Case); } } Options.store(Opts, "GetConfigPerFile", GetConfigPerFile); Options.store(Opts, "IgnoreFailedSplit", IgnoreFailedSplit); - Options.store(Opts, "IgnoreMainLikeFunctions", IgnoreMainLikeFunctions); + Options.store(Opts, "IgnoreMainLikeFunctions", + MainFileStyle->IgnoreMainLikeFunctions); } static bool matchesStyle(StringRef Name, @@ -397,7 +401,7 @@ if (isa(D) && NamingStyles[SK_ObjcIvar]) return SK_ObjcIvar; - + if (isa(D) && NamingStyles[SK_Typedef]) return SK_Typedef; @@ -499,7 +503,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]) @@ -512,8 +517,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; @@ -531,7 +537,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]) @@ -540,7 +547,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]) @@ -556,7 +564,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]) @@ -564,8 +573,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]) @@ -704,13 +714,12 @@ IdentifierNamingCheck::GetDeclFailureInfo(const NamedDecl *Decl, const SourceManager &SM) const { SourceLocation Loc = Decl->getLocation(); - ArrayRef> NamingStyles = - getStyleForFile(SM.getFilename(Loc)); + const FileStyle &FileStyle = getStyleForFile(SM.getFilename(Loc)); return getFailureInfo( - Decl->getName(), Loc, NamingStyles, - findStyleKind(Decl, NamingStyles, IgnoreMainLikeFunctions), SM, - IgnoreFailedSplit); + Decl->getName(), Loc, FileStyle.Styles, + findStyleKind(Decl, FileStyle.Styles, FileStyle.IgnoreMainLikeFunctions), + SM, IgnoreFailedSplit); } llvm::Optional @@ -719,7 +728,7 @@ SourceLocation Loc = MacroNameTok.getLocation(); return getFailureInfo(MacroNameTok.getIdentifierInfo()->getName(), Loc, - getStyleForFile(SM.getFilename(Loc)), + getStyleForFile(SM.getFilename(Loc)).Styles, SK_MacroDefinition, SM, IgnoreFailedSplit); } @@ -732,19 +741,26 @@ }}; } -ArrayRef> +const IdentifierNamingCheck::FileStyle & IdentifierNamingCheck::getStyleForFile(StringRef FileName) const { if (!GetConfigPerFile) - return MainFileStyle; - auto &Styles = NamingStylesCache[llvm::sys::path::parent_path(FileName)]; - if (Styles.empty()) { - ClangTidyOptions Options = Context->getOptionsForFile(FileName); - if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) - Styles = getNamingStyles({CheckName, Options.CheckOptions}); - else - Styles.resize(SK_Count, None); + return *MainFileStyle; + StringRef Parent = llvm::sys::path::parent_path(FileName); + auto Iter = NamingStylesCache.find(Parent); + if (Iter != NamingStylesCache.end()) + return Iter->getValue(); + + ClangTidyOptions Options = Context->getOptionsForFile(FileName); + if (Options.Checks && GlobList(*Options.Checks).contains(CheckName)) { + auto It = NamingStylesCache.try_emplace( + Parent, getFileStyle({CheckName, Options.CheckOptions})); + assert(It.second); + return It.first->getValue(); } - return Styles; + // Default construction gives an empty style. + auto It = NamingStylesCache.try_emplace(Parent); + assert(It.second); + return It.first->getValue(); } } // namespace readability