1 //==- ProgramPoint.h - Program Points for Path-Sensitive Analysis --*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface ProgramPoint, which identifies a
11 // distinct location in a function.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_ANALYSIS_PROGRAM_POINT
16 #define LLVM_CLANG_ANALYSIS_PROGRAM_POINT
18 #include "clang/Analysis/AnalysisContext.h"
19 #include "clang/Analysis/CFG.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/FoldingSet.h"
23 #include "llvm/Support/Casting.h"
29 class LocationContext;
30 class AnalysisContext;
35 enum Kind { BlockEdgeKind,
44 PostPurgeDeadSymbolsKind,
51 MinPostStmtKind = PostStmtKind,
52 MaxPostStmtKind = CallExitKind };
55 std::pair<const void *, const void *> Data;
58 // The LocationContext could be NULL to allow ProgramPoint to be used in
59 // context insensitive analysis.
60 const LocationContext *L;
64 ProgramPoint(const void* P, Kind k, const LocationContext *l,
66 : Data(P, static_cast<const void*>(NULL)), K(k), L(l), Tag(tag) {}
68 ProgramPoint(const void* P1, const void* P2, Kind k, const LocationContext *l,
70 : Data(P1, P2), K(k), L(l), Tag(tag) {}
73 const void* getData1() const { return Data.first; }
74 const void* getData2() const { return Data.second; }
77 Kind getKind() const { return K; }
79 const void *getTag() const { return Tag; }
81 const LocationContext *getLocationContext() const { return L; }
83 // For use with DenseMap. This hash is probably slow.
84 unsigned getHashValue() const {
85 llvm::FoldingSetNodeID ID;
87 return ID.ComputeHash();
90 static bool classof(const ProgramPoint*) { return true; }
92 bool operator==(const ProgramPoint & RHS) const {
93 return K == RHS.K && Data == RHS.Data && L == RHS.L && Tag == RHS.Tag;
96 bool operator!=(const ProgramPoint& RHS) const {
97 return K != RHS.K || Data != RHS.Data || L != RHS.L || Tag != RHS.Tag;
100 void Profile(llvm::FoldingSetNodeID& ID) const {
101 ID.AddInteger((unsigned) K);
102 ID.AddPointer(Data.first);
103 ID.AddPointer(Data.second);
109 class BlockEntrance : public ProgramPoint {
111 BlockEntrance(const CFGBlock* B, const LocationContext *L,
113 : ProgramPoint(B, BlockEntranceKind, L, tag) {}
115 const CFGBlock* getBlock() const {
116 return reinterpret_cast<const CFGBlock*>(getData1());
119 const CFGElement getFirstElement() const {
120 const CFGBlock* B = getBlock();
121 return B->empty() ? CFGElement() : B->front();
124 /// Create a new BlockEntrance object that is the same as the original
125 /// except for using the specified tag value.
126 BlockEntrance withTag(const void *tag) {
127 return BlockEntrance(getBlock(), getLocationContext(), tag);
130 static bool classof(const ProgramPoint* Location) {
131 return Location->getKind() == BlockEntranceKind;
135 class BlockExit : public ProgramPoint {
137 BlockExit(const CFGBlock* B, const LocationContext *L)
138 : ProgramPoint(B, BlockExitKind, L) {}
140 const CFGBlock* getBlock() const {
141 return reinterpret_cast<const CFGBlock*>(getData1());
144 const Stmt* getTerminator() const {
145 return getBlock()->getTerminator();
148 static bool classof(const ProgramPoint* Location) {
149 return Location->getKind() == BlockExitKind;
153 class StmtPoint : public ProgramPoint {
155 StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
157 : ProgramPoint(S, p2, k, L, tag) {}
159 const Stmt *getStmt() const { return (const Stmt*) getData1(); }
161 template <typename T>
162 const T* getStmtAs() const { return llvm::dyn_cast<T>(getStmt()); }
164 static bool classof(const ProgramPoint* Location) {
165 unsigned k = Location->getKind();
166 return k >= PreStmtKind && k <= MaxPostStmtKind;
171 class PreStmt : public StmtPoint {
173 PreStmt(const Stmt *S, const LocationContext *L, const void *tag,
174 const Stmt *SubStmt = 0)
175 : StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
177 const Stmt *getSubStmt() const { return (const Stmt*) getData2(); }
179 static bool classof(const ProgramPoint* Location) {
180 return Location->getKind() == PreStmtKind;
184 class PostStmt : public StmtPoint {
186 PostStmt(const Stmt* S, const void* data, Kind k, const LocationContext *L,
188 : StmtPoint(S, data, k, L, tag) {}
191 explicit PostStmt(const Stmt* S, Kind k,
192 const LocationContext *L, const void *tag = 0)
193 : StmtPoint(S, NULL, k, L, tag) {}
195 explicit PostStmt(const Stmt* S, const LocationContext *L,const void *tag = 0)
196 : StmtPoint(S, NULL, PostStmtKind, L, tag) {}
198 static bool classof(const ProgramPoint* Location) {
199 unsigned k = Location->getKind();
200 return k >= MinPostStmtKind && k <= MaxPostStmtKind;
204 class PostStmtCustom : public PostStmt {
206 PostStmtCustom(const Stmt* S,
207 const std::pair<const void*, const void*>* TaggedData,\
208 const LocationContext *L)
209 : PostStmt(S, TaggedData, PostStmtCustomKind, L) {}
211 const std::pair<const void*, const void*>& getTaggedPair() const {
213 *reinterpret_cast<const std::pair<const void*, const void*>*>(getData2());
216 const void* getTag() const { return getTaggedPair().first; }
218 const void* getTaggedData() const { return getTaggedPair().second; }
220 static bool classof(const ProgramPoint* Location) {
221 return Location->getKind() == PostStmtCustomKind;
225 // PostCondition represents the post program point of a branch condition.
226 class PostCondition : public PostStmt {
228 PostCondition(const Stmt* S, const LocationContext *L, const void *tag = 0)
229 : PostStmt(S, PostConditionKind, L, tag) {}
231 static bool classof(const ProgramPoint* Location) {
232 return Location->getKind() == PostConditionKind;
236 class LocationCheck : public StmtPoint {
238 LocationCheck(const Stmt *S, const LocationContext *L,
239 ProgramPoint::Kind K, const void *tag)
240 : StmtPoint(S, NULL, K, L, tag) {}
242 static bool classof(const ProgramPoint *location) {
243 unsigned k = location->getKind();
244 return k == PreLoadKind || k == PreStoreKind;
248 class PreLoad : public LocationCheck {
250 PreLoad(const Stmt *S, const LocationContext *L, const void *tag = 0)
251 : LocationCheck(S, L, PreLoadKind, tag) {}
253 static bool classof(const ProgramPoint *location) {
254 return location->getKind() == PreLoadKind;
258 class PreStore : public LocationCheck {
260 PreStore(const Stmt *S, const LocationContext *L, const void *tag = 0)
261 : LocationCheck(S, L, PreStoreKind, tag) {}
263 static bool classof(const ProgramPoint *location) {
264 return location->getKind() == PreStoreKind;
268 class PostLoad : public PostStmt {
270 PostLoad(const Stmt* S, const LocationContext *L, const void *tag = 0)
271 : PostStmt(S, PostLoadKind, L, tag) {}
273 static bool classof(const ProgramPoint* Location) {
274 return Location->getKind() == PostLoadKind;
278 class PostStore : public PostStmt {
280 PostStore(const Stmt* S, const LocationContext *L, const void *tag = 0)
281 : PostStmt(S, PostStoreKind, L, tag) {}
283 static bool classof(const ProgramPoint* Location) {
284 return Location->getKind() == PostStoreKind;
288 class PostLValue : public PostStmt {
290 PostLValue(const Stmt* S, const LocationContext *L, const void *tag = 0)
291 : PostStmt(S, PostLValueKind, L, tag) {}
293 static bool classof(const ProgramPoint* Location) {
294 return Location->getKind() == PostLValueKind;
298 class PostPurgeDeadSymbols : public PostStmt {
300 PostPurgeDeadSymbols(const Stmt* S, const LocationContext *L,
302 : PostStmt(S, PostPurgeDeadSymbolsKind, L, tag) {}
304 static bool classof(const ProgramPoint* Location) {
305 return Location->getKind() == PostPurgeDeadSymbolsKind;
309 class BlockEdge : public ProgramPoint {
311 BlockEdge(const CFGBlock* B1, const CFGBlock* B2, const LocationContext *L)
312 : ProgramPoint(B1, B2, BlockEdgeKind, L) {}
314 const CFGBlock* getSrc() const {
315 return static_cast<const CFGBlock*>(getData1());
318 const CFGBlock* getDst() const {
319 return static_cast<const CFGBlock*>(getData2());
322 static bool classof(const ProgramPoint* Location) {
323 return Location->getKind() == BlockEdgeKind;
327 class PostInitializer : public ProgramPoint {
329 PostInitializer(const CXXCtorInitializer *I,
330 const LocationContext *L)
331 : ProgramPoint(I, PostInitializerKind, L) {}
333 static bool classof(const ProgramPoint *Location) {
334 return Location->getKind() == PostInitializerKind;
338 class CallEnter : public StmtPoint {
340 CallEnter(const Stmt *stmt, const StackFrameContext *calleeCtx,
341 const LocationContext *callerCtx)
342 : StmtPoint(stmt, calleeCtx, CallEnterKind, callerCtx, 0) {}
344 const Stmt *getCallExpr() const {
345 return static_cast<const Stmt *>(getData1());
348 const StackFrameContext *getCalleeContext() const {
349 return static_cast<const StackFrameContext *>(getData2());
352 static bool classof(const ProgramPoint *Location) {
353 return Location->getKind() == CallEnterKind;
357 class CallExit : public StmtPoint {
359 // CallExit uses the callee's location context.
360 CallExit(const Stmt *S, const LocationContext *L)
361 : StmtPoint(S, 0, CallExitKind, L, 0) {}
363 static bool classof(const ProgramPoint *Location) {
364 return Location->getKind() == CallExitKind;
369 } // end namespace clang
372 namespace llvm { // Traits specialization for DenseMap
374 template <> struct DenseMapInfo<clang::ProgramPoint> {
376 static inline clang::ProgramPoint getEmptyKey() {
378 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
379 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
382 static inline clang::ProgramPoint getTombstoneKey() {
384 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
385 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
388 static unsigned getHashValue(const clang::ProgramPoint& Loc) {
389 return Loc.getHashValue();
392 static bool isEqual(const clang::ProgramPoint& L,
393 const clang::ProgramPoint& R) {
400 struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
402 } // end namespace llvm