Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 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 { |
Alexander Kornienko | 2150390 | 2016-06-17 09:25:24 +0000 | [diff] [blame] | 16 | |
| 17 | class MacroInfo; |
| 18 | |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 19 | namespace tidy { |
| 20 | namespace readability { |
| 21 | |
Alexander Kornienko | f8ed0a8 | 2015-08-27 18:01:58 +0000 | [diff] [blame] | 22 | /// 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 Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 35 | class IdentifierNamingCheck : public ClangTidyCheck { |
| 36 | public: |
| 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 Kornienko | 2150390 | 2016-06-17 09:25:24 +0000 | [diff] [blame] | 42 | void registerPPCallbacks(CompilerInstance &Compiler) override; |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 43 | void onEndOfTranslationUnit() override; |
| 44 | |
| 45 | enum CaseType { |
| 46 | CT_AnyCase = 0, |
| 47 | CT_LowerCase, |
| 48 | CT_CamelBack, |
| 49 | CT_UpperCase, |
| 50 | CT_CamelCase, |
Kirill Bobyrev | 5d8f071 | 2016-07-20 12:28:38 +0000 | [diff] [blame] | 51 | CT_CamelSnakeCase, |
| 52 | CT_CamelSnakeBack |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 53 | }; |
| 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 Kornienko | 3d77768 | 2015-09-28 08:59:12 +0000 | [diff] [blame] | 71 | /// \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 Kornienko | 2150390 | 2016-06-17 09:25:24 +0000 | [diff] [blame] | 73 | /// identifier usages. |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 74 | struct NamingCheckFailure { |
| 75 | std::string KindName; |
| 76 | std::string Fixup; |
Alexander Kornienko | 3d77768 | 2015-09-28 08:59:12 +0000 | [diff] [blame] | 77 | |
| 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 Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 82 | bool ShouldFix; |
Alexander Kornienko | 3d77768 | 2015-09-28 08:59:12 +0000 | [diff] [blame] | 83 | |
| 84 | /// \brief A set of all the identifier usages starting SourceLocation, in |
| 85 | /// their encoded form. |
| 86 | llvm::DenseSet<unsigned> RawUsageLocs; |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 87 | |
| 88 | NamingCheckFailure() : ShouldFix(true) {} |
| 89 | }; |
Alexander Kornienko | 2150390 | 2016-06-17 09:25:24 +0000 | [diff] [blame] | 90 | |
| 91 | typedef std::pair<SourceLocation, std::string> NamingCheckId; |
| 92 | |
| 93 | typedef llvm::DenseMap<NamingCheckId, NamingCheckFailure> |
Alexander Kornienko | 3d77768 | 2015-09-28 08:59:12 +0000 | [diff] [blame] | 94 | NamingCheckFailureMap; |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 95 | |
Alexander Kornienko | 2150390 | 2016-06-17 09:25:24 +0000 | [diff] [blame] | 96 | /// 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 Kornienko | 3d77768 | 2015-09-28 08:59:12 +0000 | [diff] [blame] | 103 | private: |
| 104 | std::vector<NamingStyle> NamingStyles; |
| 105 | bool IgnoreFailedSplit; |
| 106 | NamingCheckFailureMap NamingCheckFailures; |
Alexander Kornienko | 76c2880 | 2015-08-19 11:15:36 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace readability |
| 110 | } // namespace tidy |
| 111 | } // namespace clang |
| 112 | |
| 113 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_IDENTIFIERNAMINGCHECK_H |