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/PointerIntPair.h"
23 #include "llvm/ADT/FoldingSet.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/ADT/StringRef.h"
32 class AnalysisDeclContext;
34 class LocationContext;
35 class ProgramPointTag;
39 enum Kind { BlockEdgeKind,
48 PostPurgeDeadSymbolsKind,
54 MinPostStmtKind = PostStmtKind,
55 MaxPostStmtKind = CallExitKind,
59 llvm::PointerIntPair<const void *, 2, unsigned> Data1;
60 llvm::PointerIntPair<const void *, 2, unsigned> Data2;
62 // The LocationContext could be NULL to allow ProgramPoint to be used in
63 // context insensitive analysis.
64 llvm::PointerIntPair<const LocationContext *, 2, unsigned> L;
66 const ProgramPointTag *Tag;
71 ProgramPoint(const void *P,
73 const LocationContext *l,
74 const ProgramPointTag *tag = 0)
75 : Data1(P, ((unsigned) k) & 0x3),
76 Data2(0, (((unsigned) k) >> 2) & 0x3),
77 L(l, (((unsigned) k) >> 4) & 0x3),
79 assert(getKind() == k);
80 assert(getLocationContext() == l);
81 assert(getData1() == P);
84 ProgramPoint(const void *P1,
87 const LocationContext *l,
88 const ProgramPointTag *tag = 0)
89 : Data1(P1, ((unsigned) k) & 0x3),
90 Data2(P2, (((unsigned) k) >> 2) & 0x3),
91 L(l, (((unsigned) k) >> 4) & 0x3),
95 const void *getData1() const { return Data1.getPointer(); }
96 const void *getData2() const { return Data2.getPointer(); }
97 void setData2(const void *d) { Data2.setPointer(d); }
100 /// Create a new ProgramPoint object that is the same as the original
101 /// except for using the specified tag value.
102 ProgramPoint withTag(const ProgramPointTag *tag) const {
103 return ProgramPoint(getData1(), getData2(), getKind(),
104 getLocationContext(), tag);
107 Kind getKind() const {
108 unsigned x = L.getInt();
116 const ProgramPointTag *getTag() const { return Tag; }
118 const LocationContext *getLocationContext() const {
119 return L.getPointer();
122 // For use with DenseMap. This hash is probably slow.
123 unsigned getHashValue() const {
124 llvm::FoldingSetNodeID ID;
126 return ID.ComputeHash();
129 static bool classof(const ProgramPoint*) { return true; }
131 bool operator==(const ProgramPoint & RHS) const {
132 return Data1 == RHS.Data1 &&
133 Data2 == RHS.Data2 &&
138 bool operator!=(const ProgramPoint &RHS) const {
139 return Data1 != RHS.Data1 ||
140 Data2 != RHS.Data2 ||
145 void Profile(llvm::FoldingSetNodeID& ID) const {
146 ID.AddInteger((unsigned) getKind());
147 ID.AddPointer(getData1());
148 ID.AddPointer(getData2());
149 ID.AddPointer(getLocationContext());
153 static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
154 const LocationContext *LC,
155 const ProgramPointTag *tag);
158 class BlockEntrance : public ProgramPoint {
160 BlockEntrance(const CFGBlock *B, const LocationContext *L,
161 const ProgramPointTag *tag = 0)
162 : ProgramPoint(B, BlockEntranceKind, L, tag) {
163 assert(B && "BlockEntrance requires non-null block");
166 const CFGBlock *getBlock() const {
167 return reinterpret_cast<const CFGBlock*>(getData1());
170 const CFGElement getFirstElement() const {
171 const CFGBlock *B = getBlock();
172 return B->empty() ? CFGElement() : B->front();
175 static bool classof(const ProgramPoint* Location) {
176 return Location->getKind() == BlockEntranceKind;
180 class BlockExit : public ProgramPoint {
182 BlockExit(const CFGBlock *B, const LocationContext *L)
183 : ProgramPoint(B, BlockExitKind, L) {}
185 const CFGBlock *getBlock() const {
186 return reinterpret_cast<const CFGBlock*>(getData1());
189 const Stmt *getTerminator() const {
190 return getBlock()->getTerminator();
193 static bool classof(const ProgramPoint* Location) {
194 return Location->getKind() == BlockExitKind;
198 class StmtPoint : public ProgramPoint {
200 StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
201 const ProgramPointTag *tag)
202 : ProgramPoint(S, p2, k, L, tag) {}
204 const Stmt *getStmt() const { return (const Stmt*) getData1(); }
206 template <typename T>
207 const T* getStmtAs() const { return llvm::dyn_cast<T>(getStmt()); }
209 static bool classof(const ProgramPoint* Location) {
210 unsigned k = Location->getKind();
211 return k >= PreStmtKind && k <= MaxPostStmtKind;
216 class PreStmt : public StmtPoint {
218 PreStmt(const Stmt *S, const LocationContext *L, const ProgramPointTag *tag,
219 const Stmt *SubStmt = 0)
220 : StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
222 const Stmt *getSubStmt() const { return (const Stmt*) getData2(); }
224 static bool classof(const ProgramPoint* Location) {
225 return Location->getKind() == PreStmtKind;
229 class PostStmt : public StmtPoint {
231 PostStmt(const Stmt *S, const void *data, Kind k, const LocationContext *L,
232 const ProgramPointTag *tag = 0)
233 : StmtPoint(S, data, k, L, tag) {}
236 explicit PostStmt(const Stmt *S, Kind k,
237 const LocationContext *L, const ProgramPointTag *tag = 0)
238 : StmtPoint(S, NULL, k, L, tag) {}
240 explicit PostStmt(const Stmt *S, const LocationContext *L,
241 const ProgramPointTag *tag = 0)
242 : StmtPoint(S, NULL, PostStmtKind, L, tag) {}
244 static bool classof(const ProgramPoint* Location) {
245 unsigned k = Location->getKind();
246 return k >= MinPostStmtKind && k <= MaxPostStmtKind;
250 // PostCondition represents the post program point of a branch condition.
251 class PostCondition : public PostStmt {
253 PostCondition(const Stmt *S, const LocationContext *L,
254 const ProgramPointTag *tag = 0)
255 : PostStmt(S, PostConditionKind, L, tag) {}
257 static bool classof(const ProgramPoint* Location) {
258 return Location->getKind() == PostConditionKind;
262 class LocationCheck : public StmtPoint {
264 LocationCheck(const Stmt *S, const LocationContext *L,
265 ProgramPoint::Kind K, const ProgramPointTag *tag)
266 : StmtPoint(S, NULL, K, L, tag) {}
268 static bool classof(const ProgramPoint *location) {
269 unsigned k = location->getKind();
270 return k == PreLoadKind || k == PreStoreKind;
274 class PreLoad : public LocationCheck {
276 PreLoad(const Stmt *S, const LocationContext *L,
277 const ProgramPointTag *tag = 0)
278 : LocationCheck(S, L, PreLoadKind, tag) {}
280 static bool classof(const ProgramPoint *location) {
281 return location->getKind() == PreLoadKind;
285 class PreStore : public LocationCheck {
287 PreStore(const Stmt *S, const LocationContext *L,
288 const ProgramPointTag *tag = 0)
289 : LocationCheck(S, L, PreStoreKind, tag) {}
291 static bool classof(const ProgramPoint *location) {
292 return location->getKind() == PreStoreKind;
296 class PostLoad : public PostStmt {
298 PostLoad(const Stmt *S, const LocationContext *L,
299 const ProgramPointTag *tag = 0)
300 : PostStmt(S, PostLoadKind, L, tag) {}
302 static bool classof(const ProgramPoint* Location) {
303 return Location->getKind() == PostLoadKind;
307 /// \class Represents a program point after a store evaluation.
308 class PostStore : public PostStmt {
310 /// Construct the post store point.
311 /// \param Loc can be used to store the information about the location
312 /// used in the form it was uttered in the code.
313 PostStore(const Stmt *S, const LocationContext *L, const void *Loc,
314 const ProgramPointTag *tag = 0)
315 : PostStmt(S, PostStoreKind, L, tag) {
316 assert(getData2() == 0);
320 static bool classof(const ProgramPoint* Location) {
321 return Location->getKind() == PostStoreKind;
324 /// \brief Returns the information about the location used in the store,
325 /// how it was uttered in the code.
326 const void *getLocationValue() const {
332 class PostLValue : public PostStmt {
334 PostLValue(const Stmt *S, const LocationContext *L,
335 const ProgramPointTag *tag = 0)
336 : PostStmt(S, PostLValueKind, L, tag) {}
338 static bool classof(const ProgramPoint* Location) {
339 return Location->getKind() == PostLValueKind;
343 class PostPurgeDeadSymbols : public PostStmt {
345 PostPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
346 const ProgramPointTag *tag = 0)
347 : PostStmt(S, PostPurgeDeadSymbolsKind, L, tag) {}
349 static bool classof(const ProgramPoint* Location) {
350 return Location->getKind() == PostPurgeDeadSymbolsKind;
354 class BlockEdge : public ProgramPoint {
356 BlockEdge(const CFGBlock *B1, const CFGBlock *B2, const LocationContext *L)
357 : ProgramPoint(B1, B2, BlockEdgeKind, L) {
358 assert(B1 && "BlockEdge: source block must be non-null");
359 assert(B2 && "BlockEdge: destination block must be non-null");
362 const CFGBlock *getSrc() const {
363 return static_cast<const CFGBlock*>(getData1());
366 const CFGBlock *getDst() const {
367 return static_cast<const CFGBlock*>(getData2());
370 static bool classof(const ProgramPoint* Location) {
371 return Location->getKind() == BlockEdgeKind;
375 class PostInitializer : public ProgramPoint {
377 PostInitializer(const CXXCtorInitializer *I,
378 const LocationContext *L)
379 : ProgramPoint(I, PostInitializerKind, L) {}
381 static bool classof(const ProgramPoint *Location) {
382 return Location->getKind() == PostInitializerKind;
386 class CallEnter : public StmtPoint {
388 CallEnter(const Stmt *stmt, const StackFrameContext *calleeCtx,
389 const LocationContext *callerCtx)
390 : StmtPoint(stmt, calleeCtx, CallEnterKind, callerCtx, 0) {}
392 const Stmt *getCallExpr() const {
393 return static_cast<const Stmt *>(getData1());
396 const StackFrameContext *getCalleeContext() const {
397 return static_cast<const StackFrameContext *>(getData2());
400 static bool classof(const ProgramPoint *Location) {
401 return Location->getKind() == CallEnterKind;
405 class CallExit : public StmtPoint {
407 // CallExit uses the callee's location context.
408 CallExit(const Stmt *S, const LocationContext *L)
409 : StmtPoint(S, 0, CallExitKind, L, 0) {}
411 static bool classof(const ProgramPoint *Location) {
412 return Location->getKind() == CallExitKind;
416 /// This is a meta program point, which should be skipped by all the diagnostic
418 class EpsilonPoint : public ProgramPoint {
420 EpsilonPoint(const LocationContext *L, const void *Data1,
421 const void *Data2 = 0, const ProgramPointTag *tag = 0)
422 : ProgramPoint(Data1, Data2, EpsilonKind, L, tag) {}
424 const void *getData() const { return getData1(); }
426 static bool classof(const ProgramPoint* Location) {
427 return Location->getKind() == EpsilonKind;
431 /// ProgramPoints can be "tagged" as representing points specific to a given
432 /// analysis entity. Tags are abstract annotations, with an associated
433 /// description and potentially other information.
434 class ProgramPointTag {
436 ProgramPointTag(void *tagKind = 0) : TagKind(tagKind) {}
437 virtual ~ProgramPointTag();
438 virtual StringRef getTagDescription() const = 0;
441 /// Used to implement 'classof' in subclasses.
442 const void *getTagKind() { return TagKind; }
448 class SimpleProgramPointTag : public ProgramPointTag {
451 SimpleProgramPointTag(StringRef description);
452 StringRef getTagDescription() const;
455 } // end namespace clang
458 namespace llvm { // Traits specialization for DenseMap
460 template <> struct DenseMapInfo<clang::ProgramPoint> {
462 static inline clang::ProgramPoint getEmptyKey() {
464 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
465 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
468 static inline clang::ProgramPoint getTombstoneKey() {
470 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
471 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
474 static unsigned getHashValue(const clang::ProgramPoint &Loc) {
475 return Loc.getHashValue();
478 static bool isEqual(const clang::ProgramPoint &L,
479 const clang::ProgramPoint &R) {
486 struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
488 } // end namespace llvm