Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 1 | //=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- 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 the template classes ExplodedNode and ExplodedGraph, |
| 11 | // which represent a path-sensitive, intra-procedural "exploded graph." |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/ExplodedGraph.h" |
| 16 | #include <vector> |
| 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | |
| 21 | static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) { |
| 22 | return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P); |
| 23 | } |
| 24 | |
| 25 | void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) { |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 26 | |
| 27 | assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
| 28 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 29 | if (getKind() == Size1) { |
| 30 | if (ExplodedNodeImpl* NOld = getNode()) { |
| 31 | std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>(); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 32 | assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 33 | V->push_back(NOld); |
| 34 | V->push_back(N); |
Ted Kremenek | 45c63bd | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 35 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 36 | assert (getPtr() == (void*) V); |
| 37 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 38 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 39 | else { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 40 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 41 | assert (getKind() == Size1); |
| 42 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 43 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 44 | else { |
| 45 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 46 | getVector(getPtr()).push_back(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 47 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool ExplodedNodeImpl::NodeGroup::empty() const { |
| 51 | if (getKind() == Size1) |
| 52 | return getNode() ? false : true; |
| 53 | else |
| 54 | return getVector(getPtr()).empty(); |
| 55 | } |
| 56 | |
| 57 | unsigned ExplodedNodeImpl::NodeGroup::size() const { |
| 58 | if (getKind() == Size1) |
| 59 | return getNode() ? 1 : 0; |
| 60 | else |
| 61 | return getVector(getPtr()).size(); |
| 62 | } |
| 63 | |
| 64 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const { |
| 65 | if (getKind() == Size1) |
| 66 | return (ExplodedNodeImpl**) &P; |
| 67 | else |
| 68 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin())); |
| 69 | } |
| 70 | |
| 71 | ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const { |
| 72 | if (getKind() == Size1) |
Ted Kremenek | 160760e | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 73 | return (ExplodedNodeImpl**) (P ? &P+1 : &P); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 74 | else |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 75 | return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).end())); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | ExplodedNodeImpl::NodeGroup::~NodeGroup() { |
| 79 | if (getKind() == SizeOther) delete &getVector(getPtr()); |
| 80 | } |