diff options
author | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 13:58:24 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2024-03-13 14:11:53 +0300 |
commit | 11a895b7e15d1c5a1f52706396b82e3f9db953cb (patch) | |
tree | fabc6d883b0f946151f61ae7865cee9f529a1fdd /contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp | |
parent | 9685917341315774aad5733b1793b1e533a88bbb (diff) | |
download | ydb-11a895b7e15d1c5a1f52706396b82e3f9db953cb.tar.gz |
Export clang-format16 via ydblib project
6e6be3a95868fde888d801b7590af4044049563f
Diffstat (limited to 'contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp')
-rw-r--r-- | contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp b/contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp new file mode 100644 index 0000000000..6a86536492 --- /dev/null +++ b/contrib/libs/clang16/lib/StaticAnalyzer/Core/DynamicExtent.cpp @@ -0,0 +1,93 @@ +//===- DynamicExtent.cpp - Dynamic extent related APIs ----------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines APIs that track and query dynamic extent information. +// +//===----------------------------------------------------------------------===// + +#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h" +#include "clang/AST/Expr.h" +#include "clang/Basic/LLVM.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" + +REGISTER_MAP_WITH_PROGRAMSTATE(DynamicExtentMap, const clang::ento::MemRegion *, + clang::ento::DefinedOrUnknownSVal) + +namespace clang { +namespace ento { + +DefinedOrUnknownSVal getDynamicExtent(ProgramStateRef State, + const MemRegion *MR, SValBuilder &SVB) { + MR = MR->StripCasts(); + + if (const DefinedOrUnknownSVal *Size = State->get<DynamicExtentMap>(MR)) + return *Size; + + return MR->getMemRegionManager().getStaticSize(MR, SVB); +} + +DefinedOrUnknownSVal getElementExtent(QualType Ty, SValBuilder &SVB) { + return SVB.makeIntVal(SVB.getContext().getTypeSizeInChars(Ty).getQuantity(), + SVB.getArrayIndexType()); +} + +DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State, + const MemRegion *MR, + SValBuilder &SVB, + QualType ElementTy) { + assert(MR != nullptr && "Not-null region expected"); + MR = MR->StripCasts(); + + DefinedOrUnknownSVal Size = getDynamicExtent(State, MR, SVB); + SVal ElementSize = getElementExtent(ElementTy, SVB); + + SVal ElementCount = + SVB.evalBinOp(State, BO_Div, Size, ElementSize, SVB.getArrayIndexType()); + + return ElementCount.castAs<DefinedOrUnknownSVal>(); +} + +SVal getDynamicExtentWithOffset(ProgramStateRef State, SVal BufV) { + SValBuilder &SvalBuilder = State->getStateManager().getSValBuilder(); + const MemRegion *MRegion = BufV.getAsRegion(); + if (!MRegion) + return UnknownVal(); + RegionOffset Offset = MRegion->getAsOffset(); + if (Offset.hasSymbolicOffset()) + return UnknownVal(); + const MemRegion *BaseRegion = MRegion->getBaseRegion(); + if (!BaseRegion) + return UnknownVal(); + + NonLoc OffsetInBytes = SvalBuilder.makeArrayIndex( + Offset.getOffset() / + MRegion->getMemRegionManager().getContext().getCharWidth()); + DefinedOrUnknownSVal ExtentInBytes = + getDynamicExtent(State, BaseRegion, SvalBuilder); + + return SvalBuilder.evalBinOp(State, BinaryOperator::Opcode::BO_Sub, + ExtentInBytes, OffsetInBytes, + SvalBuilder.getArrayIndexType()); +} + +ProgramStateRef setDynamicExtent(ProgramStateRef State, const MemRegion *MR, + DefinedOrUnknownSVal Size, SValBuilder &SVB) { + MR = MR->StripCasts(); + + if (Size.isUnknown()) + return State; + + return State->set<DynamicExtentMap>(MR->StripCasts(), Size); +} + +} // namespace ento +} // namespace clang |