clang-tools  4.0.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 {
53  };
54 
55  struct NamingStyle {
57 
58  NamingStyle(CaseType Case, const std::string &Prefix,
59  const std::string &Suffix)
60  : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
61 
63  std::string Prefix;
64  std::string Suffix;
65 
66  bool isSet() const {
67  return !(Case == CT_AnyCase && Prefix.empty() && Suffix.empty());
68  }
69  };
70 
71  /// \brief Holds an identifier name check failure, tracking the kind of the
72  /// identifer, its possible fixup and the starting locations of all the
73  /// identifier usages.
75  std::string KindName;
76  std::string Fixup;
77 
78  /// \brief Whether the failure should be fixed or not.
79  ///
80  /// ie: if the identifier was used or declared within a macro we won't offer
81  /// a fixup for safety reasons.
82  bool ShouldFix;
83 
84  /// \brief A set of all the identifier usages starting SourceLocation, in
85  /// their encoded form.
86  llvm::DenseSet<unsigned> RawUsageLocs;
87 
89  };
90 
91  typedef std::pair<SourceLocation, std::string> NamingCheckId;
92 
93  typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure>
95 
96  /// Check Macros for style violations.
97  void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok,
98  const MacroInfo *MI);
99 
100  /// Add a usage of a macro if it already has a violation.
101  void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
102 
103 private:
104  std::vector<NamingStyle> NamingStyles;
105  bool IgnoreFailedSplit;
106  NamingCheckFailureMap NamingCheckFailures;
107 };
108 
109 } // namespace readability
110 } // namespace tidy
111 } // namespace clang
112 
113 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
const std::string Name
Definition: USRFinder.cpp:164
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
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:127
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:87
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:162
bool ShouldFix
Whether the failure should be fixed or not.