blob: 5d25809112c90fdaca8b79f4a884da8927bf46f7 [file] [log] [blame]
Alexander Kornienko76c28802015-08-19 11:15:36 +00001//===--- 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
15namespace clang {
Alexander Kornienko21503902016-06-17 09:25:24 +000016
17class MacroInfo;
18
Alexander Kornienko76c28802015-08-19 11:15:36 +000019namespace tidy {
20namespace readability {
21
Alexander Kornienkof8ed0a82015-08-27 18:01:58 +000022/// 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.
Alexander Kornienko76c28802015-08-19 11:15:36 +000035class IdentifierNamingCheck : public ClangTidyCheck {
36public:
37 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;
Alexander Kornienko21503902016-06-17 09:25:24 +000042 void registerPPCallbacks(CompilerInstance &Compiler) override;
Alexander Kornienko76c28802015-08-19 11:15:36 +000043 void onEndOfTranslationUnit() override;
44
45 enum CaseType {
46 CT_AnyCase = 0,
47 CT_LowerCase,
48 CT_CamelBack,
49 CT_UpperCase,
50 CT_CamelCase,
Kirill Bobyrev5d8f0712016-07-20 12:28:38 +000051 CT_CamelSnakeCase,
52 CT_CamelSnakeBack
Alexander Kornienko76c28802015-08-19 11:15:36 +000053 };
54
55 struct NamingStyle {
56 NamingStyle() : Case(CT_AnyCase) {}
57
58 NamingStyle(CaseType Case, const std::string &Prefix,
59 const std::string &Suffix)
60 : Case(Case), Prefix(Prefix), Suffix(Suffix) {}
61
62 CaseType Case;
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
Alexander Kornienko3d777682015-09-28 08:59:12 +000071 /// \brief Holds an identifier name check failure, tracking the kind of the
72 /// identifer, its possible fixup and the starting locations of all the
Alexander Kornienko21503902016-06-17 09:25:24 +000073 /// identifier usages.
Alexander Kornienko76c28802015-08-19 11:15:36 +000074 struct NamingCheckFailure {
75 std::string KindName;
76 std::string Fixup;
Alexander Kornienko3d777682015-09-28 08:59:12 +000077
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.
Alexander Kornienko76c28802015-08-19 11:15:36 +000082 bool ShouldFix;
Alexander Kornienko3d777682015-09-28 08:59:12 +000083
84 /// \brief A set of all the identifier usages starting SourceLocation, in
85 /// their encoded form.
86 llvm::DenseSet<unsigned> RawUsageLocs;
Alexander Kornienko76c28802015-08-19 11:15:36 +000087
88 NamingCheckFailure() : ShouldFix(true) {}
89 };
Alexander Kornienko21503902016-06-17 09:25:24 +000090
91 typedef std::pair<SourceLocation, std::string> NamingCheckId;
92
93 typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure>
Alexander Kornienko3d777682015-09-28 08:59:12 +000094 NamingCheckFailureMap;
Alexander Kornienko76c28802015-08-19 11:15:36 +000095
Alexander Kornienko21503902016-06-17 09:25:24 +000096 /// 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
Alexander Kornienko3d777682015-09-28 08:59:12 +0000103private:
104 std::vector<NamingStyle> NamingStyles;
105 bool IgnoreFailedSplit;
106 NamingCheckFailureMap NamingCheckFailures;
Alexander Kornienko76c28802015-08-19 11:15:36 +0000107};
108
109} // namespace readability
110} // namespace tidy
111} // namespace clang
112
113#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H