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.
34 class IdentifierNamingCheck : public ClangTidyCheck {
35 public:
36 IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
37 ~IdentifierNamingCheck();
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 {
47 CT_AnyCase = 0,
48 CT_LowerCase,
49 CT_CamelBack,
50 CT_UpperCase,
51 CT_CamelCase,
52 CT_CamelSnakeCase,
53 CT_CamelSnakeBack
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.
89 struct NamingCheckFailure {
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
103 ShouldFixStatus FixStatus = ShouldFixStatus::ShouldFix;
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>
115 NamingCheckFailureMap;
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
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeBack
Definition: IdentifierNamingCheck.h:53
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure
Holds an identifier name check failure, tracking the kind of the identifier, its possible fixup and t...
Definition: IdentifierNamingCheck.h:89
clang::tidy::readability::IdentifierNamingCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: IdentifierNamingCheck.cpp:262
clang::tidy::readability::IdentifierNamingCheck::CT_CamelSnakeCase
Definition: IdentifierNamingCheck.h:52
clang::tidy::readability::IdentifierNamingCheck::CT_LowerCase
Definition: IdentifierNamingCheck.h:48
clang::tidy::readability::IdentifierNamingCheck::CaseType
CaseType
Definition: IdentifierNamingCheck.h:46
clang::tidy::readability::IdentifierNamingCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: IdentifierNamingCheck.cpp:726
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::ShouldFix
bool ShouldFix() const
Whether the failure should be fixed or not.
Definition: IdentifierNamingCheck.h:97
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Case
llvm::Optional< CaseType > Case
Definition: IdentifierNamingCheck.h:63
clang::tidy::readability::IdentifierNamingCheck::IdentifierNamingCheck
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context)
Definition: IdentifierNamingCheck.cpp:165
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::readability::IdentifierNamingCheck::CT_AnyCase
Definition: IdentifierNamingCheck.h:47
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Suffix
std::string Suffix
Definition: IdentifierNamingCheck.h:65
clang::tidy::readability::IdentifierNamingCheck::ShouldFixStatus::ShouldFix
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::KindName
std::string KindName
Definition: IdentifierNamingCheck.h:90
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::ShouldNotify
bool ShouldNotify() const
Definition: IdentifierNamingCheck.h:99
clang::tidy::readability::IdentifierNamingCheck::CT_CamelCase
Definition: IdentifierNamingCheck.h:51
clang::tidy::readability::IdentifierNamingCheck::expandMacro
void expandMacro(const Token &MacroNameTok, const MacroInfo *MI)
Add a usage of a macro if it already has a violation.
Definition: IdentifierNamingCheck.cpp:947
clang::tidy::readability::IdentifierNamingCheck::NamingCheckId
std::pair< SourceLocation, std::string > NamingCheckId
Definition: IdentifierNamingCheck.h:112
clang::tidy::readability::IdentifierNamingCheck::ShouldFixStatus::IgnoreFailureThreshold
The fixup will conflict with a macro definition, so we can&#39;t fix it automatically.
clang::tidy::readability::IdentifierNamingCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: IdentifierNamingCheck.cpp:236
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::Fixup
std::string Fixup
Definition: IdentifierNamingCheck.h:91
clang::tidy::readability::IdentifierNamingCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: IdentifierNamingCheck.cpp:960
clang::tidy::readability::IdentifierNamingCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: IdentifierNamingCheck.cpp:198
clang::tidy::readability::IdentifierNamingCheck::CT_UpperCase
Definition: IdentifierNamingCheck.h:50
clang::tidy::readability::IdentifierNamingCheck::checkMacro
void checkMacro(SourceManager &sourceMgr, const Token &MacroNameTok, const MacroInfo *MI)
Check Macros for style violations.
Definition: IdentifierNamingCheck.cpp:913
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::IdentifierNamingCheck::NamingStyle
Definition: IdentifierNamingCheck.h:56
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailureMap
llvm::DenseMap< NamingCheckId, NamingCheckFailure > NamingCheckFailureMap
Definition: IdentifierNamingCheck.h:115
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Prefix
std::string Prefix
Definition: IdentifierNamingCheck.h:64
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::NamingStyle
NamingStyle(llvm::Optional< CaseType > Case, const std::string &Prefix, const std::string &Suffix)
Definition: IdentifierNamingCheck.h:59
clang::tidy::readability::IdentifierNamingCheck::~IdentifierNamingCheck
~IdentifierNamingCheck()
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::readability::IdentifierNamingCheck::ShouldFixStatus
ShouldFixStatus
This enum will be used in select of the diagnostic message.
Definition: IdentifierNamingCheck.h:70
clang::tidy::readability::IdentifierNamingCheck::CT_CamelBack
Definition: IdentifierNamingCheck.h:49
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::RawUsageLocs
llvm::DenseSet< unsigned > RawUsageLocs
A set of all the identifier usages starting SourceLocation, in their encoded form.
Definition: IdentifierNamingCheck.h:107
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::NamingStyle
NamingStyle()=default
clang::tidy::readability::IdentifierNamingCheck
Checks for identifiers naming style mismatch.
Definition: IdentifierNamingCheck.h:34