11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "llvm/Support/Regex.h" 24 std::string validFunctionNameRegex(
bool RequirePrefix) {
43 std::string FunctionNameMatcher =
44 std::string(RequirePrefix ?
"[A-Z][A-Z0-9]+" :
"") +
"[A-Z][a-zA-Z0-9]*";
45 return std::string(
"::(") + FunctionNameMatcher +
")$";
51 FixItHint generateFixItHint(
const FunctionDecl *Decl) {
55 if (Decl->getStorageClass() != SC_Static)
58 StringRef
Name = Decl->getName();
59 std::string NewName = Decl->getName().str();
62 bool AtWordBoundary =
true;
63 while (Index < NewName.size()) {
64 char ch = NewName[
Index];
68 NewName[
Index] = toupper(NewName[Index]);
69 AtWordBoundary =
false;
76 NewName.erase(Index, 1);
77 AtWordBoundary =
true;
83 return FixItHint::CreateReplacement(
84 CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
85 llvm::StringRef(NewName));
92 void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
94 if (!getLangOpts().ObjC)
101 unless(anyOf(isExpansionInSystemHeader(), cxxMethodDecl(),
102 hasAncestor(namespaceDecl()), isMain(),
103 matchesName(validFunctionNameRegex(
true)),
104 allOf(isStaticStorageClass(),
105 matchesName(validFunctionNameRegex(
false))))))
110 void FunctionNamingCheck::check(
const MatchFinder::MatchResult &
Result) {
111 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
113 bool IsGlobal = MatchedDecl->getStorageClass() != SC_Static;
114 diag(MatchedDecl->getLocation(),
115 "%select{static function|function in global namespace}1 named %0 must " 116 "%select{be in|have an appropriate prefix followed by}1 Pascal case as " 117 "required by Google Objective-C style guide")
118 << MatchedDecl << IsGlobal << generateFixItHint(MatchedDecl);
static constexpr llvm::StringLiteral Name
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
const SymbolIndex * Index