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