Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions clang/include/clang/CConv/AVarBoundsInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ class AvarBoundsInference {

// Infer bounds for the given key from the set of given ARR atoms.
// The flag FromPB requests the inference to use potential length variables.
bool inferBounds(BoundsKey K, bool FromPB = false);
bool inferBounds(BoundsKey K, AVarGraph &BKGraph, bool FromPB = false);
private:
bool inferPossibleBounds(BoundsKey K, ABounds *SB,
AVarGraph &BKGraph,
std::set<ABounds *> &EB);

bool intersectBounds(std::set<ProgramVar *> &ProgVars,
Expand All @@ -110,6 +111,7 @@ class AvarBoundsInference {
std::set<ABounds *> &ResBounds);

bool predictBounds(BoundsKey K, std::set<BoundsKey> &Neighbours,
AVarGraph &BKGraph,
ABounds **KB);


Expand All @@ -120,7 +122,7 @@ class AvarBoundsInference {

class AVarBoundsInfo {
public:
AVarBoundsInfo() : ProgVarGraph(this) {
AVarBoundsInfo() : ProgVarGraph(this), CtxSensProgVarGraph(this) {
BCount = 1;
PVarInfo.clear();
InProgramArrPtrBoundsKeys.clear();
Expand Down Expand Up @@ -208,7 +210,8 @@ class AVarBoundsInfo {
// Create context sensitive BoundsKey variables for the given set of
// ConstraintVariables.
bool contextualizeCVar(CallExpr *CE,
const std::set<ConstraintVariable *> &CV);
const std::set<ConstraintVariable *> &CV,
ASTContext *C);
// Get the context sensitive BoundsKey for the given key.
// If there exists no context-sensitive bounds key, we just return
// the provided key.
Expand Down Expand Up @@ -269,6 +272,9 @@ class AVarBoundsInfo {

// Graph of all program variables.
AVarGraph ProgVarGraph;
// Graph that contains only edges between context-sensitive
// BoundsKey and corresponding original BoundsKey.
AVarGraph CtxSensProgVarGraph;
// Stats on techniques used to find length for various variables.
AVarBoundsStats BoundsInferStats;
// This is the map of pointer variable bounds key and set of bounds key
Expand All @@ -293,6 +299,9 @@ class AVarBoundsInfo {

void insertProgramVar(BoundsKey NK, ProgramVar *PV);

void insertCtxSensBoundsKey(ProgramVar *OldPV, BoundsKey NK,
CtxFunctionArgScope *CFAS);

// Check if the provided bounds key corresponds to function return.
bool isFunctionReturn(BoundsKey BK);

Expand All @@ -303,9 +312,11 @@ class AVarBoundsInfo {
// returns true if any thing changed, else false.
bool keepHighestPriorityBounds(std::set<BoundsKey> &ArrPtrs);

// Perform worklist based inference on the requested array variables.
// Perform worklist based inference on the requested array variables using
// the provided graph.
// The flag FromPB requests the algorithm to use potential length variables.
bool performWorkListInference(std::set<BoundsKey> &ArrNeededBounds,
AVarGraph &BKGraph,
bool FromPB = false);

void insertParamKey(ParamDeclType ParamDecl, BoundsKey NK);
Expand Down
15 changes: 11 additions & 4 deletions clang/include/clang/CConv/ConstraintVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,20 @@ enum ConsAction {
void constrainConsVarGeq(const std::set<ConstraintVariable *> &LHS,
const std::set<ConstraintVariable *> &RHS,
Constraints &CS, PersistentSourceLoc *PL,
ConsAction CA, bool doEqType, ProgramInfo *Info);
ConsAction CA, bool doEqType,
ProgramInfo *Info,
bool HandleBoundsKey = true);
void constrainConsVarGeq(ConstraintVariable *LHS, const CVarSet &RHS,
Constraints &CS, PersistentSourceLoc *PL,
ConsAction CA, bool doEqType, ProgramInfo *Info);
void constrainConsVarGeq(ConstraintVariable *LHS, ConstraintVariable *RHS,
ConsAction CA, bool doEqType,
ProgramInfo *Info,
bool HandleBoundsKey = true);
void constrainConsVarGeq(ConstraintVariable *LHS,
ConstraintVariable *RHS,
Constraints &CS, PersistentSourceLoc *PL,
ConsAction CA, bool doEqType, ProgramInfo *Info);
ConsAction CA, bool doEqType,
ProgramInfo *Info,
bool HandleBoundsKey = true);

// True if [C] is a PVConstraint that contains at least one Atom (i.e.,
// it represents a C pointer)
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/CConv/PersistentSourceLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ class PersistentSourceLoc {
static
PersistentSourceLoc mkPSL(const clang::Stmt *S, clang::ASTContext &Context);

static
PersistentSourceLoc mkPSL(const clang::Expr *E, clang::ASTContext &Context);

private:
// Create a PersistentSourceLoc based on absolute file path
// from the given SourceRange and SourceLocation.
Expand Down
95 changes: 81 additions & 14 deletions clang/include/clang/CConv/ProgramVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdint.h>
#include <string>
#include "clang/AST/ASTContext.h"
#include "clang/CConv/PersistentSourceLoc.h"

typedef uint32_t BoundsKey;

Expand All @@ -28,17 +29,18 @@ class ProgramVarScope {
FunctionScopeKind,
// Function parameter scope.
FunctionParamScopeKind,
// Context sensitive argument scope.
CtxFunctionArgScopeKind,
// Struct scope.
StructScopeKind,
// Global scope.
GlobalScopeKind,
};
ScopeKind getKind() const { return Kind; }

private:
ScopeKind Kind;
protected:
ProgramVarScope(ScopeKind K): Kind(K) { }
ScopeKind Kind;
public:
virtual ~ProgramVarScope() { }

Expand Down Expand Up @@ -116,7 +118,7 @@ class StructScope : public ProgramVarScope {

private:
std::string StName;
static std::map<std::string, StructScope*> StScopeMap;
static std::map<std::string, StructScope *> StScopeMap;
};

class FunctionScope;
Expand All @@ -135,7 +137,8 @@ class FunctionParamScope : public ProgramVarScope {
}

bool operator==(const ProgramVarScope &O) const {
if (const FunctionParamScope *FPS = clang::dyn_cast<FunctionParamScope>(&O)) {
if (const FunctionParamScope *FPS =
Comment thread
Machiry marked this conversation as resolved.
Outdated
clang::dyn_cast<FunctionParamScope>(&O)) {
return (FPS->FName == FName && FPS->IsStatic == IsStatic);
}
return false;
Expand All @@ -153,15 +156,73 @@ class FunctionParamScope : public ProgramVarScope {
return "FuncParm_" + FName;
}

static FunctionParamScope *getFunctionParamScope(std::string FnName,
bool IsSt);
std::string getFName() const {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this instead return a const std::string & or llvm::StringRef to avoid copying the string?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Done.

return FName;
}

private:
bool getIsStatic() const {
return IsStatic;
}

static FunctionParamScope *
getFunctionParamScope(std::string FnName, bool IsSt);

protected:
std::string FName;
bool IsStatic;

private:
static std::map<std::pair<std::string, bool>,
FunctionParamScope*> FnParmScopeMap;
FunctionParamScope *> FnParmScopeMap;
};

// Context-sensitive arguments scope.
class CtxFunctionArgScope : public FunctionParamScope {
public:
friend class FunctionScope;
CtxFunctionArgScope(std::string FN, bool IsSt,
const PersistentSourceLoc &CtxPSL) :
FunctionParamScope(FN, IsSt) {
PSL = CtxPSL;
this->Kind = CtxFunctionArgScopeKind;
}

virtual ~CtxFunctionArgScope() { }

static bool classof(const ProgramVarScope *S) {
return S->getKind() == CtxFunctionArgScopeKind;
}

bool operator==(const ProgramVarScope &O) const {
if (const CtxFunctionArgScope *FPS =
clang::dyn_cast<CtxFunctionArgScope>(&O)) {
return (FPS->FName == FName &&
FPS->IsStatic == IsStatic &&
!(FPS->PSL < PSL || PSL < FPS->PSL));
}
return false;
}

bool operator!=(const ProgramVarScope &O) const {
return !(*this == O);
}

bool operator<(const ProgramVarScope &O) const {
return clang::isa<GlobalScope>(&O);
}

std::string getStr() const {
return "CtxFuncArg_" + FName;
}

static CtxFunctionArgScope *
getCtxFunctionParamScope(FunctionParamScope *FPS,
const PersistentSourceLoc &PSL);

private:
PersistentSourceLoc PSL;

static std::map<std::tuple<std::string, bool, PersistentSourceLoc>,
CtxFunctionArgScope *> CtxFnArgScopeMap;
};

class FunctionScope : public ProgramVarScope {
Expand All @@ -177,10 +238,12 @@ class FunctionScope : public ProgramVarScope {
}

bool operator==(const ProgramVarScope &O) const {
if (const FunctionScope *FS = clang::dyn_cast<FunctionScope>(&O)) {
if (const FunctionScope *FS =
clang::dyn_cast<FunctionScope>(&O)) {
return (FS->FName == FName && FS->IsStatic == IsStatic);
}
if (const FunctionParamScope *FPS = clang::dyn_cast<FunctionParamScope>(&O)) {
if (const FunctionParamScope *FPS =
clang::dyn_cast<FunctionParamScope>(&O)) {
return (FPS->FName == FName && FPS->IsStatic == IsStatic);
}
return false;
Expand All @@ -198,25 +261,29 @@ class FunctionScope : public ProgramVarScope {
return "InFunc_" + FName;
}

static FunctionScope *getFunctionScope(std::string FnName, bool IsSt);
static FunctionScope *getFunctionScope(std::string FnName,
bool IsSt);

private:
std::string FName;
bool IsStatic;

static std::map<std::pair<std::string, bool>, FunctionScope*> FnScopeMap;
static std::map<std::pair<std::string, bool>, FunctionScope *>
FnScopeMap;
};

// Class that represents a program variable along with its scope.
class ProgramVar {
public:
ProgramVar(BoundsKey VK, std::string VName, ProgramVarScope *PVS, bool IsCons) :
ProgramVar(BoundsKey VK, std::string VName, ProgramVarScope *PVS,
bool IsCons) :
K(VK), VarName(VName), VScope(PVS), IsConstant(IsCons) { }

ProgramVar(BoundsKey VK, std::string VName, ProgramVarScope *PVS) :
ProgramVar(VK, VName, PVS, false) { }

ProgramVarScope *getScope() { return VScope; }
void setScope(ProgramVarScope *PVS) { this->VScope = PVS; }
BoundsKey getKey() { return K; }
bool IsNumConstant() { return IsConstant; }
std::string mkString(bool GetKey = false);
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/CConv/RewriteUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ class ArrayBoundsRewriter {
ArrayBoundsRewriter(ASTContext *C, ProgramInfo &I): Context(C), Info(I) {}
// Get the string representation of the bounds for the given variable.
std::string getBoundsString(PVConstraint *PV, Decl *D, bool Isitype = false);
// Check if the constraint variable has newly created bounds string.
bool hasNewBoundsString(PVConstraint *PV, Decl *D, bool Isitype = false);
private:
ASTContext *Context;
ProgramInfo &Info;
Expand Down
Loading