Skip to content

Split variable adder and constrain variable passes #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions clang/include/clang/3C/ProgramInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ class ProgramInfo : public ProgramVariableAdder {

void addTypedef(PersistentSourceLoc PSL, bool ShouldCheck);

// For each pointer type in the declaration of D, add a variable to the
// constraint system for that pointer type.
void addVariable(clang::DeclaratorDecl *D, clang::ASTContext *AstContext);

private:
// List of constraint variables for declarations, indexed by their location in
// the source. This information persists across invocations of the constraint
Expand Down Expand Up @@ -199,10 +203,6 @@ class ProgramInfo : public ProgramVariableAdder {
// Retrieves a FVConstraint* from a Decl (which could be static, or global)
FVConstraint *getFuncFVConstraint(FunctionDecl *FD, ASTContext *C);

// For each pointer type in the declaration of D, add a variable to the
// constraint system for that pointer type.
void addVariable(clang::DeclaratorDecl *D, clang::ASTContext *AstContext);
Copy link
Collaborator

@john-h-kastner john-h-kastner Feb 24, 2021

Choose a reason for hiding this comment

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

This was originally made private so that it could be exposed by the ProgramVariableAdder interface used by VariableAdderVisitor. The idea was that only VariableAdderVisitor should be creating variables, so restricting access to this function makes it less likely that variables are added elsewhere.

It looks like you made this change because you need seenTypedef and addTypedef, so I suggest adding these functions to ProgramVariableAdder and keeping addVariable private.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, I forgot to ask about this. It looks like we have multiple interfaces, but only use one class. But this seems like a decent explanation so I'll follow your suggestion unless I hear otherwise.


void insertIntoPtrSourceMap(const PersistentSourceLoc *PSL,
ConstraintVariable *CV);

Expand Down
44 changes: 23 additions & 21 deletions clang/lib/3C/ConstraintBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class FunctionVisitor : public RecursiveASTVisitor<FunctionVisitor> {
constrainConsVarGeq(ParameterDC, ArgumentConstraints, CS, &PL,
Wild_to_Safe, false, &Info, false);

if (AllTypes && TFD != nullptr) {
if (AllTypes && TFD != nullptr && I < TFD->getNumParams()) {
auto *PVD = TFD->getParamDecl(I);
auto &ABI = Info.getABoundsInfo();
// Here, we need to handle context-sensitive assignment.
Expand Down Expand Up @@ -481,20 +481,6 @@ class ConstraintGenVisitor : public RecursiveASTVisitor<ConstraintGenVisitor> {
TypeVarInfo &TVI)
: Context(Context), Info(I), CB(Info, Context), TVInfo(TVI), ISD() {}

bool VisitTypedefDecl(TypedefDecl* TD) {
CVarSet empty;
auto PSL = PersistentSourceLoc::mkPSL(TD, *Context);
// If we haven't seen this typedef before, initialize it's entry in the
// typedef map. If we have seen it before, and we need to preserve the
// constraints contained within it
if (!Info.seenTypedef(PSL))
// Add this typedef to the program info, if it contains a ptr to
// an anonymous struct we mark as not being rewritable
Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD));

return true;
}

bool VisitVarDecl(VarDecl *G) {

if (G->hasGlobalStorage() && isPtrOrArrayType(G->getType())) {
Expand Down Expand Up @@ -565,9 +551,23 @@ class ConstraintGenVisitor : public RecursiveASTVisitor<ConstraintGenVisitor> {
// visitor which is executed before both of the other visitors.
class VariableAdderVisitor : public RecursiveASTVisitor<VariableAdderVisitor> {
public:
explicit VariableAdderVisitor(ASTContext *Context, ProgramVariableAdder &VA)
: Context(Context), VarAdder(VA) {}
explicit VariableAdderVisitor(ASTContext *Context, ProgramInfo &I)
: Context(Context), Info(I) {}


bool VisitTypedefDecl(TypedefDecl* TD) {
CVarSet empty;
auto PSL = PersistentSourceLoc::mkPSL(TD, *Context);
// If we haven't seen this typedef before, initialize it's entry in the
// typedef map. If we have seen it before, and we need to preserve the
// constraints contained within it
if (!Info.seenTypedef(PSL))
// Add this typedef to the program info, if it contains a ptr to
// an anonymous struct we mark as not being rewritable
Info.addTypedef(PSL, !PtrToStructDef::containsPtrToStructDef(TD));

return true;
}

bool VisitVarDecl(VarDecl *D) {
FullSourceLoc FL = Context->getFullLoc(D->getBeginLoc());
Expand All @@ -579,7 +579,7 @@ class VariableAdderVisitor : public RecursiveASTVisitor<VariableAdderVisitor> {
bool VisitFunctionDecl(FunctionDecl *D) {
FullSourceLoc FL = Context->getFullLoc(D->getBeginLoc());
if (FL.isValid())
VarAdder.addVariable(D, Context);
Info.addVariable(D, Context);
return true;
}

Expand All @@ -603,12 +603,12 @@ class VariableAdderVisitor : public RecursiveASTVisitor<VariableAdderVisitor> {

private:
ASTContext *Context;
ProgramVariableAdder &VarAdder;
ProgramInfo &Info;

void addVariable(DeclaratorDecl *D) {
VarAdder.addABoundsVariable(D);
Info.addABoundsVariable(D);
if (isPtrOrArrayType(D->getType()))
VarAdder.addVariable(D, Context);
Info.addVariable(D, Context);
}
};

Expand Down Expand Up @@ -640,6 +640,8 @@ void ConstraintBuilderConsumer::HandleTranslationUnit(ASTContext &C) {
// variable information gathered in the type variable traversal.
VAV.TraverseDecl(D);
CSBV.TraverseDecl(D);
}
for (const auto &D : TUD->decls()) {
TV.TraverseDecl(D);
GV.TraverseDecl(D);
}
Expand Down