RangeConstraintManager.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. //== RangeConstraintManager.cpp - Manage range constraints.------*- 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 RangeConstraintManager, a class that tracks simple
  11. // equality and inequality constraints on symbolic values of ProgramState.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
  18. #include "llvm/ADT/FoldingSet.h"
  19. #include "llvm/ADT/ImmutableSet.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace clang;
  22. using namespace ento;
  23. void RangeSet::IntersectInRange(BasicValueFactory &BV, Factory &F,
  24. const llvm::APSInt &Lower, const llvm::APSInt &Upper,
  25. PrimRangeSet &newRanges, PrimRangeSet::iterator &i,
  26. PrimRangeSet::iterator &e) const {
  27. // There are six cases for each range R in the set:
  28. // 1. R is entirely before the intersection range.
  29. // 2. R is entirely after the intersection range.
  30. // 3. R contains the entire intersection range.
  31. // 4. R starts before the intersection range and ends in the middle.
  32. // 5. R starts in the middle of the intersection range and ends after it.
  33. // 6. R is entirely contained in the intersection range.
  34. // These correspond to each of the conditions below.
  35. for (/* i = begin(), e = end() */; i != e; ++i) {
  36. if (i->To() < Lower) {
  37. continue;
  38. }
  39. if (i->From() > Upper) {
  40. break;
  41. }
  42. if (i->Includes(Lower)) {
  43. if (i->Includes(Upper)) {
  44. newRanges =
  45. F.add(newRanges, Range(BV.getValue(Lower), BV.getValue(Upper)));
  46. break;
  47. } else
  48. newRanges = F.add(newRanges, Range(BV.getValue(Lower), i->To()));
  49. } else {
  50. if (i->Includes(Upper)) {
  51. newRanges = F.add(newRanges, Range(i->From(), BV.getValue(Upper)));
  52. break;
  53. } else
  54. newRanges = F.add(newRanges, *i);
  55. }
  56. }
  57. }
  58. const llvm::APSInt &RangeSet::getMinValue() const {
  59. assert(!isEmpty());
  60. return ranges.begin()->From();
  61. }
  62. bool RangeSet::pin(llvm::APSInt &Lower, llvm::APSInt &Upper) const {
  63. // This function has nine cases, the cartesian product of range-testing
  64. // both the upper and lower bounds against the symbol's type.
  65. // Each case requires a different pinning operation.
  66. // The function returns false if the described range is entirely outside
  67. // the range of values for the associated symbol.
  68. APSIntType Type(getMinValue());
  69. APSIntType::RangeTestResultKind LowerTest = Type.testInRange(Lower, true);
  70. APSIntType::RangeTestResultKind UpperTest = Type.testInRange(Upper, true);
  71. switch (LowerTest) {
  72. case APSIntType::RTR_Below:
  73. switch (UpperTest) {
  74. case APSIntType::RTR_Below:
  75. // The entire range is outside the symbol's set of possible values.
  76. // If this is a conventionally-ordered range, the state is infeasible.
  77. if (Lower <= Upper)
  78. return false;
  79. // However, if the range wraps around, it spans all possible values.
  80. Lower = Type.getMinValue();
  81. Upper = Type.getMaxValue();
  82. break;
  83. case APSIntType::RTR_Within:
  84. // The range starts below what's possible but ends within it. Pin.
  85. Lower = Type.getMinValue();
  86. Type.apply(Upper);
  87. break;
  88. case APSIntType::RTR_Above:
  89. // The range spans all possible values for the symbol. Pin.
  90. Lower = Type.getMinValue();
  91. Upper = Type.getMaxValue();
  92. break;
  93. }
  94. break;
  95. case APSIntType::RTR_Within:
  96. switch (UpperTest) {
  97. case APSIntType::RTR_Below:
  98. // The range wraps around, but all lower values are not possible.
  99. Type.apply(Lower);
  100. Upper = Type.getMaxValue();
  101. break;
  102. case APSIntType::RTR_Within:
  103. // The range may or may not wrap around, but both limits are valid.
  104. Type.apply(Lower);
  105. Type.apply(Upper);
  106. break;
  107. case APSIntType::RTR_Above:
  108. // The range starts within what's possible but ends above it. Pin.
  109. Type.apply(Lower);
  110. Upper = Type.getMaxValue();
  111. break;
  112. }
  113. break;
  114. case APSIntType::RTR_Above:
  115. switch (UpperTest) {
  116. case APSIntType::RTR_Below:
  117. // The range wraps but is outside the symbol's set of possible values.
  118. return false;
  119. case APSIntType::RTR_Within:
  120. // The range starts above what's possible but ends within it (wrap).
  121. Lower = Type.getMinValue();
  122. Type.apply(Upper);
  123. break;
  124. case APSIntType::RTR_Above:
  125. // The entire range is outside the symbol's set of possible values.
  126. // If this is a conventionally-ordered range, the state is infeasible.
  127. if (Lower <= Upper)
  128. return false;
  129. // However, if the range wraps around, it spans all possible values.
  130. Lower = Type.getMinValue();
  131. Upper = Type.getMaxValue();
  132. break;
  133. }
  134. break;
  135. }
  136. return true;
  137. }
  138. // Returns a set containing the values in the receiving set, intersected with
  139. // the closed range [Lower, Upper]. Unlike the Range type, this range uses
  140. // modular arithmetic, corresponding to the common treatment of C integer
  141. // overflow. Thus, if the Lower bound is greater than the Upper bound, the
  142. // range is taken to wrap around. This is equivalent to taking the
  143. // intersection with the two ranges [Min, Upper] and [Lower, Max],
  144. // or, alternatively, /removing/ all integers between Upper and Lower.
  145. RangeSet RangeSet::Intersect(BasicValueFactory &BV, Factory &F,
  146. llvm::APSInt Lower, llvm::APSInt Upper) const {
  147. if (!pin(Lower, Upper))
  148. return F.getEmptySet();
  149. PrimRangeSet newRanges = F.getEmptySet();
  150. PrimRangeSet::iterator i = begin(), e = end();
  151. if (Lower <= Upper)
  152. IntersectInRange(BV, F, Lower, Upper, newRanges, i, e);
  153. else {
  154. // The order of the next two statements is important!
  155. // IntersectInRange() does not reset the iteration state for i and e.
  156. // Therefore, the lower range most be handled first.
  157. IntersectInRange(BV, F, BV.getMinValue(Upper), Upper, newRanges, i, e);
  158. IntersectInRange(BV, F, Lower, BV.getMaxValue(Lower), newRanges, i, e);
  159. }
  160. return newRanges;
  161. }
  162. void RangeSet::print(raw_ostream &os) const {
  163. bool isFirst = true;
  164. os << "{ ";
  165. for (iterator i = begin(), e = end(); i != e; ++i) {
  166. if (isFirst)
  167. isFirst = false;
  168. else
  169. os << ", ";
  170. os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
  171. << ']';
  172. }
  173. os << " }";
  174. }
  175. std::unique_ptr<ConstraintManager>
  176. ento::CreateRangeConstraintManager(ProgramStateManager &StMgr, SubEngine *Eng) {
  177. return llvm::make_unique<RangeConstraintManager>(Eng, StMgr.getSValBuilder());
  178. }
  179. bool RangeConstraintManager::canReasonAbout(SVal X) const {
  180. Optional<nonloc::SymbolVal> SymVal = X.getAs<nonloc::SymbolVal>();
  181. if (SymVal && SymVal->isExpression()) {
  182. const SymExpr *SE = SymVal->getSymbol();
  183. if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
  184. switch (SIE->getOpcode()) {
  185. // We don't reason yet about bitwise-constraints on symbolic values.
  186. case BO_And:
  187. case BO_Or:
  188. case BO_Xor:
  189. return false;
  190. // We don't reason yet about these arithmetic constraints on
  191. // symbolic values.
  192. case BO_Mul:
  193. case BO_Div:
  194. case BO_Rem:
  195. case BO_Shl:
  196. case BO_Shr:
  197. return false;
  198. // All other cases.
  199. default:
  200. return true;
  201. }
  202. }
  203. if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
  204. // FIXME: Handle <=> here.
  205. if (BinaryOperator::isEqualityOp(SSE->getOpcode()) ||
  206. BinaryOperator::isRelationalOp(SSE->getOpcode())) {
  207. // We handle Loc <> Loc comparisons, but not (yet) NonLoc <> NonLoc.
  208. if (Loc::isLocType(SSE->getLHS()->getType())) {
  209. assert(Loc::isLocType(SSE->getRHS()->getType()));
  210. return true;
  211. }
  212. }
  213. }
  214. return false;
  215. }
  216. return true;
  217. }
  218. ConditionTruthVal RangeConstraintManager::checkNull(ProgramStateRef State,
  219. SymbolRef Sym) {
  220. const RangeSet *Ranges = State->get<ConstraintRange>(Sym);
  221. // If we don't have any information about this symbol, it's underconstrained.
  222. if (!Ranges)
  223. return ConditionTruthVal();
  224. // If we have a concrete value, see if it's zero.
  225. if (const llvm::APSInt *Value = Ranges->getConcreteValue())
  226. return *Value == 0;
  227. BasicValueFactory &BV = getBasicVals();
  228. APSIntType IntType = BV.getAPSIntType(Sym->getType());
  229. llvm::APSInt Zero = IntType.getZeroValue();
  230. // Check if zero is in the set of possible values.
  231. if (Ranges->Intersect(BV, F, Zero, Zero).isEmpty())
  232. return false;
  233. // Zero is a possible value, but it is not the /only/ possible value.
  234. return ConditionTruthVal();
  235. }
  236. const llvm::APSInt *RangeConstraintManager::getSymVal(ProgramStateRef St,
  237. SymbolRef Sym) const {
  238. const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(Sym);
  239. return T ? T->getConcreteValue() : nullptr;
  240. }
  241. /// Scan all symbols referenced by the constraints. If the symbol is not alive
  242. /// as marked in LSymbols, mark it as dead in DSymbols.
  243. ProgramStateRef
  244. RangeConstraintManager::removeDeadBindings(ProgramStateRef State,
  245. SymbolReaper &SymReaper) {
  246. bool Changed = false;
  247. ConstraintRangeTy CR = State->get<ConstraintRange>();
  248. ConstraintRangeTy::Factory &CRFactory = State->get_context<ConstraintRange>();
  249. for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
  250. SymbolRef Sym = I.getKey();
  251. if (SymReaper.maybeDead(Sym)) {
  252. Changed = true;
  253. CR = CRFactory.remove(CR, Sym);
  254. }
  255. }
  256. return Changed ? State->set<ConstraintRange>(CR) : State;
  257. }
  258. /// Return a range set subtracting zero from \p Domain.
  259. static RangeSet assumeNonZero(
  260. BasicValueFactory &BV,
  261. RangeSet::Factory &F,
  262. SymbolRef Sym,
  263. RangeSet Domain) {
  264. APSIntType IntType = BV.getAPSIntType(Sym->getType());
  265. return Domain.Intersect(BV, F, ++IntType.getZeroValue(),
  266. --IntType.getZeroValue());
  267. }
  268. /// Apply implicit constraints for bitwise OR- and AND-.
  269. /// For unsigned types, bitwise OR with a constant always returns
  270. /// a value greater-or-equal than the constant, and bitwise AND
  271. /// returns a value less-or-equal then the constant.
  272. ///
  273. /// Pattern matches the expression \p Sym against those rule,
  274. /// and applies the required constraints.
  275. /// \p Input Previously established expression range set
  276. static RangeSet applyBitwiseConstraints(
  277. BasicValueFactory &BV,
  278. RangeSet::Factory &F,
  279. RangeSet Input,
  280. const SymIntExpr* SIE) {
  281. QualType T = SIE->getType();
  282. bool IsUnsigned = T->isUnsignedIntegerType();
  283. const llvm::APSInt &RHS = SIE->getRHS();
  284. const llvm::APSInt &Zero = BV.getAPSIntType(T).getZeroValue();
  285. BinaryOperator::Opcode Operator = SIE->getOpcode();
  286. // For unsigned types, the output of bitwise-or is bigger-or-equal than RHS.
  287. if (Operator == BO_Or && IsUnsigned)
  288. return Input.Intersect(BV, F, RHS, BV.getMaxValue(T));
  289. // Bitwise-or with a non-zero constant is always non-zero.
  290. if (Operator == BO_Or && RHS != Zero)
  291. return assumeNonZero(BV, F, SIE, Input);
  292. // For unsigned types, or positive RHS,
  293. // bitwise-and output is always smaller-or-equal than RHS (assuming two's
  294. // complement representation of signed types).
  295. if (Operator == BO_And && (IsUnsigned || RHS >= Zero))
  296. return Input.Intersect(BV, F, BV.getMinValue(T), RHS);
  297. return Input;
  298. }
  299. RangeSet RangeConstraintManager::getRange(ProgramStateRef State,
  300. SymbolRef Sym) {
  301. if (ConstraintRangeTy::data_type *V = State->get<ConstraintRange>(Sym))
  302. return *V;
  303. // Lazily generate a new RangeSet representing all possible values for the
  304. // given symbol type.
  305. BasicValueFactory &BV = getBasicVals();
  306. QualType T = Sym->getType();
  307. RangeSet Result(F, BV.getMinValue(T), BV.getMaxValue(T));
  308. // References are known to be non-zero.
  309. if (T->isReferenceType())
  310. return assumeNonZero(BV, F, Sym, Result);
  311. // Known constraints on ranges of bitwise expressions.
  312. if (const SymIntExpr* SIE = dyn_cast<SymIntExpr>(Sym))
  313. return applyBitwiseConstraints(BV, F, Result, SIE);
  314. return Result;
  315. }
  316. //===------------------------------------------------------------------------===
  317. // assumeSymX methods: protected interface for RangeConstraintManager.
  318. //===------------------------------------------------------------------------===/
  319. // The syntax for ranges below is mathematical, using [x, y] for closed ranges
  320. // and (x, y) for open ranges. These ranges are modular, corresponding with
  321. // a common treatment of C integer overflow. This means that these methods
  322. // do not have to worry about overflow; RangeSet::Intersect can handle such a
  323. // "wraparound" range.
  324. // As an example, the range [UINT_MAX-1, 3) contains five values: UINT_MAX-1,
  325. // UINT_MAX, 0, 1, and 2.
  326. ProgramStateRef
  327. RangeConstraintManager::assumeSymNE(ProgramStateRef St, SymbolRef Sym,
  328. const llvm::APSInt &Int,
  329. const llvm::APSInt &Adjustment) {
  330. // Before we do any real work, see if the value can even show up.
  331. APSIntType AdjustmentType(Adjustment);
  332. if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
  333. return St;
  334. llvm::APSInt Lower = AdjustmentType.convert(Int) - Adjustment;
  335. llvm::APSInt Upper = Lower;
  336. --Lower;
  337. ++Upper;
  338. // [Int-Adjustment+1, Int-Adjustment-1]
  339. // Notice that the lower bound is greater than the upper bound.
  340. RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, Upper, Lower);
  341. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  342. }
  343. ProgramStateRef
  344. RangeConstraintManager::assumeSymEQ(ProgramStateRef St, SymbolRef Sym,
  345. const llvm::APSInt &Int,
  346. const llvm::APSInt &Adjustment) {
  347. // Before we do any real work, see if the value can even show up.
  348. APSIntType AdjustmentType(Adjustment);
  349. if (AdjustmentType.testInRange(Int, true) != APSIntType::RTR_Within)
  350. return nullptr;
  351. // [Int-Adjustment, Int-Adjustment]
  352. llvm::APSInt AdjInt = AdjustmentType.convert(Int) - Adjustment;
  353. RangeSet New = getRange(St, Sym).Intersect(getBasicVals(), F, AdjInt, AdjInt);
  354. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  355. }
  356. RangeSet RangeConstraintManager::getSymLTRange(ProgramStateRef St,
  357. SymbolRef Sym,
  358. const llvm::APSInt &Int,
  359. const llvm::APSInt &Adjustment) {
  360. // Before we do any real work, see if the value can even show up.
  361. APSIntType AdjustmentType(Adjustment);
  362. switch (AdjustmentType.testInRange(Int, true)) {
  363. case APSIntType::RTR_Below:
  364. return F.getEmptySet();
  365. case APSIntType::RTR_Within:
  366. break;
  367. case APSIntType::RTR_Above:
  368. return getRange(St, Sym);
  369. }
  370. // Special case for Int == Min. This is always false.
  371. llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
  372. llvm::APSInt Min = AdjustmentType.getMinValue();
  373. if (ComparisonVal == Min)
  374. return F.getEmptySet();
  375. llvm::APSInt Lower = Min - Adjustment;
  376. llvm::APSInt Upper = ComparisonVal - Adjustment;
  377. --Upper;
  378. return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
  379. }
  380. ProgramStateRef
  381. RangeConstraintManager::assumeSymLT(ProgramStateRef St, SymbolRef Sym,
  382. const llvm::APSInt &Int,
  383. const llvm::APSInt &Adjustment) {
  384. RangeSet New = getSymLTRange(St, Sym, Int, Adjustment);
  385. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  386. }
  387. RangeSet RangeConstraintManager::getSymGTRange(ProgramStateRef St,
  388. SymbolRef Sym,
  389. const llvm::APSInt &Int,
  390. const llvm::APSInt &Adjustment) {
  391. // Before we do any real work, see if the value can even show up.
  392. APSIntType AdjustmentType(Adjustment);
  393. switch (AdjustmentType.testInRange(Int, true)) {
  394. case APSIntType::RTR_Below:
  395. return getRange(St, Sym);
  396. case APSIntType::RTR_Within:
  397. break;
  398. case APSIntType::RTR_Above:
  399. return F.getEmptySet();
  400. }
  401. // Special case for Int == Max. This is always false.
  402. llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
  403. llvm::APSInt Max = AdjustmentType.getMaxValue();
  404. if (ComparisonVal == Max)
  405. return F.getEmptySet();
  406. llvm::APSInt Lower = ComparisonVal - Adjustment;
  407. llvm::APSInt Upper = Max - Adjustment;
  408. ++Lower;
  409. return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
  410. }
  411. ProgramStateRef
  412. RangeConstraintManager::assumeSymGT(ProgramStateRef St, SymbolRef Sym,
  413. const llvm::APSInt &Int,
  414. const llvm::APSInt &Adjustment) {
  415. RangeSet New = getSymGTRange(St, Sym, Int, Adjustment);
  416. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  417. }
  418. RangeSet RangeConstraintManager::getSymGERange(ProgramStateRef St,
  419. SymbolRef Sym,
  420. const llvm::APSInt &Int,
  421. const llvm::APSInt &Adjustment) {
  422. // Before we do any real work, see if the value can even show up.
  423. APSIntType AdjustmentType(Adjustment);
  424. switch (AdjustmentType.testInRange(Int, true)) {
  425. case APSIntType::RTR_Below:
  426. return getRange(St, Sym);
  427. case APSIntType::RTR_Within:
  428. break;
  429. case APSIntType::RTR_Above:
  430. return F.getEmptySet();
  431. }
  432. // Special case for Int == Min. This is always feasible.
  433. llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
  434. llvm::APSInt Min = AdjustmentType.getMinValue();
  435. if (ComparisonVal == Min)
  436. return getRange(St, Sym);
  437. llvm::APSInt Max = AdjustmentType.getMaxValue();
  438. llvm::APSInt Lower = ComparisonVal - Adjustment;
  439. llvm::APSInt Upper = Max - Adjustment;
  440. return getRange(St, Sym).Intersect(getBasicVals(), F, Lower, Upper);
  441. }
  442. ProgramStateRef
  443. RangeConstraintManager::assumeSymGE(ProgramStateRef St, SymbolRef Sym,
  444. const llvm::APSInt &Int,
  445. const llvm::APSInt &Adjustment) {
  446. RangeSet New = getSymGERange(St, Sym, Int, Adjustment);
  447. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  448. }
  449. RangeSet RangeConstraintManager::getSymLERange(
  450. llvm::function_ref<RangeSet()> RS,
  451. const llvm::APSInt &Int,
  452. const llvm::APSInt &Adjustment) {
  453. // Before we do any real work, see if the value can even show up.
  454. APSIntType AdjustmentType(Adjustment);
  455. switch (AdjustmentType.testInRange(Int, true)) {
  456. case APSIntType::RTR_Below:
  457. return F.getEmptySet();
  458. case APSIntType::RTR_Within:
  459. break;
  460. case APSIntType::RTR_Above:
  461. return RS();
  462. }
  463. // Special case for Int == Max. This is always feasible.
  464. llvm::APSInt ComparisonVal = AdjustmentType.convert(Int);
  465. llvm::APSInt Max = AdjustmentType.getMaxValue();
  466. if (ComparisonVal == Max)
  467. return RS();
  468. llvm::APSInt Min = AdjustmentType.getMinValue();
  469. llvm::APSInt Lower = Min - Adjustment;
  470. llvm::APSInt Upper = ComparisonVal - Adjustment;
  471. return RS().Intersect(getBasicVals(), F, Lower, Upper);
  472. }
  473. RangeSet RangeConstraintManager::getSymLERange(ProgramStateRef St,
  474. SymbolRef Sym,
  475. const llvm::APSInt &Int,
  476. const llvm::APSInt &Adjustment) {
  477. return getSymLERange([&] { return getRange(St, Sym); }, Int, Adjustment);
  478. }
  479. ProgramStateRef
  480. RangeConstraintManager::assumeSymLE(ProgramStateRef St, SymbolRef Sym,
  481. const llvm::APSInt &Int,
  482. const llvm::APSInt &Adjustment) {
  483. RangeSet New = getSymLERange(St, Sym, Int, Adjustment);
  484. return New.isEmpty() ? nullptr : St->set<ConstraintRange>(Sym, New);
  485. }
  486. ProgramStateRef RangeConstraintManager::assumeSymWithinInclusiveRange(
  487. ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
  488. const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
  489. RangeSet New = getSymGERange(State, Sym, From, Adjustment);
  490. if (New.isEmpty())
  491. return nullptr;
  492. RangeSet Out = getSymLERange([&] { return New; }, To, Adjustment);
  493. return Out.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, Out);
  494. }
  495. ProgramStateRef RangeConstraintManager::assumeSymOutsideInclusiveRange(
  496. ProgramStateRef State, SymbolRef Sym, const llvm::APSInt &From,
  497. const llvm::APSInt &To, const llvm::APSInt &Adjustment) {
  498. RangeSet RangeLT = getSymLTRange(State, Sym, From, Adjustment);
  499. RangeSet RangeGT = getSymGTRange(State, Sym, To, Adjustment);
  500. RangeSet New(RangeLT.addRange(F, RangeGT));
  501. return New.isEmpty() ? nullptr : State->set<ConstraintRange>(Sym, New);
  502. }
  503. //===------------------------------------------------------------------------===
  504. // Pretty-printing.
  505. //===------------------------------------------------------------------------===/
  506. void RangeConstraintManager::print(ProgramStateRef St, raw_ostream &Out,
  507. const char *nl, const char *sep) {
  508. ConstraintRangeTy Ranges = St->get<ConstraintRange>();
  509. if (Ranges.isEmpty()) {
  510. Out << nl << sep << "Ranges are empty." << nl;
  511. return;
  512. }
  513. Out << nl << sep << "Ranges of symbol values:";
  514. for (ConstraintRangeTy::iterator I = Ranges.begin(), E = Ranges.end(); I != E;
  515. ++I) {
  516. Out << nl << ' ' << I.getKey() << " : ";
  517. I.getData().print(Out);
  518. }
  519. Out << nl;
  520. }