1 //===- ConstraintManager.cpp - Constraints on symbolic values. ------------===//
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/ConstraintManager.h"
15 #include "clang/AST/Type.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
20 
21 using namespace clang;
22 using namespace ento;
23 
24 ConstraintManager::~ConstraintManager() = default;
25 
26 static DefinedSVal getLocFromSymbol(const ProgramStateRef &State,
27                                     SymbolRef Sym) {
28   const MemRegion *R =
29       State->getStateManager().getRegionManager().getSymbolicRegion(Sym);
30   return loc::MemRegionVal(R);
31 }
32 
33 ConditionTruthVal ConstraintManager::checkNull(ProgramStateRef State,
34                                                SymbolRef Sym) {
35   QualType Ty = Sym->getType();
36   DefinedSVal V = Loc::isLocType(Ty) ? getLocFromSymbol(State, Sym)
37                                      : nonloc::SymbolVal(Sym);
38   const ProgramStatePair &P = assumeDual(State, V);
39   if (P.first && !P.second)
40     return ConditionTruthVal(false);
41   if (!P.first && P.second)
42     return ConditionTruthVal(true);
43   return {};
44 }
45