1a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care//===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===// 2a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// 3a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// The LLVM Compiler Infrastructure 4a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// 5a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// This file is distributed under the University of Illinois Open Source 6a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// License. See LICENSE.TXT for details. 7a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// 8a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care//===----------------------------------------------------------------------===// 9a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// 10a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// This file defines several static functions for use in checkers. 11a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// 12a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care//===----------------------------------------------------------------------===// 13a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 149b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" 15a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care#include "clang/AST/Expr.h" 16a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 17a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// Recursively find any substatements containing macros 189ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekbool clang::ento::containsMacro(const Stmt *S) { 19a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (S->getLocStart().isMacroID()) 20a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 21a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 22a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (S->getLocEnd().isMacroID()) 23a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 24a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 25a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); 26a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care ++I) 27a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (const Stmt *child = *I) 28a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (containsMacro(child)) 29a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 30a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 31a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return false; 32a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care} 33a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 34a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// Recursively find any substatements containing enum constants 359ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekbool clang::ento::containsEnum(const Stmt *S) { 36a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); 37a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 38a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (DR && isa<EnumConstantDecl>(DR->getDecl())) 39a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 40a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 41a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); 42a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care ++I) 43a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (const Stmt *child = *I) 44a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (containsEnum(child)) 45a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 46a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 47a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return false; 48a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care} 49a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 50a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// Recursively find any substatements containing static vars 519ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekbool clang::ento::containsStaticLocal(const Stmt *S) { 52a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); 53a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 54a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (DR) 55a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) 56a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (VD->isStaticLocal()) 57a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 58a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 59a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); 60a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care ++I) 61a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (const Stmt *child = *I) 62a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (containsStaticLocal(child)) 63a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 64a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 65a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return false; 66a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care} 67a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 68156361d434cdcb4cc88169dee5b3d5ca25c7f012Eli Friedman// Recursively find any substatements containing __builtin_offsetof 699ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekbool clang::ento::containsBuiltinOffsetOf(const Stmt *S) { 70156361d434cdcb4cc88169dee5b3d5ca25c7f012Eli Friedman if (isa<OffsetOfExpr>(S)) 71156361d434cdcb4cc88169dee5b3d5ca25c7f012Eli Friedman return true; 72156361d434cdcb4cc88169dee5b3d5ca25c7f012Eli Friedman 73a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); 74a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care ++I) 75a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (const Stmt *child = *I) 76a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care if (containsBuiltinOffsetOf(child)) 77a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return true; 78a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care 79a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care return false; 80a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care} 81