clang-tools 23.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 IgnoreMacros(Options.get("IgnoreMacros", false)) {}
23
25 auto NamespaceMatcher = AllowInternalLinkage
26 ? namespaceDecl(unless(isAnonymous()))
27 : namespaceDecl();
28 auto GlobalContext =
29 varDecl(hasGlobalStorage(),
30 hasDeclContext(anyOf(NamespaceMatcher, translationUnitDecl())));
31
32 auto GlobalVariable = varDecl(
33 GlobalContext,
34 AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))
35 : varDecl(),
36 AllowThreadLocal ? varDecl(unless(hasThreadStorageDuration()))
37 : varDecl(),
38 unless(anyOf(
39 isConstexpr(), hasType(isConstQualified()),
40 hasType(referenceType())))); // References can't be changed, only the
41 // data they reference can be changed.
42
43 auto GlobalReferenceToNonConst =
44 varDecl(GlobalContext, hasType(referenceType()),
45 unless(hasType(references(qualType(isConstQualified())))));
46
47 auto GlobalPointerToNonConst = varDecl(
48 GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));
49
50 Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);
51 Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),
52 this);
53 Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),
54 this);
55}
56
58 const MatchFinder::MatchResult &Result) {
59 if (const auto *Variable =
60 Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {
61 const SourceLocation Loc = Variable->getLocation();
62 if (IgnoreMacros && Loc.isMacroID())
63 return;
64
65 diag(Loc, "variable %0 is non-const and globally "
66 "accessible, consider making it const")
67 << Variable; // FIXME: Add fix-it hint to Variable
68 // Don't return early, a non-const variable may also be a pointer or
69 // reference to non-const data.
70 }
71
72 if (const auto *VD =
73 Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {
74 const SourceLocation Loc = VD->getLocation();
75 if (IgnoreMacros && Loc.isMacroID())
76 return;
77
78 diag(Loc,
79 "variable %0 provides global access to a non-const object; consider "
80 "making the %select{referenced|pointed-to}1 data 'const'")
81 << VD
82 << VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
83 }
84}
85
88 Options.store(Opts, "AllowInternalLinkage", AllowInternalLinkage);
89 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
90}
91
92} // 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