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 
17 using namespace clang;
18 using namespace ento;
19 
20 ConstraintManager::~ConstraintManager() {}
21 
22 static 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.
31 ConditionTruthVal ConstraintManager::isNull(ProgramStateRef State,
32                                             SymbolRef Sym) {
33   // Disable recursive notification of clients.
34   llvm::SaveAndRestore<bool> DisableNotify(NotifyAssumeClients, false);
35 
36   QualType Ty = Sym->getType();
37   DefinedSVal V = Loc::isLocType(Ty) ? getLocFromSymbol(State, Sym)
38                                      : nonloc::SymbolVal(Sym);
39   const ProgramStatePair &P = assumeDual(State, V);
40   if (P.first && !P.second)
41     return ConditionTruthVal(false);
42   if (!P.first && P.second)
43     return ConditionTruthVal(true);
44   return ConditionTruthVal();
45 }
46