1 | //===--- GlobalVariableDeclarationCheck.cpp - clang-tidy-------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "GlobalVariableDeclarationCheck.h" |
10 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
11 | #include "llvm/ADT/StringExtras.h" |
12 | #include "llvm/ADT/StringRef.h" |
13 | |
14 | #include <string> |
15 | |
16 | using namespace clang::ast_matchers; |
17 | |
18 | namespace clang::tidy::google::objc { |
19 | |
20 | namespace { |
21 | |
22 | AST_MATCHER(VarDecl, isLocalVariable) { return Node.isLocalVarDecl(); } |
23 | |
24 | FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) { |
25 | if (IsConst && (Decl->getStorageClass() != SC_Static)) { |
26 | // No fix available if it is not a static constant, since it is difficult |
27 | // to determine the proper fix in this case. |
28 | return {}; |
29 | } |
30 | |
31 | char FC = Decl->getName()[0]; |
32 | if (!llvm::isAlpha(C: FC) || Decl->getName().size() == 1) { |
33 | // No fix available if first character is not alphabetical character, or it |
34 | // is a single-character variable, since it is difficult to determine the |
35 | // proper fix in this case. Users should create a proper variable name by |
36 | // their own. |
37 | return {}; |
38 | } |
39 | char SC = Decl->getName()[1]; |
40 | if ((FC == 'k' || FC == 'g') && !llvm::isAlpha(C: SC)) { |
41 | // No fix available if the prefix is correct but the second character is |
42 | // not alphabetical, since it is difficult to determine the proper fix in |
43 | // this case. |
44 | return {}; |
45 | } |
46 | |
47 | auto NewName = (IsConst ? "k" : "g" ) + |
48 | llvm::StringRef(std::string(1, FC)).upper() + |
49 | Decl->getName().substr(1).str(); |
50 | |
51 | return FixItHint::CreateReplacement( |
52 | RemoveRange: CharSourceRange::getTokenRange(R: SourceRange(Decl->getLocation())), |
53 | Code: llvm::StringRef(NewName)); |
54 | } |
55 | } // namespace |
56 | |
57 | void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) { |
58 | // need to add two matchers since we need to bind different ids to distinguish |
59 | // constants and variables. Since bind() can only be called on node matchers, |
60 | // we cannot make it in one matcher. |
61 | // |
62 | // Note that hasGlobalStorage() matches static variables declared locally |
63 | // inside a function or method, so we need to exclude those with |
64 | // isLocalVariable(). |
65 | Finder->addMatcher( |
66 | NodeMatch: varDecl(hasGlobalStorage(), unless(hasType(InnerMatcher: isConstQualified())), |
67 | unless(isLocalVariable()), unless(matchesName(RegExp: "::g[A-Z]" ))) |
68 | .bind(ID: "global_var" ), |
69 | Action: this); |
70 | Finder->addMatcher(NodeMatch: varDecl(hasGlobalStorage(), hasType(InnerMatcher: isConstQualified()), |
71 | unless(isLocalVariable()), |
72 | unless(matchesName(RegExp: "::(k[A-Z])|([A-Z][A-Z0-9])" ))) |
73 | .bind(ID: "global_const" ), |
74 | Action: this); |
75 | } |
76 | |
77 | void GlobalVariableDeclarationCheck::check( |
78 | const MatchFinder::MatchResult &Result) { |
79 | if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>(ID: "global_var" )) { |
80 | if (Decl->isStaticDataMember()) |
81 | return; |
82 | diag(Decl->getLocation(), |
83 | "non-const global variable '%0' must have a name which starts with " |
84 | "'g[A-Z]'" ) |
85 | << Decl->getName() << generateFixItHint(Decl, IsConst: false); |
86 | } |
87 | if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>(ID: "global_const" )) { |
88 | if (Decl->isStaticDataMember()) |
89 | return; |
90 | diag(Decl->getLocation(), |
91 | "const global variable '%0' must have a name which starts with " |
92 | "an appropriate prefix" ) |
93 | << Decl->getName() << generateFixItHint(Decl, IsConst: true); |
94 | } |
95 | } |
96 | |
97 | } // namespace clang::tidy::google::objc |
98 | |