clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 namespace tidy {
17 namespace readability {
18
19 /// Checks for identifiers naming style mismatch.
20 ///
21 /// This check will try to enforce coding guidelines on the identifiers naming.
22 /// It supports `lower_case`, `UPPER_CASE`, `camelBack` and `CamelCase` casing
23 /// and tries to convert from one to another if a mismatch is detected.
24 ///
25 /// It also supports a fixed prefix and suffix that will be prepended or
26 /// appended to the identifiers, regardless of the casing.
27 ///
28 /// Many configuration options are available, in order to be able to create
29 /// different rules for different kind of identifier. In general, the
30 /// rules are falling back to a more generic rule if the specific case is not
31 /// configured.
32 class IdentifierNamingCheck : public ClangTidyCheck {
33 public:
34 IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context);
35
36 void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
37 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
38 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
39 void onEndOfTranslationUnit() override;
40
41 enum CaseType {
42 CT_AnyCase = 0,
43 CT_LowerCase,
44 CT_CamelBack,
45 CT_UpperCase,
46 CT_CamelCase,
47 };
48
49 struct NamingStyle {
50 NamingStyle() : Case(CT_AnyCase) {}
51
52 NamingStyle(CaseType Case, const std::string &Prefix,
53 const std::string &Suffix)
54 : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
55
56 CaseType Case;
57 std::string Prefix;
58 std::string Suffix;
59
60 bool isSet() const {
61 return !(Case == CT_AnyCase && Prefix.empty() && Suffix.empty());
62 }
63 };
64
65 /// \brief Holds an identifier name check failure, tracking the kind of the
66 /// identifer, its possible fixup and the starting locations of all the
67 /// idenfiier usages.
68 struct NamingCheckFailure {
69 std::string KindName;
70 std::string Fixup;
71
72 /// \brief Whether the failure should be fixed or not.
73 ///
74 /// ie: if the identifier was used or declared within a macro we won't offer
75 /// a fixup for safety reasons.
76 bool ShouldFix;
77
78 /// \brief A set of all the identifier usages starting SourceLocation, in
79 /// their encoded form.
80 llvm::DenseSet<unsigned> RawUsageLocs;
81
82 NamingCheckFailure() : ShouldFix(true) {}
83 };
84 typedef llvm::DenseMap<const NamedDecl *, NamingCheckFailure>
85 NamingCheckFailureMap;
86
87 private:
88 std::vector<NamingStyle> NamingStyles;
89 bool IgnoreFailedSplit;
90 NamingCheckFailureMap NamingCheckFailures;
91 };
92
93 } // namespace readability
94 } // namespace tidy
95 } // namespace clang
96
97 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::NamingCheckFailure
NamingCheckFailure()
Definition: IdentifierNamingCheck.h:82
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure
Holds an identifier name check failure, tracking the kind of the identifer, its possible fixup and th...
Definition: IdentifierNamingCheck.h:68
clang::tidy::readability::IdentifierNamingCheck::CT_LowerCase
Definition: IdentifierNamingCheck.h:43
clang::tidy::readability::IdentifierNamingCheck::CaseType
CaseType
Definition: IdentifierNamingCheck.h:41
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:521
clang::tidy::readability::IdentifierNamingCheck::IdentifierNamingCheck
IdentifierNamingCheck(StringRef Name, ClangTidyContext *Context)
Definition: IdentifierNamingCheck.cpp:86
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:102
clang::tidy::readability::IdentifierNamingCheck::CT_AnyCase
Definition: IdentifierNamingCheck.h:42
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::NamingStyle
NamingStyle(CaseType Case, const std::string &Prefix, const std::string &Suffix)
Definition: IdentifierNamingCheck.h:52
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Suffix
std::string Suffix
Definition: IdentifierNamingCheck.h:58
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::NamingStyle
NamingStyle()
Definition: IdentifierNamingCheck.h:50
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::KindName
std::string KindName
Definition: IdentifierNamingCheck.h:69
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::isSet
bool isSet() const
Definition: IdentifierNamingCheck.h:60
clang::tidy::readability::IdentifierNamingCheck::CT_CamelCase
Definition: IdentifierNamingCheck.h:46
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Case
CaseType Case
Definition: IdentifierNamingCheck.h:56
clang::tidy::readability::IdentifierNamingCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
Definition: IdentifierNamingCheck.cpp:138
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:82
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::Fixup
std::string Fixup
Definition: IdentifierNamingCheck.h:70
clang::tidy::readability::IdentifierNamingCheck::onEndOfTranslationUnit
void onEndOfTranslationUnit() override
Definition: IdentifierNamingCheck.cpp:655
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:108
clang::tidy::readability::IdentifierNamingCheck::CT_UpperCase
Definition: IdentifierNamingCheck.h:45
clang::tidy::readability::IdentifierNamingCheck::NamingStyle
Definition: IdentifierNamingCheck.h:49
clang::tidy::readability::IdentifierNamingCheck::NamingStyle::Prefix
std::string Prefix
Definition: IdentifierNamingCheck.h:57
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::readability::IdentifierNamingCheck::CT_CamelBack
Definition: IdentifierNamingCheck.h:44
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:80
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailureMap
llvm::DenseMap< const NamedDecl *, NamingCheckFailure > NamingCheckFailureMap
Definition: IdentifierNamingCheck.h:85
clang::tidy::readability::IdentifierNamingCheck
Checks for identifiers naming style mismatch.
Definition: IdentifierNamingCheck.h:32
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121
clang::tidy::readability::IdentifierNamingCheck::NamingCheckFailure::ShouldFix
bool ShouldFix
Whether the failure should be fixed or not.
Definition: IdentifierNamingCheck.h:76