comparison clang-tools-extra/clang-tidy/readability/IdentifierNamingCheck.h @ 236:c4bab56944e8 llvm-original

LLVM 16
author kono
date Wed, 09 Nov 2022 17:45:10 +0900
parents 79ff65ed7e25
children 1f2b6ac9f198
comparison
equal deleted inserted replaced
232:70dce7da266c 236:c4bab56944e8
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H 10 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
11 11
12 #include "../utils/RenamerClangTidyCheck.h" 12 #include "../utils/RenamerClangTidyCheck.h"
13 #include "llvm/ADT/Optional.h" 13 #include "llvm/ADT/Optional.h"
14 namespace clang { 14 namespace clang {
15
16 class MacroInfo;
17
18 namespace tidy { 15 namespace tidy {
19 namespace readability { 16 namespace readability {
17
18 enum StyleKind : int;
20 19
21 /// Checks for identifiers naming style mismatch. 20 /// Checks for identifiers naming style mismatch.
22 /// 21 ///
23 /// This check will try to enforce coding guidelines on the identifiers naming. 22 /// This check will try to enforce coding guidelines on the identifiers naming.
24 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing 23 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
46 CT_CamelCase, 45 CT_CamelCase,
47 CT_CamelSnakeCase, 46 CT_CamelSnakeCase,
48 CT_CamelSnakeBack 47 CT_CamelSnakeBack
49 }; 48 };
50 49
50 enum HungarianPrefixType {
51 HPT_Off = 0,
52 HPT_On,
53 HPT_LowerCase,
54 HPT_CamelCase,
55 };
56
57 struct HungarianNotationOption {
58 HungarianNotationOption() : HPType(HungarianPrefixType::HPT_Off) {}
59
60 llvm::Optional<CaseType> Case;
61 HungarianPrefixType HPType;
62 llvm::StringMap<std::string> General;
63 llvm::StringMap<std::string> CString;
64 llvm::StringMap<std::string> PrimitiveType;
65 llvm::StringMap<std::string> UserDefinedType;
66 llvm::StringMap<std::string> DerivedType;
67 };
68
51 struct NamingStyle { 69 struct NamingStyle {
52 NamingStyle() = default; 70 NamingStyle() = default;
53 71
54 NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix, 72 NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
55 const std::string &Suffix, const std::string &IgnoredRegexpStr); 73 const std::string &Suffix, const std::string &IgnoredRegexpStr,
74 HungarianPrefixType HPType);
56 NamingStyle(const NamingStyle &O) = delete; 75 NamingStyle(const NamingStyle &O) = delete;
57 NamingStyle &operator=(NamingStyle &&O) = default; 76 NamingStyle &operator=(NamingStyle &&O) = default;
58 NamingStyle(NamingStyle &&O) = default; 77 NamingStyle(NamingStyle &&O) = default;
59 78
60 llvm::Optional<CaseType> Case; 79 llvm::Optional<CaseType> Case;
62 std::string Suffix; 81 std::string Suffix;
63 // Store both compiled and non-compiled forms so original value can be 82 // Store both compiled and non-compiled forms so original value can be
64 // serialized 83 // serialized
65 llvm::Regex IgnoredRegexp; 84 llvm::Regex IgnoredRegexp;
66 std::string IgnoredRegexpStr; 85 std::string IgnoredRegexpStr;
86
87 HungarianPrefixType HPType;
88 };
89
90 struct HungarianNotation {
91 public:
92 bool checkOptionValid(int StyleKindIndex) const;
93 bool isOptionEnabled(StringRef OptionKey,
94 const llvm::StringMap<std::string> &StrMap) const;
95 void loadDefaultConfig(
96 IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
97 void loadFileConfig(
98 const ClangTidyCheck::OptionsView &Options,
99 IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
100
101 bool removeDuplicatedPrefix(
102 SmallVector<StringRef, 8> &Words,
103 const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
104
105 std::string getPrefix(
106 const Decl *D,
107 const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
108
109 std::string getDataTypePrefix(
110 StringRef TypeName, const NamedDecl *ND,
111 const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
112
113 std::string getClassPrefix(
114 const CXXRecordDecl *CRD,
115 const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
116
117 std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
118 std::string getDeclTypeName(const NamedDecl *ND) const;
67 }; 119 };
68 120
69 struct FileStyle { 121 struct FileStyle {
70 FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {} 122 FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
71 FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles, 123 FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles,
72 bool IgnoreMainLike) 124 HungarianNotationOption HNOption, bool IgnoreMainLike)
73 : Styles(std::move(Styles)), IsActive(true), 125 : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
74 IgnoreMainLikeFunctions(IgnoreMainLike) {} 126 IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
75 127
76 ArrayRef<Optional<NamingStyle>> getStyles() const { 128 ArrayRef<Optional<NamingStyle>> getStyles() const {
77 assert(IsActive); 129 assert(IsActive);
78 return Styles; 130 return Styles;
79 } 131 }
132
133 const HungarianNotationOption &getHNOption() const {
134 assert(IsActive);
135 return HNOption;
136 }
137
80 bool isActive() const { return IsActive; } 138 bool isActive() const { return IsActive; }
81 bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; } 139 bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
82 140
83 private: 141 private:
84 SmallVector<Optional<NamingStyle>, 0> Styles; 142 SmallVector<Optional<NamingStyle>, 0> Styles;
143 HungarianNotationOption HNOption;
85 bool IsActive; 144 bool IsActive;
86 bool IgnoreMainLikeFunctions; 145 bool IgnoreMainLikeFunctions;
87 }; 146 };
88 147
148 IdentifierNamingCheck::FileStyle
149 getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const;
150
151 bool
152 matchesStyle(StringRef Type, StringRef Name,
153 const IdentifierNamingCheck::NamingStyle &Style,
154 const IdentifierNamingCheck::HungarianNotationOption &HNOption,
155 const NamedDecl *Decl) const;
156
157 std::string
158 fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
159 const IdentifierNamingCheck::NamingStyle &Style,
160 const IdentifierNamingCheck::HungarianNotationOption &HNOption,
161 IdentifierNamingCheck::CaseType Case) const;
162
163 std::string
164 fixupWithStyle(StringRef Type, StringRef Name,
165 const IdentifierNamingCheck::NamingStyle &Style,
166 const IdentifierNamingCheck::HungarianNotationOption &HNOption,
167 const Decl *D) const;
168
169 StyleKind findStyleKind(
170 const NamedDecl *D,
171 ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
172 bool IgnoreMainLikeFunctions) const;
173
174 llvm::Optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
175 StringRef Type, StringRef Name, const NamedDecl *ND,
176 SourceLocation Location,
177 ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
178 const IdentifierNamingCheck::HungarianNotationOption &HNOption,
179 StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
180
181 bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
182 bool IncludeMainLike) const;
183
89 private: 184 private:
90 llvm::Optional<FailureInfo> 185 llvm::Optional<FailureInfo>
91 GetDeclFailureInfo(const NamedDecl *Decl, 186 getDeclFailureInfo(const NamedDecl *Decl,
92 const SourceManager &SM) const override; 187 const SourceManager &SM) const override;
93 llvm::Optional<FailureInfo> 188 llvm::Optional<FailureInfo>
94 GetMacroFailureInfo(const Token &MacroNameTok, 189 getMacroFailureInfo(const Token &MacroNameTok,
95 const SourceManager &SM) const override; 190 const SourceManager &SM) const override;
96 DiagInfo GetDiagInfo(const NamingCheckId &ID, 191 DiagInfo getDiagInfo(const NamingCheckId &ID,
97 const NamingCheckFailure &Failure) const override; 192 const NamingCheckFailure &Failure) const override;
98 193
99 const FileStyle &getStyleForFile(StringRef FileName) const; 194 const FileStyle &getStyleForFile(StringRef FileName) const;
100 195
101 /// Stores the style options as a vector, indexed by the specified \ref 196 /// Stores the style options as a vector, indexed by the specified \ref
102 /// StyleKind, for a given directory. 197 /// StyleKind, for a given directory.
103 mutable llvm::StringMap<FileStyle> NamingStylesCache; 198 mutable llvm::StringMap<FileStyle> NamingStylesCache;
104 FileStyle *MainFileStyle; 199 FileStyle *MainFileStyle;
105 ClangTidyContext *const Context; 200 ClangTidyContext *Context;
106 const std::string CheckName; 201 const StringRef CheckName;
107 const bool GetConfigPerFile; 202 const bool GetConfigPerFile;
108 const bool IgnoreFailedSplit; 203 const bool IgnoreFailedSplit;
204 HungarianNotation HungarianNotation;
109 }; 205 };
110 206
111 } // namespace readability 207 } // namespace readability
112 template <> 208 template <>
113 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> { 209 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {