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 /// Checks for identifiers naming style mismatch.
22 ///
23 /// This check will try to enforce coding guidelines on the identifiers naming.
24 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
25 /// and tries to convert from one to another if a mismatch is detected.
26 ///
27 /// It also supports a fixed prefix and suffix that will be prepended or
28 /// appended to the identifiers, regardless of the casing.
29 ///
30 /// Many configuration options are available, in order to be able to create
31 /// different rules for different kind of identifier. In general, the
32 /// rules are falling back to a more generic rule if the specific case is not
33 /// configured.
34 class IdentifierNamingCheck final : public RenamerClangTidyCheck {
35 public:
36   IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
37   ~IdentifierNamingCheck();
38 
39   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
40 
41   enum CaseType {
42     CT_AnyCase = 0,
43     CT_LowerCase,
44     CT_CamelBack,
45     CT_UpperCase,
46     CT_CamelCase,
47     CT_CamelSnakeCase,
48     CT_CamelSnakeBack
49   };
50 
51   struct NamingStyle {
52     NamingStyle() = default;
53 
54     NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
55                 const std::string &Suffix, const std::string &IgnoredRegexpStr);
56     NamingStyle(const NamingStyle &O) = delete;
57     NamingStyle &operator=(NamingStyle &&O) = default;
58     NamingStyle(NamingStyle &&O) = default;
59 
60     llvm::Optional<CaseType> Case;
61     std::string Prefix;
62     std::string Suffix;
63     // Store both compiled and non-compiled forms so original value can be
64     // serialized
65     llvm::Regex IgnoredRegexp;
66     std::string IgnoredRegexpStr;
67   };
68 
69   struct FileStyle {
FileStyleFileStyle70     FileStyle() : IsActive(false), IgnoreMainLikeFunctions(false) {}
FileStyleFileStyle71     FileStyle(SmallVectorImpl<Optional<NamingStyle>> &&Styles,
72               bool IgnoreMainLike)
73         : Styles(std::move(Styles)), IsActive(true),
74           IgnoreMainLikeFunctions(IgnoreMainLike) {}
75 
getStylesFileStyle76     ArrayRef<Optional<NamingStyle>> getStyles() const {
77       assert(IsActive);
78       return Styles;
79     }
isActiveFileStyle80     bool isActive() const { return IsActive; }
isIgnoringMainLikeFunctionFileStyle81     bool isIgnoringMainLikeFunction() const { return IgnoreMainLikeFunctions; }
82 
83   private:
84     SmallVector<Optional<NamingStyle>, 0> Styles;
85     bool IsActive;
86     bool IgnoreMainLikeFunctions;
87   };
88 
89 private:
90   llvm::Optional<FailureInfo>
91   GetDeclFailureInfo(const NamedDecl *Decl,
92                      const SourceManager &SM) const override;
93   llvm::Optional<FailureInfo>
94   GetMacroFailureInfo(const Token &MacroNameTok,
95                       const SourceManager &SM) const override;
96   DiagInfo GetDiagInfo(const NamingCheckId &ID,
97                        const NamingCheckFailure &Failure) const override;
98 
99   const FileStyle &getStyleForFile(StringRef FileName) const;
100 
101   /// Stores the style options as a vector, indexed by the specified \ref
102   /// StyleKind, for a given directory.
103   mutable llvm::StringMap<FileStyle> NamingStylesCache;
104   FileStyle *MainFileStyle;
105   ClangTidyContext *const Context;
106   const std::string CheckName;
107   const bool GetConfigPerFile;
108   const bool IgnoreFailedSplit;
109 };
110 
111 } // namespace readability
112 template <>
113 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
114   static llvm::ArrayRef<
115       std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
116   getEnumMapping();
117 };
118 } // namespace tidy
119 } // namespace clang
120 
121 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
122