clang-tools 18.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>
14#include <string>
15namespace clang::tidy {
16namespace readability {
17
18enum StyleKind : int;
19
20/// Checks for identifiers naming style mismatch.
21///
22/// This check will try to enforce coding guidelines on the identifiers naming.
23/// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
24/// and tries to convert from one to another if a mismatch is detected.
25///
26/// It also supports a fixed prefix and suffix that will be prepended or
27/// appended to the identifiers, regardless of the casing.
28///
29/// Many configuration options are available, in order to be able to create
30/// different rules for different kind of identifier. In general, the
31/// rules are falling back to a more generic rule if the specific case is not
32/// configured.
34public:
35 IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
37
38 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39
40 enum CaseType {
49 };
50
56 };
57
60
61 std::optional<CaseType> Case;
63 llvm::StringMap<std::string> General;
64 llvm::StringMap<std::string> CString;
65 llvm::StringMap<std::string> PrimitiveType;
66 llvm::StringMap<std::string> UserDefinedType;
67 llvm::StringMap<std::string> DerivedType;
68 };
69
70 struct NamingStyle {
71 NamingStyle() = default;
72
73 NamingStyle(std::optional<CaseType> Case, StringRef Prefix,
74 StringRef Suffix, StringRef IgnoredRegexpStr,
76 NamingStyle(const NamingStyle &O) = delete;
78 NamingStyle(NamingStyle &&O) = default;
79
80 std::optional<CaseType> Case;
81 std::string Prefix;
82 std::string Suffix;
83 // Store both compiled and non-compiled forms so original value can be
84 // serialized
85 llvm::Regex IgnoredRegexp;
86 std::string IgnoredRegexpStr;
87
89 };
90
92 public:
93 bool checkOptionValid(int StyleKindIndex) const;
94 bool isOptionEnabled(StringRef OptionKey,
95 const llvm::StringMap<std::string> &StrMap) const;
96
97 size_t getAsteriskCount(const std::string &TypeName) const;
98 size_t getAsteriskCount(const std::string &TypeName,
99 const NamedDecl *ND) const;
100
103 void loadFileConfig(
106
108 SmallVector<StringRef, 8> &Words,
110
111 std::string getPrefix(
112 const Decl *D,
114
115 std::string getDataTypePrefix(
116 StringRef TypeName, const NamedDecl *ND,
118
119 std::string getClassPrefix(
120 const CXXRecordDecl *CRD,
122
123 std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
124 std::string getDeclTypeName(const NamedDecl *ND) const;
125 };
126
127 struct FileStyle {
128 FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
129 FileStyle(SmallVectorImpl<std::optional<NamingStyle>> &&Styles,
130 HungarianNotationOption HNOption, bool IgnoreMainLike)
131 : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
132 IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
133
134 ArrayRef<std::optional<NamingStyle>> getStyles() const {
135 assert(IsActive);
136 return Styles;
137 }
138
140 assert(IsActive);
141 return HNOption;
142 }
143
144 bool isActive() const { return IsActive; }
145 bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
146
147 private:
148 SmallVector<std::optional<NamingStyle>, 0> Styles;
150 bool IsActive;
151 bool IgnoreMainLikeFunctions;
152 };
153
156
157 bool
158 matchesStyle(StringRef Type, StringRef Name,
161 const NamedDecl *Decl) const;
162
163 std::string
164 fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
168
169 std::string
170 fixupWithStyle(StringRef Type, StringRef Name,
173 const Decl *D) const;
174
176 const NamedDecl *D,
177 ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
178 bool IgnoreMainLikeFunctions) const;
179
180 std::optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
181 StringRef Type, StringRef Name, const NamedDecl *ND,
182 SourceLocation Location,
183 ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
185 StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
186
187 bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
188 bool IncludeMainLike) const;
189
190private:
191 std::optional<FailureInfo>
192 getDeclFailureInfo(const NamedDecl *Decl,
193 const SourceManager &SM) const override;
194 std::optional<FailureInfo>
195 getMacroFailureInfo(const Token &MacroNameTok,
196 const SourceManager &SM) const override;
197 DiagInfo getDiagInfo(const NamingCheckId &ID,
198 const NamingCheckFailure &Failure) const override;
199
200 const FileStyle &getStyleForFile(StringRef FileName) const;
201
202 /// Stores the style options as a vector, indexed by the specified \ref
203 /// StyleKind, for a given directory.
204 mutable llvm::StringMap<FileStyle> NamingStylesCache;
205 FileStyle *MainFileStyle;
206 ClangTidyContext *Context;
207 const bool GetConfigPerFile;
208 const bool IgnoreFailedSplit;
209 HungarianNotation HungarianNotation;
210};
211
212} // namespace readability
213template <>
214struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
215 static llvm::ArrayRef<
216 std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
218};
219} // namespace clang::tidy
220
221#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
const FunctionDecl * Decl
NodeType Type
llvm::StringRef Name
StringRef FileName
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, StringRef > 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