clang-tools  3.9.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 
17 class MacroInfo;
18 
19 namespace tidy {
20 namespace readability {
21 
22 /// Checks for identifiers naming style mismatch.
23 ///
24 /// This check will try to enforce coding guidelines on the identifiers naming.
25 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
26 /// and tries to convert from one to another if a mismatch is detected.
27 ///
28 /// It also supports a fixed prefix and suffix that will be prepended or
29 /// appended to the identifiers, regardless of the casing.
30 ///
31 /// Many configuration options are available, in order to be able to create
32 /// different rules for different kind of identifier. In general, the
33 /// rules are falling back to a more generic rule if the specific case is not
34 /// configured.
36 public:
38 
39  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
40  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
41  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
42  void registerPPCallbacks(CompilerInstance &Compiler) override;
43  void onEndOfTranslationUnit() override;
44 
45  enum CaseType {
51  };
52 
53  struct NamingStyle {
55 
56  NamingStyle(CaseType Case, const std::string &Prefix,
57  const std::string &Suffix)
58  : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
59 
61  std::string Prefix;
62  std::string Suffix;
63 
64  bool isSet() const {
65  return !(Case == CT_AnyCase && Prefix.empty() && Suffix.empty());
66  }
67  };
68 
69  /// \brief Holds an identifier name check failure, tracking the kind of the
70  /// identifer, its possible fixup and the starting locations of all the
71  /// identifier usages.
73  std::string KindName;
74  std::string Fixup;
75 
76  /// \brief Whether the failure should be fixed or not.
77  ///
78  /// ie: if the identifier was used or declared within a macro we won't offer
79  /// a fixup for safety reasons.
80  bool ShouldFix;
81 
82  /// \brief A set of all the identifier usages starting SourceLocation, in
83  /// their encoded form.
84  llvm::DenseSet<unsigned> RawUsageLocs;
85 
87  };
88 
89  typedef std::pair<SourceLocation, std::string> NamingCheckId;
90 
91  typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure>
93 
94  /// Check Macros for style violations.
95  void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok,
96  const MacroInfo *MI);
97 
98  /// Add a usage of a macro if it already has a violation.
99  void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
100 
101 private:
102  std::vector<NamingStyle> NamingStyles;
103  bool IgnoreFailedSplit;
104  NamingCheckFailureMap NamingCheckFailures;
105 };
106 
107 } // namespace readability
108 } // namespace tidy
109 } // namespace clang
110 
111 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
const std::string Name
Definition: USRFinder.cpp:140
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
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:110
NamingStyle(CaseType Case, const std::string &Prefix, const std::string &Suffix)
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI)
Add a usage of a macro if it already has a violation.
std::pair< SourceLocation, std::string > NamingCheckId
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers 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...
void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok, const MacroInfo *MI)
Check Macros for style violations.
llvm::DenseMap< NamingCheckId, NamingCheckFailure > NamingCheckFailureMap
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.
Checks for identifiers naming style mismatch.
const NamedDecl * Result
Definition: USRFinder.cpp:137
bool ShouldFix
Whether the failure should be fixed or not.