BasicValueFactory.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //=== BasicValueFactory.cpp - Basic values for Path Sens analysis --*- 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 BasicValueFactory, a class that manages the lifetime
  11. // of APSInt objects and symbolic constraints used by ExprEngine
  12. // and related classes.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "clang/AST/ASTContext.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
  18. using namespace clang;
  19. using namespace ento;
  20. void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
  21. llvm::ImmutableList<SVal> L) {
  22. T.Profile(ID);
  23. ID.AddPointer(L.getInternalPointer());
  24. }
  25. void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
  26. const StoreRef &store,
  27. const TypedValueRegion *region) {
  28. ID.AddPointer(store.getStore());
  29. ID.AddPointer(region);
  30. }
  31. typedef std::pair<SVal, uintptr_t> SValData;
  32. typedef std::pair<SVal, SVal> SValPair;
  33. namespace llvm {
  34. template<> struct FoldingSetTrait<SValData> {
  35. static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
  36. X.first.Profile(ID);
  37. ID.AddPointer( (void*) X.second);
  38. }
  39. };
  40. template<> struct FoldingSetTrait<SValPair> {
  41. static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
  42. X.first.Profile(ID);
  43. X.second.Profile(ID);
  44. }
  45. };
  46. }
  47. typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData> >
  48. PersistentSValsTy;
  49. typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair> >
  50. PersistentSValPairsTy;
  51. BasicValueFactory::~BasicValueFactory() {
  52. // Note that the dstor for the contents of APSIntSet will never be called,
  53. // so we iterate over the set and invoke the dstor for each APSInt. This
  54. // frees an aux. memory allocated to represent very large constants.
  55. for (APSIntSetTy::iterator I=APSIntSet.begin(), E=APSIntSet.end(); I!=E; ++I)
  56. I->getValue().~APSInt();
  57. delete (PersistentSValsTy*) PersistentSVals;
  58. delete (PersistentSValPairsTy*) PersistentSValPairs;
  59. }
  60. const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
  61. llvm::FoldingSetNodeID ID;
  62. void *InsertPos;
  63. typedef llvm::FoldingSetNodeWrapper<llvm::APSInt> FoldNodeTy;
  64. X.Profile(ID);
  65. FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
  66. if (!P) {
  67. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  68. new (P) FoldNodeTy(X);
  69. APSIntSet.InsertNode(P, InsertPos);
  70. }
  71. return *P;
  72. }
  73. const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
  74. bool isUnsigned) {
  75. llvm::APSInt V(X, isUnsigned);
  76. return getValue(V);
  77. }
  78. const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
  79. bool isUnsigned) {
  80. llvm::APSInt V(BitWidth, isUnsigned);
  81. V = X;
  82. return getValue(V);
  83. }
  84. const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
  85. unsigned bits = Ctx.getTypeSize(T);
  86. llvm::APSInt V(bits,
  87. T->isUnsignedIntegerOrEnumerationType() || Loc::isLocType(T));
  88. V = X;
  89. return getValue(V);
  90. }
  91. const CompoundValData*
  92. BasicValueFactory::getCompoundValData(QualType T,
  93. llvm::ImmutableList<SVal> Vals) {
  94. llvm::FoldingSetNodeID ID;
  95. CompoundValData::Profile(ID, T, Vals);
  96. void *InsertPos;
  97. CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
  98. if (!D) {
  99. D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
  100. new (D) CompoundValData(T, Vals);
  101. CompoundValDataSet.InsertNode(D, InsertPos);
  102. }
  103. return D;
  104. }
  105. const LazyCompoundValData*
  106. BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
  107. const TypedValueRegion *region) {
  108. llvm::FoldingSetNodeID ID;
  109. LazyCompoundValData::Profile(ID, store, region);
  110. void *InsertPos;
  111. LazyCompoundValData *D =
  112. LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
  113. if (!D) {
  114. D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
  115. new (D) LazyCompoundValData(store, region);
  116. LazyCompoundValDataSet.InsertNode(D, InsertPos);
  117. }
  118. return D;
  119. }
  120. const llvm::APSInt*
  121. BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
  122. const llvm::APSInt& V1, const llvm::APSInt& V2) {
  123. switch (Op) {
  124. default:
  125. assert (false && "Invalid Opcode.");
  126. case BO_Mul:
  127. return &getValue( V1 * V2 );
  128. case BO_Div:
  129. return &getValue( V1 / V2 );
  130. case BO_Rem:
  131. return &getValue( V1 % V2 );
  132. case BO_Add:
  133. return &getValue( V1 + V2 );
  134. case BO_Sub:
  135. return &getValue( V1 - V2 );
  136. case BO_Shl: {
  137. // FIXME: This logic should probably go higher up, where we can
  138. // test these conditions symbolically.
  139. // FIXME: Expand these checks to include all undefined behavior.
  140. if (V2.isSigned() && V2.isNegative())
  141. return NULL;
  142. uint64_t Amt = V2.getZExtValue();
  143. if (Amt > V1.getBitWidth())
  144. return NULL;
  145. return &getValue( V1.operator<<( (unsigned) Amt ));
  146. }
  147. case BO_Shr: {
  148. // FIXME: This logic should probably go higher up, where we can
  149. // test these conditions symbolically.
  150. // FIXME: Expand these checks to include all undefined behavior.
  151. if (V2.isSigned() && V2.isNegative())
  152. return NULL;
  153. uint64_t Amt = V2.getZExtValue();
  154. if (Amt > V1.getBitWidth())
  155. return NULL;
  156. return &getValue( V1.operator>>( (unsigned) Amt ));
  157. }
  158. case BO_LT:
  159. return &getTruthValue( V1 < V2 );
  160. case BO_GT:
  161. return &getTruthValue( V1 > V2 );
  162. case BO_LE:
  163. return &getTruthValue( V1 <= V2 );
  164. case BO_GE:
  165. return &getTruthValue( V1 >= V2 );
  166. case BO_EQ:
  167. return &getTruthValue( V1 == V2 );
  168. case BO_NE:
  169. return &getTruthValue( V1 != V2 );
  170. // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
  171. case BO_And:
  172. return &getValue( V1 & V2 );
  173. case BO_Or:
  174. return &getValue( V1 | V2 );
  175. case BO_Xor:
  176. return &getValue( V1 ^ V2 );
  177. }
  178. }
  179. const std::pair<SVal, uintptr_t>&
  180. BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
  181. // Lazily create the folding set.
  182. if (!PersistentSVals) PersistentSVals = new PersistentSValsTy();
  183. llvm::FoldingSetNodeID ID;
  184. void *InsertPos;
  185. V.Profile(ID);
  186. ID.AddPointer((void*) Data);
  187. PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
  188. typedef llvm::FoldingSetNodeWrapper<SValData> FoldNodeTy;
  189. FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
  190. if (!P) {
  191. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  192. new (P) FoldNodeTy(std::make_pair(V, Data));
  193. Map.InsertNode(P, InsertPos);
  194. }
  195. return P->getValue();
  196. }
  197. const std::pair<SVal, SVal>&
  198. BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
  199. // Lazily create the folding set.
  200. if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
  201. llvm::FoldingSetNodeID ID;
  202. void *InsertPos;
  203. V1.Profile(ID);
  204. V2.Profile(ID);
  205. PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
  206. typedef llvm::FoldingSetNodeWrapper<SValPair> FoldNodeTy;
  207. FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
  208. if (!P) {
  209. P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
  210. new (P) FoldNodeTy(std::make_pair(V1, V2));
  211. Map.InsertNode(P, InsertPos);
  212. }
  213. return P->getValue();
  214. }
  215. const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
  216. return &getPersistentSValWithData(X, 0).first;
  217. }