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_PROGRAMPOINT_H
16 #define LLVM_CLANG_ANALYSIS_PROGRAMPOINT_H
18 #include "clang/Analysis/AnalysisDeclContext.h"
19 #include "clang/Analysis/CFG.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/DataTypes.h"
33 class AnalysisDeclContext;
35 class LocationContext;
37 /// ProgramPoints can be "tagged" as representing points specific to a given
38 /// analysis entity. Tags are abstract annotations, with an associated
39 /// description and potentially other information.
40 class ProgramPointTag {
42 ProgramPointTag(void *tagKind = nullptr) : TagKind(tagKind) {}
43 virtual ~ProgramPointTag();
44 virtual StringRef getTagDescription() const = 0;
47 /// Used to implement 'isKind' in subclasses.
48 const void *getTagKind() { return TagKind; }
54 class SimpleProgramPointTag : public ProgramPointTag {
57 SimpleProgramPointTag(StringRef MsgProvider, StringRef Msg);
58 StringRef getTagDescription() const override;
63 enum Kind { BlockEdgeKind,
67 PreStmtPurgeDeadSymbolsKind,
68 PostStmtPurgeDeadSymbolsKind,
76 PostAllocatorCallKind,
77 MinPostStmtKind = PostStmtKind,
78 MaxPostStmtKind = PostAllocatorCallKind,
85 MinImplicitCallKind = PreImplicitCallKind,
86 MaxImplicitCallKind = PostImplicitCallKind,
92 llvm::PointerIntPair<const void *, 2, unsigned> Data2;
94 // The LocationContext could be NULL to allow ProgramPoint to be used in
95 // context insensitive analysis.
96 llvm::PointerIntPair<const LocationContext *, 2, unsigned> L;
98 llvm::PointerIntPair<const ProgramPointTag *, 2, unsigned> Tag;
101 ProgramPoint() = default;
102 ProgramPoint(const void *P,
104 const LocationContext *l,
105 const ProgramPointTag *tag = nullptr)
107 Data2(nullptr, (((unsigned) k) >> 0) & 0x3),
108 L(l, (((unsigned) k) >> 2) & 0x3),
109 Tag(tag, (((unsigned) k) >> 4) & 0x3) {
110 assert(getKind() == k);
111 assert(getLocationContext() == l);
112 assert(getData1() == P);
115 ProgramPoint(const void *P1,
118 const LocationContext *l,
119 const ProgramPointTag *tag = nullptr)
121 Data2(P2, (((unsigned) k) >> 0) & 0x3),
122 L(l, (((unsigned) k) >> 2) & 0x3),
123 Tag(tag, (((unsigned) k) >> 4) & 0x3) {}
126 const void *getData1() const { return Data1; }
127 const void *getData2() const { return Data2.getPointer(); }
128 void setData2(const void *d) { Data2.setPointer(d); }
131 /// Create a new ProgramPoint object that is the same as the original
132 /// except for using the specified tag value.
133 ProgramPoint withTag(const ProgramPointTag *tag) const {
134 return ProgramPoint(getData1(), getData2(), getKind(),
135 getLocationContext(), tag);
138 /// Convert to the specified ProgramPoint type, asserting that this
139 /// ProgramPoint is of the desired type.
142 assert(T::isKind(*this));
144 ProgramPoint& PP = t;
149 /// Convert to the specified ProgramPoint type, returning None if this
150 /// ProgramPoint is not of the desired type.
152 Optional<T> getAs() const {
153 if (!T::isKind(*this))
156 ProgramPoint& PP = t;
161 Kind getKind() const {
162 unsigned x = Tag.getInt();
170 /// Is this a program point corresponding to purge/removal of dead
171 /// symbols and bindings.
174 return (K == PostStmtPurgeDeadSymbolsKind ||
175 K == PreStmtPurgeDeadSymbolsKind);
178 const ProgramPointTag *getTag() const { return Tag.getPointer(); }
180 const LocationContext *getLocationContext() const {
181 return L.getPointer();
184 const StackFrameContext *getStackFrame() const {
185 return getLocationContext()->getStackFrame();
188 // For use with DenseMap. This hash is probably slow.
189 unsigned getHashValue() const {
190 llvm::FoldingSetNodeID ID;
192 return ID.ComputeHash();
195 bool operator==(const ProgramPoint & RHS) const {
196 return Data1 == RHS.Data1 &&
197 Data2 == RHS.Data2 &&
202 bool operator!=(const ProgramPoint &RHS) const {
203 return Data1 != RHS.Data1 ||
204 Data2 != RHS.Data2 ||
209 void Profile(llvm::FoldingSetNodeID& ID) const {
210 ID.AddInteger((unsigned) getKind());
211 ID.AddPointer(getData1());
212 ID.AddPointer(getData2());
213 ID.AddPointer(getLocationContext());
214 ID.AddPointer(getTag());
217 static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
218 const LocationContext *LC,
219 const ProgramPointTag *tag);
222 class BlockEntrance : public ProgramPoint {
224 BlockEntrance(const CFGBlock *B, const LocationContext *L,
225 const ProgramPointTag *tag = nullptr)
226 : ProgramPoint(B, BlockEntranceKind, L, tag) {
227 assert(B && "BlockEntrance requires non-null block");
230 const CFGBlock *getBlock() const {
231 return reinterpret_cast<const CFGBlock*>(getData1());
234 Optional<CFGElement> getFirstElement() const {
235 const CFGBlock *B = getBlock();
236 return B->empty() ? Optional<CFGElement>() : B->front();
240 friend class ProgramPoint;
241 BlockEntrance() = default;
242 static bool isKind(const ProgramPoint &Location) {
243 return Location.getKind() == BlockEntranceKind;
247 class BlockExit : public ProgramPoint {
249 BlockExit(const CFGBlock *B, const LocationContext *L)
250 : ProgramPoint(B, BlockExitKind, L) {}
252 const CFGBlock *getBlock() const {
253 return reinterpret_cast<const CFGBlock*>(getData1());
256 const Stmt *getTerminator() const {
257 return getBlock()->getTerminator();
261 friend class ProgramPoint;
262 BlockExit() = default;
263 static bool isKind(const ProgramPoint &Location) {
264 return Location.getKind() == BlockExitKind;
268 class StmtPoint : public ProgramPoint {
270 StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
271 const ProgramPointTag *tag)
272 : ProgramPoint(S, p2, k, L, tag) {
276 const Stmt *getStmt() const { return (const Stmt*) getData1(); }
278 template <typename T>
279 const T* getStmtAs() const { return dyn_cast<T>(getStmt()); }
282 StmtPoint() = default;
284 friend class ProgramPoint;
285 static bool isKind(const ProgramPoint &Location) {
286 unsigned k = Location.getKind();
287 return k >= PreStmtKind && k <= MaxPostStmtKind;
292 class PreStmt : public StmtPoint {
294 PreStmt(const Stmt *S, const LocationContext *L, const ProgramPointTag *tag,
295 const Stmt *SubStmt = nullptr)
296 : StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
298 const Stmt *getSubStmt() const { return (const Stmt*) getData2(); }
301 friend class ProgramPoint;
303 static bool isKind(const ProgramPoint &Location) {
304 return Location.getKind() == PreStmtKind;
308 class PostStmt : public StmtPoint {
310 PostStmt() = default;
311 PostStmt(const Stmt *S, const void *data, Kind k, const LocationContext *L,
312 const ProgramPointTag *tag = nullptr)
313 : StmtPoint(S, data, k, L, tag) {}
316 explicit PostStmt(const Stmt *S, Kind k, const LocationContext *L,
317 const ProgramPointTag *tag = nullptr)
318 : StmtPoint(S, nullptr, k, L, tag) {}
320 explicit PostStmt(const Stmt *S, const LocationContext *L,
321 const ProgramPointTag *tag = nullptr)
322 : StmtPoint(S, nullptr, PostStmtKind, L, tag) {}
325 friend class ProgramPoint;
326 static bool isKind(const ProgramPoint &Location) {
327 unsigned k = Location.getKind();
328 return k >= MinPostStmtKind && k <= MaxPostStmtKind;
332 // PostCondition represents the post program point of a branch condition.
333 class PostCondition : public PostStmt {
335 PostCondition(const Stmt *S, const LocationContext *L,
336 const ProgramPointTag *tag = nullptr)
337 : PostStmt(S, PostConditionKind, L, tag) {}
340 friend class ProgramPoint;
341 PostCondition() = default;
342 static bool isKind(const ProgramPoint &Location) {
343 return Location.getKind() == PostConditionKind;
347 class LocationCheck : public StmtPoint {
349 LocationCheck() = default;
350 LocationCheck(const Stmt *S, const LocationContext *L,
351 ProgramPoint::Kind K, const ProgramPointTag *tag)
352 : StmtPoint(S, nullptr, K, L, tag) {}
355 friend class ProgramPoint;
356 static bool isKind(const ProgramPoint &location) {
357 unsigned k = location.getKind();
358 return k == PreLoadKind || k == PreStoreKind;
362 class PreLoad : public LocationCheck {
364 PreLoad(const Stmt *S, const LocationContext *L,
365 const ProgramPointTag *tag = nullptr)
366 : LocationCheck(S, L, PreLoadKind, tag) {}
369 friend class ProgramPoint;
371 static bool isKind(const ProgramPoint &location) {
372 return location.getKind() == PreLoadKind;
376 class PreStore : public LocationCheck {
378 PreStore(const Stmt *S, const LocationContext *L,
379 const ProgramPointTag *tag = nullptr)
380 : LocationCheck(S, L, PreStoreKind, tag) {}
383 friend class ProgramPoint;
384 PreStore() = default;
385 static bool isKind(const ProgramPoint &location) {
386 return location.getKind() == PreStoreKind;
390 class PostLoad : public PostStmt {
392 PostLoad(const Stmt *S, const LocationContext *L,
393 const ProgramPointTag *tag = nullptr)
394 : PostStmt(S, PostLoadKind, L, tag) {}
397 friend class ProgramPoint;
398 PostLoad() = default;
399 static bool isKind(const ProgramPoint &Location) {
400 return Location.getKind() == PostLoadKind;
404 /// Represents a program point after a store evaluation.
405 class PostStore : public PostStmt {
407 /// Construct the post store point.
408 /// \param Loc can be used to store the information about the location
409 /// used in the form it was uttered in the code.
410 PostStore(const Stmt *S, const LocationContext *L, const void *Loc,
411 const ProgramPointTag *tag = nullptr)
412 : PostStmt(S, PostStoreKind, L, tag) {
413 assert(getData2() == nullptr);
417 /// Returns the information about the location used in the store,
418 /// how it was uttered in the code.
419 const void *getLocationValue() const {
424 friend class ProgramPoint;
425 PostStore() = default;
426 static bool isKind(const ProgramPoint &Location) {
427 return Location.getKind() == PostStoreKind;
431 class PostLValue : public PostStmt {
433 PostLValue(const Stmt *S, const LocationContext *L,
434 const ProgramPointTag *tag = nullptr)
435 : PostStmt(S, PostLValueKind, L, tag) {}
438 friend class ProgramPoint;
439 PostLValue() = default;
440 static bool isKind(const ProgramPoint &Location) {
441 return Location.getKind() == PostLValueKind;
445 /// Represents a point after we ran remove dead bindings BEFORE
446 /// processing the given statement.
447 class PreStmtPurgeDeadSymbols : public StmtPoint {
449 PreStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
450 const ProgramPointTag *tag = nullptr)
451 : StmtPoint(S, nullptr, PreStmtPurgeDeadSymbolsKind, L, tag) { }
454 friend class ProgramPoint;
455 PreStmtPurgeDeadSymbols() = default;
456 static bool isKind(const ProgramPoint &Location) {
457 return Location.getKind() == PreStmtPurgeDeadSymbolsKind;
461 /// Represents a point after we ran remove dead bindings AFTER
462 /// processing the given statement.
463 class PostStmtPurgeDeadSymbols : public StmtPoint {
465 PostStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
466 const ProgramPointTag *tag = nullptr)
467 : StmtPoint(S, nullptr, PostStmtPurgeDeadSymbolsKind, L, tag) { }
470 friend class ProgramPoint;
471 PostStmtPurgeDeadSymbols() = default;
472 static bool isKind(const ProgramPoint &Location) {
473 return Location.getKind() == PostStmtPurgeDeadSymbolsKind;
477 class BlockEdge : public ProgramPoint {
479 BlockEdge(const CFGBlock *B1, const CFGBlock *B2, const LocationContext *L)
480 : ProgramPoint(B1, B2, BlockEdgeKind, L) {
481 assert(B1 && "BlockEdge: source block must be non-null");
482 assert(B2 && "BlockEdge: destination block must be non-null");
485 const CFGBlock *getSrc() const {
486 return static_cast<const CFGBlock*>(getData1());
489 const CFGBlock *getDst() const {
490 return static_cast<const CFGBlock*>(getData2());
494 friend class ProgramPoint;
495 BlockEdge() = default;
496 static bool isKind(const ProgramPoint &Location) {
497 return Location.getKind() == BlockEdgeKind;
501 class PostInitializer : public ProgramPoint {
503 /// Construct a PostInitializer point that represents a location after
504 /// CXXCtorInitializer expression evaluation.
506 /// \param I The initializer.
507 /// \param Loc The location of the field being initialized.
508 PostInitializer(const CXXCtorInitializer *I,
510 const LocationContext *L)
511 : ProgramPoint(I, Loc, PostInitializerKind, L) {}
513 const CXXCtorInitializer *getInitializer() const {
514 return static_cast<const CXXCtorInitializer *>(getData1());
517 /// Returns the location of the field.
518 const void *getLocationValue() const {
523 friend class ProgramPoint;
524 PostInitializer() = default;
525 static bool isKind(const ProgramPoint &Location) {
526 return Location.getKind() == PostInitializerKind;
530 /// Represents an implicit call event.
532 /// The nearest statement is provided for diagnostic purposes.
533 class ImplicitCallPoint : public ProgramPoint {
535 ImplicitCallPoint(const Decl *D, SourceLocation Loc, Kind K,
536 const LocationContext *L, const ProgramPointTag *Tag)
537 : ProgramPoint(Loc.getPtrEncoding(), D, K, L, Tag) {}
539 const Decl *getDecl() const { return static_cast<const Decl *>(getData2()); }
540 SourceLocation getLocation() const {
541 return SourceLocation::getFromPtrEncoding(getData1());
545 ImplicitCallPoint() = default;
547 friend class ProgramPoint;
548 static bool isKind(const ProgramPoint &Location) {
549 return Location.getKind() >= MinImplicitCallKind &&
550 Location.getKind() <= MaxImplicitCallKind;
554 /// Represents a program point just before an implicit call event.
556 /// Explicit calls will appear as PreStmt program points.
557 class PreImplicitCall : public ImplicitCallPoint {
559 PreImplicitCall(const Decl *D, SourceLocation Loc, const LocationContext *L,
560 const ProgramPointTag *Tag = nullptr)
561 : ImplicitCallPoint(D, Loc, PreImplicitCallKind, L, Tag) {}
564 friend class ProgramPoint;
565 PreImplicitCall() = default;
566 static bool isKind(const ProgramPoint &Location) {
567 return Location.getKind() == PreImplicitCallKind;
571 /// Represents a program point just after an implicit call event.
573 /// Explicit calls will appear as PostStmt program points.
574 class PostImplicitCall : public ImplicitCallPoint {
576 PostImplicitCall(const Decl *D, SourceLocation Loc, const LocationContext *L,
577 const ProgramPointTag *Tag = nullptr)
578 : ImplicitCallPoint(D, Loc, PostImplicitCallKind, L, Tag) {}
581 friend class ProgramPoint;
582 PostImplicitCall() = default;
583 static bool isKind(const ProgramPoint &Location) {
584 return Location.getKind() == PostImplicitCallKind;
588 class PostAllocatorCall : public StmtPoint {
590 PostAllocatorCall(const Stmt *S, const LocationContext *L,
591 const ProgramPointTag *Tag = nullptr)
592 : StmtPoint(S, nullptr, PostAllocatorCallKind, L, Tag) {}
595 friend class ProgramPoint;
596 PostAllocatorCall() = default;
597 static bool isKind(const ProgramPoint &Location) {
598 return Location.getKind() == PostAllocatorCallKind;
602 /// Represents a point when we begin processing an inlined call.
603 /// CallEnter uses the caller's location context.
604 class CallEnter : public ProgramPoint {
606 CallEnter(const Stmt *stmt, const StackFrameContext *calleeCtx,
607 const LocationContext *callerCtx)
608 : ProgramPoint(stmt, calleeCtx, CallEnterKind, callerCtx, nullptr) {}
610 const Stmt *getCallExpr() const {
611 return static_cast<const Stmt *>(getData1());
614 const StackFrameContext *getCalleeContext() const {
615 return static_cast<const StackFrameContext *>(getData2());
618 /// Returns the entry block in the CFG for the entered function.
619 const CFGBlock *getEntry() const {
620 const StackFrameContext *CalleeCtx = getCalleeContext();
621 const CFG *CalleeCFG = CalleeCtx->getCFG();
622 return &(CalleeCFG->getEntry());
626 friend class ProgramPoint;
627 CallEnter() = default;
628 static bool isKind(const ProgramPoint &Location) {
629 return Location.getKind() == CallEnterKind;
633 /// Represents a point when we start the call exit sequence (for inlined call).
635 /// The call exit is simulated with a sequence of nodes, which occur between
636 /// CallExitBegin and CallExitEnd. The following operations occur between the
637 /// two program points:
639 /// - Bind the return value
640 /// - Run Remove dead bindings (to clean up the dead symbols from the callee).
642 class CallExitBegin : public ProgramPoint {
644 // CallExitBegin uses the callee's location context.
645 CallExitBegin(const StackFrameContext *L, const ReturnStmt *RS)
646 : ProgramPoint(RS, CallExitBeginKind, L, nullptr) { }
648 const ReturnStmt *getReturnStmt() const {
649 return static_cast<const ReturnStmt *>(getData1());
653 friend class ProgramPoint;
654 CallExitBegin() = default;
655 static bool isKind(const ProgramPoint &Location) {
656 return Location.getKind() == CallExitBeginKind;
660 /// Represents a point when we finish the call exit sequence (for inlined call).
661 /// \sa CallExitBegin
662 class CallExitEnd : public ProgramPoint {
664 // CallExitEnd uses the caller's location context.
665 CallExitEnd(const StackFrameContext *CalleeCtx,
666 const LocationContext *CallerCtx)
667 : ProgramPoint(CalleeCtx, CallExitEndKind, CallerCtx, nullptr) {}
669 const StackFrameContext *getCalleeContext() const {
670 return static_cast<const StackFrameContext *>(getData1());
674 friend class ProgramPoint;
675 CallExitEnd() = default;
676 static bool isKind(const ProgramPoint &Location) {
677 return Location.getKind() == CallExitEndKind;
681 /// Represents a point when we exit a loop.
682 /// When this ProgramPoint is encountered we can be sure that the symbolic
683 /// execution of the corresponding LoopStmt is finished on the given path.
684 /// Note: It is possible to encounter a LoopExit element when we haven't even
685 /// encountered the loop itself. At the current state not all loop exits will
686 /// result in a LoopExit program point.
687 class LoopExit : public ProgramPoint {
689 LoopExit(const Stmt *LoopStmt, const LocationContext *LC)
690 : ProgramPoint(LoopStmt, nullptr, LoopExitKind, LC) {}
692 const Stmt *getLoopStmt() const {
693 return static_cast<const Stmt *>(getData1());
697 friend class ProgramPoint;
698 LoopExit() = default;
699 static bool isKind(const ProgramPoint &Location) {
700 return Location.getKind() == LoopExitKind;
704 /// This is a meta program point, which should be skipped by all the diagnostic
706 class EpsilonPoint : public ProgramPoint {
708 EpsilonPoint(const LocationContext *L, const void *Data1,
709 const void *Data2 = nullptr,
710 const ProgramPointTag *tag = nullptr)
711 : ProgramPoint(Data1, Data2, EpsilonKind, L, tag) {}
713 const void *getData() const { return getData1(); }
716 friend class ProgramPoint;
717 EpsilonPoint() = default;
718 static bool isKind(const ProgramPoint &Location) {
719 return Location.getKind() == EpsilonKind;
723 } // end namespace clang
726 namespace llvm { // Traits specialization for DenseMap
728 template <> struct DenseMapInfo<clang::ProgramPoint> {
730 static inline clang::ProgramPoint getEmptyKey() {
732 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
733 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), nullptr);
736 static inline clang::ProgramPoint getTombstoneKey() {
738 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
739 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), nullptr);
742 static unsigned getHashValue(const clang::ProgramPoint &Loc) {
743 return Loc.getHashValue();
746 static bool isEqual(const clang::ProgramPoint &L,
747 const clang::ProgramPoint &R) {
754 struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
756 } // end namespace llvm