blob: be9fee548f255c546a05a77d1d8e013c44f5b12e [file] [log] [blame]
Tom Carea9fbf5b2010-07-27 23:26:07 +00001//===---- 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
Ted Kremenek21142582010-12-23 19:38:26 +000014#include "clang/StaticAnalyzer/PathSensitive/CheckerHelpers.h"
Tom Carea9fbf5b2010-07-27 23:26:07 +000015#include "clang/AST/Expr.h"
16
17// Recursively find any substatements containing macros
Ted Kremenek9ef65372010-12-23 07:20:52 +000018bool clang::ento::containsMacro(const Stmt *S) {
Tom Carea9fbf5b2010-07-27 23:26:07 +000019 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
Ted Kremenek9ef65372010-12-23 07:20:52 +000035bool clang::ento::containsEnum(const Stmt *S) {
Tom Carea9fbf5b2010-07-27 23:26:07 +000036 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
Ted Kremenek9ef65372010-12-23 07:20:52 +000051bool clang::ento::containsStaticLocal(const Stmt *S) {
Tom Carea9fbf5b2010-07-27 23:26:07 +000052 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
Eli Friedman156361d2010-08-05 09:43:11 +000068// Recursively find any substatements containing __builtin_offsetof
Ted Kremenek9ef65372010-12-23 07:20:52 +000069bool clang::ento::containsBuiltinOffsetOf(const Stmt *S) {
Eli Friedman156361d2010-08-05 09:43:11 +000070 if (isa<OffsetOfExpr>(S))
71 return true;
72
Tom Carea9fbf5b2010-07-27 23:26:07 +000073 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}