void MyChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) { const FunctionDecl *FD = C.getCalleeDecl(CE); if (!FD) return; if (FD->getName() == "malloc") { CheckerContext PredecessorCtx = C.getPredecessor(); if (PredecessorCtx.inTopFrame()) { reportWithPreceding(CE, PredecessorCtx); } } }
void MyChecker::checkPostStmt(const AssignStmt *S, CheckerContext &C) { CheckerContext PredecessorCtx = C.getPredecessor(); if (PredecessorCtx.getBody()->getStmtClass(S) == Stmt::DeclStmtClass) { reportWithPreceding(S, PredecessorCtx); } }This example checks if the current statement is an assignment statement and if so, it uses getPredecessor() to obtain the context in effect before executing the statement. Then, it retrieves the previous body statement class using getStmtClass() and checks if it is a declaration statement. If it is, it calls the reportWithPreceding() function to generate a warning.