clang  15.0.0git
Iterator.h
Go to the documentation of this file.
1 //=== Iterator.h - Common functions for iterator checkers. ---------*- C++ -*-//
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 // Defines common functions to be used by the itertor checkers .
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ITERATOR_H
14 #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_ITERATOR_H
15 
19 
20 namespace clang {
21 namespace ento {
22 namespace iterator {
23 
24 // Abstract position of an iterator. This helps to handle all three kinds
25 // of operators in a common way by using a symbolic position.
27 private:
28 
29  // Container the iterator belongs to
30  const MemRegion *Cont;
31 
32  // Whether iterator is valid
33  const bool Valid;
34 
35  // Abstract offset
36  const SymbolRef Offset;
37 
38  IteratorPosition(const MemRegion *C, bool V, SymbolRef Of)
39  : Cont(C), Valid(V), Offset(Of) {}
40 
41 public:
42  const MemRegion *getContainer() const { return Cont; }
43  bool isValid() const { return Valid; }
44  SymbolRef getOffset() const { return Offset; }
45 
47  return IteratorPosition(Cont, false, Offset);
48  }
49 
51  return IteratorPosition(C, true, Of);
52  }
53 
55  return IteratorPosition(Cont, Valid, NewOf);
56  }
57 
58  IteratorPosition reAssign(const MemRegion *NewCont) const {
59  return IteratorPosition(NewCont, Valid, Offset);
60  }
61 
62  bool operator==(const IteratorPosition &X) const {
63  return Cont == X.Cont && Valid == X.Valid && Offset == X.Offset;
64  }
65 
66  bool operator!=(const IteratorPosition &X) const { return !(*this == X); }
67 
68  void Profile(llvm::FoldingSetNodeID &ID) const {
69  ID.AddPointer(Cont);
70  ID.AddInteger(Valid);
71  ID.Add(Offset);
72  }
73 };
74 
75 // Structure to record the symbolic begin and end position of a container
76 struct ContainerData {
77 private:
78  const SymbolRef Begin, End;
79 
80  ContainerData(SymbolRef B, SymbolRef E) : Begin(B), End(E) {}
81 
82 public:
84  return ContainerData(B, nullptr);
85  }
86 
88  return ContainerData(nullptr, E);
89  }
90 
91  SymbolRef getBegin() const { return Begin; }
92  SymbolRef getEnd() const { return End; }
93 
94  ContainerData newBegin(SymbolRef B) const { return ContainerData(B, End); }
95 
97 
98  bool operator==(const ContainerData &X) const {
99  return Begin == X.Begin && End == X.End;
100  }
101 
102  bool operator!=(const ContainerData &X) const { return !(*this == X); }
103 
104  void Profile(llvm::FoldingSetNodeID &ID) const {
105  ID.Add(Begin);
106  ID.Add(End);
107  }
108 };
109 
112 class ContainerMap {};
113 
114 using IteratorSymbolMapTy =
116 using IteratorRegionMapTy =
118 using ContainerMapTy =
120 
121 } // namespace iterator
122 
123 template<>
124 struct ProgramStateTrait<iterator::IteratorSymbolMap>
125  : public ProgramStatePartialTrait<iterator::IteratorSymbolMapTy> {
126  static void *GDMIndex() { static int Index; return &Index; }
127 };
128 
129 template<>
130 struct ProgramStateTrait<iterator::IteratorRegionMap>
131  : public ProgramStatePartialTrait<iterator::IteratorRegionMapTy> {
132  static void *GDMIndex() { static int Index; return &Index; }
133 };
134 
135 template<>
136 struct ProgramStateTrait<iterator::ContainerMap>
137  : public ProgramStatePartialTrait<iterator::ContainerMapTy> {
138  static void *GDMIndex() { static int Index; return &Index; }
139 };
140 
141 namespace iterator {
142 
143 bool isIteratorType(const QualType &Type);
144 bool isIterator(const CXXRecordDecl *CRD);
146 bool isInsertCall(const FunctionDecl *Func);
147 bool isEraseCall(const FunctionDecl *Func);
148 bool isEraseAfterCall(const FunctionDecl *Func);
149 bool isEmplaceCall(const FunctionDecl *Func);
162 const ContainerData *getContainerData(ProgramStateRef State,
163  const MemRegion *Cont);
164 const IteratorPosition *getIteratorPosition(ProgramStateRef State,
165  const SVal &Val);
167  const IteratorPosition &Pos);
169  const MemRegion *Cont, const Stmt* S,
170  const LocationContext *LCtx,
171  unsigned blockCount);
173  const SVal &Iter,
175  const SVal &Distance);
177  long Scale);
180 bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
182 
183 } // namespace iterator
184 } // namespace ento
185 } // namespace clang
186 
187 #endif
clang::ento::ProgramStatePartialTrait
Definition: ProgramStateTrait.h:29
clang::ento::iterator::IteratorRegionMapTy
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, IteratorPosition) IteratorRegionMapTy
Definition: Iterator.h:117
clang::ento::iterator::IteratorPosition::invalidate
IteratorPosition invalidate() const
Definition: Iterator.h:46
clang::LocationContext
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
Definition: AnalysisDeclContext.h:215
clang::ento::iterator::ContainerMap
Definition: Iterator.h:112
clang::ento::iterator::IteratorPosition::operator==
bool operator==(const IteratorPosition &X) const
Definition: Iterator.h:62
clang::ento::iterator::isIteratorType
bool isIteratorType(const QualType &Type)
Definition: Iterator.cpp:19
clang::ento::ProgramStateRef
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
Definition: ProgramState_Fwd.h:37
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:731
clang::ento::iterator::IteratorPosition
Definition: Iterator.h:26
clang::ento::iterator::ContainerData::operator!=
bool operator!=(const ContainerData &X) const
Definition: Iterator.h:102
clang::ento::iterator::ContainerMapTy
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, ContainerData) ContainerMapTy
Definition: Iterator.h:119
clang::ento::SymbolRef
const SymExpr * SymbolRef
Definition: SymExpr.h:111
clang::tooling::X
static ToolExecutorPluginRegistry::Add< AllTUsToolExecutorPlugin > X("all-TUs", "Runs FrontendActions on all TUs in the compilation database. " "Tool results are stored in memory.")
clang::ento::ProgramStateTrait< iterator::IteratorSymbolMap >::GDMIndex
static void * GDMIndex()
Definition: Iterator.h:126
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1556
Offset
unsigned Offset
Definition: Format.cpp:2574
clang::ento::MemRegion
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:95
SymExpr.h
V
#define V(N, I)
Definition: ASTContext.h:3176
clang::ento::iterator::isIterator
bool isIterator(const CXXRecordDecl *CRD)
Definition: Iterator.cpp:27
clang::ento::iterator::createIteratorPosition
ProgramStateRef createIteratorPosition(ProgramStateRef State, const SVal &Val, const MemRegion *Cont, const Stmt *S, const LocationContext *LCtx, unsigned blockCount)
Definition: Iterator.cpp:210
clang::ento::iterator::isComparisonOperator
bool isComparisonOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:71
clang::ento::iterator::assumeNoOverflow
ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym, long Scale)
Definition: Iterator.cpp:265
clang::ento::iterator::IteratorPosition::reAssign
IteratorPosition reAssign(const MemRegion *NewCont) const
Definition: Iterator.h:58
clang::ento::SymExpr
Symbolic value.
Definition: SymExpr.h:29
clang::ento::iterator::IteratorPosition::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Iterator.h:68
clang::ento::iterator::ContainerData::fromEnd
static ContainerData fromEnd(SymbolRef E)
Definition: Iterator.h:87
clang::ento::ProgramStateTrait
Definition: ProgramState.h:50
clang::ento::iterator::getIteratorPosition
const IteratorPosition * getIteratorPosition(ProgramStateRef State, const SVal &Val)
Definition: Iterator.cpp:184
clang::ento::iterator::isIncrementOperator
bool isIncrementOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:153
clang::ento::iterator::ContainerData::getEnd
SymbolRef getEnd() const
Definition: Iterator.h:92
clang::ento::iterator::ContainerData::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Iterator.h:104
clang::ento::iterator::isDereferenceOperator
bool isDereferenceOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:140
clang::ento::iterator::isEraseCall
bool isEraseCall(const FunctionDecl *Func)
Definition: Iterator.cpp:98
clang::ento::iterator::IteratorPosition::operator!=
bool operator!=(const IteratorPosition &X) const
Definition: Iterator.h:66
clang::ento::iterator::ContainerData::newEnd
ContainerData newEnd(SymbolRef E) const
Definition: Iterator.h:96
clang::ento::iterator::IteratorPosition::getPosition
static IteratorPosition getPosition(const MemRegion *C, SymbolRef Of)
Definition: Iterator.h:50
clang::OverloadedOperatorKind
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
clang::ento::iterator::ContainerData::getBegin
SymbolRef getBegin() const
Definition: Iterator.h:91
CLANG_ENTO_PROGRAMSTATE_MAP
#define CLANG_ENTO_PROGRAMSTATE_MAP(Key, Value)
Helper for registering a map trait.
Definition: ProgramStateTrait.h:73
clang::ento::iterator::getContainerData
const ContainerData * getContainerData(ProgramStateRef State, const MemRegion *Cont)
Definition: Iterator.cpp:179
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
Begin
SourceLocation Begin
Definition: USRLocFinder.cpp:165
clang::ento::iterator::IteratorSymbolMap
Definition: Iterator.h:110
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1126
clang::UnaryOperatorKind
UnaryOperatorKind
Definition: OperationKinds.h:30
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
clang::ento::iterator::ContainerData::operator==
bool operator==(const ContainerData &X) const
Definition: Iterator.h:98
clang::ento::iterator::IteratorPosition::getOffset
SymbolRef getOffset() const
Definition: Iterator.h:44
clang::ento::iterator::ContainerData
Definition: Iterator.h:76
clang::ento::iterator::ContainerData::newBegin
ContainerData newBegin(SymbolRef B) const
Definition: Iterator.h:94
ProgramState.h
clang::ento::iterator::IteratorPosition::getContainer
const MemRegion * getContainer() const
Definition: Iterator.h:42
clang::ento::iterator::compare
bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2, BinaryOperator::Opcode Opc)
Definition: Iterator.cpp:299
clang::ento::iterator::isEraseAfterCall
bool isEraseAfterCall(const FunctionDecl *Func)
Definition: Iterator.cpp:112
clang::ento::iterator::IteratorPosition::isValid
bool isValid() const
Definition: Iterator.h:43
clang::Builtin::ID
ID
Definition: Builtins.h:52
clang
Definition: CalledOnceCheck.h:17
clang::ento::iterator::isRandomIncrOrDecrOperator
bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:169
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::ento::iterator::isEmplaceCall
bool isEmplaceCall(const FunctionDecl *Func)
Definition: Iterator.cpp:87
clang::ento::iterator::setIteratorPosition
ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val, const IteratorPosition &Pos)
Definition: Iterator.cpp:197
clang::ento::iterator::IteratorSymbolMapTy
CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef, IteratorPosition) IteratorSymbolMapTy
Definition: Iterator.h:115
clang::ento::ProgramStateTrait< iterator::ContainerMap >::GDMIndex
static void * GDMIndex()
Definition: Iterator.h:138
clang::ento::iterator::ContainerData::fromBegin
static ContainerData fromBegin(SymbolRef B)
Definition: Iterator.h:83
clang::ento::iterator::isInsertCall
bool isInsertCall(const FunctionDecl *Func)
Definition: Iterator.cpp:76
clang::ento::iterator::isAccessOperator
bool isAccessOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:126
clang::ento::iterator::isDecrementOperator
bool isDecrementOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:161
DynamicType.h
clang::ento::ProgramStateTrait< iterator::IteratorRegionMap >::GDMIndex
static void * GDMIndex()
Definition: Iterator.h:132
clang::ento::iterator::IteratorRegionMap
Definition: Iterator.h:111
clang::ento::iterator::IteratorPosition::setTo
IteratorPosition setTo(SymbolRef NewOf) const
Definition: Iterator.h:54
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1872
clang::ento::iterator::advancePosition
ProgramStateRef advancePosition(ProgramStateRef State, const SVal &Iter, OverloadedOperatorKind Op, const SVal &Distance)
Definition: Iterator.cpp:224