aboutsummaryrefslogtreecommitdiff
blob: b2ad27647f9fd5471d02540328ab35af89de50ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//===--- GlobalVariableDeclarationCheck.cpp - clang-tidy-------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "GlobalVariableDeclarationCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"

#include <string>

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace google {
namespace objc {

namespace {

AST_MATCHER(VarDecl, isLocalVariable) {
  return Node.isLocalVarDecl();
}

FixItHint generateFixItHint(const VarDecl *Decl, bool IsConst) {
  char FC = Decl->getName()[0];
  if (!llvm::isAlpha(FC) || Decl->getName().size() == 1) {
    // No fix available if first character is not alphabetical character, or it
    // is a single-character variable, since it is difficult to determine the 
    // proper fix in this case. Users should create a proper variable name by
    // their own.
    return FixItHint();
  }
  char SC = Decl->getName()[1];
  if ((FC == 'k' || FC == 'g') && !llvm::isAlpha(SC)) {
    // No fix available if the prefix is correct but the second character is not
    // alphabetical, since it is difficult to determine the proper fix in this
    // case.
    return FixItHint();
  }
  auto NewName = (IsConst ? "k" : "g") +
                 llvm::StringRef(std::string(1, FC)).upper() +
                 Decl->getName().substr(1).str();
  return FixItHint::CreateReplacement(
      CharSourceRange::getTokenRange(SourceRange(Decl->getLocation())),
      llvm::StringRef(NewName));
}
}  // namespace

void GlobalVariableDeclarationCheck::registerMatchers(MatchFinder *Finder) {
  // The relevant Style Guide rule only applies to Objective-C.
  if (!getLangOpts().ObjC)
    return;

  // need to add two matchers since we need to bind different ids to distinguish
  // constants and variables. Since bind() can only be called on node matchers,
  // we cannot make it in one matcher.
  //
  // Note that hasGlobalStorage() matches static variables declared locally
  // inside a function or method, so we need to exclude those with
  // isLocalVariable().
  Finder->addMatcher(
      varDecl(hasGlobalStorage(), unless(hasType(isConstQualified())),
              unless(isLocalVariable()), unless(matchesName("::g[A-Z]")))
          .bind("global_var"),
      this);
  Finder->addMatcher(varDecl(hasGlobalStorage(), hasType(isConstQualified()),
                             unless(isLocalVariable()),
                             unless(matchesName("::(k[A-Z]|[A-Z]{2,})")))
                         .bind("global_const"),
                     this);
}

void GlobalVariableDeclarationCheck::check(
    const MatchFinder::MatchResult &Result) {
  if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_var")) {
    diag(Decl->getLocation(),
         "non-const global variable '%0' must have a name which starts with "
         "'g[A-Z]'")
        << Decl->getName() << generateFixItHint(Decl, false);
  }
  if (const auto *Decl = Result.Nodes.getNodeAs<VarDecl>("global_const")) {
    diag(Decl->getLocation(),
         "const global variable '%0' must have a name which starts with "
         "an appropriate prefix")
        << Decl->getName() << generateFixItHint(Decl, true);
  }
}

}  // namespace objc
}  // namespace google
}  // namespace tidy
}  // namespace clang