10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
18 const auto GlobalVarDecl =
19 varDecl(hasGlobalStorage(),
20 hasDeclContext(anyOf(translationUnitDecl(),
23 unless(isConstexpr()));
25 const auto ReferencesUndefinedGlobalVar = declRefExpr(hasDeclaration(
26 varDecl(GlobalVarDecl, unless(isDefinition())).bind(
"referencee")));
29 traverse(TK_AsIs, varDecl(GlobalVarDecl, isDefinition(),
30 hasInitializer(expr(hasDescendant(
31 ReferencesUndefinedGlobalVar))))
37 const auto *
const Var = Result.Nodes.getNodeAs<VarDecl>(
"var");
39 if (Var->getLocation().isMacroID())
41 const auto *
const Referencee = Result.Nodes.getNodeAs<VarDecl>(
"referencee");
43 const auto *
const ReferenceeDef = Referencee->getDefinition();
44 if (ReferenceeDef !=
nullptr &&
45 Result.SourceManager->isBeforeInTranslationUnit(
46 ReferenceeDef->getLocation(), Var->getLocation())) {
49 diag(Var->getLocation(),
50 "initializing non-local variable with non-const expression depending on "
51 "uninitialized non-local variable %0")
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.