//== MemRegion.h - Abstract memory regions for static 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 MemRegion and its subclasses. MemRegion defines a
// partially-typed abstraction of memory useful for path-sensitive dataflow
// analyses.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_ANALYSIS_MEMREGION_H
#define LLVM_CLANG_ANALYSIS_MEMREGION_H
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Checker/PathSensitive/SVals.h"
#include "llvm/Support/Casting.h"
#include "llvm/ADT/FoldingSet.h"
#include <string>
namespace llvm {
class BumpPtrAllocator;
class raw_ostream;
}
namespace clang {
class MemRegionManager;
class MemSpaceRegion;
class LocationContext;
class StackFrameContext;
class VarRegion;
//===----------------------------------------------------------------------===//
// Base region classes.
//===----------------------------------------------------------------------===//
/// MemRegion - The root abstract class for all memory regions.
class MemRegion : public llvm::FoldingSetNode {
friend class MemRegionManager;
public:
enum Kind {
// Memory spaces.
BEG_MEMSPACES,
GenericMemSpaceRegionKind = BEG_MEMSPACES,
StackLocalsSpaceRegionKind,
StackArgumentsSpaceRegionKind,
HeapSpaceRegionKind,
UnknownSpaceRegionKind,
GlobalsSpaceRegionKind,
END_MEMSPACES = GlobalsSpaceRegionKind,
// Untyped regions.
SymbolicRegionKind,
AllocaRegionKind,
// Typed regions.
BEG_TYPED_REGIONS,
FunctionTextRegionKind = BEG_TYPED_REGIONS,
BlockTextRegionKind,
BlockDataRegionKind,
CompoundLiteralRegionKind,
CXXThisRegionKind,
StringRegionKind,
ElementRegionKind,
// Decl Regions.
BEG_DECL_REGIONS,
VarRegionKind = BEG_DECL_REGIONS,
FieldRegionKind,
ObjCIvarRegionKind,
CXXObjectRegionKind,
END_DECL_REGIONS = CXXObjectRegionKind,
END_TYPED_REGIONS = END_DECL_REGIONS
};
private:
const Kind kind;
protected:
MemRegion(Kind k) : kind(k) {}
virtual ~MemRegion();
public:
ASTContext &getContext() const;
virtual void Profile(llvm::FoldingSetNodeID& ID) const = 0;
virtual MemRegionManager* getMemRegionManager() const = 0;
std::string getString() const;
const MemSpaceRegion *getMemorySpace() const;
const MemRegion *getBaseRegion() const;
const MemRegion *StripCasts() const;
bool hasGlobalsOrParametersStorage() const;
bool hasStackStorage() const;
bool hasStackNonParametersStorage() const;
bool hasStackParametersStorage() const;
virtual void dumpToStream(llvm::raw_ostream& os) const;
void dump() const;
Kind getKind() const { return kind; }
template<typename RegionTy> const RegionTy* getAs() const;
virtual bool isBoundable() const { return false; }
static bool classof(const MemRegion*) { return true; }
};
/// MemSpaceRegion - A memory region that represents and "memory space";
/// for example, the set of global variables, the stack frame, etc.
class MemSpaceRegion : public MemRegion {
protected:
friend class MemRegionManager;
MemRegionManager *Mgr;
MemSpaceRegion(MemRegionManager *mgr, Kind k = GenericMemSpaceRegionKind)
: MemRegion(k), Mgr(mgr) {
assert(classof(this));
}
MemRegionManager* getMemRegionManager() const { return Mgr; }
public:
bool isBoundable() const { return false; }
void Profile(llvm::FoldingSetNodeID &ID) const;
static bool classof(const MemRegion *R) {
Kind k = R->getKind();
return k >= BEG_MEMSPACES && k <= END_MEMSPACES;
}
};
class GlobalsSpaceRegion : public MemSpaceRegion {
friend class MemRegionManager;
GlobalsSpaceRegion(MemRegionManager *mgr)
: MemSpaceRegion(mgr, GlobalsSpaceRegionKind) {}
public:
static bool classof(const MemRegion *R) {
return R->getKind() == GlobalsSpaceRegionKind;
}
};
class HeapSpaceRegion : public MemSpaceRegion {
friend class MemRegionManager;
HeapSpaceRegion(MemRegionManager *mgr)
: MemSpaceRegion(mgr, HeapSpaceRegionKind) {}
public:
static bool classof(const MemRegion *R) {
return R->getKind() == HeapSpaceRegionKind;
}
};
class UnknownSpaceRegion : public MemSpaceRegion {
friend class MemRegionManager;
UnknownSpaceRegion(MemRegionManager *mgr)
: MemSpaceRegion(mgr, UnknownSpaceRegionKind) {}
public:
static