25 auto NamespaceMatcher = AllowInternalLinkage
26 ? namespaceDecl(unless(isAnonymous()))
29 varDecl(hasGlobalStorage(),
30 hasDeclContext(anyOf(NamespaceMatcher, translationUnitDecl())));
32 auto GlobalVariable = varDecl(
34 AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))
36 AllowThreadLocal ? varDecl(unless(hasThreadStorageDuration()))
39 isConstexpr(), hasType(isConstQualified()),
40 hasType(referenceType()))));
43 auto GlobalReferenceToNonConst =
44 varDecl(GlobalContext, hasType(referenceType()),
45 unless(hasType(references(qualType(isConstQualified())))));
47 auto GlobalPointerToNonConst = varDecl(
48 GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));
50 Finder->addMatcher(GlobalVariable.bind(
"non-const_variable"),
this);
51 Finder->addMatcher(GlobalReferenceToNonConst.bind(
"indirection_to_non-const"),
53 Finder->addMatcher(GlobalPointerToNonConst.bind(
"indirection_to_non-const"),
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())
65 diag(Loc,
"variable %0 is non-const and globally "
66 "accessible, consider making it const")
73 Result.Nodes.getNodeAs<VarDecl>(
"indirection_to_non-const")) {
74 const SourceLocation Loc = VD->getLocation();
75 if (IgnoreMacros && Loc.isMacroID())
79 "variable %0 provides global access to a non-const object; consider "
80 "making the %select{referenced|pointed-to}1 data 'const'")
82 << VD->getType()->isPointerType();
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.