1//===- ConstraintManager.cpp - Constraints on symbolic values. ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9//  This file defined the interface to manage constraints on symbolic values.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h"
14#include "clang/AST/Type.h"
15#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
16#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19
20using namespace clang;
21using namespace ento;
22
23ConstraintManager::~ConstraintManager() = default;
24
25static DefinedSVal getLocFromSymbol(const ProgramStateRef &State,
26                                    SymbolRef Sym) {
27  const MemRegion *R =
28      State->getStateManager().getRegionManager().getSymbolicRegion(Sym);
29  return loc::MemRegionVal(R);
30}
31
32ConditionTruthVal ConstraintManager::checkNull(ProgramStateRef State,
33                                               SymbolRef Sym) {
34  QualType Ty = Sym->getType();
35  DefinedSVal V = Loc::isLocType(Ty) ? getLocFromSymbol(State, Sym)
36                                     : nonloc::SymbolVal(Sym);
37  const ProgramStatePair &P = assumeDual(State, V);
38  if (P.first && !P.second)
39    return ConditionTruthVal(false);
40  if (!P.first && P.second)
41    return ConditionTruthVal(true);
42  return {};
43}
44