A program profiler written in C++ as the last project of my abstract data structures class. I cannot take credit for all the code included as some of the basic stuff was written by my professor, Dr. Jonathan I. Maletic, but I did write the algorithms for searching and inserting the necessary lines of code to profile a C++ program.
/////////////////////////////////////////////////////////////////////
// Searches an AST and returns a vector of list iterators pointing
// to the AST children that have a tag matching that specified
// REQUIRES: A vector of list iterators
//
std::vector::iterator>& AST::deepScan(std::string searchTag, std::vector::iterator>& vecToPopulate) {
std::list::iterator ptr = child.begin();
if (isStopTag(tag)){
return vecToPopulate;
}
while (ptr != child.end()) {
(*ptr)->deepScan(searchTag, vecToPopulate);
if ((*ptr)->tag == searchTag) {
vecToPopulate.push_back(ptr);
}
++ptr;
}
return vecToPopulate;
}
/////////////////////////////////////////////////////////////////////
// Adds in the includes and profile variables in a main file.
//
void AST::mainHeader(const std::vector& profileName) {
std::list::iterator ptr = child.begin();
while (ptr != child.end()) {
if ((*ptr)->tag == "function") {
break;
}
++ptr;
}
/////////////////////////////////////////////////////////////////////
// Create include directive
AST* cpp_include = new AST(token, "\n\n// Include header for profiling\n#include \"profile.hpp\"\n");
child.insert(ptr, cpp_include);
/////////////////////////////////////////////////////////////////////
// Create profile declaration for each in profileName
for (unsigned long i = 0; i < profileName.size(); ++i) {
std::string profName = profileName[i];
std::string profileDec = "profile " + profName + "(\"";
unsigned int lastUnderscoreIndex = 0;
for (unsigned int j = 0; j < profName.size(); ++j) {
if (profName[j] == '_') lastUnderscoreIndex = j;
}
profName[lastUnderscoreIndex] = '.';
profileDec += profName + "\");\n";
if (i == profileName.size() - 1) profileDec += "\n";
AST* profNode = new AST(token, profileDec);
child.insert(ptr, profNode);
}
}
/////////////////////////////////////////////////////////////////////
// Adds in the includes and profile variables for non-main files
//
void AST::fileHeader(const std::string& profileName) {
std::list::iterator ptr = child.begin();
while (ptr != child.end()) {
if ((*ptr)->tag == "function") {
break;
}
++ptr;
}
/////////////////////////////////////////////////////////////////////
// Create include directive
AST* cpp_include = new AST(token, "\n\n// Include header for profiling\n#include \"profile.hpp\"\n");
child.insert(ptr, cpp_include);
/////////////////////////////////////////////////////////////////////
// Create profile declaration for each in profileName
std::string profName = profileName;
std::string profileDec = "extern profile " + profName + ";\n\n";
AST* profNode = new AST(token, profileDec);
child.insert(ptr, profNode);
}
GitHub Link