clang API Documentation

CheckerHelpers.cpp
Go to the documentation of this file.
00001 //===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines several static functions for use in checkers.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
00015 #include "clang/AST/Expr.h"
00016 
00017 // Recursively find any substatements containing macros
00018 bool clang::ento::containsMacro(const Stmt *S) {
00019   if (S->getLocStart().isMacroID())
00020     return true;
00021 
00022   if (S->getLocEnd().isMacroID())
00023     return true;
00024 
00025   for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
00026       ++I)
00027     if (const Stmt *child = *I)
00028       if (containsMacro(child))
00029         return true;
00030 
00031   return false;
00032 }
00033 
00034 // Recursively find any substatements containing enum constants
00035 bool clang::ento::containsEnum(const Stmt *S) {
00036   const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
00037 
00038   if (DR && isa<EnumConstantDecl>(DR->getDecl()))
00039     return true;
00040 
00041   for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
00042       ++I)
00043     if (const Stmt *child = *I)
00044       if (containsEnum(child))
00045         return true;
00046 
00047   return false;
00048 }
00049 
00050 // Recursively find any substatements containing static vars
00051 bool clang::ento::containsStaticLocal(const Stmt *S) {
00052   const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
00053 
00054   if (DR)
00055     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
00056       if (VD->isStaticLocal())
00057         return true;
00058 
00059   for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
00060       ++I)
00061     if (const Stmt *child = *I)
00062       if (containsStaticLocal(child))
00063         return true;
00064 
00065   return false;
00066 }
00067 
00068 // Recursively find any substatements containing __builtin_offsetof
00069 bool clang::ento::containsBuiltinOffsetOf(const Stmt *S) {
00070   if (isa<OffsetOfExpr>(S))
00071     return true;
00072 
00073   for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
00074       ++I)
00075     if (const Stmt *child = *I)
00076       if (containsBuiltinOffsetOf(child))
00077         return true;
00078 
00079   return false;
00080 }