| 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 |
namespace clang::tidy { |
| 15 |
namespace readability { |
| 16 |
|
| 17 |
enum 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. |
| 32 |
class IdentifierNamingCheck final : public RenamerClangTidyCheck { |
| 33 |
public: |
| 34 |
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context); |
| 35 |
~IdentifierNamingCheck(); |
| 36 |
|
| 37 |
void storeOptions(ClangTidyOptions::OptionMap &Opts) override; |
| 38 |
|
| 39 |
enum CaseType { |
| 40 |
CT_AnyCase = 0, |
| 41 |
CT_LowerCase, |
| 42 |
CT_CamelBack, |
| 43 |
CT_UpperCase, |
| 44 |
CT_CamelCase, |
| 45 |
CT_CamelSnakeCase, |
| 46 |
CT_CamelSnakeBack |
| 47 |
}; |
| 48 |
|
| 49 |
enum HungarianPrefixType { |
| 50 |
HPT_Off = 0, |
| 51 |
HPT_On, |
| 52 |
HPT_LowerCase, |
| 53 |
HPT_CamelCase, |
| 54 |
}; |
| 55 |
|
| 56 |
struct HungarianNotationOption { |
| 57 |
HungarianNotationOption() : HPType(HungarianPrefixType::HPT_Off) {} |
| 58 |
|
| 59 |
std::optional<CaseType> Case; |
| 60 |
HungarianPrefixType HPType; |
| 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, const std::string &Prefix, |
| 72 |
const std::string &Suffix, const std::string &IgnoredRegexpStr, |
| 73 |
HungarianPrefixType HPType); |
| 74 |
NamingStyle(const NamingStyle &O) = delete; |
| 75 |
NamingStyle &operator=(NamingStyle &&O) = default; |
| 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 |
|
| 86 |
HungarianPrefixType HPType; |
| 87 |
}; |
| 88 |
|
| 89 |
struct HungarianNotation { |
| 90 |
public: |
| 91 |
bool checkOptionValid(int StyleKindIndex) const; |
| 92 |
bool isOptionEnabled(StringRef OptionKey, |
| 93 |
const llvm::StringMap<std::string> &StrMap) const; |
| 94 |
void loadDefaultConfig( |
| 95 |
IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 96 |
void loadFileConfig( |
| 97 |
const ClangTidyCheck::OptionsView &Options, |
| 98 |
IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 99 |
|
| 100 |
bool removeDuplicatedPrefix( |
| 101 |
SmallVector<StringRef, 8> &Words, |
| 102 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 103 |
|
| 104 |
std::string getPrefix( |
| 105 |
const Decl *D, |
| 106 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 107 |
|
| 108 |
std::string getDataTypePrefix( |
| 109 |
StringRef TypeName, const NamedDecl *ND, |
| 110 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 111 |
|
| 112 |
std::string getClassPrefix( |
| 113 |
const CXXRecordDecl *CRD, |
| 114 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption) const; |
| 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 |
|
| 132 |
const HungarianNotationOption &getHNOption() const { |
| 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; |
| 142 |
HungarianNotationOption HNOption; |
| 143 |
bool IsActive; |
| 144 |
bool IgnoreMainLikeFunctions; |
| 145 |
}; |
| 146 |
|
| 147 |
IdentifierNamingCheck::FileStyle |
| 148 |
getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const; |
| 149 |
|
| 150 |
bool |
| 151 |
matchesStyle(StringRef Type, StringRef Name, |
| 152 |
const IdentifierNamingCheck::NamingStyle &Style, |
| 153 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption, |
| 154 |
const NamedDecl *Decl) const; |
| 155 |
|
| 156 |
std::string |
| 157 |
fixupWithCase(StringRef Type, StringRef Name, const Decl *D, |
| 158 |
const IdentifierNamingCheck::NamingStyle &Style, |
| 159 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption, |
| 160 |
IdentifierNamingCheck::CaseType Case) const; |
| 161 |
|
| 162 |
std::string |
| 163 |
fixupWithStyle(StringRef Type, StringRef Name, |
| 164 |
const IdentifierNamingCheck::NamingStyle &Style, |
| 165 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption, |
| 166 |
const Decl *D) const; |
| 167 |
|
| 168 |
StyleKind findStyleKind( |
| 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, |
| 177 |
const IdentifierNamingCheck::HungarianNotationOption &HNOption, |
| 178 |
StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const; |
| 179 |
|
| 180 |
bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl, |
| 181 |
bool IncludeMainLike) const; |
| 182 |
|
| 183 |
private: |
| 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 |
| 207 |
template <> |
| 208 |
struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> { |
| 209 |
static llvm::ArrayRef< |
| 210 |
std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>> |
| 211 |
getEnumMapping(); |
| 212 |
}; |
| 213 |
} // namespace clang::tidy |
| 214 |
|
| 215 |
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H |