10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "llvm/Support/Regex.h" 23 std::string validFunctionNameRegex(
bool RequirePrefix) {
42 std::string FunctionNameMatcher =
43 std::string(RequirePrefix ?
"[A-Z][A-Z0-9]+" :
"") +
"[A-Z][a-zA-Z0-9]*";
44 return std::string(
"::(") + FunctionNameMatcher +
")$";
50 FixItHint generateFixItHint(
const FunctionDecl *
Decl) {
54 if (Decl->getStorageClass() != SC_Static)
57 StringRef
Name = Decl->getName();
58 std::string NewName = Decl->getName().str();
61 bool AtWordBoundary =
true;
62 while (Index < NewName.size()) {
63 char ch = NewName[
Index];
67 NewName[
Index] = toupper(NewName[Index]);
68 AtWordBoundary =
false;
75 NewName.erase(Index, 1);
76 AtWordBoundary =
true;
82 return FixItHint::CreateReplacement(
84 llvm::StringRef(NewName));
91 void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
93 if (!getLangOpts().ObjC)
104 unless(anyOf(isExpansionInSystemHeader(), cxxMethodDecl(),
105 hasAncestor(namespaceDecl()), isMain(), isImplicit(),
106 matchesName(validFunctionNameRegex(
true)),
107 allOf(isStaticStorageClass(),
108 matchesName(validFunctionNameRegex(
false))))))
113 void FunctionNamingCheck::check(
const MatchFinder::MatchResult &Result) {
114 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
116 bool IsGlobal = MatchedDecl->getStorageClass() != SC_Static;
117 diag(MatchedDecl->getLocation(),
118 "%select{static function|function in global namespace}1 named %0 must " 119 "%select{be in|have an appropriate prefix followed by}1 Pascal case as " 120 "required by Google Objective-C style guide")
121 << MatchedDecl << IsGlobal << generateFixItHint(MatchedDecl);
const FunctionDecl * Decl
static constexpr llvm::StringLiteral Name
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const SymbolIndex * Index