Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 1 | //===--- PropertyDeclarationCheck.cpp - clang-tidy-------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "PropertyDeclarationCheck.h" |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 10 | #include <algorithm> |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 11 | #include "../utils/OptionsUtils.h" |
| 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 14 | #include "clang/Basic/CharInfo.h" |
Yan Zhang | ae5bc5ae | 2018-02-06 21:40:38 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/Regex.h" |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang::ast_matchers; |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace tidy { |
| 23 | namespace objc { |
| 24 | |
| 25 | namespace { |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 26 | |
| 27 | // For StandardProperty the naming style is 'lowerCamelCase'. |
| 28 | // For CategoryProperty especially in categories of system class, |
| 29 | // to avoid naming conflict, the suggested naming style is |
| 30 | // 'abc_lowerCamelCase' (adding lowercase prefix followed by '_'). |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 31 | // Regardless of the style, all acronyms and initialisms should be capitalized. |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 32 | enum NamingStyle { |
| 33 | StandardProperty = 1, |
| 34 | CategoryProperty = 2, |
| 35 | }; |
| 36 | |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 37 | /// For now we will only fix 'CamelCase' or 'abc_CamelCase' property to |
| 38 | /// 'camelCase' or 'abc_camelCase'. For other cases the users need to |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 39 | /// come up with a proper name by their own. |
| 40 | /// FIXME: provide fix for snake_case to snakeCase |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 41 | FixItHint generateFixItHint(const ObjCPropertyDecl *Decl, NamingStyle Style) { |
| 42 | auto Name = Decl->getName(); |
| 43 | auto NewName = Decl->getName().str(); |
| 44 | size_t Index = 0; |
| 45 | if (Style == CategoryProperty) { |
| 46 | Index = Name.find_first_of('_') + 1; |
| 47 | NewName.replace(0, Index - 1, Name.substr(0, Index - 1).lower()); |
| 48 | } |
| 49 | if (Index < Name.size()) { |
| 50 | NewName[Index] = tolower(NewName[Index]); |
| 51 | if (NewName != Name) { |
| 52 | return FixItHint::CreateReplacement( |
| 53 | CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())), |
| 54 | llvm::StringRef(NewName)); |
| 55 | } |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 56 | } |
| 57 | return FixItHint(); |
| 58 | } |
| 59 | |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 60 | std::string validPropertyNameRegex(bool UsedInMatcher) { |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 61 | // Allow any of these names: |
| 62 | // foo |
| 63 | // fooBar |
| 64 | // url |
| 65 | // urlString |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 66 | // ID |
| 67 | // IDs |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 68 | // URL |
| 69 | // URLString |
Yan Zhang | 8c298d2 | 2018-01-17 00:19:35 +0000 | [diff] [blame] | 70 | // bundleID |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 71 | // CIColor |
| 72 | // |
| 73 | // Disallow names of this form: |
| 74 | // LongString |
| 75 | // |
| 76 | // aRbITRaRyCapS is allowed to avoid generating false positives for names |
| 77 | // like isVitaminBSupplement, CProgrammingLanguage, and isBeforeM. |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 78 | std::string StartMatcher = UsedInMatcher ? "::" : "^"; |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 79 | return StartMatcher + "([a-z]|[A-Z][A-Z0-9])[a-z0-9A-Z]*$"; |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool hasCategoryPropertyPrefix(llvm::StringRef PropertyName) { |
| 83 | auto RegexExp = llvm::Regex("^[a-zA-Z]+_[a-zA-Z0-9][a-zA-Z0-9_]+$"); |
| 84 | return RegexExp.match(PropertyName); |
| 85 | } |
| 86 | |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 87 | bool prefixedPropertyNameValid(llvm::StringRef PropertyName) { |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 88 | size_t Start = PropertyName.find_first_of('_'); |
| 89 | assert(Start != llvm::StringRef::npos && Start + 1 < PropertyName.size()); |
| 90 | auto Prefix = PropertyName.substr(0, Start); |
| 91 | if (Prefix.lower() != Prefix) { |
| 92 | return false; |
| 93 | } |
| 94 | auto RegexExp = |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 95 | llvm::Regex(llvm::StringRef(validPropertyNameRegex(false))); |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 96 | return RegexExp.match(PropertyName.substr(Start + 1)); |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 97 | } |
| 98 | } // namespace |
| 99 | |
| 100 | PropertyDeclarationCheck::PropertyDeclarationCheck(StringRef Name, |
| 101 | ClangTidyContext *Context) |
| 102 | : ClangTidyCheck(Name, Context), |
Ben Hamilton | f94c10d | 2018-01-22 15:45:25 +0000 | [diff] [blame] | 103 | SpecialAcronyms( |
| 104 | utils::options::parseStringList(Options.get("Acronyms", ""))), |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 105 | IncludeDefaultAcronyms(Options.get("IncludeDefaultAcronyms", true)), |
| 106 | EscapedAcronyms() {} |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 107 | |
| 108 | void PropertyDeclarationCheck::registerMatchers(MatchFinder *Finder) { |
Yan Zhang | c7faee7 | 2018-03-07 18:59:25 +0000 | [diff] [blame] | 109 | // this check should only be applied to ObjC sources. |
Yan Zhang | cc1e9c4 | 2018-11-01 17:36:18 +0000 | [diff] [blame] | 110 | if (!getLangOpts().ObjC) return; |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 111 | |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 112 | Finder->addMatcher( |
| 113 | objcPropertyDecl( |
| 114 | // the property name should be in Lower Camel Case like |
| 115 | // 'lowerCamelCase' |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 116 | unless(matchesName(validPropertyNameRegex(true)))) |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 117 | .bind("property"), |
| 118 | this); |
| 119 | } |
| 120 | |
| 121 | void PropertyDeclarationCheck::check(const MatchFinder::MatchResult &Result) { |
| 122 | const auto *MatchedDecl = |
| 123 | Result.Nodes.getNodeAs<ObjCPropertyDecl>("property"); |
| 124 | assert(MatchedDecl->getName().size() > 0); |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 125 | auto *DeclContext = MatchedDecl->getDeclContext(); |
| 126 | auto *CategoryDecl = llvm::dyn_cast<ObjCCategoryDecl>(DeclContext); |
Yan Zhang | 4f9ead2 | 2018-05-04 18:14:08 +0000 | [diff] [blame] | 127 | |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 128 | if (CategoryDecl != nullptr && |
| 129 | hasCategoryPropertyPrefix(MatchedDecl->getName())) { |
Stephane Moore | af4755a | 2018-12-05 03:44:03 +0000 | [diff] [blame] | 130 | if (!prefixedPropertyNameValid(MatchedDecl->getName()) || |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 131 | CategoryDecl->IsClassExtension()) { |
| 132 | NamingStyle Style = CategoryDecl->IsClassExtension() ? StandardProperty |
| 133 | : CategoryProperty; |
| 134 | diag(MatchedDecl->getLocation(), |
| 135 | "property name '%0' not using lowerCamelCase style or not prefixed " |
| 136 | "in a category, according to the Apple Coding Guidelines") |
| 137 | << MatchedDecl->getName() << generateFixItHint(MatchedDecl, Style); |
| 138 | } |
| 139 | return; |
| 140 | } |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 141 | diag(MatchedDecl->getLocation(), |
Yan Zhang | 75b3b54 | 2018-01-30 01:44:00 +0000 | [diff] [blame] | 142 | "property name '%0' not using lowerCamelCase style or not prefixed in " |
| 143 | "a category, according to the Apple Coding Guidelines") |
| 144 | << MatchedDecl->getName() |
| 145 | << generateFixItHint(MatchedDecl, StandardProperty); |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void PropertyDeclarationCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 149 | Options.store(Opts, "Acronyms", |
| 150 | utils::options::serializeStringList(SpecialAcronyms)); |
Ben Hamilton | f94c10d | 2018-01-22 15:45:25 +0000 | [diff] [blame] | 151 | Options.store(Opts, "IncludeDefaultAcronyms", IncludeDefaultAcronyms); |
Ben Hamilton | 52161a5 | 2017-11-13 23:54:31 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | } // namespace objc |
| 155 | } // namespace tidy |
| 156 | } // namespace clang |