ConstraintManager.cpp 1.7 KB

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