opnsense-src/include/clang/Analysis/ProgramPoint.h

348 lines
9.6 KiB
C
Raw Normal View History

2009-06-02 13:58:47 -04:00
//==- 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
2009-10-14 14:03:49 -04:00
#include "clang/Analysis/CFG.h"
2009-11-04 10:04:32 -05:00
#include "llvm/System/DataTypes.h"
2009-06-02 13:58:47 -04:00
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/Support/Casting.h"
#include <cassert>
#include <utility>
namespace clang {
2009-10-14 14:03:49 -04:00
class LocationContext;
2009-06-02 13:58:47 -04:00
class ProgramPoint {
public:
2009-10-14 14:03:49 -04:00
enum Kind { BlockEdgeKind,
BlockEntranceKind,
BlockExitKind,
PreStmtKind,
PostStmtKind,
2009-11-18 09:59:57 -05:00
PreLoadKind,
2009-10-14 14:03:49 -04:00
PostLoadKind,
2009-11-18 09:59:57 -05:00
PreStoreKind,
2009-10-14 14:03:49 -04:00
PostStoreKind,
PostPurgeDeadSymbolsKind,
PostStmtCustomKind,
PostLValueKind,
2009-06-02 13:58:47 -04:00
MinPostStmtKind = PostStmtKind,
MaxPostStmtKind = PostLValueKind };
private:
2009-10-14 14:03:49 -04:00
std::pair<const void *, const void *> Data;
Kind K;
// The LocationContext could be NULL to allow ProgramPoint to be used in
// context insensitive analysis.
const LocationContext *L;
2009-06-02 13:58:47 -04:00
const void *Tag;
protected:
2009-10-14 14:03:49 -04:00
ProgramPoint(const void* P, Kind k, const LocationContext *l,
const void *tag = 0)
: Data(P, NULL), K(k), L(l), Tag(tag) {}
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
ProgramPoint(const void* P1, const void* P2, Kind k, const LocationContext *l,
const void *tag = 0)
: Data(P1, P2), K(k), L(l), Tag(tag) {}
protected:
const void* getData1() const { return Data.first; }
const void* getData2() const { return Data.second; }
2009-06-02 13:58:47 -04:00
const void *getTag() const { return Tag; }
2009-10-14 14:03:49 -04:00
public:
Kind getKind() const { return K; }
const LocationContext *getLocationContext() const { return L; }
2009-06-02 13:58:47 -04:00
// For use with DenseMap. This hash is probably slow.
unsigned getHashValue() const {
llvm::FoldingSetNodeID ID;
2009-10-14 14:03:49 -04:00
Profile(ID);
2009-06-02 13:58:47 -04:00
return ID.ComputeHash();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint*) { return true; }
bool operator==(const ProgramPoint & RHS) const {
2009-10-14 14:03:49 -04:00
return K == RHS.K && Data == RHS.Data && L == RHS.L && Tag == RHS.Tag;
2009-06-02 13:58:47 -04:00
}
bool operator!=(const ProgramPoint& RHS) const {
2009-10-14 14:03:49 -04:00
return K != RHS.K || Data != RHS.Data || L != RHS.L || Tag != RHS.Tag;
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
void Profile(llvm::FoldingSetNodeID& ID) const {
2009-10-14 14:03:49 -04:00
ID.AddInteger((unsigned) K);
ID.AddPointer(Data.first);
ID.AddPointer(Data.second);
ID.AddPointer(L);
2009-06-02 13:58:47 -04:00
ID.AddPointer(Tag);
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
class BlockEntrance : public ProgramPoint {
public:
2009-10-14 14:03:49 -04:00
BlockEntrance(const CFGBlock* B, const LocationContext *L,
const void *tag = 0)
: ProgramPoint(B, BlockEntranceKind, L, tag) {}
2009-06-02 13:58:47 -04:00
CFGBlock* getBlock() const {
2009-10-14 14:03:49 -04:00
return const_cast<CFGBlock*>(reinterpret_cast<const CFGBlock*>(getData1()));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2010-01-01 05:34:51 -05:00
CFGElement getFirstElement() const {
2009-10-14 14:03:49 -04:00
const CFGBlock* B = getBlock();
2010-01-01 05:34:51 -05:00
return B->empty() ? CFGElement() : B->front();
}
Stmt *getFirstStmt() const {
return getFirstElement().getStmt();
2009-06-02 13:58:47 -04:00
}
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == BlockEntranceKind;
}
};
class BlockExit : public ProgramPoint {
public:
2009-10-14 14:03:49 -04:00
BlockExit(const CFGBlock* B, const LocationContext *L)
: ProgramPoint(B, BlockExitKind, L) {}
2009-06-02 13:58:47 -04:00
CFGBlock* getBlock() const {
2009-10-14 14:03:49 -04:00
return const_cast<CFGBlock*>(reinterpret_cast<const CFGBlock*>(getData1()));
2009-06-02 13:58:47 -04:00
}
Stmt* getLastStmt() const {
2009-10-14 14:03:49 -04:00
const CFGBlock* B = getBlock();
2010-01-01 05:34:51 -05:00
return B->empty() ? CFGElement() : B->back();
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
Stmt* getTerminator() const {
return getBlock()->getTerminator();
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == BlockExitKind;
}
};
2009-10-14 14:03:49 -04:00
class StmtPoint : public ProgramPoint {
public:
StmtPoint(const Stmt *S, const void *p2, Kind k, const LocationContext *L,
const void *tag)
: ProgramPoint(S, p2, k, L, tag) {}
const Stmt *getStmt() const { return (const Stmt*) getData1(); }
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
template <typename T>
const T* getStmtAs() const { return llvm::dyn_cast<T>(getStmt()); }
static bool classof(const ProgramPoint* Location) {
unsigned k = Location->getKind();
return k >= PreStmtKind && k <= MaxPostStmtKind;
}
};
class PreStmt : public StmtPoint {
2009-06-02 13:58:47 -04:00
public:
2009-10-14 14:03:49 -04:00
PreStmt(const Stmt *S, const LocationContext *L, const void *tag,
const Stmt *SubStmt = 0)
: StmtPoint(S, SubStmt, PreStmtKind, L, tag) {}
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
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 LocationContext *L, const void *tag = 0)
: StmtPoint(S, NULL, k, L, tag) {}
PostStmt(const Stmt* S, const void* data, Kind k, const LocationContext *L,
const void *tag =0)
: StmtPoint(S, data, k, L, tag) {}
public:
explicit PostStmt(const Stmt* S, const LocationContext *L,const void *tag = 0)
: StmtPoint(S, NULL, PostStmtKind, L, tag) {}
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
unsigned k = Location->getKind();
return k >= MinPostStmtKind && k <= MaxPostStmtKind;
}
};
class PostStmtCustom : public PostStmt {
public:
PostStmtCustom(const Stmt* S,
2009-10-14 14:03:49 -04:00
const std::pair<const void*, const void*>* TaggedData,\
const LocationContext *L)
: PostStmt(S, TaggedData, PostStmtCustomKind, L) {}
2009-06-02 13:58:47 -04:00
const std::pair<const void*, const void*>& getTaggedPair() const {
2009-10-14 14:03:49 -04:00
return
*reinterpret_cast<const std::pair<const void*, const void*>*>(getData2());
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const void* getTag() const { return getTaggedPair().first; }
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const void* getTaggedData() const { return getTaggedPair().second; }
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == PostStmtCustomKind;
}
};
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
class LocationCheck : public StmtPoint {
protected:
LocationCheck(const Stmt *S, const LocationContext *L,
ProgramPoint::Kind K, const void *tag)
: StmtPoint(S, NULL, K, L, tag) {}
static bool classof(const ProgramPoint *location) {
unsigned k = location->getKind();
return k == PreLoadKind || k == PreStoreKind;
2009-06-02 13:58:47 -04:00
}
};
2009-11-18 09:59:57 -05:00
class PreLoad : public LocationCheck {
2009-06-02 13:58:47 -04:00
public:
2009-11-18 09:59:57 -05:00
PreLoad(const Stmt *S, const LocationContext *L, const void *tag = 0)
: LocationCheck(S, L, PreLoadKind, tag) {}
static bool classof(const ProgramPoint *location) {
return location->getKind() == PreLoadKind;
2009-06-02 13:58:47 -04:00
}
};
2009-10-14 14:03:49 -04:00
2009-11-18 09:59:57 -05:00
class PreStore : public LocationCheck {
2009-06-02 13:58:47 -04:00
public:
2009-11-18 09:59:57 -05:00
PreStore(const Stmt *S, const LocationContext *L, const void *tag = 0)
: LocationCheck(S, L, PreStoreKind, tag) {}
static bool classof(const ProgramPoint *location) {
return location->getKind() == PreStoreKind;
2009-06-02 13:58:47 -04:00
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
class PostLoad : public PostStmt {
public:
2009-10-14 14:03:49 -04:00
PostLoad(const Stmt* S, const LocationContext *L, const void *tag = 0)
: PostStmt(S, PostLoadKind, L, tag) {}
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == PostLoadKind;
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
class PostStore : public PostStmt {
public:
2009-10-14 14:03:49 -04:00
PostStore(const Stmt* S, const LocationContext *L, const void *tag = 0)
: PostStmt(S, PostStoreKind, L, tag) {}
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == PostStoreKind;
}
};
class PostLValue : public PostStmt {
public:
2009-10-14 14:03:49 -04:00
PostLValue(const Stmt* S, const LocationContext *L, const void *tag = 0)
: PostStmt(S, PostLValueKind, L, tag) {}
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == PostLValueKind;
}
2009-10-14 14:03:49 -04:00
};
2009-06-02 13:58:47 -04:00
class PostPurgeDeadSymbols : public PostStmt {
public:
2009-10-14 14:03:49 -04:00
PostPurgeDeadSymbols(const Stmt* S, const LocationContext *L,
const void *tag = 0)
: PostStmt(S, PostPurgeDeadSymbolsKind, L, tag) {}
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == PostPurgeDeadSymbolsKind;
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
class BlockEdge : public ProgramPoint {
public:
2009-10-14 14:03:49 -04:00
BlockEdge(const CFGBlock* B1, const CFGBlock* B2, const LocationContext *L)
: ProgramPoint(B1, B2, BlockEdgeKind, L) {}
2009-06-02 13:58:47 -04:00
CFGBlock* getSrc() const {
2009-10-14 14:03:49 -04:00
return const_cast<CFGBlock*>(static_cast<const CFGBlock*>(getData1()));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
CFGBlock* getDst() const {
2009-10-14 14:03:49 -04:00
return const_cast<CFGBlock*>(static_cast<const CFGBlock*>(getData2()));
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
static bool classof(const ProgramPoint* Location) {
return Location->getKind() == BlockEdgeKind;
}
};
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
} // end namespace clang
2009-10-14 14:03:49 -04:00
namespace llvm { // Traits specialization for DenseMap
2009-06-02 13:58:47 -04:00
template <> struct DenseMapInfo<clang::ProgramPoint> {
static inline clang::ProgramPoint getEmptyKey() {
uintptr_t x =
2009-10-14 14:03:49 -04:00
reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getEmptyKey()) & ~0x7;
return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
2009-06-02 13:58:47 -04:00
}
static inline clang::ProgramPoint getTombstoneKey() {
uintptr_t x =
2009-10-14 14:03:49 -04:00
reinterpret_cast<uintptr_t>(DenseMapInfo<void*>::getTombstoneKey()) & ~0x7;
return clang::BlockEntrance(reinterpret_cast<clang::CFGBlock*>(x), 0);
2009-06-02 13:58:47 -04:00
}
static unsigned getHashValue(const clang::ProgramPoint& Loc) {
return Loc.getHashValue();
}
static bool isEqual(const clang::ProgramPoint& L,
const clang::ProgramPoint& R) {
return L == R;
}
};
2009-12-15 13:49:47 -05:00
template <>
struct isPodLike<clang::ProgramPoint> { static const bool value = true; };
2009-06-02 13:58:47 -04:00
} // end namespace llvm
#endif