clang-tools 17.0.0git
IdentifierNamingCheck.h
Go to the documentation of this file.
1//===--- IdentifierNamingCheck.h - clang-tidy -------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
11
12#include "../utils/RenamerClangTidyCheck.h"
13#include <optional>
14namespace clang::tidy {
15namespace readability {
16
17enum StyleKind : int;
18
19/// Checks for identifiers naming style mismatch.
20///
21/// This check will try to enforce coding guidelines on the identifiers naming.
22/// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
23/// and tries to convert from one to another if a mismatch is detected.
24///
25/// It also supports a fixed prefix and suffix that will be prepended or
26/// appended to the identifiers, regardless of the casing.
27///
28/// Many configuration options are available, in order to be able to create
29/// different rules for different kind of identifier. In general, the
30/// rules are falling back to a more generic rule if the specific case is not
31/// configured.
33public:
34 IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
36
37 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
38
39 enum CaseType {
47 };
48
54 };
55
58
59 std::optional<CaseType> Case;
61 llvm::StringMap<std::string> General;
62 llvm::StringMap<std::string> CString;
63 llvm::StringMap<std::string> PrimitiveType;
64 llvm::StringMap<std::string> UserDefinedType;
65 llvm::StringMap<std::string> DerivedType;
66 };
67
68 struct NamingStyle {
69 NamingStyle() = default;
70
71 NamingStyle(std::optional<CaseType> Case, StringRef Prefix,
72 StringRef Suffix, StringRef IgnoredRegexpStr,
74 NamingStyle(const NamingStyle &O) = delete;
76 NamingStyle(NamingStyle &&O) = default;
77
78 std::optional<CaseType> Case;
79 std::string Prefix;
80 std::string Suffix;
81 // Store both compiled and non-compiled forms so original value can be
82 // serialized
83 llvm::Regex IgnoredRegexp;
84 std::string IgnoredRegexpStr;
85
87 };
88
90 public:
91 bool checkOptionValid(int StyleKindIndex) const;
92 bool isOptionEnabled(StringRef OptionKey,
93 const llvm::StringMap<std::string> &StrMap) const;
96 void loadFileConfig(
99
101 SmallVector<StringRef, 8> &Words,
103
104 std::string getPrefix(
105 const Decl *D,
107
108 std::string getDataTypePrefix(
109 StringRef TypeName, const NamedDecl *ND,
111
112 std::string getClassPrefix(
113 const CXXRecordDecl *CRD,
115
116 std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
117 std::string getDeclTypeName(const NamedDecl *ND) const;
118 };
119
120 struct FileStyle {
121 FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
122 FileStyle(SmallVectorImpl<std::optional<NamingStyle>> &&Styles,
123 HungarianNotationOption HNOption, bool IgnoreMainLike)
124 : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
125 IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
126
127 ArrayRef<std::optional<NamingStyle>> getStyles() const {
128 assert(IsActive);
129 return Styles;
130 }
131
133 assert(IsActive);
134 return HNOption;
135 }
136
137 bool isActive() const { return IsActive; }
138 bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
139
140 private:
141 SmallVector<std::optional<NamingStyle>, 0> Styles;
143 bool IsActive;
144 bool IgnoreMainLikeFunctions;
145 };
146
149
150 bool
151 matchesStyle(StringRef Type, StringRef Name,
154 const NamedDecl *Decl) const;
155
156 std::string
157 fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
161
162 std::string
163 fixupWithStyle(StringRef Type, StringRef Name,
166 const Decl *D) const;
167
169 const NamedDecl *D,
170 ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
171 bool IgnoreMainLikeFunctions) const;
172
173 std::optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
174 StringRef Type, StringRef Name, const NamedDecl *ND,
175 SourceLocation Location,
176 ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
178 StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
179
180 bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
181 bool IncludeMainLike) const;
182
183private:
184 std::optional<FailureInfo>
185 getDeclFailureInfo(const NamedDecl *Decl,
186 const SourceManager &SM) const override;
187 std::optional<FailureInfo>
188 getMacroFailureInfo(const Token &MacroNameTok,
189 const SourceManager &SM) const override;
190 DiagInfo getDiagInfo(const NamingCheckId &ID,
191 const NamingCheckFailure &Failure) const override;
192
193 const FileStyle &getStyleForFile(StringRef FileName) const;
194
195 /// Stores the style options as a vector, indexed by the specified \ref
196 /// StyleKind, for a given directory.
197 mutable llvm::StringMap<FileStyle> NamingStylesCache;
198 FileStyle *MainFileStyle;
199 ClangTidyContext *Context;
200 const StringRef CheckName;
201 const bool GetConfigPerFile;
202 const bool IgnoreFailedSplit;
203 HungarianNotation HungarianNotation;
204};
205
206} // namespace readability
207template <>
208struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
209 static llvm::ArrayRef<
210 std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
212};
213} // namespace clang::tidy
214
215#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
const FunctionDecl * Decl
NodeType Type
StringRef FileName
Token Name
Provides access to the ClangTidyCheck options via check-local names.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on c...
std::pair< SourceLocation, std::string > NamingCheckId
Checks for identifiers naming style mismatch.
std::string fixupWithCase(StringRef Type, StringRef Name, const Decl *D, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, IdentifierNamingCheck::CaseType Case) const
std::optional< RenamerClangTidyCheck::FailureInfo > getFailureInfo(StringRef Type, StringRef Name, const NamedDecl *ND, SourceLocation Location, ArrayRef< std::optional< IdentifierNamingCheck::NamingStyle > > NamingStyles, const IdentifierNamingCheck::HungarianNotationOption &HNOption, StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const
StyleKind findStyleKind(const NamedDecl *D, ArrayRef< std::optional< IdentifierNamingCheck::NamingStyle > > NamingStyles, bool IgnoreMainLikeFunctions) const
std::string fixupWithStyle(StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const Decl *D) const
IdentifierNamingCheck::FileStyle getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const
bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl, bool IncludeMainLike) const
bool matchesStyle(StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const NamedDecl *Decl) const
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
llvm::StringMap< ClangTidyValue > OptionMap
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete
FileStyle(SmallVectorImpl< std::optional< NamingStyle > > &&Styles, HungarianNotationOption HNOption, bool IgnoreMainLike)
ArrayRef< std::optional< NamingStyle > > getStyles() const
bool isOptionEnabled(StringRef OptionKey, const llvm::StringMap< std::string > &StrMap) const
bool removeDuplicatedPrefix(SmallVector< StringRef, 8 > &Words, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
void loadFileConfig(const ClangTidyCheck::OptionsView &Options, IdentifierNamingCheck::HungarianNotationOption &HNOption) const
void loadDefaultConfig(IdentifierNamingCheck::HungarianNotationOption &HNOption) const
std::string getClassPrefix(const CXXRecordDecl *CRD, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
std::string getPrefix(const Decl *D, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
std::string getDataTypePrefix(StringRef TypeName, const NamedDecl *ND, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
NamingStyle(std::optional< CaseType > Case, StringRef Prefix, StringRef Suffix, StringRef IgnoredRegexpStr, HungarianPrefixType HPType)
NamingStyle & operator=(NamingStyle &&O)=default