clang 17.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
20namespace clang {
21namespace ento {
22namespace 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.
27private:
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
41public:
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
77private:
78 const SymbolRef Begin, End;
79
80 ContainerData(SymbolRef B, SymbolRef E) : Begin(B), End(E) {}
81
82public:
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
113
120
121} // namespace iterator
122
123template<>
124struct ProgramStateTrait<iterator::IteratorSymbolMap>
125 : public ProgramStatePartialTrait<iterator::IteratorSymbolMapTy> {
126 static void *GDMIndex() { static int Index; return &Index; }
127};
128
129template<>
130struct ProgramStateTrait<iterator::IteratorRegionMap>
131 : public ProgramStatePartialTrait<iterator::IteratorRegionMapTy> {
132 static void *GDMIndex() { static int Index; return &Index; }
133};
134
135template<>
136struct ProgramStateTrait<iterator::ContainerMap>
137 : public ProgramStatePartialTrait<iterator::ContainerMapTy> {
138 static void *GDMIndex() { static int Index; return &Index; }
139};
140
141namespace iterator {
142
143bool isIteratorType(const QualType &Type);
144bool isIterator(const CXXRecordDecl *CRD);
146bool isInsertCall(const FunctionDecl *Func);
147bool isEraseCall(const FunctionDecl *Func);
148bool isEraseAfterCall(const FunctionDecl *Func);
149bool isEmplaceCall(const FunctionDecl *Func);
162const ContainerData *getContainerData(ProgramStateRef State,
163 const MemRegion *Cont);
164const 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);
178bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2,
180bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
182
183} // namespace iterator
184} // namespace ento
185} // namespace clang
186
187#endif
#define V(N, I)
Definition: ASTContext.h:3211
static CompilationDatabasePluginRegistry::Add< FixedCompilationDatabasePlugin > X("fixed-compilation-database", "Reads plain-text flags file")
unsigned Offset
Definition: Format.cpp:2774
#define CLANG_ENTO_PROGRAMSTATE_MAP(Key, Value)
Helper for registering a map trait.
SourceLocation Begin
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
Represents a function declaration or definition.
Definition: Decl.h:1919
It wraps the AnalysisDeclContext to represent both the call stack with the help of StackFrameContext ...
A (possibly-)qualified type.
Definition: Type.h:736
Stmt - This represents one statement.
Definition: Stmt.h:72
The base class of the type hierarchy.
Definition: Type.h:1566
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:95
Symbolic value.
Definition: SymExpr.h:29
bool isEraseCall(const FunctionDecl *Func)
Definition: Iterator.cpp:98
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, ContainerData) ContainerMapTy
Definition: Iterator.h:119
bool isIterator(const CXXRecordDecl *CRD)
Definition: Iterator.cpp:27
bool isInsertCall(const FunctionDecl *Func)
Definition: Iterator.cpp:76
bool isIteratorType(const QualType &Type)
Definition: Iterator.cpp:19
bool isAccessOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:126
ProgramStateRef advancePosition(ProgramStateRef State, const SVal &Iter, OverloadedOperatorKind Op, const SVal &Distance)
Definition: Iterator.cpp:224
bool isEmplaceCall(const FunctionDecl *Func)
Definition: Iterator.cpp:87
CLANG_ENTO_PROGRAMSTATE_MAP(SymbolRef, IteratorPosition) IteratorSymbolMapTy
Definition: Iterator.h:115
const IteratorPosition * getIteratorPosition(ProgramStateRef State, const SVal &Val)
Definition: Iterator.cpp:184
bool isEraseAfterCall(const FunctionDecl *Func)
Definition: Iterator.cpp:112
ProgramStateRef assumeNoOverflow(ProgramStateRef State, SymbolRef Sym, long Scale)
Definition: Iterator.cpp:265
ProgramStateRef setIteratorPosition(ProgramStateRef State, const SVal &Val, const IteratorPosition &Pos)
Definition: Iterator.cpp:197
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, IteratorPosition) IteratorRegionMapTy
Definition: Iterator.h:117
bool isRandomIncrOrDecrOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:169
const ContainerData * getContainerData(ProgramStateRef State, const MemRegion *Cont)
Definition: Iterator.cpp:179
bool compare(ProgramStateRef State, SymbolRef Sym1, SymbolRef Sym2, BinaryOperator::Opcode Opc)
Definition: Iterator.cpp:299
bool isDecrementOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:161
bool isComparisonOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:71
bool isDereferenceOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:140
bool isIncrementOperator(OverloadedOperatorKind OK)
Definition: Iterator.cpp:153
ProgramStateRef createIteratorPosition(ProgramStateRef State, const SVal &Val, const MemRegion *Cont, const Stmt *S, const LocationContext *LCtx, unsigned blockCount)
Definition: Iterator.cpp:210
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition: SymExpr.h:111
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
BinaryOperatorKind
@ C
Languages that the frontend can parse and compile.
UnaryOperatorKind
ContainerData newEnd(SymbolRef E) const
Definition: Iterator.h:96
static ContainerData fromEnd(SymbolRef E)
Definition: Iterator.h:87
ContainerData newBegin(SymbolRef B) const
Definition: Iterator.h:94
bool operator==(const ContainerData &X) const
Definition: Iterator.h:98
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Iterator.h:104
bool operator!=(const ContainerData &X) const
Definition: Iterator.h:102
static ContainerData fromBegin(SymbolRef B)
Definition: Iterator.h:83
IteratorPosition reAssign(const MemRegion *NewCont) const
Definition: Iterator.h:58
bool operator!=(const IteratorPosition &X) const
Definition: Iterator.h:66
IteratorPosition setTo(SymbolRef NewOf) const
Definition: Iterator.h:54
bool operator==(const IteratorPosition &X) const
Definition: Iterator.h:62
const MemRegion * getContainer() const
Definition: Iterator.h:42
static IteratorPosition getPosition(const MemRegion *C, SymbolRef Of)
Definition: Iterator.h:50
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Iterator.h:68
IteratorPosition invalidate() const
Definition: Iterator.h:46