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