clang  14.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 {
67  return Cont != X.Cont || Valid != X.Valid || Offset != X.Offset;
68  }
69 
70  void Profile(llvm::FoldingSetNodeID &ID) const {
71  ID.AddPointer(Cont);
72  ID.AddInteger(Valid);
73  ID.Add(Offset);
74  }
75 };
76 
77 // Structure to record the symbolic begin and end position of a container
78 struct ContainerData {
79 private:
80  const SymbolRef Begin, End;
81 
82  ContainerData(SymbolRef B, SymbolRef E) : Begin(B), End(E) {}
83 
84 public:
86  return ContainerData(B, nullptr);
87  }
88 
90  return ContainerData(nullptr, E);
91  }
92 
93  SymbolRef getBegin() const { return Begin; }
94  SymbolRef getEnd() const { return End; }
95 
96  ContainerData newBegin(SymbolRef B) const { return ContainerData(B, End); }
97 
99 
100  bool operator==(const ContainerData &X) const {
101  return Begin == X.Begin && End == X.End;
102  }
103 
104  bool operator!=(const ContainerData &X) const {
105  return Begin != X.Begin || End != X.End;
106  }
107 
108  void Profile(llvm::FoldingSetNodeID &ID) const {
109  ID.Add(Begin);
110  ID.Add(End);
111  }
112 };
113 
116 class ContainerMap {};
117 
118 using IteratorSymbolMapTy =
120 using IteratorRegionMapTy =
122 using ContainerMapTy =
124 
125 } // namespace iterator
126 
127 template<>
128 struct ProgramStateTrait<iterator::IteratorSymbolMap>
129  : public ProgramStatePartialTrait<iterator::IteratorSymbolMapTy> {
130  static void *GDMIndex() { static int Index; return &Index; }
131 };
132 
133 template<>
134 struct ProgramStateTrait<iterator::IteratorRegionMap>
135  : public ProgramStatePartialTrait<iterator::IteratorRegionMapTy> {
136  static void *GDMIndex() { static int Index; return &Index; }
137 };
138 
139 template<>
140 struct ProgramStateTrait<iterator::ContainerMap>
141  : public ProgramStatePartialTrait<iterator::ContainerMapTy> {
142  static void *GDMIndex() { static int Index; return &Index; }
143 };
144 
145 namespace iterator {
146 
147 bool isIteratorType(const QualType &Type);
148 bool isIterator(const CXXRecordDecl *CRD);
150 bool isInsertCall(const FunctionDecl *Func);
151 bool isEraseCall(const FunctionDecl *Func);
152 bool isEraseAfterCall(const FunctionDecl *Func);
153 bool isEmplaceCall(const FunctionDecl *Func);
166 const ContainerData *getContainerData(ProgramStateRef State,
167  const MemRegion *Cont);
168 const IteratorPosition *getIteratorPosition(ProgramStateRef State,
169  const SVal &Val);
171  const IteratorPosition &Pos);
173  const MemRegion *Cont, const Stmt* S,
174  const LocationContext *LCtx,
175  unsigned blockCount);
177  const SVal &Iter,
179  const SVal &Distance);
181  long Scale);
184 bool compare(ProgramStateRef State, NonLoc NL1, NonLoc NL2,
186 
187 } // namespace iterator
188 } // namespace ento
189 } // namespace clang
190 
191 #endif
clang::ento::ProgramStatePartialTrait
Definition: ProgramState.h:50
clang::ento::iterator::IteratorRegionMapTy
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, IteratorPosition) IteratorRegionMapTy
Definition: Iterator.h:121
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:116
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:673
clang::ento::iterator::IteratorPosition
Definition: Iterator.h:26
clang::ento::iterator::ContainerData::operator!=
bool operator!=(const ContainerData &X) const
Definition: Iterator.h:104
clang::ento::iterator::ContainerMapTy
CLANG_ENTO_PROGRAMSTATE_MAP(const MemRegion *, ContainerData) ContainerMapTy
Definition: Iterator.h:123
clang::ento::SymbolRef
const SymExpr * SymbolRef
Definition: SymExpr.h:110
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:130
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
Offset
unsigned Offset
Definition: Format.cpp:2335
clang::ento::MemRegion
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
SymExpr.h
V
#define V(N, I)
Definition: ASTContext.h:3121
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:70
clang::ento::iterator::ContainerData::fromEnd
static ContainerData fromEnd(SymbolRef E)
Definition: Iterator.h:89
clang::ento::ProgramStateTrait
Definition: ProgramState.h:52
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:94
clang::ento::iterator::ContainerData::Profile
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: Iterator.h:108
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:98
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:93
CLANG_ENTO_PROGRAMSTATE_MAP
#define CLANG_ENTO_PROGRAMSTATE_MAP(Key, Value)
Helper for registering a map trait.
Definition: ProgramStateTrait.h:70
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:255
Begin
SourceLocation Begin
Definition: USRLocFinder.cpp:165
clang::ento::iterator::IteratorSymbolMap
Definition: Iterator.h:114
State
LineState State
Definition: UnwrappedLineFormatter.cpp:986
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:100
clang::ento::iterator::IteratorPosition::getOffset
SymbolRef getOffset() const
Definition: Iterator.h:44
clang::ento::iterator::ContainerData
Definition: Iterator.h:78
clang::ento::iterator::ContainerData::newBegin
ContainerData newBegin(SymbolRef B) const
Definition: Iterator.h:96
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:48
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:119
clang::ento::ProgramStateTrait< iterator::ContainerMap >::GDMIndex
static void * GDMIndex()
Definition: Iterator.h:142
clang::ento::iterator::ContainerData::fromBegin
static ContainerData fromBegin(SymbolRef B)
Definition: Iterator.h:85
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:136
clang::ento::iterator::IteratorRegionMap
Definition: Iterator.h:115
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:1856
clang::ento::iterator::advancePosition
ProgramStateRef advancePosition(ProgramStateRef State, const SVal &Iter, OverloadedOperatorKind Op, const SVal &Distance)
Definition: Iterator.cpp:224