clang-tools 8.0.0

FunctionNamingCheck.cpp
Go to the documentation of this file.
1 //===--- FunctionNamingCheck.cpp - clang-tidy -----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "FunctionNamingCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "llvm/Support/Regex.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace objc {
21
22 namespace {
23
24 std::string validFunctionNameRegex(bool RequirePrefix) {
25 // Allow the following name patterns for all functions:
26 // • ABFoo (prefix + UpperCamelCase)
27 // • ABURL (prefix + capitalized acronym/initialism)
28 //
29 // If no prefix is required, additionally allow the following name patterns:
30 // • Foo (UpperCamelCase)
31 // • URL (capitalized acronym/initialism)
32 //
33 // The function name following the prefix can contain standard and
34 // non-standard capitalized character sequences including acronyms,
35 // initialisms, and prefixes of symbols (e.g., UIColorFromNSString). For this
36 // reason, the regex only verifies that the function name after the prefix
37 // begins with a capital letter followed by an arbitrary sequence of
38 // alphanumeric characters.
39 //
40 // If a prefix is required, the regex checks for a capital letter followed by
41 // another capital letter or number that is part of the prefix and another
42 // capital letter or number that begins the name following the prefix.
43 std::string FunctionNameMatcher =
44 std::string(RequirePrefix ? "[A-Z][A-Z0-9]+" : "") + "[A-Z][a-zA-Z0-9]*";
45 return std::string("::(") + FunctionNameMatcher + ")$";
46 }
47
48 /// For now we will only fix functions of static storage class with names like
49 /// 'functionName' or 'function_name' and convert them to 'FunctionName'. For
50 /// other cases the user must determine an appropriate name on their own.
51 FixItHint generateFixItHint(const FunctionDecl *Decl) {
52 // A fixit can be generated for functions of static storage class but
53 // otherwise the check cannot determine the appropriate function name prefix
54 // to use.
55 if (Decl->getStorageClass() != SC_Static)
56 return FixItHint();
57
58 StringRef Name = Decl->getName();
59 std::string NewName = Decl->getName().str();
60
61 size_t Index = 0;
62 bool AtWordBoundary = true;
63 while (Index < NewName.size()) {
64 char ch = NewName[Index];
65 if (isalnum(ch)) {
66 // Capitalize the first letter after every word boundary.
67 if (AtWordBoundary) {
68 NewName[Index] = toupper(NewName[Index]);
69 AtWordBoundary = false;
70 }
71
72 // Advance the index after every alphanumeric character.
73 Index++;
74 } else {
75 // Strip out any characters other than alphanumeric characters.
76 NewName.erase(Index, 1);
77 AtWordBoundary = true;
78 }
79 }
80
81 // Generate a fixit hint if the new name is different.
82 if (NewName != Name)
83 return FixItHint::CreateReplacement(
84 CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
85 llvm::StringRef(NewName));
86
87 return FixItHint();
88 }
89
90 } // namespace
91
92 void FunctionNamingCheck::registerMatchers(MatchFinder *Finder) {
93 // This check should only be applied to Objective-C sources.
94 if (!getLangOpts().ObjC)
95 return;
96
97 // Match function declarations that are not in system headers and are not
98 // main.
99 Finder->addMatcher(
100 functionDecl(
101 unless(anyOf(isExpansionInSystemHeader(), cxxMethodDecl(),
102 hasAncestor(namespaceDecl()), isMain(),
103 matchesName(validFunctionNameRegex(true)),
104 allOf(isStaticStorageClass(),
105 matchesName(validFunctionNameRegex(false))))))
106 .bind("function"),
107 this);
108 }
109
110 void FunctionNamingCheck::check(const MatchFinder::MatchResult &Result) {
111 const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("function");
112
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);
119 }
120
121 } // namespace objc
122 } // namespace google
123 } // namespace tidy
124 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:15
FunctionNamingCheck.h
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
Index
const SymbolIndex * Index
Definition: Dexp.cpp:85