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