Tom Care | a9fbf5b | 2010-07-27 23:26:07 +0000 | [diff] [blame] | 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/Checker/PathSensitive/CheckerHelpers.h" |
| 15 | #include "clang/AST/Expr.h" |
| 16 | |
| 17 | // Recursively find any substatements containing macros |
| 18 | bool clang::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 |
| 35 | bool clang::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 |
| 51 | bool clang::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 | |
Eli Friedman | 156361d | 2010-08-05 09:43:11 +0000 | [diff] [blame^] | 68 | // Recursively find any substatements containing __builtin_offsetof |
Tom Care | a9fbf5b | 2010-07-27 23:26:07 +0000 | [diff] [blame] | 69 | bool clang::containsBuiltinOffsetOf(const Stmt *S) { |
| 70 | const UnaryOperator *UO = dyn_cast<UnaryOperator>(S); |
| 71 | |
| 72 | if (UO && UO->getOpcode() == UnaryOperator::OffsetOf) |
| 73 | return true; |
| 74 | |
Eli Friedman | 156361d | 2010-08-05 09:43:11 +0000 | [diff] [blame^] | 75 | if (isa<OffsetOfExpr>(S)) |
| 76 | return true; |
| 77 | |
Tom Care | a9fbf5b | 2010-07-27 23:26:07 +0000 | [diff] [blame] | 78 | for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); |
| 79 | ++I) |
| 80 | if (const Stmt *child = *I) |
| 81 | if (containsBuiltinOffsetOf(child)) |
| 82 | return true; |
| 83 | |
| 84 | return false; |
| 85 | } |