clang-tools  14.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 "llvm/ADT/Optional.h"
14 namespace clang {
15 
16 class MacroInfo;
17 
18 namespace tidy {
19 namespace readability {
20 
21 enum StyleKind : int;
22 
23 /// Checks for identifiers naming style mismatch.
24 ///
25 /// This check will try to enforce coding guidelines on the identifiers naming.
26 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
27 /// and tries to convert from one to another if a mismatch is detected.
28 ///
29 /// It also supports a fixed prefix and suffix that will be prepended or
30 /// appended to the identifiers, regardless of the casing.
31 ///
32 /// Many configuration options are available, in order to be able to create
33 /// different rules for different kind of identifier. In general, the
34 /// rules are falling back to a more generic rule if the specific case is not
35 /// configured.
37 public:
38  IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
40 
41  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
42 
43  enum CaseType {
51  };
52 
54  HPT_Off = 0,
58  };
59 
62 
63  llvm::Optional<CaseType> Case;
65  llvm::StringMap<std::string> General;
66  llvm::StringMap<std::string> CString;
67  llvm::StringMap<std::string> PrimitiveType;
68  llvm::StringMap<std::string> UserDefinedType;
69  llvm::StringMap<std::string> DerivedType;
70  };
71 
72  struct NamingStyle {
73  NamingStyle() = default;
74 
75  NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
76  const std::string &Suffix, const std::string &IgnoredRegexpStr,
78  NamingStyle(const NamingStyle &O) = delete;
79  NamingStyle &operator=(NamingStyle &&O) = default;
80  NamingStyle(NamingStyle &&O) = default;
81 
82  llvm::Optional<CaseType> Case;
83  std::string Prefix;
84  std::string Suffix;
85  // Store both compiled and non-compiled forms so original value can be
86  // serialized
87  llvm::Regex IgnoredRegexp;
88  std::string IgnoredRegexpStr;
89 
91  };
92 
94  public:
95  bool checkOptionValid(int StyleKindIndex, StringRef StyleString) const;
96  bool isOptionEnabled(StringRef OptionKey,
97  const llvm::StringMap<std::string> &StrMap) const;
98  void loadDefaultConfig(
100  void loadFileConfig(
103 
105  SmallVector<StringRef, 8> &Words,
106  const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
107 
108  std::string getPrefix(
109  const Decl *D,
110  const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
111 
112  std::string getDataTypePrefix(
113  StringRef TypeName, const NamedDecl *ND,
114  const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
115 
116  std::string getClassPrefix(
117  const CXXRecordDecl *CRD,
118  const IdentifierNamingCheck::HungarianNotationOption &HNOption) const;
119 
120  std::string getEnumPrefix(const EnumConstantDecl *ECD) const;
121  std::string getDeclTypeName(const NamedDecl *ND) const;
122  };
123 
124  struct FileStyle {
125  FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
126  FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles,
127  HungarianNotationOption HNOption, bool IgnoreMainLike)
128  : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
129  IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
130 
131  ArrayRef<Optional<NamingStyle>> getStyles() const {
132  assert(IsActive);
133  return Styles;
134  }
135 
137  assert(IsActive);
138  return HNOption;
139  }
140 
141  bool isActive() const { return IsActive; }
142  bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
143 
144  private:
145  SmallVector<Optional<NamingStyle>, 0> Styles;
146  HungarianNotationOption HNOption;
147  bool IsActive;
148  bool IgnoreMainLikeFunctions;
149  };
150 
153 
154  bool
155  matchesStyle(StringRef Type, StringRef Name,
158  const NamedDecl *Decl) const;
159 
160  std::string
161  fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
165 
166  std::string
167  fixupWithStyle(StringRef Type, StringRef Name,
170  const Decl *D) const;
171 
173  const NamedDecl *D,
174  ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
175  bool IgnoreMainLikeFunctions) const;
176 
177  llvm::Optional<RenamerClangTidyCheck::FailureInfo> getFailureInfo(
178  StringRef Type, StringRef Name, const NamedDecl *ND,
179  SourceLocation Location,
180  ArrayRef<llvm::Optional<IdentifierNamingCheck::NamingStyle>> NamingStyles,
182  StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
183 
184  bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
185  bool IncludeMainLike) const;
186 
187 private:
188  llvm::Optional<FailureInfo>
189  getDeclFailureInfo(const NamedDecl *Decl,
190  const SourceManager &SM) const override;
191  llvm::Optional<FailureInfo>
192  getMacroFailureInfo(const Token &MacroNameTok,
193  const SourceManager &SM) const override;
194  DiagInfo getDiagInfo(const NamingCheckId &ID,
195  const NamingCheckFailure &Failure) const override;
196 
197  const FileStyle &getStyleForFile(StringRef FileName) const;
198 
199  /// Stores the style options as a vector, indexed by the specified \ref
200  /// StyleKind, for a given directory.
201  mutable llvm::StringMap<FileStyle> NamingStylesCache;
202  FileStyle *MainFileStyle;
203  ClangTidyContext *Context;
204  const std::string CheckName;
205  const bool GetConfigPerFile;
206  const bool IgnoreFailedSplit;
207  HungarianNotation HungarianNotation;
208 };
209 
210 } // namespace readability
211 template <>
212 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
213  static llvm::ArrayRef<
214  std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
215  getEnumMapping();
216 };
217 } // namespace tidy
218 } // namespace clang
219 
220 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::loadDefaultConfig
void loadDefaultConfig(IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:683
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::CString
llvm::StringMap< std::string > CString
Definition: IdentifierNamingCheck.h:66
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::isOptionEnabled
bool isOptionEnabled(StringRef OptionKey, const llvm::StringMap< std::string > &StrMap) const
Definition: IdentifierNamingCheck.cpp:422
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::operator=
NamingStyle & operator=(NamingStyle &&O)=default
clang::tidy::readability::IdentifierNamingCheck::FileStyle::getStyles
ArrayRef< Optional< NamingStyle > > getStyles() const
Definition: IdentifierNamingCheck.h:131
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::IdentifierNamingCheck::CT_UpperCase
@ CT_UpperCase
Definition: IdentifierNamingCheck.h:47
Location
Definition: Modularize.cpp:382
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeBack
@ CT_CamelSnakeBack
Definition: IdentifierNamingCheck.h:50
clang::tidy::readability::IdentifierNamingCheck::FileStyle::isIgnoringMainLikeFunction
bool isIgnoringMainLikeFunction() const
Definition: IdentifierNamingCheck.h:142
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation
Definition: IdentifierNamingCheck.h:93
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::IgnoredRegexpStr
std::string IgnoredRegexpStr
Definition: IdentifierNamingCheck.h:88
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Prefix
std::string Prefix
Definition: IdentifierNamingCheck.h:83
clang::tidy::readability::IdentifierNamingCheck::HPT_CamelCase
@ HPT_CamelCase
Definition: IdentifierNamingCheck.h:57
clang::tidy::readability::IdentifierNamingCheck::FileStyle::getHNOption
const HungarianNotationOption & getHNOption() const
Definition: IdentifierNamingCheck.h:136
clang::tidy::readability::IdentifierNamingCheck::~IdentifierNamingCheck
~IdentifierNamingCheck()
clang::tidy::OptionEnumMapping
This class should be specialized by any enum type that needs to be converted to and from an llvm::Str...
Definition: ClangTidyCheck.h:30
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::IgnoredRegexp
llvm::Regex IgnoredRegexp
Definition: IdentifierNamingCheck.h:87
clang::tidy::readability::IdentifierNamingCheck::fixupWithCase
std::string fixupWithCase(StringRef Type, StringRef Name, const Decl *D, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, IdentifierNamingCheck::CaseType Case) const
Definition: IdentifierNamingCheck.cpp:862
clang::tidy::readability::IdentifierNamingCheck::IdentifierNamingCheck
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context)
Definition: IdentifierNamingCheck.cpp:393
clang::tidy::readability::IdentifierNamingCheck::findStyleKind
StyleKind findStyleKind(const NamedDecl *D, ArrayRef< llvm::Optional< IdentifierNamingCheck::NamingStyle >> NamingStyles, bool IgnoreMainLikeFunctions) const
Definition: IdentifierNamingCheck.cpp:1060
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::HPType
HungarianPrefixType HPType
Definition: IdentifierNamingCheck.h:90
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getDataTypePrefix
std::string getDataTypePrefix(StringRef TypeName, const NamedDecl *ND, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:537
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::removeDuplicatedPrefix
bool removeDuplicatedPrefix(SmallVector< StringRef, 8 > &Words, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:514
clang::tidy::readability::IdentifierNamingCheck::HPT_On
@ HPT_On
Definition: IdentifierNamingCheck.h:55
clang::tidy::readability::IdentifierNamingCheck::CT_AnyCase
@ CT_AnyCase
Definition: IdentifierNamingCheck.h:44
clang::tidy::readability::IdentifierNamingCheck::FileStyle::FileStyle
FileStyle(SmallVectorImpl< Optional< NamingStyle >> &&Styles, HungarianNotationOption HNOption, bool IgnoreMainLike)
Definition: IdentifierNamingCheck.h:126
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Suffix
std::string Suffix
Definition: IdentifierNamingCheck.h:84
clang::tidy::readability::IdentifierNamingCheck::HPT_Off
@ HPT_Off
Definition: IdentifierNamingCheck.h:54
clang::tidy::readability::IdentifierNamingCheck::matchesStyle
bool matchesStyle(StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const NamedDecl *Decl) const
Definition: IdentifierNamingCheck.cpp:826
clang::tidy::readability::IdentifierNamingCheck::getFileStyleFromOptions
IdentifierNamingCheck::FileStyle getFileStyleFromOptions(const ClangTidyCheck::OptionsView &Options) const
Definition: IdentifierNamingCheck.cpp:245
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getPrefix
std::string getPrefix(const Decl *D, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:491
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getDeclTypeName
std::string getDeclTypeName(const NamedDecl *ND) const
Definition: IdentifierNamingCheck.cpp:291
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::HPType
HungarianPrefixType HPType
Definition: IdentifierNamingCheck.h:64
clang::tidy::readability::IdentifierNamingCheck::CaseType
CaseType
Definition: IdentifierNamingCheck.h:43
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getClassPrefix
std::string getClassPrefix(const CXXRecordDecl *CRD, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:626
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::Case
llvm::Optional< CaseType > Case
Definition: IdentifierNamingCheck.h:63
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::DerivedType
llvm::StringMap< std::string > DerivedType
Definition: IdentifierNamingCheck.h:69
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::readability::IdentifierNamingCheck::HPT_LowerCase
@ HPT_LowerCase
Definition: IdentifierNamingCheck.h:56
clang::tidy::readability::IdentifierNamingCheck::FileStyle
Definition: IdentifierNamingCheck.h:124
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::General
llvm::StringMap< std::string > General
Definition: IdentifierNamingCheck.h:65
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::readability::IdentifierNamingCheck::CT_CamelCase
@ CT_CamelCase
Definition: IdentifierNamingCheck.h:48
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::loadFileConfig
void loadFileConfig(const ClangTidyCheck::OptionsView &Options, IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:435
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::tidy::readability::IdentifierNamingCheck::NamingStyle
Definition: IdentifierNamingCheck.h:72
clang::tidy::readability::StyleKind
StyleKind
Definition: IdentifierNamingCheck.cpp:125
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeCase
@ CT_CamelSnakeCase
Definition: IdentifierNamingCheck.h:49
clang::tidy::RenamerClangTidyCheck
Base class for clang-tidy checks that want to flag declarations and/or macros for renaming based on c...
Definition: RenamerClangTidyCheck.h:28
clang::tidy::OptionEnumMapping::getEnumMapping
static ArrayRef< std::pair< T, StringRef > > getEnumMapping()=delete
clang::tidy::RenamerClangTidyCheck::NamingCheckId
std::pair< SourceLocation, std::string > NamingCheckId
Definition: RenamerClangTidyCheck.h:106
ID
static char ID
Definition: Logger.cpp:74
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::checkOptionValid
bool checkOptionValid(int StyleKindIndex, StringRef StyleString) const
Definition: IdentifierNamingCheck.cpp:410
clang::tidy::readability::IdentifierNamingCheck::fixupWithStyle
std::string fixupWithStyle(StringRef Type, StringRef Name, const IdentifierNamingCheck::NamingStyle &Style, const IdentifierNamingCheck::HungarianNotationOption &HNOption, const Decl *D) const
Definition: IdentifierNamingCheck.cpp:1030
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Case
llvm::Optional< CaseType > Case
Definition: IdentifierNamingCheck.h:82
clang::tidy::readability::IdentifierNamingCheck
Checks for identifiers naming style mismatch.
Definition: IdentifierNamingCheck.h:36
clang::tidy::readability::IdentifierNamingCheck::CT_LowerCase
@ CT_LowerCase
Definition: IdentifierNamingCheck.h:45
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::IdentifierNamingCheck::HungarianPrefixType
HungarianPrefixType
Definition: IdentifierNamingCheck.h:53
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::UserDefinedType
llvm::StringMap< std::string > UserDefinedType
Definition: IdentifierNamingCheck.h:68
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::PrimitiveType
llvm::StringMap< std::string > PrimitiveType
Definition: IdentifierNamingCheck.h:67
clang::tidy::readability::IdentifierNamingCheck::FileStyle::FileStyle
FileStyle()
Definition: IdentifierNamingCheck.h:125
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::HungarianNotationOption
HungarianNotationOption()
Definition: IdentifierNamingCheck.h:61
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getEnumPrefix
std::string getEnumPrefix(const EnumConstantDecl *ECD) const
Definition: IdentifierNamingCheck.cpp:640
clang::tidy::readability::IdentifierNamingCheck::CT_CamelBack
@ CT_CamelBack
Definition: IdentifierNamingCheck.h:46
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::NamingStyle
NamingStyle()=default
clang::tidy::readability::IdentifierNamingCheck::getFailureInfo
llvm::Optional< RenamerClangTidyCheck::FailureInfo > getFailureInfo(StringRef Type, StringRef Name, const NamedDecl *ND, SourceLocation Location, ArrayRef< llvm::Optional< IdentifierNamingCheck::NamingStyle >> NamingStyles, const IdentifierNamingCheck::HungarianNotationOption &HNOption, StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const
Definition: IdentifierNamingCheck.cpp:1348
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption
Definition: IdentifierNamingCheck.h:60
clang::tidy::readability::IdentifierNamingCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: IdentifierNamingCheck.cpp:792
clang::tidy::readability::IdentifierNamingCheck::FileStyle::isActive
bool isActive() const
Definition: IdentifierNamingCheck.h:141
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition: ClangTidyCheck.h:140
clang::tidy::readability::IdentifierNamingCheck::isParamInMainLikeFunction
bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl, bool IncludeMainLike) const
Definition: IdentifierNamingCheck.cpp:966