clang-tools 20.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/ASTMatchers/ASTMatchFinder.h"
11#include "clang/ASTMatchers/ASTMatchers.h"
12
13using namespace clang::ast_matchers;
14
16
18 StringRef Name, ClangTidyContext *Context)
19 : ClangTidyCheck(Name, Context),
20 AllowInternalLinkage(Options.get("AllowInternalLinkage", false)) {}
21
23 auto NamespaceMatcher = AllowInternalLinkage
24 ? namespaceDecl(unless(isAnonymous()))
25 : namespaceDecl();
26 auto GlobalContext =
27 varDecl(hasGlobalStorage(),
28 hasDeclContext(anyOf(NamespaceMatcher, translationUnitDecl())));
29
30 auto GlobalVariable = varDecl(
31 GlobalContext,
32 AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))
33 : varDecl(),
34 unless(anyOf(
35 isConstexpr(), hasType(isConstQualified()),
36 hasType(referenceType())))); // References can't be changed, only the
37 // data they reference can be changed.
38
39 auto GlobalReferenceToNonConst =
40 varDecl(GlobalContext, hasType(referenceType()),
41 unless(hasType(references(qualType(isConstQualified())))));
42
43 auto GlobalPointerToNonConst = varDecl(
44 GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));
45
46 Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);
47 Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),
48 this);
49 Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),
50 this);
51}
52
54 const MatchFinder::MatchResult &Result) {
55 if (const auto *Variable =
56 Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {
57 diag(Variable->getLocation(), "variable %0 is non-const and globally "
58 "accessible, consider making it const")
59 << Variable; // FIXME: Add fix-it hint to Variable
60 // Don't return early, a non-const variable may also be a pointer or
61 // reference to non-const data.
62 }
63
64 if (const auto *VD =
65 Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {
66 diag(VD->getLocation(),
67 "variable %0 provides global access to a non-const object; consider "
68 "making the %select{referenced|pointed-to}1 data 'const'")
69 << VD
70 << VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
71 }
72}
73
76 Options.store(Opts, "AllowInternalLinkage", AllowInternalLinkage);
77}
78
79} // namespace clang::tidy::cppcoreguidelines
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.
llvm::StringMap< ClangTidyValue > OptionMap