11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace cppcoreguidelines {
20 void InterfacesGlobalInitCheck::registerMatchers(MatchFinder *Finder) {
22 allOf(hasGlobalStorage(),
23 hasDeclContext(anyOf(translationUnitDecl(),
26 unless(isConstexpr()));
28 const auto ReferencesUndefinedGlobalVar = declRefExpr(hasDeclaration(
29 varDecl(IsGlobal, unless(isDefinition())).bind(
"referencee")));
32 varDecl(IsGlobal, isDefinition(),
33 hasInitializer(expr(hasDescendant(ReferencesUndefinedGlobalVar))))
38 void InterfacesGlobalInitCheck::check(
const MatchFinder::MatchResult &Result) {
39 const auto *
const Var = Result.Nodes.getNodeAs<VarDecl>(
"var");
41 if (Var->getLocation().isMacroID())
43 const auto *
const Referencee = Result.Nodes.getNodeAs<VarDecl>(
"referencee");
45 const auto *
const ReferenceeDef = Referencee->getDefinition();
46 if (ReferenceeDef !=
nullptr &&
47 Result.SourceManager->isBeforeInTranslationUnit(
48 ReferenceeDef->getLocation(), Var->getLocation())) {
51 diag(Var->getLocation(),
52 "initializing non-local variable with non-const expression depending on " 53 "uninitialized non-local variable %0")