1//== ConstraintManager.cpp - Constraints on symbolic values -----*- 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 defined the interface to manage constraints on symbolic values.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
15#include "llvm/Support/SaveAndRestore.h"
16
17using namespace clang;
18using namespace ento;
19
20ConstraintManager::~ConstraintManager() {}
21
22static DefinedSVal getLocFromSymbol(const ProgramStateRef &State,
23                                    SymbolRef Sym) {
24  const MemRegion *R = State->getStateManager().getRegionManager()
25                                               .getSymbolicRegion(Sym);
26  return loc::MemRegionVal(R);
27}
28
29/// Convenience method to query the state to see if a symbol is null or
30/// not null, or neither assumption can be made.
31ConditionTruthVal ConstraintManager::isNull(ProgramStateRef State,
32                                            SymbolRef Sym) {
33  // Disable recursive notification of clients.
34  llvm::SaveAndRestore<bool> DisableNotify(NotifyAssumeClients, false);
35
36  ProgramStateManager &Mgr = State->getStateManager();
37  QualType Ty = Sym->getType(Mgr.getContext());
38  DefinedSVal V = Loc::isLocType(Ty) ? getLocFromSymbol(State, Sym)
39                                     : nonloc::SymbolVal(Sym);
40  const ProgramStatePair &P = assumeDual(State, V);
41  if (P.first && !P.second)
42    return ConditionTruthVal(false);
43  if (!P.first && P.second)
44    return ConditionTruthVal(true);
45  return ConditionTruthVal();
46}
47