aboutsummaryrefslogtreecommitdiff
path: root/Analysis/GRConstantPropagation.cpp
blob: 8bc0b3b89ef6bc44dc074c09c78400f30ce41b1c (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
88
89
90
91
92
93
94
95
96
//===-- GRConstantPropagation.cpp --------------------------------*- C++ -*-==//
//   
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//               Constant Propagation via Graph Reachability
//
//  This files defines a simple analysis that performs path-sensitive
//  constant propagation within a function.  An example use of this analysis
//  is to perform simple checks for NULL dereferences.
//
//===----------------------------------------------------------------------===//

#include "clang/Analysis/PathSensitive/ExplodedGraph.h"
#include "clang/AST/Expr.h"
#include "clang/AST/CFG.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/ImmutableMap.h"

using namespace clang;
using llvm::APInt;
using llvm::APFloat;
using llvm::dyn_cast;
using llvm::cast;

//===----------------------------------------------------------------------===//
// ConstV - Represents a variant over APInt, APFloat, and const char
//===----------------------------------------------------------------------===//

namespace {
class ConstV {
  uintptr_t Data;
public:
  enum VariantType { VTString = 0x0, VTObjCString = 0x1,
                     VTFloat  = 0x2, VTInt = 0x3,
                     Flags    = 0x3 };
  
  ConstV(const StringLiteral* v) 
    : Data(reinterpret_cast<uintptr_t>(v) | VTString) {}
  
  ConstV(const ObjCStringLiteral* v)
    : Data(reinterpret_cast<uintptr_t>(v) | VTObjCString) {} 
           
  ConstV(llvm::APInt* v)
    : Data(reinterpret_cast<uintptr_t>(v) | VTInt) {}
  
  ConstV(llvm::APFloat* v)
    : Data(reinterpret_cast<uintptr_t>(v) | VTFloat) {}
  

  inline void* getData() const { return (void*) (Data & ~Flags); }
  inline VariantType getVT() const { return (VariantType) (Data & Flags); } 
  
  inline void Profile(llvm::FoldingSetNodeID& ID) const {
    ID.AddPointer(getData());
  }
};
} // end anonymous namespace

// Overload machinery for casting from ConstV to contained classes.

namespace llvm {

#define CV_OBJ_CAST(CLASS,FLAG)\
template<> inline bool isa<CLASS,ConstV>(const ConstV& V) {\
  return V.getVT() == FLAG;\
}\
\
template <> struct cast_retty_impl<CLASS, ConstV> {\
  typedef const CLASS* ret_type;\
};
  
CV_OBJ_CAST(APInt,ConstV::VTInt)
CV_OBJ_CAST(APFloat,ConstV::VTFloat)
CV_OBJ_CAST(StringLiteral,ConstV::VTString)
CV_OBJ_CAST(ObjCStringLiteral,ConstV::VTObjCString)  

#undef CV_OBJ_CAST
  
template <> struct simplify_type<ConstV> {
  typedef void* SimpleType;
  static SimpleType getSimplifiedValue(const ConstV &Val) { 
    return Val.getData();
  }
};

} // end llvm namespace