clang-tools 19.0.0git
AvoidNonConstGlobalVariablesCheck.cpp
Go to the documentation of this file.
1//===--- AvoidNonConstGlobalVariablesCheck.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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/ASTMatchers/ASTMatchers.h"
13
14using namespace clang::ast_matchers;
15
17
19 auto GlobalContext =
20 varDecl(hasGlobalStorage(),
21 hasDeclContext(anyOf(namespaceDecl(), translationUnitDecl())));
22
23 auto GlobalVariable = varDecl(
24 GlobalContext,
25 unless(anyOf(
26 isConstexpr(), hasType(isConstQualified()),
27 hasType(referenceType())))); // References can't be changed, only the
28 // data they reference can be changed.
29
30 auto GlobalReferenceToNonConst =
31 varDecl(GlobalContext, hasType(referenceType()),
32 unless(hasType(references(qualType(isConstQualified())))));
33
34 auto GlobalPointerToNonConst = varDecl(
35 GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));
36
37 Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);
38 Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),
39 this);
40 Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),
41 this);
42}
43
45 const MatchFinder::MatchResult &Result) {
46
47 if (const auto *Variable =
48 Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {
49 diag(Variable->getLocation(), "variable %0 is non-const and globally "
50 "accessible, consider making it const")
51 << Variable; // FIXME: Add fix-it hint to Variable
52 // Don't return early, a non-const variable may also be a pointer or
53 // reference to non-const data.
54 }
55
56 if (const auto *VD =
57 Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {
58 diag(VD->getLocation(),
59 "variable %0 provides global access to a non-const object; consider "
60 "making the %select{referenced|pointed-to}1 data 'const'")
61 << VD
62 << VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
63 }
64}
65
66} // namespace clang::tidy::cppcoreguidelines
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.