void SomeChecker::checkPostStmt(const Stmt *S, CheckerContext &C) const { if (const DeclRefExpr *DRE = dyn_cast(S)) { const VarDecl *VD = dyn_cast (DRE->getDecl()); if (!VD || !VD->getType()->isPointerType()) return; ProgramStateRef State = C.getState(); if (State->isNull(DRE->getDecl())) reportBug("Null pointer dereference", C); } }
void SomeChecker::checkPreStmt(const Stmt *S, CheckerContext &C) const { ProgramStateRef State = C.getState(); if (const DeclStmt *DS = dyn_castThis example demonstrates how to modify the program state using the set function. The checker works by initializing all pointer variables to a default value. The current state is obtained using getState and then modified using the set function. Finally, the modified state is added to the transition list using addTransition. Package library: Clang Static Analyzer.(S)) { for (const auto *D : DS->decls()) { if (const VarDecl *VD = dyn_cast (D)) { if (VD->getType()->isPointerType()) { State = State->set (VD, CustomData()); } } } } C.addTransition(State); }