//==- ProgramPoint.h - Program Points for Path-Sensitive Analysis --*- C++ -*-// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the interface ProgramPoint, which identifies a // distinct location in a function. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_ANALYSIS_PROGRAM_POINT #define LLVM_CLANG_ANALYSIS_PROGRAM_POINT #include "clang/Analysis/CFG.h" #include "llvm/Support/DataTypes.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/Support/Casting.h" #include #include namespace clang { class ProgramPoint { public: enum Kind { BlockEdgeKind, BlockEntranceKind, BlockExitKind, PreStmtKind, // Keep the following together and in this order. PostStmtKind, PostLocationChecksSucceedKind, PostOutOfBoundsCheckFailedKind, PostNullCheckFailedKind, PostUndefLocationCheckFailedKind, PostLoadKind, PostStoreKind, PostPurgeDeadSymbolsKind, PostStmtCustomKind, PostLValueKind, MinPostStmtKind = PostStmtKind, MaxPostStmtKind = PostLValueKind }; private: std::pair Data; Kind K; const void *Tag; protected: ProgramPoint(const void* P, Kind k, const void *tag = 0) : Data(P, NULL), K(k), Tag(tag) {} ProgramPoint(const void* P1, const void* P2, Kind k, const void *tag = 0) : Data(P1, P2), K(k), Tag(tag) {} protected: const void* getData1() const { return Data.first; } const void* getData2() const { return Data.second; } const void *getTag() const { return Tag; } public: Kind getKind() const { return K; } // For use with DenseMap. This hash is probably slow. unsigned getHashValue() const { llvm::FoldingSetNodeID ID; Profile(ID); return ID.ComputeHash(); } static bool classof(const ProgramPoint*) { return true; } bool operator==(const ProgramPoint & RHS) const { return K == RHS.K && Data == RHS.Data && Tag == RHS.Tag; } bool operator!=(const ProgramPoint& RHS) const { return K != RHS.K || Data != RHS.Data || Tag != RHS.Tag; } void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger((unsigned) K); ID.AddPointer(Data.first); ID.AddPointer(Data.second); ID.AddPointer(Tag); } }; class BlockEntrance : public ProgramPoint { public: BlockEntrance(const CFGBlock* B, const void *tag = 0) : ProgramPoint(B, BlockEntranceKind, tag) {} CFGBlock* getBlock() const { return const_cast(reinterpret_cast(getData1())); } Stmt* getFirstStmt() const { const CFGBlock* B = getBlock(); return B->empty() ? NULL : B->front(); } static bool classof(const ProgramPoint* Location) { return Location->getKind() == BlockEntranceKind; } }; class BlockExit : public ProgramPoint { public: BlockExit(const CFGBlock* B) : ProgramPoint(B, BlockExitKind) {} CFGBlock* getBlock() const { return const_cast(reinterpret_cast(getData1())); } Stmt* getLastStmt() const { const CFGBlock* B = getBlock(); return B->empty() ? NULL : B->back(); } Stmt* getTerminator() const { return getBlock()->getTerminator(); } static bool classof(const ProgramPoint* Location) { return Location->getKind() == BlockExitKind; } }; class StmtPoint : public ProgramPoint { public: StmtPoint(const Stmt *S, const void *p2, Kind k, const void *tag) : ProgramPoint(S, p2, k, tag) {} const Stmt *getStmt() const { return (const Stmt*) getData1(); } template const T* getStmtAs() const { return llvm::dyn_cast(getStmt()); } static bool classof(const ProgramPoint* Location) { unsigned k = Location->getKind(); return k >= PreStmtKind && k <= MaxPostStmtKind; } }; class PreStmt : public StmtPoint { public: PreStmt(const Stmt *S, const void *tag, const Stmt *SubStmt = 0) : StmtPoint(S, SubStmt, PreStmtKind, tag) {} const Stmt *getSubStmt() const { return (const Stmt*) getData2(); } static bool classof(const ProgramPoint* Location) { return Location->getKind() == PreStmtKind; } }; class PostStmt : public StmtPoint { protected: PostStmt(const Stmt* S, Kind k, const void *tag = 0) : StmtPoint(S, NULL, k, tag) {} PostStmt(const Stmt* S, const void* data, Kind k, const void *tag =0) : StmtPoint(S, data, k, tag) {} public: explicit PostStmt(const Stmt* S, const void *tag = 0) : StmtPoint(S, NULL, PostStmtKind, tag) {} static bool classof(const ProgramPoint* Location) { unsigned k = Location->getKind(); return k >= MinPostStmtKind && k <= MaxPostStmtKind; } }; class PostLocationChecksSucceed : public PostStmt { public: PostLocationChecksSucceed(const Stmt* S, const void *tag = 0) : PostStmt(S, PostLocationChecksSucceedKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostLocationChecksSucceedKind; } }; class PostStmtCustom : public PostStmt { public: PostStmtCustom(const Stmt* S, const std::pair* TaggedData) : PostStmt(S, TaggedData, PostStmtCustomKind) {} const std::pair& getTaggedPair() const { return *reinterpret_cast*>(getData2()); } const void* getTag() const { return getTaggedPair().first; } const void* getTaggedData() const { return getTaggedPair().second; } static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostStmtCustomKind; } }; class PostOutOfBoundsCheckFailed : public PostStmt { public: PostOutOfBoundsCheckFailed(const Stmt* S, const void *tag = 0) : PostStmt(S, PostOutOfBoundsCheckFailedKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostOutOfBoundsCheckFailedKind; } }; class PostUndefLocationCheckFailed : public PostStmt { public: PostUndefLocationCheckFailed(const Stmt* S, const void *tag = 0) : PostStmt(S, PostUndefLocationCheckFailedKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostUndefLocationCheckFailedKind; } }; class PostNullCheckFailed : public PostStmt { public: PostNullCheckFailed(const Stmt* S, const void *tag = 0) : PostStmt(S, PostNullCheckFailedKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostNullCheckFailedKind; } }; class PostLoad : public PostStmt { public: PostLoad(const Stmt* S, const void *tag = 0) : PostStmt(S, PostLoadKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostLoadKind; } }; class PostStore : public PostStmt { public: PostStore(const Stmt* S, const void *tag = 0) : PostStmt(S, PostStoreKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostStoreKind; } }; class PostLValue : public PostStmt { public: PostLValue(const Stmt* S, const void *tag = 0) : PostStmt(S, PostLValueKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostLValueKind; } }; class PostPurgeDeadSymbols : public PostStmt { public: PostPurgeDeadSymbols(const Stmt* S, const void *tag = 0) : PostStmt(S, PostPurgeDeadSymbolsKind, tag) {} static bool classof(const ProgramPoint* Location) { return Location->getKind() == PostPurgeDeadSymbolsKind; } }; class BlockEdge : public ProgramPoint { public: BlockEdge(const CFGBlock* B1, const CFGBlock* B2) : ProgramPoint(B1, B2, BlockEdgeKind) {} CFGBlock* getSrc() const { return const_cast(static_cast(getData1())); } CFGBlock* getDst() const { return const_cast(static_cast(getData2())); } static bool classof(const ProgramPoint* Location) { return Location->getKind() == BlockEdgeKind; } }; } // end namespace clang namespace llvm { // Traits specialization for DenseMap template <> struct DenseMapInfo { static inline clang::ProgramPoint getEmptyKey() { uintptr_t x = reinterpret_cast(DenseMapInfo::getEmptyKey()) & ~0x7; return clang::BlockEntrance(reinterpret_cast(x)); } static inline clang::ProgramPoint getTombstoneKey() { uintptr_t x = reinterpret_cast(DenseMapInfo::getTombstoneKey()) & ~0x7; return clang::BlockEntrance(reinterpret_cast(x)); } static unsigned getHashValue(const clang::ProgramPoint& Loc) { return Loc.getHashValue(); } static bool isEqual(const clang::ProgramPoint& L, const clang::ProgramPoint& R) { return L == R; } static bool isPod() { return true; } }; } // end namespace llvm #endif