10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "llvm/Support/Regex.h"
20std::string validFunctionNameRegex(
bool RequirePrefix) {
39 std::string FunctionNameMatcher =
40 std::string(RequirePrefix ?
"[A-Z][A-Z0-9]+" :
"") +
"[A-Z][a-zA-Z0-9]*";
41 return std::string(
"::(") + FunctionNameMatcher +
")$";
47FixItHint generateFixItHint(
const FunctionDecl *
Decl) {
51 if (
Decl->getStorageClass() != SC_Static)
55 std::string NewName =
Decl->getName().str();
58 bool AtWordBoundary =
true;
59 while (Index < NewName.size()) {
60 char Ch = NewName[Index];
64 NewName[Index] = toupper(NewName[Index]);
65 AtWordBoundary =
false;
72 NewName.erase(Index, 1);
73 AtWordBoundary =
true;
79 return FixItHint::CreateReplacement(
80 CharSourceRange::getTokenRange(SourceRange(
Decl->getLocation())),
81 llvm::StringRef(NewName));
97 unless(anyOf(isExpansionInSystemHeader(), cxxMethodDecl(),
98 hasAncestor(namespaceDecl()), isMain(), isImplicit(),
99 matchesName(validFunctionNameRegex(
true)),
100 allOf(isStaticStorageClass(),
101 matchesName(validFunctionNameRegex(
false))))))
107 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>(
"function");
109 bool IsGlobal = MatchedDecl->getStorageClass() != SC_Static;
110 diag(MatchedDecl->getLocation(),
111 "%select{static function|function in global namespace}1 named %0 must "
112 "%select{be in|have an appropriate prefix followed by}1 Pascal case as "
113 "required by Google Objective-C style guide")
114 << MatchedDecl << IsGlobal << generateFixItHint(MatchedDecl);
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.