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 MinPostStmtKind = PostStmtKind,
77 MaxPostStmtKind = PostLValueKind,
84 MinImplicitCallKind = PreImplicitCallKind,
85 MaxImplicitCallKind = PostImplicitCallKind,
91 llvm::PointerIntPair<const void *, 2, unsigned> Data2;
93 // The LocationContext could be NULL to allow ProgramPoint to be used in
94 // context insensitive analysis.
95 llvm::PointerIntPair<const LocationContext *, 2, unsigned> L;
97 llvm::PointerIntPair<const ProgramPointTag *, 2, unsigned> Tag;
101 ProgramPoint(const void *P,
103 const LocationContext *l,
104 const ProgramPointTag *tag = nullptr)
106 Data2(nullptr, (((unsigned) k) >> 0) & 0x3),
107 L(l, (((unsigned) k) >> 2) & 0x3),
108 Tag(tag, (((unsigned) k) >> 4) & 0x3) {
109 assert(getKind() == k);
110 assert(getLocationContext() == l);
111 assert(getData1() == P);
114 ProgramPoint(const void *P1,
117 const LocationContext *l,
118 const ProgramPointTag *tag = nullptr)
120 Data2(P2, (((unsigned) k) >> 0) & 0x3),
121 L(l, (((unsigned) k) >> 2) & 0x3),
122 Tag(tag, (((unsigned) k) >> 4) & 0x3) {}
125 const void *getData1() const { return Data1; }
126 const void *getData2() const { return Data2.getPointer(); }
127 void setData2(const void *d) { Data2.setPointer(d); }
130 /// Create a new ProgramPoint object that is the same as the original
131 /// except for using the specified tag value.
132 ProgramPoint withTag(const ProgramPointTag *tag) const {
133 return ProgramPoint(getData1(), getData2(), getKind(),
134 getLocationContext(), tag);
137 /// \brief Convert to the specified ProgramPoint type, asserting that this
138 /// ProgramPoint is of the desired type.
141 assert(T::isKind(*this));
143 ProgramPoint& PP = t;
148 /// \brief Convert to the specified ProgramPoint type, returning None if this
149 /// ProgramPoint is not of the desired type.
151 Optional<T> getAs() const {
152 if (!T::isKind(*this))
155 ProgramPoint& PP = t;
160 Kind getKind() const {
161 unsigned x = Tag.getInt();
169 /// \brief Is this a program point corresponding to purge/removal of dead
170 /// symbols and bindings.
173 return (K == PostStmtPurgeDeadSymbolsKind ||
174 K == PreStmtPurgeDeadSymbolsKind);
177 const ProgramPointTag *getTag() const { return Tag.getPointer(); }
179 const LocationContext *getLocationContext() const {
180 return L.getPointer();
183 // For use with DenseMap. This hash is probably slow.
184 unsigned getHashValue() const {
185 llvm::FoldingSetNodeID ID;
187 return ID.ComputeHash();
190 bool operator==(const ProgramPoint & RHS) const {
191 return Data1 == RHS.Data1 &&
192 Data2 == RHS.Data2 &&
197 bool operator!=(const ProgramPoint &RHS) const {
198 return Data1 != RHS.Data1 ||
199 Data2 != RHS.Data2 ||
204 void Profile(llvm::FoldingSetNodeID& ID) const {
205 ID.AddInteger((unsigned) getKind());
206 ID.AddPointer(getData1());
207 ID.AddPointer(getData2());
208 ID.AddPointer(getLocationContext());
209 ID.AddPointer(getTag());
212 static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K,
213 const LocationContext *LC,
214 const ProgramPointTag *tag);
217 class BlockEntrance : public ProgramPoint {
219 BlockEntrance(const CFGBlock *B, const LocationContext *L,
220 const ProgramPointTag *tag = nullptr)
221 : ProgramPoint(B, BlockEntranceKind, L, tag) {
222 assert(B && "BlockEntrance requires non-null block");
225 const CFGBlock *getBlock() const {
226 return reinterpret_cast<const CFGBlock*>(getData1());
229 Optional<CFGElement> getFirstElement() const {
230 const CFGBlock *B = getBlock();
231 return B->empty() ? Optional<CFGElement>() : B->front();
235 friend class ProgramPoint;
237 static bool isKind(const ProgramPoint &Location) {
238 return Location.getKind() == BlockEntranceKind;
242 class BlockExit : public ProgramPoint {
244 BlockExit(const CFGBlock *B, const LocationContext *L)
245 : ProgramPoint(B, BlockExitKind, L) {}
247 const CFGBlock *getBlock() const {
248 return reinterpret_cast<const CFGBlock*>(getData1());
251 const Stmt *getTerminator() const {
252 return getBlock()->getTerminator();
256 friend class ProgramPoint;
258 static bool isKind(const ProgramPoint &Location) {
259 return Location.getKind() == BlockExitKind;
263 class StmtPoint : public ProgramPoint {
265 StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
266 const ProgramPointTag *tag)
267 : ProgramPoint(S, p2, k, L, tag) {
271 const Stmt *getStmt() const { return (const Stmt*) getData1(); }
273 template <typename T>
274 const T* getStmtAs() const { return dyn_cast<T>(getStmt()); }
279 friend class ProgramPoint;
280 static bool isKind(const ProgramPoint &Location) {
281 unsigned k = Location.getKind();
282 return k >= PreStmtKind && k <= MaxPostStmtKind;
287 class PreStmt : public StmtPoint {
289 PreStmt(const Stmt *S, const LocationContext *L, const ProgramPointTag *tag,
290 const Stmt *SubStmt = nullptr)
291 : StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
293 const Stmt *getSubStmt() const { return (const Stmt*) getData2(); }
296 friend class ProgramPoint;
298 static bool isKind(const ProgramPoint &Location) {
299 return Location.getKind() == PreStmtKind;
303 class PostStmt : public StmtPoint {
306 PostStmt(const Stmt *S, const void *data, Kind k, const LocationContext *L,
307 const ProgramPointTag *tag = nullptr)
308 : StmtPoint(S, data, k, L, tag) {}
311 explicit PostStmt(const Stmt *S, Kind k, const LocationContext *L,
312 const ProgramPointTag *tag = nullptr)
313 : StmtPoint(S, nullptr, k, L, tag) {}
315 explicit PostStmt(const Stmt *S, const LocationContext *L,
316 const ProgramPointTag *tag = nullptr)
317 : StmtPoint(S, nullptr, PostStmtKind, L, tag) {}
320 friend class ProgramPoint;
321 static bool isKind(const ProgramPoint &Location) {
322 unsigned k = Location.getKind();
323 return k >= MinPostStmtKind && k <= MaxPostStmtKind;
327 // PostCondition represents the post program point of a branch condition.
328 class PostCondition : public PostStmt {
330 PostCondition(const Stmt *S, const LocationContext *L,
331 const ProgramPointTag *tag = nullptr)
332 : PostStmt(S, PostConditionKind, L, tag) {}
335 friend class ProgramPoint;
337 static bool isKind(const ProgramPoint &Location) {
338 return Location.getKind() == PostConditionKind;
342 class LocationCheck : public StmtPoint {
345 LocationCheck(const Stmt *S, const LocationContext *L,
346 ProgramPoint::Kind K, const ProgramPointTag *tag)
347 : StmtPoint(S, nullptr, K, L, tag) {}
350 friend class ProgramPoint;
351 static bool isKind(const ProgramPoint &location) {
352 unsigned k = location.getKind();
353 return k == PreLoadKind || k == PreStoreKind;
357 class PreLoad : public LocationCheck {
359 PreLoad(const Stmt *S, const LocationContext *L,
360 const ProgramPointTag *tag = nullptr)
361 : LocationCheck(S, L, PreLoadKind, tag) {}
364 friend class ProgramPoint;
366 static bool isKind(const ProgramPoint &location) {
367 return location.getKind() == PreLoadKind;
371 class PreStore : public LocationCheck {
373 PreStore(const Stmt *S, const LocationContext *L,
374 const ProgramPointTag *tag = nullptr)
375 : LocationCheck(S, L, PreStoreKind, tag) {}
378 friend class ProgramPoint;
380 static bool isKind(const ProgramPoint &location) {
381 return location.getKind() == PreStoreKind;
385 class PostLoad : public PostStmt {
387 PostLoad(const Stmt *S, const LocationContext *L,
388 const ProgramPointTag *tag = nullptr)
389 : PostStmt(S, PostLoadKind, L, tag) {}
392 friend class ProgramPoint;
394 static bool isKind(const ProgramPoint &Location) {
395 return Location.getKind() == PostLoadKind;
399 /// \brief Represents a program point after a store evaluation.
400 class PostStore : public PostStmt {
402 /// Construct the post store point.
403 /// \param Loc can be used to store the information about the location
404 /// used in the form it was uttered in the code.
405 PostStore(const Stmt *S, const LocationContext *L, const void *Loc,
406 const ProgramPointTag *tag = nullptr)
407 : PostStmt(S, PostStoreKind, L, tag) {
408 assert(getData2() == nullptr);
412 /// \brief Returns the information about the location used in the store,
413 /// how it was uttered in the code.
414 const void *getLocationValue() const {
419 friend class ProgramPoint;
421 static bool isKind(const ProgramPoint &Location) {
422 return Location.getKind() == PostStoreKind;
426 class PostLValue : public PostStmt {
428 PostLValue(const Stmt *S, const LocationContext *L,
429 const ProgramPointTag *tag = nullptr)
430 : PostStmt(S, PostLValueKind, L, tag) {}
433 friend class ProgramPoint;
435 static bool isKind(const ProgramPoint &Location) {
436 return Location.getKind() == PostLValueKind;
440 /// Represents a point after we ran remove dead bindings BEFORE
441 /// processing the given statement.
442 class PreStmtPurgeDeadSymbols : public StmtPoint {
444 PreStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
445 const ProgramPointTag *tag = nullptr)
446 : StmtPoint(S, nullptr, PreStmtPurgeDeadSymbolsKind, L, tag) { }
449 friend class ProgramPoint;
450 PreStmtPurgeDeadSymbols() {}
451 static bool isKind(const ProgramPoint &Location) {
452 return Location.getKind() == PreStmtPurgeDeadSymbolsKind;
456 /// Represents a point after we ran remove dead bindings AFTER
457 /// processing the given statement.
458 class PostStmtPurgeDeadSymbols : public StmtPoint {
460 PostStmtPurgeDeadSymbols(const Stmt *S, const LocationContext *L,
461 const ProgramPointTag *tag = nullptr)
462 : StmtPoint(S, nullptr, PostStmtPurgeDeadSymbolsKind, L, tag) { }
465 friend class ProgramPoint;
466 PostStmtPurgeDeadSymbols() {}
467 static bool isKind(const ProgramPoint &Location) {
468 return Location.getKind() == PostStmtPurgeDeadSymbolsKind;
472 class BlockEdge : public ProgramPoint {
474 BlockEdge(const CFGBlock *B1, const CFGBlock *B2, const LocationContext *L)
475 : ProgramPoint(B1, B2, BlockEdgeKind, L) {
476 assert(B1 && "BlockEdge: source block must be non-null");
477 assert(B2 && "BlockEdge: destination block must be non-null");
480 const CFGBlock *getSrc() const {
481 return static_cast<const CFGBlock*>(getData1());
484 const CFGBlock *getDst() const {
485 return static_cast<const CFGBlock*>(getData2());
489 friend class ProgramPoint;
491 static bool isKind(const ProgramPoint &Location) {
492 return Location.getKind() == BlockEdgeKind;
496 class PostInitializer : public ProgramPoint {
498 /// \brief Construct a PostInitializer point that represents a location after
499 /// CXXCtorInitializer expression evaluation.
501 /// \param I The initializer.
502 /// \param Loc The location of the field being initialized.
503 PostInitializer(const CXXCtorInitializer *I,
505 const LocationContext *L)
506 : ProgramPoint(I, Loc, PostInitializerKind, L) {}
508 const CXXCtorInitializer *getInitializer() const {
509 return static_cast<const CXXCtorInitializer *>(getData1());
512 /// \brief Returns the location of the field.
513 const void *getLocationValue() const {
518 friend class ProgramPoint;
520 static bool isKind(const ProgramPoint &Location) {
521 return Location.getKind() == PostInitializerKind;
525 /// Represents an implicit call event.
527 /// The nearest statement is provided for diagnostic purposes.
528 class ImplicitCallPoint : public ProgramPoint {
530 ImplicitCallPoint(const Decl *D, SourceLocation Loc, Kind K,
531 const LocationContext *L, const ProgramPointTag *Tag)
532 : ProgramPoint(Loc.getPtrEncoding(), D, K, L, Tag) {}
534 const Decl *getDecl() const { return static_cast<const Decl *>(getData2()); }
535 SourceLocation getLocation() const {
536 return SourceLocation::getFromPtrEncoding(getData1());
540 ImplicitCallPoint() {}
542 friend class ProgramPoint;
543 static bool isKind(const ProgramPoint &Location) {
544 return Location.getKind() >= MinImplicitCallKind &&
545 Location.getKind() <= MaxImplicitCallKind;
549 /// Represents a program point just before an implicit call event.
551 /// Explicit calls will appear as PreStmt program points.
552 class PreImplicitCall : public ImplicitCallPoint {
554 PreImplicitCall(const Decl *D, SourceLocation Loc, const LocationContext *L,
555 const ProgramPointTag *Tag = nullptr)
556 : ImplicitCallPoint(D, Loc, PreImplicitCallKind, L, Tag) {}
559 friend class ProgramPoint;
561 static bool isKind(const ProgramPoint &Location) {
562 return Location.getKind() == PreImplicitCallKind;
566 /// Represents a program point just after an implicit call event.
568 /// Explicit calls will appear as PostStmt program points.
569 class PostImplicitCall : public ImplicitCallPoint {
571 PostImplicitCall(const Decl *D, SourceLocation Loc, const LocationContext *L,
572 const ProgramPointTag *Tag = nullptr)
573 : ImplicitCallPoint(D, Loc, PostImplicitCallKind, L, Tag) {}
576 friend class ProgramPoint;
577 PostImplicitCall() {}
578 static bool isKind(const ProgramPoint &Location) {
579 return Location.getKind() == PostImplicitCallKind;
583 /// Represents a point when we begin processing an inlined call.
584 /// CallEnter uses the caller's location context.
585 class CallEnter : public ProgramPoint {
587 CallEnter(const Stmt *stmt, const StackFrameContext *calleeCtx,
588 const LocationContext *callerCtx)
589 : ProgramPoint(stmt, calleeCtx, CallEnterKind, callerCtx, nullptr) {}
591 const Stmt *getCallExpr() const {
592 return static_cast<const Stmt *>(getData1());
595 const StackFrameContext *getCalleeContext() const {
596 return static_cast<const StackFrameContext *>(getData2());
599 /// Returns the entry block in the CFG for the entered function.
600 const CFGBlock *getEntry() const {
601 const StackFrameContext *CalleeCtx = getCalleeContext();
602 const CFG *CalleeCFG = CalleeCtx->getCFG();
603 return &(CalleeCFG->getEntry());
607 friend class ProgramPoint;
609 static bool isKind(const ProgramPoint &Location) {
610 return Location.getKind() == CallEnterKind;
614 /// Represents a point when we start the call exit sequence (for inlined call).
616 /// The call exit is simulated with a sequence of nodes, which occur between
617 /// CallExitBegin and CallExitEnd. The following operations occur between the
618 /// two program points:
620 /// - Bind the return value
621 /// - Run Remove dead bindings (to clean up the dead symbols from the callee).
623 class CallExitBegin : public ProgramPoint {
625 // CallExitBegin uses the callee's location context.
626 CallExitBegin(const StackFrameContext *L, const ReturnStmt *RS)
627 : ProgramPoint(RS, CallExitBeginKind, L, nullptr) { }
630 friend class ProgramPoint;
632 static bool isKind(const ProgramPoint &Location) {
633 return Location.getKind() == CallExitBeginKind;
637 /// Represents a point when we finish the call exit sequence (for inlined call).
638 /// \sa CallExitBegin
639 class CallExitEnd : public ProgramPoint {
641 // CallExitEnd uses the caller's location context.
642 CallExitEnd(const StackFrameContext *CalleeCtx,
643 const LocationContext *CallerCtx)
644 : ProgramPoint(CalleeCtx, CallExitEndKind, CallerCtx, nullptr) {}
646 const StackFrameContext *getCalleeContext() const {
647 return static_cast<const StackFrameContext *>(getData1());
651 friend class ProgramPoint;
653 static bool isKind(const ProgramPoint &Location) {
654 return Location.getKind() == CallExitEndKind;
658 /// Represents a point when we exit a loop.
659 /// When this ProgramPoint is encountered we can be sure that the symbolic
660 /// execution of the corresponding LoopStmt is finished on the given path.
661 /// Note: It is possible to encounter a LoopExit element when we haven't even
662 /// encountered the loop itself. At the current state not all loop exits will
663 /// result in a LoopExit program point.
664 class LoopExit : public ProgramPoint {
666 LoopExit(const Stmt *LoopStmt, const LocationContext *LC)
667 : ProgramPoint(LoopStmt, nullptr, LoopExitKind, LC) {}
669 const Stmt *getLoopStmt() const {
670 return static_cast<const Stmt *>(getData1());
674 friend class ProgramPoint;
676 static bool isKind(const ProgramPoint &Location) {
677 return Location.getKind() == LoopExitKind;
681 /// This is a meta program point, which should be skipped by all the diagnostic
683 class EpsilonPoint : public ProgramPoint {
685 EpsilonPoint(const LocationContext *L, const void *Data1,
686 const void *Data2 = nullptr,
687 const ProgramPointTag *tag = nullptr)
688 : ProgramPoint(Data1, Data2, EpsilonKind, L, tag) {}
690 const void *getData() const { return getData1(); }
693 friend class ProgramPoint;
695 static bool isKind(const ProgramPoint &Location) {
696 return Location.getKind() == EpsilonKind;
700 } // end namespace clang
703 namespace llvm { // Traits specialization for DenseMap
705 template <> struct DenseMapInfo<clang::ProgramPoint> {
707 static inline clang::ProgramPoint getEmptyKey() {
709 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
710 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), nullptr);
713 static inline clang::ProgramPoint getTombstoneKey() {
715 reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
716 return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), nullptr);
719 static unsigned getHashValue(const clang::ProgramPoint &Loc) {
720 return Loc.getHashValue();
723 static bool isEqual(const clang::ProgramPoint &L,
724 const clang::ProgramPoint &R) {
731 struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
733 } // end namespace llvm