clang-tools 22.0.0git
AvoidNonConstGlobalVariablesCheck.cpp
Go to the documentation of this file.
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-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 AllowThreadLocal(Options.get("AllowThreadLocal", false)) {}
22
24 auto NamespaceMatcher = AllowInternalLinkage
25 ? 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 the
40 // 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
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 Variable
63 // Don't return early, a non-const variable may also be a pointer or
64 // 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 << VD
73 << VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
74 }
75}
76
79 Options.store(Opts, "AllowInternalLinkage", AllowInternalLinkage);
80}
81
82} // namespace clang::tidy::cppcoreguidelines
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
llvm::StringMap< ClangTidyValue > OptionMap