IdentifierNamingCheck.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
  9. #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
  10. #include "../utils/RenamerClangTidyCheck.h"
  11. #include <optional>
  12. namespace clang::tidy {
  13. namespace readability {
  14. enum StyleKind : int;
  15. /// Checks for identifiers naming style mismatch.
  16. ///
  17. /// This check will try to enforce coding guidelines on the identifiers naming.
  18. /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
  19. /// and tries to convert from one to another if a mismatch is detected.
  20. ///
  21. /// It also supports a fixed prefix and suffix that will be prepended or
  22. /// appended to the identifiers, regardless of the casing.
  23. ///
  24. /// Many configuration options are available, in order to be able to create
  25. /// different rules for different kind of identifier. In general, the
  26. /// rules are falling back to a more generic rule if the specific case is not
  27. /// configured.
  28. class IdentifierNamingCheck final : public RenamerClangTidyCheck {
  29. public:
  30. IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
  31. ~IdentifierNamingCheck();
  32. void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
  33. enum CaseType {
  34. CT_AnyCase = 0,
  35. CT_LowerCase,
  36. CT_CamelBack,
  37. CT_UpperCase,
  38. CT_CamelCase,
  39. CT_CamelSnakeCase,
  40. CT_CamelSnakeBack
  41. };
  42. enum HungarianPrefixType {
  43. HPT_Off = 0,
  44. HPT_On,
  45. HPT_LowerCase,
  46. HPT_CamelCase,
  47. };
  48. struct HungarianNotationOption {
  49. HungarianNotationOption() : HPType(HungarianPrefixType::HPT_Off) {}
  50. std::optional<CaseType> Case;
  51. HungarianPrefixType HPType;
  52. llvm::StringMap<std::string> General;
  53. llvm::StringMap<std::string> CString;
  54. llvm::StringMap<std::string> PrimitiveType;
  55. llvm::StringMap<std::string> UserDefinedType;
  56. llvm::StringMap<std::string> DerivedType;
  57. };
  58. struct NamingStyle {
  59. NamingStyle() = default;
  60. NamingStyle(std::optional<CaseType> Case, const std::string &Prefix,
  61. const std::string &Suffix, const std::string &IgnoredRegexpStr,
  62. HungarianPrefixType HPType);
  63. NamingStyle(const NamingStyle &O) = delete;
  64. NamingStyle &operator=(NamingStyle &&O) = default;
  65. NamingStyle(NamingStyle &&O) = default;
  66. std::optional<CaseType> Case;
  67. std::string Prefix;
  68. std::string Suffix;
  69. // Store both compiled and non-compiled forms so original value can be
  70. // serialized
  71. llvm::Regex IgnoredRegexp;
  72. std::string IgnoredRegexpStr;
  73. HungarianPrefixType HPType;
  74. };
  75. struct HungarianNotation {
  76. public:
  77. bool checkOptionValid(int StyleKindIndex) const;
  78. bool isOptionEnabled(StringRef OptionKey,
  79. const llvm::StringMap<std::string> &StrMap) const;
  80. void loadDefaultConfig(
  81. IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  82. void loadFileConfig(
  83. const ClangTidyCheck::OptionsView &Options,
  84. IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  85. bool removeDuplicatedPrefix(
  86. SmallVector<StringRef, 8> &Words,
  87. const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  88. std::string getPrefix(
  89. const Decl *D,
  90. const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  91. std::string getDataTypePrefix(
  92. StringRef TypeName, const NamedDecl *ND,
  93. const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  94. std::string getClassPrefix(
  95. const CXXRecordDecl *CRD,
  96. const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
  97. std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
  98. std::string getDeclTypeName(const NamedDecl *ND) const;
  99. };
  100. struct FileStyle {
  101. FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
  102. FileStyle(SmallVectorImpl<std::optional<NamingStyle>> &&Styles,
  103. HungarianNotationOption HNOption, bool IgnoreMainLike)
  104. : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
  105. IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
  106. ArrayRef<std::optional<NamingStyle>> getStyles() const {
  107. assert(IsActive);
  108. return Styles;
  109. }
  110. const HungarianNotationOption &getHNOption() const {
  111. assert(IsActive);
  112. return HNOption;
  113. }
  114. bool isActive() const { return IsActive; }
  115. bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
  116. private:
  117. SmallVector<std::optional<NamingStyle>, 0> Styles;
  118. HungarianNotationOption HNOption;
  119. bool IsActive;
  120. bool IgnoreMainLikeFunctions;
  121. };
  122. IdentifierNamingCheck::FileStyle
  123. getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const;
  124. bool
  125. matchesStyle(StringRef Type, StringRef Name,
  126. const IdentifierNamingCheck::NamingStyle &Style,
  127. const IdentifierNamingCheck::HungarianNotationOption &HNOption,
  128. const NamedDecl *Decl) const;
  129. std::string
  130. fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
  131. const IdentifierNamingCheck::NamingStyle &Style,
  132. const IdentifierNamingCheck::HungarianNotationOption &HNOption,
  133. IdentifierNamingCheck::CaseType Case) const;
  134. std::string
  135. fixupWithStyle(StringRef Type, StringRef Name,
  136. const IdentifierNamingCheck::NamingStyle &Style,
  137. const IdentifierNamingCheck::HungarianNotationOption &HNOption,
  138. const Decl *D) const;
  139. StyleKind findStyleKind(
  140. const NamedDecl *D,
  141. ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
  142. bool IgnoreMainLikeFunctions) const;
  143. std::optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
  144. StringRef Type, StringRef Name, const NamedDecl *ND,
  145. SourceLocation Location,
  146. ArrayRef<std::optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
  147. const IdentifierNamingCheck::HungarianNotationOption &HNOption,
  148. StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
  149. bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
  150. bool IncludeMainLike) const;
  151. private:
  152. std::optional<FailureInfo>
  153. getDeclFailureInfo(const NamedDecl *Decl,
  154. const SourceManager &SM) const override;
  155. std::optional<FailureInfo>
  156. getMacroFailureInfo(const Token &MacroNameTok,
  157. const SourceManager &SM) const override;
  158. DiagInfo getDiagInfo(const NamingCheckId &ID,
  159. const NamingCheckFailure &Failure) const override;
  160. const FileStyle &getStyleForFile(StringRef FileName) const;
  161. /// Stores the style options as a vector, indexed by the specified \ref
  162. /// StyleKind, for a given directory.
  163. mutable llvm::StringMap<FileStyle> NamingStylesCache;
  164. FileStyle *MainFileStyle;
  165. ClangTidyContext *Context;
  166. const StringRef CheckName;
  167. const bool GetConfigPerFile;
  168. const bool IgnoreFailedSplit;
  169. HungarianNotation HungarianNotation;
  170. };
  171. } // namespace readability
  172. template <>
  173. struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
  174. static llvm::ArrayRef<
  175. std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
  176. getEnumMapping();
  177. };
  178. } // namespace clang::tidy
  179. #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H