clang-tools  15.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 namespace tidy {
16 namespace readability {
17 
18 enum 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.
34 public:
35  IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
37 
38  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39 
40  enum CaseType {
48  };
49 
51  HPT_Off = 0,
55  };
56 
59 
60  llvm::Optional<CaseType> Case;
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 
69  struct NamingStyle {
70  NamingStyle() = default;
71 
72  NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
73  const std::string &Suffix, const std::string &IgnoredRegexpStr,
75  NamingStyle(const NamingStyle &O) = delete;
76  NamingStyle &operator=(NamingStyle &&O) = default;
77  NamingStyle(NamingStyle &&O) = default;
78 
79  llvm::Optional<CaseType> Case;
80  std::string Prefix;
81  std::string Suffix;
82  // Store both compiled and non-compiled forms so original value can be
83  // serialized
84  llvm::Regex IgnoredRegexp;
85  std::string IgnoredRegexpStr;
86 
88  };
89 
91  public:
92  bool checkOptionValid(int StyleKindIndex) const;
93  bool isOptionEnabled(StringRef OptionKey,
94  const llvm::StringMap<std::string> &StrMap) const;
95  void loadDefaultConfig(
97  void loadFileConfig(
100 
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;
119  };
120 
121  struct FileStyle {
122  FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
123  FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles,
124  HungarianNotationOption HNOption, bool IgnoreMainLike)
125  : Styles(std::move(Styles)), HNOption(std::move(HNOption)),
126  IsActive(true), IgnoreMainLikeFunctions(IgnoreMainLike) {}
127 
128  ArrayRef<Optional<NamingStyle>> getStyles() const {
129  assert(IsActive);
130  return Styles;
131  }
132 
134  assert(IsActive);
135  return HNOption;
136  }
137 
138  bool isActive() const { return IsActive; }
139  bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
140 
141  private:
142  SmallVector<Optional<NamingStyle>, 0> Styles;
143  HungarianNotationOption HNOption;
144  bool IsActive;
145  bool IgnoreMainLikeFunctions;
146  };
147 
150 
151  bool
152  matchesStyle(StringRef Type, StringRef Name,
155  const NamedDecl *Decl) const;
156 
157  std::string
158  fixupWithCase(StringRef Type, StringRef Name, const Decl *D,
162 
163  std::string
164  fixupWithStyle(StringRef Type, StringRef Name,
167  const Decl *D) const;
168 
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,
179  StyleKind SK, const SourceManager &SM, bool IgnoreFailedSplit) const;
180 
181  bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl,
182  bool IncludeMainLike) const;
183 
184 private:
185  llvm::Optional<FailureInfo>
186  getDeclFailureInfo(const NamedDecl *Decl,
187  const SourceManager &SM) const override;
188  llvm::Optional<FailureInfo>
189  getMacroFailureInfo(const Token &MacroNameTok,
190  const SourceManager &SM) const override;
191  DiagInfo getDiagInfo(const NamingCheckId &ID,
192  const NamingCheckFailure &Failure) const override;
193 
194  const FileStyle &getStyleForFile(StringRef FileName) const;
195 
196  /// Stores the style options as a vector, indexed by the specified \ref
197  /// StyleKind, for a given directory.
198  mutable llvm::StringMap<FileStyle> NamingStylesCache;
199  FileStyle *MainFileStyle;
200  ClangTidyContext *Context;
201  const StringRef CheckName;
202  const bool GetConfigPerFile;
203  const bool IgnoreFailedSplit;
204  HungarianNotation HungarianNotation;
205 };
206 
207 } // namespace readability
208 template <>
209 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
210  static llvm::ArrayRef<
211  std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
212  getEnumMapping();
213 };
214 } // namespace tidy
215 } // namespace clang
216 
217 #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:693
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::CString
llvm::StringMap< std::string > CString
Definition: IdentifierNamingCheck.h:63
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::isOptionEnabled
bool isOptionEnabled(StringRef OptionKey, const llvm::StringMap< std::string > &StrMap) const
Definition: IdentifierNamingCheck.cpp:418
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:128
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::readability::IdentifierNamingCheck::CT_UpperCase
@ CT_UpperCase
Definition: IdentifierNamingCheck.h:44
Location
Definition: Modularize.cpp:381
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeBack
@ CT_CamelSnakeBack
Definition: IdentifierNamingCheck.h:47
clang::tidy::readability::IdentifierNamingCheck::FileStyle::isIgnoringMainLikeFunction
bool isIgnoringMainLikeFunction() const
Definition: IdentifierNamingCheck.h:139
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation
Definition: IdentifierNamingCheck.h:90
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::IgnoredRegexpStr
std::string IgnoredRegexpStr
Definition: IdentifierNamingCheck.h:85
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Prefix
std::string Prefix
Definition: IdentifierNamingCheck.h:80
clang::tidy::readability::IdentifierNamingCheck::HPT_CamelCase
@ HPT_CamelCase
Definition: IdentifierNamingCheck.h:54
clang::tidy::readability::IdentifierNamingCheck::FileStyle::getHNOption
const HungarianNotationOption & getHNOption() const
Definition: IdentifierNamingCheck.h:133
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:29
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::IgnoredRegexp
llvm::Regex IgnoredRegexp
Definition: IdentifierNamingCheck.h:84
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:871
clang::tidy::readability::IdentifierNamingCheck::IdentifierNamingCheck
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context)
Definition: IdentifierNamingCheck.cpp:389
clang::tidy::readability::IdentifierNamingCheck::findStyleKind
StyleKind findStyleKind(const NamedDecl *D, ArrayRef< llvm::Optional< IdentifierNamingCheck::NamingStyle >> NamingStyles, bool IgnoreMainLikeFunctions) const
Definition: IdentifierNamingCheck.cpp:1069
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::HPType
HungarianPrefixType HPType
Definition: IdentifierNamingCheck.h:87
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getDataTypePrefix
std::string getDataTypePrefix(StringRef TypeName, const NamedDecl *ND, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:547
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::removeDuplicatedPrefix
bool removeDuplicatedPrefix(SmallVector< StringRef, 8 > &Words, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:524
clang::tidy::readability::IdentifierNamingCheck::HPT_On
@ HPT_On
Definition: IdentifierNamingCheck.h:52
clang::tidy::readability::IdentifierNamingCheck::CT_AnyCase
@ CT_AnyCase
Definition: IdentifierNamingCheck.h:41
clang::tidy::readability::IdentifierNamingCheck::FileStyle::FileStyle
FileStyle(SmallVectorImpl< Optional< NamingStyle >> &&Styles, HungarianNotationOption HNOption, bool IgnoreMainLike)
Definition: IdentifierNamingCheck.h:123
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Suffix
std::string Suffix
Definition: IdentifierNamingCheck.h:81
clang::tidy::readability::IdentifierNamingCheck::HPT_Off
@ HPT_Off
Definition: IdentifierNamingCheck.h:51
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:835
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:501
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getDeclTypeName
std::string getDeclTypeName(const NamedDecl *ND) const
Definition: IdentifierNamingCheck.cpp:288
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::HPType
HungarianPrefixType HPType
Definition: IdentifierNamingCheck.h:61
clang::tidy::readability::IdentifierNamingCheck::CaseType
CaseType
Definition: IdentifierNamingCheck.h:40
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getClassPrefix
std::string getClassPrefix(const CXXRecordDecl *CRD, const IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:636
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::Case
llvm::Optional< CaseType > Case
Definition: IdentifierNamingCheck.h:60
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::DerivedType
llvm::StringMap< std::string > DerivedType
Definition: IdentifierNamingCheck.h:66
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:415
clang::tidy::readability::IdentifierNamingCheck::HPT_LowerCase
@ HPT_LowerCase
Definition: IdentifierNamingCheck.h:53
clang::tidy::readability::IdentifierNamingCheck::FileStyle
Definition: IdentifierNamingCheck.h:121
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::General
llvm::StringMap< std::string > General
Definition: IdentifierNamingCheck.h:62
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:67
clang::tidy::readability::IdentifierNamingCheck::CT_CamelCase
@ CT_CamelCase
Definition: IdentifierNamingCheck.h:45
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::loadFileConfig
void loadFileConfig(const ClangTidyCheck::OptionsView &Options, IdentifierNamingCheck::HungarianNotationOption &HNOption) const
Definition: IdentifierNamingCheck.cpp:431
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
FileName
StringRef FileName
Definition: KernelNameRestrictionCheck.cpp:46
clang::tidy::readability::IdentifierNamingCheck::NamingStyle
Definition: IdentifierNamingCheck.h:69
clang::tidy::readability::StyleKind
StyleKind
Definition: IdentifierNamingCheck.cpp:125
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeCase
@ CT_CamelSnakeCase
Definition: IdentifierNamingCheck.h:46
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) const
Definition: IdentifierNamingCheck.cpp:406
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:1039
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Case
llvm::Optional< CaseType > Case
Definition: IdentifierNamingCheck.h:79
clang::tidy::readability::IdentifierNamingCheck
Checks for identifiers naming style mismatch.
Definition: IdentifierNamingCheck.h:33
clang::tidy::readability::IdentifierNamingCheck::CT_LowerCase
@ CT_LowerCase
Definition: IdentifierNamingCheck.h:42
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::IdentifierNamingCheck::HungarianPrefixType
HungarianPrefixType
Definition: IdentifierNamingCheck.h:50
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::UserDefinedType
llvm::StringMap< std::string > UserDefinedType
Definition: IdentifierNamingCheck.h:65
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::PrimitiveType
llvm::StringMap< std::string > PrimitiveType
Definition: IdentifierNamingCheck.h:64
clang::tidy::readability::IdentifierNamingCheck::FileStyle::FileStyle
FileStyle()
Definition: IdentifierNamingCheck.h:122
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption::HungarianNotationOption
HungarianNotationOption()
Definition: IdentifierNamingCheck.h:58
clang::tidy::readability::IdentifierNamingCheck::HungarianNotation::getEnumPrefix
std::string getEnumPrefix(const EnumConstantDecl *ECD) const
Definition: IdentifierNamingCheck.cpp:650
clang::tidy::readability::IdentifierNamingCheck::CT_CamelBack
@ CT_CamelBack
Definition: IdentifierNamingCheck.h:43
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:1357
clang::tidy::readability::IdentifierNamingCheck::HungarianNotationOption
Definition: IdentifierNamingCheck.h:57
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:802
clang::tidy::readability::IdentifierNamingCheck::FileStyle::isActive
bool isActive() const
Definition: IdentifierNamingCheck.h:138
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition: ClangTidyCheck.h:139
clang::tidy::readability::IdentifierNamingCheck::isParamInMainLikeFunction
bool isParamInMainLikeFunction(const ParmVarDecl &ParmDecl, bool IncludeMainLike) const
Definition: IdentifierNamingCheck.cpp:975