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