From 4ffea976e7311dbe32dde887517c4e96f6b9556d Mon Sep 17 00:00:00 2001 From: vzyrianov Date: Fri, 10 Nov 2017 23:10:35 -0500 Subject: [PATCH 1/3] Abstracting map> into VariableRelationship class --- src/VariableRelationships.hpp | 85 +++++++++++++++++++++++++++++++++++ src/srcPtrPolicyTemplates.hpp | 69 ++++++++++++---------------- 2 files changed, 113 insertions(+), 41 deletions(-) create mode 100644 src/VariableRelationships.hpp diff --git a/src/VariableRelationships.hpp b/src/VariableRelationships.hpp new file mode 100644 index 0000000..d61e86e --- /dev/null +++ b/src/VariableRelationships.hpp @@ -0,0 +1,85 @@ +/** + * @file VariableRelationships.hpp + * + * @copyright Copyright (C) 2015-2016 srcML, LLC. (www.srcML.org) + * + * This file is part of srcPtr. + * + * srcPtr is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * srcPtr is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with srcPtr. If not, see . + */ + +#ifndef INCLUDED_VARIABLE_RELATIONSHIPS +#define INCLUDED_VARIABLE_RELATIONSHIPS + +#include + +#include +#include +#include + +class VariableRelationships { +public: + + void AddRelationship(Variable who, Variable what) { + relationships[who].insert(what); + }; + + std::set GetRelationships(Variable who) { + return relationships[who]; + }; + + void Print() { + std::cout << "_____________________________________________________" << std::endl << std::endl; + for (auto x : relationships) { + std::cout << x.first.linenumber << " - " << x.first.nameoftype << (x.first.isPointer ? "*" : "") << (x.first.isReference ? "&" : "") << " " << x.first << std::endl << std::endl; + for (auto y : x.second) + std::cout << y.linenumber << " - " << y.nameoftype << " " << y << " " << std::endl; + std::cout << "_____________________________________________________" << std::endl << std::endl; + } + }; + + void PrintGraphViz() { + std::cout << "digraph pointers {\n"; + + for (auto x : relationships) { + for (auto y : x.second) { + if(y.nameofidentifier != "") + std::cout << " \"" << x.first << "\" -> \"" << y << "\" \n"; + } + } + + std::cout << "}"; + }; + + std::vector GetKeys() { + std::vector keys; + for(auto x : relationships) + keys.push_back(x.first); + return keys; + } + + void Clear() { + relationships.clear(); + } + + void ClearRelationships(Variable var) { + relationships[var].clear(); + } + +private: + + std::map> relationships; +}; + +#endif \ No newline at end of file diff --git a/src/srcPtrPolicyTemplates.hpp b/src/srcPtrPolicyTemplates.hpp index 506d2c2..deb79d4 100644 --- a/src/srcPtrPolicyTemplates.hpp +++ b/src/srcPtrPolicyTemplates.hpp @@ -24,6 +24,7 @@ #include #include +#include #include @@ -60,43 +61,28 @@ class srcPtrAndersen { ~srcPtrAndersen() { }; void AddPointsToRelationship(Variable lhs, Variable rhs) { - pointsto[lhs].insert(rhs);// Adds reference only if lhs doesn't already point to rhs + pointsto.AddRelationship(lhs, rhs); } void AddAssignmentRelationship(Variable lhs, Variable rhs) { - if (std::find(pointerqueue[lhs].begin(), pointerqueue[lhs].end(), rhs) == pointerqueue[lhs].end()) { - pointerqueue[lhs].push_back(rhs); - } + pointerqueue.AddRelationship(lhs, rhs); } void Print() { Finalize(); - std::cout << "_____________________________________________________" << std::endl << std::endl; - for (auto x : pointsto) { - std::cout << x.first.linenumber << " - " << x.first.nameoftype << (x.first.isPointer ? " * " : " & ") << x.first << std::endl << std::endl; - for (auto y : x.second) - std::cout << y.linenumber << " - " << y.nameoftype << " " << y << " " << std::endl; - std::cout << "_____________________________________________________" << std::endl << std::endl; - } + + pointsto.Print(); } void PrintGraphViz() { Finalize(); - std::cout << "digraph pointers {\n"; - for (auto x : pointsto) { - for (auto y : x.second) { - if(y.nameofidentifier != "") - std::cout << " \"" << x.first << "\" -> \"" << y << "\" \n"; - } - } - - std::cout << "}"; + pointsto.PrintGraphViz(); } std::vector GetPointsTo(Variable ptr) { Finalize(); - std::set x = pointsto.at(ptr); // No const overload for operator[] + std::set x = pointsto.GetRelationships(ptr); // No const overload for operator[] std::vector result; for(Variable member : x) @@ -106,10 +92,8 @@ class srcPtrAndersen { std::vector GetPointers() { Finalize(); - std::vector pointers; - for(auto x : pointsto) - pointers.push_back(x.first); - return pointers; + + return pointsto.GetKeys(); } srcPtrAndersen *Clone() const { @@ -118,11 +102,13 @@ class srcPtrAndersen { //Resolves dependencies of each pointer void Finalize() { - for(auto var : pointsto) { - FinalizeVar(var.first, 0); + std::vector pointsto_keys = pointsto.GetKeys(); + for(auto var : pointsto_keys) { + FinalizeVar(var, 0); } - for(auto var : pointerqueue) { - FinalizeVar(var.first, 0); + std::vector pointerqueue_keys = pointerqueue.GetKeys(); + for(auto var : pointerqueue_keys) { + FinalizeVar(var, 0); } } @@ -131,29 +117,30 @@ class srcPtrAndersen { //Resolves dependencies of pointer void FinalizeVar(Variable ptr, int depth) { if(depth < MAX_DEPTH) { - for (auto var : pointerqueue[ptr]) { - if(pointerqueue[var].size() == 0) { - for (auto element : pointsto[var]) - pointsto[ptr].insert(element); + for (auto var : pointerqueue.GetRelationships(ptr)) { + if(pointerqueue.GetRelationships(var).size() == 0) { + for (auto element : pointsto.GetRelationships(var)) + pointsto.AddRelationship(ptr, element); } else { FinalizeVar(var, ++depth); - for (auto element : pointsto[var]) - pointsto[ptr].insert(element); + for (auto element : pointsto.GetRelationships(var)) + pointsto.AddRelationship(ptr, element); } } - pointerqueue[ptr].clear(); + + pointerqueue.ClearRelationships(ptr); } } - std::map> pointsto; - std::map> pointerqueue; + VariableRelationships pointsto; + VariableRelationships pointerqueue; }; /* class srcPtrDataSteensgaard { public: void AddPointsToRelationship(Variable lhs, Variable rhs) { - + } void Print() { @@ -164,8 +151,8 @@ class srcPtrDataSteensgaard { } private: - std::vector rank (100); - std::vector parent (100); + std::vector rank (100); + std::vector parent (100); disjoint_sets ds(&rank[0], &parent[0]); };*/ From 173c3a04b653270799de6f2dc2b4db7676086e0e Mon Sep 17 00:00:00 2001 From: vzyrianov Date: Fri, 17 Nov 2017 22:48:32 -0500 Subject: [PATCH 2/3] Adding cloning directions to README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 32cef58..2f54f68 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # srcPtr -Lightweight pointer analysis tool. \ No newline at end of file +Lightweight pointer analysis tool. + +When cloning make sure to run `git clone --recursive https://github.com/srcML/srcPtr.git` so that all the submodules are also cloned. From 90d8e53a1938e44140b67b4f6681a1975ba63ab7 Mon Sep 17 00:00:00 2001 From: vzyrianov Date: Sun, 19 Nov 2017 22:37:45 -0500 Subject: [PATCH 3/3] Decreasing max recursion depth for added performance --- src/srcPtrPolicyTemplates.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/srcPtrPolicyTemplates.hpp b/src/srcPtrPolicyTemplates.hpp index deb79d4..4a0e4cc 100644 --- a/src/srcPtrPolicyTemplates.hpp +++ b/src/srcPtrPolicyTemplates.hpp @@ -56,7 +56,7 @@ class srcPtrEmptyAlgorithm { class srcPtrAndersen { public: - const int MAX_DEPTH = 50; //Max recursion depth for computing the transitive closure. Used to prevent infinite loops with cyclic dependencies of pointers. + const int MAX_DEPTH = 10; //Max recursion depth for computing the transitive closure. Used to prevent infinite loops with cyclic dependencies of pointers. ~srcPtrAndersen() { };