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 namespace clang {
14 
15 class MacroInfo;
16 
17 namespace tidy {
18 namespace readability {
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.
33 class IdentifierNamingCheck final : public RenamerClangTidyCheck {
34 public:
35   IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
36   ~IdentifierNamingCheck();
37 
38   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39 
40   enum CaseType {
41     CT_AnyCase = 0,
42     CT_LowerCase,
43     CT_CamelBack,
44     CT_UpperCase,
45     CT_CamelCase,
46     CT_CamelSnakeCase,
47     CT_CamelSnakeBack
48   };
49 
50   struct NamingStyle {
51     NamingStyle() = default;
52 
NamingStyleNamingStyle53     NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
54                 const std::string &Suffix)
55         : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
56 
57     llvm::Optional<CaseType> Case;
58     std::string Prefix;
59     std::string Suffix;
60   };
61 
62 private:
63   llvm::Optional<FailureInfo>
64   GetDeclFailureInfo(const NamedDecl *Decl,
65                      const SourceManager &SM) const override;
66   llvm::Optional<FailureInfo>
67   GetMacroFailureInfo(const Token &MacroNameTok,
68                       const SourceManager &SM) const override;
69   DiagInfo GetDiagInfo(const NamingCheckId &ID,
70                        const NamingCheckFailure &Failure) const override;
71 
72   std::vector<llvm::Optional<NamingStyle>> NamingStyles;
73   const bool IgnoreFailedSplit;
74   const bool IgnoreMainLikeFunctions;
75 };
76 
77 } // namespace readability
78 template <>
79 struct OptionEnumMapping<readability::IdentifierNamingCheck::CaseType> {
80   static llvm::ArrayRef<
81       std::pair<readability::IdentifierNamingCheck::CaseType, StringRef>>
82   getEnumMapping();
83 };
84 } // namespace tidy
85 } // namespace clang
86 
87 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
88