24 auto NamespaceMatcher = AllowInternalLinkage
25 ? namespaceDecl(unless(isAnonymous()))
28 varDecl(hasGlobalStorage(),
29 hasDeclContext(anyOf(NamespaceMatcher, translationUnitDecl())));
31 auto GlobalVariable = varDecl(
33 AllowInternalLinkage ? varDecl(unless(isStaticStorageClass()))
35 AllowThreadLocal ? varDecl(unless(hasThreadStorageDuration()))
38 isConstexpr(), hasType(isConstQualified()),
39 hasType(referenceType()))));
42 auto GlobalReferenceToNonConst =
43 varDecl(GlobalContext, hasType(referenceType()),
44 unless(hasType(references(qualType(isConstQualified())))));
46 auto GlobalPointerToNonConst = varDecl(
47 GlobalContext, hasType(pointerType(pointee(unless(isConstQualified())))));
49 Finder->addMatcher(GlobalVariable.bind(
"non-const_variable"),
this);
50 Finder->addMatcher(GlobalReferenceToNonConst.bind(
"indirection_to_non-const"),
52 Finder->addMatcher(GlobalPointerToNonConst.bind(
"indirection_to_non-const"),
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")
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'")
73 << VD->getType()->isPointerType();
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.