10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 17 namespace cppcoreguidelines {
19 void InterfacesGlobalInitCheck::registerMatchers(MatchFinder *Finder) {
20 const auto GlobalVarDecl =
21 varDecl(hasGlobalStorage(),
22 hasDeclContext(anyOf(translationUnitDecl(),
25 unless(isConstexpr()));
27 const auto ReferencesUndefinedGlobalVar = declRefExpr(hasDeclaration(
28 varDecl(GlobalVarDecl, unless(isDefinition())).bind(
"referencee")));
31 varDecl(GlobalVarDecl, isDefinition(),
32 hasInitializer(expr(hasDescendant(ReferencesUndefinedGlobalVar))))
37 void InterfacesGlobalInitCheck::check(
const MatchFinder::MatchResult &Result) {
38 const auto *
const Var = Result.Nodes.getNodeAs<VarDecl>(
"var");
40 if (Var->getLocation().isMacroID())
42 const auto *
const Referencee = Result.Nodes.getNodeAs<VarDecl>(
"referencee");
44 const auto *
const ReferenceeDef = Referencee->getDefinition();
45 if (ReferenceeDef !=
nullptr &&
46 Result.SourceManager->isBeforeInTranslationUnit(
47 ReferenceeDef->getLocation(), Var->getLocation())) {
50 diag(Var->getLocation(),
51 "initializing non-local variable with non-const expression depending on " 52 "uninitialized non-local variable %0")
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//