clang-tools  10.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);
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(const SourceManager &SM, Preprocessor *PP,
43  Preprocessor *ModuleExpanderPP) override;
44  void onEndOfTranslationUnit() override;
45 
46  enum CaseType {
54  };
55 
56  struct NamingStyle {
57  NamingStyle() = default;
58 
59  NamingStyle(llvm::Optional<CaseType> Case, const std::string &Prefix,
60  const std::string &Suffix)
61  : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
62 
63  llvm::Optional<CaseType> Case;
64  std::string Prefix;
65  std::string Suffix;
66  };
67 
68  /// This enum will be used in %select of the diagnostic message.
69  /// Each value below IgnoreFailureThreshold should have an error message.
70  enum class ShouldFixStatus {
71  ShouldFix,
72  ConflictsWithKeyword, /// The fixup will conflict with a language keyword,
73  /// so we can't fix it automatically.
74  ConflictsWithMacroDefinition, /// The fixup will conflict with a macro
75  /// definition, so we can't fix it
76  /// automatically.
77 
78  /// Values pass this threshold will be ignored completely
79  /// i.e no message, no fixup.
80  IgnoreFailureThreshold,
81 
82  InsideMacro, /// If the identifier was used or declared within a macro we
83  /// won't offer a fixup for safety reasons.
84  };
85 
86  /// Holds an identifier name check failure, tracking the kind of the
87  /// identifier, its possible fixup and the starting locations of all the
88  /// identifier usages.
90  std::string KindName;
91  std::string Fixup;
92 
93  /// Whether the failure should be fixed or not.
94  ///
95  /// ie: if the identifier was used or declared within a macro we won't offer
96  /// a fixup for safety reasons.
97  bool ShouldFix() const { return FixStatus == ShouldFixStatus::ShouldFix; }
98 
99  bool ShouldNotify() const {
100  return FixStatus < ShouldFixStatus::IgnoreFailureThreshold;
101  }
102 
104 
105  /// A set of all the identifier usages starting SourceLocation, in
106  /// their encoded form.
107  llvm::DenseSet<unsigned> RawUsageLocs;
108 
109  NamingCheckFailure() = default;
110  };
111 
112  typedef std::pair<SourceLocation, std::string> NamingCheckId;
113 
114  typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure>
116 
117  /// Check Macros for style violations.
118  void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok,
119  const MacroInfo *MI);
120 
121  /// Add a usage of a macro if it already has a violation.
122  void expandMacro(const Token &MacroNameTok, const MacroInfo *MI);
123 
124 private:
125  std::vector<llvm::Optional<NamingStyle>> NamingStyles;
126  bool IgnoreFailedSplit;
127  NamingCheckFailureMap NamingCheckFailures;
128 };
129 
130 } // namespace readability
131 } // namespace tidy
132 } // namespace clang
133 
134 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
Holds an identifier name check failure, tracking the kind of the identifier, its possible fixup and t...
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.
bool ShouldFix() const
Whether the failure should be fixed or not.
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
The fixup will conflict with a macro definition, so we can&#39;t fix it automatically.
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.
ShouldFixStatus
This enum will be used in select of the diagnostic message.
llvm::DenseSet< unsigned > RawUsageLocs
A set of all the identifier usages starting SourceLocation, in their encoded form.
Checks for identifiers naming style mismatch.