clang-tools  3.8.0
IdentifierNamingCheck.h
Go to the documentation of this file.
1 //===--- IdentifierNamingCheck.h - clang-tidy -------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
12 
13 #include "../ClangTidy.h"
14 
15 namespace clang {
16 namespace tidy {
17 namespace readability {
18 
19 /// Checks for identifiers naming style mismatch.
20 ///
21 /// This check will try to enforce coding guidelines on the identifiers naming.
22 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
23 /// and tries to convert from one to another if a mismatch is detected.
24 ///
25 /// It also supports a fixed prefix and suffix that will be prepended or
26 /// appended to the identifiers, regardless of the casing.
27 ///
28 /// Many configuration options are available, in order to be able to create
29 /// different rules for different kind of identifier. In general, the
30 /// rules are falling back to a more generic rule if the specific case is not
31 /// configured.
33 public:
35 
36  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
37  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
38  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
39  void onEndOfTranslationUnit() override;
40 
41  enum CaseType {
47  };
48 
49  struct NamingStyle {
51 
52  NamingStyle(CaseType Case, const std::string &Prefix,
53  const std::string &Suffix)
54  : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
55 
57  std::string Prefix;
58  std::string Suffix;
59 
60  bool isSet() const {
61  return !(Case == CT_AnyCase && Prefix.empty() && Suffix.empty());
62  }
63  };
64 
65  /// \brief Holds an identifier name check failure, tracking the kind of the
66  /// identifer, its possible fixup and the starting locations of all the
67  /// idenfiier usages.
69  std::string KindName;
70  std::string Fixup;
71 
72  /// \brief Whether the failure should be fixed or not.
73  ///
74  /// ie: if the identifier was used or declared within a macro we won't offer
75  /// a fixup for safety reasons.
76  bool ShouldFix;
77 
78  /// \brief A set of all the identifier usages starting SourceLocation, in
79  /// their encoded form.
80  llvm::DenseSet<unsigned> RawUsageLocs;
81 
83  };
84  typedef llvm::DenseMap<const NamedDecl *, NamingCheckFailure>
86 
87 private:
88  std::vector<NamingStyle> NamingStyles;
89  bool IgnoreFailedSplit;
90  NamingCheckFailureMap NamingCheckFailures;
91 };
92 
93 } // namespace readability
94 } // namespace tidy
95 } // namespace clang
96 
97 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
StringHandle Name
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
Holds an identifier name check failure, tracking the kind of the identifer, its possible fixup and th...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context)
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
NamingStyle(CaseType Case, const std::string &Prefix, const std::string &Suffix)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
std::map< std::string, std::string > OptionMap
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::DenseSet< unsigned > RawUsageLocs
A set of all the identifier usages starting SourceLocation, in their encoded form.
llvm::DenseMap< const NamedDecl *, NamingCheckFailure > NamingCheckFailureMap
Checks for identifiers naming style mismatch.
const NamedDecl * Result
Definition: USRFinder.cpp:121
bool ShouldFix
Whether the failure should be fixed or not.