clang-tools  9.0.0
IdentifierNamingCheck.h
Go to the documentation of this file.
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 "../ClangTidyCheck.h"
13 
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.
35 public:
36  IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
37 
38  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
39  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
40  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
41  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
42  Preprocessor *ModuleExpanderPP) override;
43  void onEndOfTranslationUnit() override;
44 
45  enum CaseType {
53  };
54 
55  struct NamingStyle {
56  NamingStyle() = default;
57 
58  NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
59  const std::string &Suffix)
60  : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
61 
62  llvm::Optional<CaseType> Case;
63  std::string Prefix;
64  std::string Suffix;
65  };
66 
67  /// \brief Holds an identifier name check failure, tracking the kind of the
68  /// identifer, its possible fixup and the starting locations of all the
69  /// identifier usages.
71  std::string KindName;
72  std::string Fixup;
73 
74  /// \brief Whether the failure should be fixed or not.
75  ///
76  /// ie: if the identifier was used or declared within a macro we won't offer
77  /// a fixup for safety reasons.
78  bool ShouldFix;
79 
80  /// \brief A set of all the identifier usages starting SourceLocation, in
81  /// their encoded form.
82  llvm::DenseSet<unsigned> RawUsageLocs;
83 
84  NamingCheckFailure() : ShouldFix(true) {}
85  };
86 
87  typedef std::pair<SourceLocation, std::string> NamingCheckId;
88 
89  typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure>
91 
92  /// Check Macros for style violations.
93  void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok,
94  const MacroInfo *MI);
95 
96  /// Add a usage of a macro if it already has a violation.
97  void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
98 
99 private:
100  std::vector<llvm::Optional<NamingStyle>> NamingStyles;
101  bool IgnoreFailedSplit;
102  NamingCheckFailureMap NamingCheckFailures;
103 };
104 
105 } // namespace readability
106 } // namespace tidy
107 } // namespace clang
108 
109 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
Holds an identifier name check failure, tracking the kind of the identifer, its possible fixup and th...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
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.
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.
static constexpr llvm::StringLiteral Name
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.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::DenseMap< NamingCheckId, NamingCheckFailure > NamingCheckFailureMap
NamingStyle(llvm::Optional< CaseType > Case, const std::string &Prefix, const std::string &Suffix)
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: Rename.cpp:36
llvm::DenseSet< unsigned > RawUsageLocs
A set of all the identifier usages starting SourceLocation, in their encoded form.
Checks for identifiers naming style mismatch.
bool ShouldFix
Whether the failure should be fixed or not.