12 #include "../utils/OptionsUtils.h" 13 #include "clang/AST/ASTContext.h" 14 #include "clang/ASTMatchers/ASTMatchFinder.h" 15 #include "clang/Basic/CharInfo.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/Support/Regex.h" 42 FixItHint generateFixItHint(
const ObjCPropertyDecl *Decl,
NamingStyle Style) {
43 auto Name = Decl->getName();
44 auto NewName = Decl->getName().str();
46 if (Style == CategoryProperty) {
47 Index =
Name.find_first_of(
'_') + 1;
48 NewName.replace(0, Index - 1,
Name.substr(0, Index - 1).lower());
50 if (Index <
Name.size()) {
51 NewName[
Index] = tolower(NewName[Index]);
52 if (NewName !=
Name) {
53 return FixItHint::CreateReplacement(
54 CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
55 llvm::StringRef(NewName));
61 std::string validPropertyNameRegex(
bool UsedInMatcher) {
79 std::string StartMatcher = UsedInMatcher ?
"::" :
"^";
80 return StartMatcher +
"([a-z]|[A-Z][A-Z0-9])[a-z0-9A-Z]*$";
83 bool hasCategoryPropertyPrefix(llvm::StringRef PropertyName) {
84 auto RegexExp = llvm::Regex(
"^[a-zA-Z]+_[a-zA-Z0-9][a-zA-Z0-9_]+$");
85 return RegexExp.match(PropertyName);
88 bool prefixedPropertyNameValid(llvm::StringRef PropertyName) {
89 size_t Start = PropertyName.find_first_of(
'_');
90 assert(Start != llvm::StringRef::npos && Start + 1 < PropertyName.size());
91 auto Prefix = PropertyName.substr(0, Start);
92 if (Prefix.lower() != Prefix) {
96 llvm::Regex(llvm::StringRef(validPropertyNameRegex(
false)));
97 return RegexExp.match(PropertyName.substr(Start + 1));
101 PropertyDeclarationCheck::PropertyDeclarationCheck(StringRef
Name,
106 IncludeDefaultAcronyms(Options.get(
"IncludeDefaultAcronyms", true)),
117 unless(matchesName(validPropertyNameRegex(
true))))
123 const auto *MatchedDecl =
124 Result.Nodes.getNodeAs<ObjCPropertyDecl>(
"property");
125 assert(MatchedDecl->getName().size() > 0);
126 auto *DeclContext = MatchedDecl->getDeclContext();
127 auto *CategoryDecl = llvm::dyn_cast<ObjCCategoryDecl>(DeclContext);
129 if (CategoryDecl !=
nullptr &&
130 hasCategoryPropertyPrefix(MatchedDecl->getName())) {
131 if (!prefixedPropertyNameValid(MatchedDecl->getName()) ||
132 CategoryDecl->IsClassExtension()) {
133 NamingStyle Style = CategoryDecl->IsClassExtension() ? StandardProperty
135 diag(MatchedDecl->getLocation(),
136 "property name '%0' not using lowerCamelCase style or not prefixed " 137 "in a category, according to the Apple Coding Guidelines")
138 << MatchedDecl->getName() << generateFixItHint(MatchedDecl, Style);
142 diag(MatchedDecl->getLocation(),
143 "property name '%0' not using lowerCamelCase style or not prefixed in " 144 "a category, according to the Apple Coding Guidelines")
145 << MatchedDecl->getName()
146 << generateFixItHint(MatchedDecl, StandardProperty);
152 Options.
store(Opts,
"IncludeDefaultAcronyms", IncludeDefaultAcronyms);
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
void storeOptions(ClangTidyOptions::OptionMap &Options) override
Should store all options supported by this check with their current values or default values for opti...
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
LangOptions getLangOpts() const
Returns the language options from the context.
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const SymbolIndex * Index