| 1 | //===--- OpaqueSTLFunctionsModeling.cpp -----------------------------------===// |
| 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 | // Models STL functions whose best accurate model is to invalidate their |
| 10 | // arguments. Only functions where this simple approach is sufficient and won't |
| 11 | // interfere with the modeling of other checkers should be put here. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
| 16 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 17 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h" |
| 18 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace ento; |
| 23 | |
| 24 | namespace { |
| 25 | class OpaqueSTLFunctionsModeling : public Checker<eval::Call> { |
| 26 | public: |
| 27 | bool evalCall(const CallEvent &Call, CheckerContext &C) const; |
| 28 | |
| 29 | private: |
| 30 | const CallDescriptionSet ModeledFunctions{ |
| 31 | {CDM::SimpleFunc, {"std" , "sort" }}, |
| 32 | {CDM::SimpleFunc, {"std" , "stable_sort" }}, |
| 33 | {CDM::SimpleFunc, {"std" , "inplace_merge" }}}; |
| 34 | }; |
| 35 | } // namespace |
| 36 | |
| 37 | bool OpaqueSTLFunctionsModeling::evalCall(const CallEvent &Call, |
| 38 | CheckerContext &C) const { |
| 39 | if (!ModeledFunctions.contains(Call)) |
| 40 | return false; |
| 41 | |
| 42 | ProgramStateRef InvalidatedRegionsState = |
| 43 | Call.invalidateRegions(BlockCount: C.blockCount(), State: C.getState()); |
| 44 | C.addTransition(State: InvalidatedRegionsState); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | void ento::registerOpaqueSTLFunctionsModeling(CheckerManager &Mgr) { |
| 49 | Mgr.registerChecker<OpaqueSTLFunctionsModeling>(); |
| 50 | } |
| 51 | |
| 52 | bool ento::shouldRegisterOpaqueSTLFunctionsModeling(const CheckerManager &Mgr) { |
| 53 | return Mgr.getLangOpts().CPlusPlus; |
| 54 | } |
| 55 | |