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,
43 PreStmtPurgeDeadSymbolsKind,
44 PostStmtPurgeDeadSymbolsKind,
52 MinPostStmtKind = PostStmtKind,
53 MaxPostStmtKind = PostLValueKind,
60 MinImplicitCallKind = PreImplicitCallKind,
61 MaxImplicitCallKind = PostImplicitCallKind,
66 llvm::PointerIntPair<const void *, 2, unsigned> Data2;
68 // The LocationContext could be NULL to allow ProgramPoint to be used in
69 // context insensitive analysis.
70 llvm::PointerIntPair<const LocationContext *, 2, unsigned> L;
72 llvm::PointerIntPair<const ProgramPointTag *, 2, unsigned> Tag;
77 ProgramPoint(const void *P,
79 const LocationContext *l,
80 const ProgramPointTag *tag = 0)
82 Data2(0, (((unsigned) k) >> 0) & 0x3),
83 L(l, (((unsigned) k) >> 2) & 0x3),
84 Tag(tag, (((unsigned) k) >> 4) & 0x3) {
85 assert(getKind() == k);
86 assert(getLocationContext() == l);
87 assert(getData1() == P);
90 ProgramPoint(const void *P1,
93 const LocationContext *l,
94 const ProgramPointTag *tag = 0)
96 Data2(P2, (((unsigned) k) >> 0) & 0x3),
97 L(l, (((unsigned) k) >> 2) & 0x3),
98 Tag(tag, (((unsigned) k) >> 4) & 0x3) {}
101 const void *getData1() const { return Data1; }
102 const void *getData2() const { return Data2.getPointer(); }
103 void setData2(const void *d) { Data2.setPointer(d); }
106 /// Create a new ProgramPoint object that is the same as the original
107 /// except for using the specified tag value.
108 ProgramPoint withTag(const ProgramPointTag *tag) const {
109 return ProgramPoint(getData1(), getData2(), getKind(),
110 getLocationContext(), tag);
113 Kind getKind() const {
114 unsigned x = Tag.getInt();
122 /// \brief Is this a program point corresponding to purge/removal of dead
123 /// symbols and bindings.
126 return (K == PostStmtPurgeDeadSymbolsKind ||
127 K == PreStmtPurgeDeadSymbolsKind);
130 const ProgramPointTag *getTag() const { return Tag.getPointer(); }
132 const LocationContext *getLocationContext() const {
133 return L.getPointer();
136 // For use with DenseMap. This hash is probably slow.
137 unsigned getHashValue() const {
138 llvm::FoldingSetNodeID ID;
140 return ID.ComputeHash();
143 bool operator==(const ProgramPoint & RHS) const {
144 return Data1 == RHS.Data1 &&
145 Data2 == RHS.Data2 &&
150 bool operator!=(const ProgramPoint &RHS) const {
151 return Data1 != RHS.Data1 ||
152 Data2 != RHS.Data2 ||
157 void Profile(llvm::FoldingSetNodeID& ID) const {
158 ID.AddInteger((unsigned) getKind());
159 ID.AddPointer(getData1());
160 ID.AddPointer(getData2());
161 ID.AddPointer(getLocationContext());
162 ID.AddPointer(getTag());
165 static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
166 const LocationContext *LC,
167 const ProgramPointTag *tag);
170 class BlockEntrance : public ProgramPoint {
172 BlockEntrance(const CFGBlock *B, const LocationContext *L,
173 const ProgramPointTag *tag = 0)
174 : ProgramPoint(B, BlockEntranceKind, L, tag) {
175 assert(B && "BlockEntrance requires non-null block");
178 const CFGBlock *getBlock() const {
179 return reinterpret_cast<const CFGBlock*>(getData1());
182 const CFGElement getFirstElement() const {
183 const CFGBlock *B = getBlock();
184 return B->empty() ? CFGElement() : B->front();
187 static bool classof(const ProgramPoint* Location) {
188 return Location->getKind() == BlockEntranceKind;
192 class BlockExit : public ProgramPoint {
194 BlockExit(const CFGBlock *B, const LocationContext *L)
195 : ProgramPoint(B, BlockExitKind, L) {}
197 const CFGBlock *getBlock() const {
198 return reinterpret_cast<const CFGBlock*>(getData1());
201 const Stmt *getTerminator() const {
202 return getBlock()->getTerminator();
205 static bool classof(const ProgramPoint* Location) {
206 return Location->getKind() == BlockExitKind;
210 class StmtPoint : public ProgramPoint {
212 StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
213 const ProgramPointTag *tag)
214 : ProgramPoint(S, p2, k, L, tag) {
218 const Stmt *getStmt() const { return (const Stmt*) getData1(); }
220 template <typename T>
221 const T* getStmtAs() const { return llvm::dyn_cast<T>(getStmt()); }
223 static bool classof(const ProgramPoint* Location) {
224 unsigned k = Location->getKind();
225 return k >= PreStmtKind && k <= MaxPostStmtKind;
230 class PreStmt : public StmtPoint {
232 PreStmt(const Stmt *S, const LocationContext *L, const ProgramPointTag *tag,
233 const Stmt *SubStmt = 0)
234 : StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
236 const Stmt *getSubStmt() const { return (const Stmt*) getData2(); }
238 static bool classof(const ProgramPoint* Location) {
239 return Location->getKind() == PreStmtKind;
243 class PostStmt : public StmtPoint {
245 PostStmt(const Stmt *S, const void *data, Kind k, const LocationContext *L,
246 const ProgramPointTag *tag = 0)
247 : StmtPoint(S, data, k, L, tag) {}
250 explicit PostStmt(const Stmt *S, Kind k,
251 const LocationContext *L, const ProgramPointTag *tag = 0)
252 : StmtPoint(S, NULL, k, L, tag) {}
254 explicit PostStmt(const Stmt *S, const LocationContext *L,
255 const ProgramPointTag *tag = 0)
256 : StmtPoint(S, NULL, PostStmtKind, L, tag) {}
258 static bool classof(const ProgramPoint* Location) {
259 unsigned k = Location->getKind();
260 return k >= MinPostStmtKind && k <= MaxPostStmtKind;
264 // PostCondition represents the post program point of a branch condition.
265 class PostCondition : public PostStmt {
267 PostCondition(const Stmt *S, const LocationContext *L,
268 const ProgramPointTag *tag = 0)
269 : PostStmt(S, PostConditionKind, L, tag) {}
271 static bool classof(const ProgramPoint* Location) {
272 return Location->getKind() == PostConditionKind;
276 class LocationCheck : public StmtPoint {
278 LocationCheck(const Stmt *S, const LocationContext *L,
279 ProgramPoint::Kind K, const ProgramPointTag *tag)
280 : StmtPoint(S, NULL, K, L, tag) {}
282 static bool classof(const ProgramPoint *location) {
283 unsigned k = location->getKind();
284 return k == PreLoadKind || k == PreStoreKind;
288 class PreLoad : public LocationCheck {
290 PreLoad(const Stmt *S, const LocationContext *L,
291 const ProgramPointTag *tag = 0)
292 : LocationCheck(S, L, PreLoadKind, tag) {}
294 static bool classof(const ProgramPoint *location) {
295 return location->getKind() == PreLoadKind;
299 class PreStore : public LocationCheck {
301 PreStore(const Stmt *S, const LocationContext *L,
302 const ProgramPointTag *tag = 0)
303 : LocationCheck(S, L, PreStoreKind, tag) {}
305 static bool classof(const ProgramPoint *location) {
306 return location->getKind() == PreStoreKind;
310 class PostLoad : public PostStmt {
312 PostLoad(const Stmt *S, const LocationContext *L,
313 const ProgramPointTag *tag = 0)
314 : PostStmt(S, PostLoadKind, L, tag) {}
316 static bool classof(const ProgramPoint* Location) {
317 return Location->getKind() == PostLoadKind;
321 /// \brief Represents a program point after a store evaluation.
322 class PostStore : public PostStmt {
324 /// Construct the post store point.
325 /// \param Loc can be used to store the information about the location
326 /// used in the form it was uttered in the code.
327 PostStore(const Stmt *S, const LocationContext *L, const void *Loc,
328 const ProgramPointTag *tag = 0)
329 : PostStmt(S, PostStoreKind, L, tag) {
330 assert(getData2() == 0);
334 static bool classof(const ProgramPoint* Location) {
335 return Location->getKind() == PostStoreKind;
338 /// \brief Returns the information about the location used in the store,
339 /// how it was uttered in the code.
340 const void *getLocationValue() const {
346 class PostLValue : public PostStmt {
348 PostLValue(const Stmt *S, const LocationContext *L,
349 const ProgramPointTag *tag = 0)
350 : PostStmt(S, PostLValueKind, L, tag) {}
352 static bool classof(const ProgramPoint* Location) {
353 return Location->getKind() == PostLValueKind;
357 /// Represents a point after we ran remove dead bindings BEFORE
358 /// processing the given statement.
359 class PreStmtPurgeDeadSymbols : public StmtPoint {
361 PreStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
362 const ProgramPointTag *tag = 0)
363 : StmtPoint(S, 0, PreStmtPurgeDeadSymbolsKind, L, tag) { }
365 static bool classof(const ProgramPoint* Location) {
366 return Location->getKind() == PreStmtPurgeDeadSymbolsKind;
370 /// Represents a point after we ran remove dead bindings AFTER
371 /// processing the given statement.
372 class PostStmtPurgeDeadSymbols : public StmtPoint {
374 PostStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
375 const ProgramPointTag *tag = 0)
376 : StmtPoint(S, 0, PostStmtPurgeDeadSymbolsKind, L, tag) { }
378 static bool classof(const ProgramPoint* Location) {
379 return Location->getKind() == PostStmtPurgeDeadSymbolsKind;
383 class BlockEdge : public ProgramPoint {
385 BlockEdge(const CFGBlock *B1, const CFGBlock *B2, const LocationContext *L)
386 : ProgramPoint(B1, B2, BlockEdgeKind, L) {
387 assert(B1 && "BlockEdge: source block must be non-null");
388 assert(B2 && "BlockEdge: destination block must be non-null");
391 const CFGBlock *getSrc() const {
392 return static_cast<const CFGBlock*>(getData1());
395 const CFGBlock *getDst() const {
396 return static_cast<const CFGBlock*>(getData2());
399 static bool classof(const ProgramPoint* Location) {
400 return Location->getKind() == BlockEdgeKind;
404 class PostInitializer : public ProgramPoint {
406 PostInitializer(const CXXCtorInitializer *I,
407 const LocationContext *L)
408 : ProgramPoint(I, PostInitializerKind, L) {}
410 static bool classof(const ProgramPoint *Location) {
411 return Location->getKind() == PostInitializerKind;
415 /// Represents an implicit call event.
417 /// The nearest statement is provided for diagnostic purposes.
418 class ImplicitCallPoint : public ProgramPoint {
420 ImplicitCallPoint(const Decl *D, SourceLocation Loc, Kind K,
421 const LocationContext *L, const ProgramPointTag *Tag)
422 : ProgramPoint(Loc.getPtrEncoding(), D, K, L, Tag) {}
424 const Decl *getDecl() const { return static_cast<const Decl *>(getData2()); }
425 SourceLocation getLocation() const {
426 return SourceLocation::getFromPtrEncoding(getData1());
429 static bool classof(const ProgramPoint *Location) {
430 return Location->getKind() >= MinImplicitCallKind &&
431 Location->getKind() <= MaxImplicitCallKind;
435 /// Represents a program point just before an implicit call event.
437 /// Explicit calls will appear as PreStmt program points.
438 class PreImplicitCall : public ImplicitCallPoint {
440 PreImplicitCall(const Decl *D, SourceLocation Loc,
441 const LocationContext *L, const ProgramPointTag *Tag = 0)
442 : ImplicitCallPoint(D, Loc, PreImplicitCallKind, L, Tag) {}
444 static bool classof(const ProgramPoint *Location) {
445 return Location->getKind() == PreImplicitCallKind;
449 /// Represents a program point just after an implicit call event.
451 /// Explicit calls will appear as PostStmt program points.
452 class PostImplicitCall : public ImplicitCallPoint {
454 PostImplicitCall(const Decl *D, SourceLocation Loc,
455 const LocationContext *L, const ProgramPointTag *Tag = 0)
456 : ImplicitCallPoint(D, Loc, PostImplicitCallKind, L, Tag) {}
458 static bool classof(const ProgramPoint *Location) {
459 return Location->getKind() == PostImplicitCallKind;
463 /// Represents a point when we begin processing an inlined call.
464 /// CallEnter uses the caller's location context.
465 class CallEnter : public ProgramPoint {
467 CallEnter(const Stmt *stmt, const StackFrameContext *calleeCtx,
468 const LocationContext *callerCtx)
469 : ProgramPoint(stmt, calleeCtx, CallEnterKind, callerCtx, 0) {}
471 const Stmt *getCallExpr() const {
472 return static_cast<const Stmt *>(getData1());
475 const StackFrameContext *getCalleeContext() const {
476 return static_cast<const StackFrameContext *>(getData2());
479 static bool classof(const ProgramPoint *Location) {
480 return Location->getKind() == CallEnterKind;
484 /// Represents a point when we start the call exit sequence (for inlined call).
486 /// The call exit is simulated with a sequence of nodes, which occur between
487 /// CallExitBegin and CallExitEnd. The following operations occur between the
488 /// two program points:
490 /// - Bind the return value
491 /// - Run Remove dead bindings (to clean up the dead symbols from the callee).
493 class CallExitBegin : public ProgramPoint {
495 // CallExitBegin uses the callee's location context.
496 CallExitBegin(const StackFrameContext *L)
497 : ProgramPoint(0, CallExitBeginKind, L, 0) {}
499 static bool classof(const ProgramPoint *Location) {
500 return Location->getKind() == CallExitBeginKind;
504 /// Represents a point when we finish the call exit sequence (for inlined call).
505 /// \sa CallExitBegin
506 class CallExitEnd : public ProgramPoint {
508 // CallExitEnd uses the caller's location context.
509 CallExitEnd(const StackFrameContext *CalleeCtx,
510 const LocationContext *CallerCtx)
511 : ProgramPoint(CalleeCtx, CallExitEndKind, CallerCtx, 0) {}
513 const StackFrameContext *getCalleeContext() const {
514 return static_cast<const StackFrameContext *>(getData1());
517 static bool classof(const ProgramPoint *Location) {
518 return Location->getKind() == CallExitEndKind;
522 /// This is a meta program point, which should be skipped by all the diagnostic
524 class EpsilonPoint : public ProgramPoint {
526 EpsilonPoint(const LocationContext *L, const void *Data1,
527 const void *Data2 = 0, const ProgramPointTag *tag = 0)
528 : ProgramPoint(Data1, Data2, EpsilonKind, L, tag) {}
530 const void *getData() const { return getData1(); }
532 static bool classof(const ProgramPoint* Location) {
533 return Location->getKind() == EpsilonKind;
537 /// ProgramPoints can be "tagged" as representing points specific to a given
538 /// analysis entity. Tags are abstract annotations, with an associated
539 /// description and potentially other information.
540 class ProgramPointTag {
542 ProgramPointTag(void *tagKind = 0) : TagKind(tagKind) {}
543 virtual ~ProgramPointTag();
544 virtual StringRef getTagDescription() const = 0;
547 /// Used to implement 'classof' in subclasses.
548 const void *getTagKind() { return TagKind; }
554 class SimpleProgramPointTag : public ProgramPointTag {
557 SimpleProgramPointTag(StringRef description);
558 StringRef getTagDescription() const;
561 } // end namespace clang
564 namespace llvm { // Traits specialization for DenseMap
566 template <> struct DenseMapInfo<clang::ProgramPoint> {
568 static inline clang::ProgramPoint getEmptyKey() {
570 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
571 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
574 static inline clang::ProgramPoint getTombstoneKey() {
576 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
577 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
580 static unsigned getHashValue(const clang::ProgramPoint &Loc) {
581 return Loc.getHashValue();
584 static bool isEqual(const clang::ProgramPoint &L,
585 const clang::ProgramPoint &R) {
592 struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
594 } // end namespace llvm