aboutsummaryrefslogtreecommitdiff
path: root/Analysis/ExplodedGraph.cpp
blob: 61548e9b27412bbaffb820c590f47c3cfa8094f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- C++ -*------=//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the template classes ExplodedNode and ExplodedGraph,
//  which represent a path-sensitive, intra-procedural "exploded graph."
//
//===----------------------------------------------------------------------===//

#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
#include <vector>

using namespace clang;


static inline std::vector<ExplodedNodeImpl*>& getVector(void* P) {
  return *reinterpret_cast<std::vector<ExplodedNodeImpl*>*>(P);
}

void ExplodedNodeImpl::NodeGroup::addNode(ExplodedNodeImpl* N) {
  if (getKind() == Size1) {
    if (ExplodedNodeImpl* NOld = getNode()) {
      std::vector<ExplodedNodeImpl*>* V = new std::vector<ExplodedNodeImpl*>();
      V->push_back(NOld);
      V->push_back(N);
      P = reinterpret_cast<uintptr_t>(V) & SizeOther;
    }
    else
      P = reinterpret_cast<uintptr_t>(N);
  }
  else
    getVector(getPtr()).push_back(N);
}

bool ExplodedNodeImpl::NodeGroup::empty() const {
  if (getKind() == Size1)
    return getNode() ? false : true;
  else
    return getVector(getPtr()).empty();
}

unsigned ExplodedNodeImpl::NodeGroup::size() const {
  if (getKind() == Size1)
    return getNode() ? 1 : 0;
  else
    return getVector(getPtr()).size();
}

ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::begin() const {
  if (getKind() == Size1)
    return (ExplodedNodeImpl**) &P;
  else
    return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).begin()));
}

ExplodedNodeImpl** ExplodedNodeImpl::NodeGroup::end() const {
  if (getKind() == Size1)
    return ((ExplodedNodeImpl**) &P)+1;
  else
    return const_cast<ExplodedNodeImpl**>(&*(getVector(getPtr()).rbegin())+1);
}

ExplodedNodeImpl::NodeGroup::~NodeGroup() {
  if (getKind() == SizeOther) delete &getVector(getPtr());
}


ExplodedGraphImpl::~ExplodedGraphImpl() {
  // Delete the FoldingSet's in Nodes.  Note that the contents
  // of the FoldingSets are nodes allocated from the BumpPtrAllocator,
  // so all of those will get nuked when that object is destroyed.
  for (EdgeNodeSetMap::iterator I=Nodes.begin(), E=Nodes.end(); I!=E; ++I) {
    llvm::FoldingSet<ExplodedNodeImpl>* ENodes = 
      reinterpret_cast<llvm::FoldingSet<ExplodedNodeImpl>*>(I->second);
    
    for (llvm::FoldingSet<ExplodedNodeImpl>::iterator
         I=ENodes->begin(), E=ENodes->end(); I!=E; ++I)
      delete &*I;
    
    delete ENodes;
  }
}