CheckerHelpers.cpp revision a9fbf5ba297d77a34d564055f1f05414e0224bf9
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
14a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care#include "clang/Checker/PathSensitive/CheckerHelpers.h"
15a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care#include "clang/AST/Expr.h"
16a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care
17a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// Recursively find any substatements containing macros
18a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Carebool clang::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
35a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Carebool clang::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
51a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Carebool clang::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
68a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care// Recursively find any substatements containing __builtin_offset_of
69a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Carebool clang::containsBuiltinOffsetOf(const Stmt *S) {
70a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care  const UnaryOperator *UO = dyn_cast<UnaryOperator>(S);
71a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care
72a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care  if (UO && UO->getOpcode() == UnaryOperator::OffsetOf)
73a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care    return true;
74a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care
75a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care  for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
76a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care      ++I)
77a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care    if (const Stmt *child = *I)
78a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care      if (containsBuiltinOffsetOf(child))
79a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care        return true;
80a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care
81a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care  return false;
82a9fbf5ba297d77a34d564055f1f05414e0224bf9Tom Care}
83