brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.4 KiB · 2c0baa5 Raw
83 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "AvoidNonConstGlobalVariablesCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11#include "clang/ASTMatchers/ASTMatchers.h"12 13using namespace clang::ast_matchers;14 15namespace clang::tidy::cppcoreguidelines {16 17AvoidNonConstGlobalVariablesCheck::AvoidNonConstGlobalVariablesCheck(18    StringRef Name, ClangTidyContext *Context)19    : ClangTidyCheck(Name, Context),20      AllowInternalLinkage(Options.get("AllowInternalLinkage", false)),21      AllowThreadLocal(Options.get("AllowThreadLocal", false)) {}22 23void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {24  auto NamespaceMatcher = AllowInternalLinkage25                              ? namespaceDecl(unless(isAnonymous()))26                              : namespaceDecl();27  auto GlobalContext =28      varDecl(hasGlobalStorage(),29              hasDeclContext(anyOf(NamespaceMatcher, translationUnitDecl())));30 31  auto GlobalVariable = varDecl(32      GlobalContext,33      AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))34                           : varDecl(),35      AllowThreadLocal ? varDecl(unless(hasThreadStorageDuration()))36                       : varDecl(),37      unless(anyOf(38          isConstexpr(), hasType(isConstQualified()),39          hasType(referenceType())))); // References can't be changed, only the40                                       // data they reference can be changed.41 42  auto GlobalReferenceToNonConst =43      varDecl(GlobalContext, hasType(referenceType()),44              unless(hasType(references(qualType(isConstQualified())))));45 46  auto GlobalPointerToNonConst = varDecl(47      GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));48 49  Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);50  Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),51                     this);52  Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),53                     this);54}55 56void AvoidNonConstGlobalVariablesCheck::check(57    const MatchFinder::MatchResult &Result) {58  if (const auto *Variable =59          Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {60    diag(Variable->getLocation(), "variable %0 is non-const and globally "61                                  "accessible, consider making it const")62        << Variable; // FIXME: Add fix-it hint to Variable63    // Don't return early, a non-const variable may also be a pointer or64    // reference to non-const data.65  }66 67  if (const auto *VD =68          Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {69    diag(VD->getLocation(),70         "variable %0 provides global access to a non-const object; consider "71         "making the %select{referenced|pointed-to}1 data 'const'")72        << VD73        << VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable74  }75}76 77void AvoidNonConstGlobalVariablesCheck::storeOptions(78    ClangTidyOptions::OptionMap &Opts) {79  Options.store(Opts, "AllowInternalLinkage", AllowInternalLinkage);80}81 82} // namespace clang::tidy::cppcoreguidelines83