<commit_before>#include "WrapperGenerator.h"

using namespace std;

void WrapperGenerator::initOpToGenMap() {
    shared_ptr<TracingInnerWrapperGenerator> traceGen;
    shared_ptr<CachingIPCInnerWrapperGenerator> cachingIPCGen;
    shared_ptr<NonCachingIPCInnerWrapperGenerator> nonCachingIPCGen;

    traceGen = make_shared<TracingInnerWrapperGenerator>(operationMap);
    cachingIPCGen = make_shared<CachingIPCInnerWrapperGenerator>();
    nonCachingIPCGen = make_shared<NonCachingIPCInnerWrapperGenerator>();

    wrapperToGenMap = WrapperGenMap({{"MUTEX_LOCK", traceGen}, 
                                     {"MUTEX_UNLOCK", traceGen},
                                     {"CV_WAIT", traceGen},
                                     {"CV_BROADCAST", traceGen},
                                     {"CV_SIGNAL", traceGen},
                                     {"QUEUE_ENQUEUE", traceGen},
                                     {"QUEUE_DEQUEUE", traceGen},
                                     {"MESSAGE_SEND", traceGen},
                                     {"MESSAGE_RECEIVE", traceGen},
                                     {"MKNOD", cachingIPCGen},
                                     {"OPEN", cachingIPCGen},
                                     {"PIPE", cachingIPCGen},
                                     {"MSGGET", cachingIPCGen},
                                     {"READ", nonCachingIPCGen},
                                     {"WRITE", nonCachingIPCGen},
                                     {"MSGRCV", nonCachingIPCGen},
                                     {"MSGSND", nonCachingIPCGen}});
}

WrapperGenerator::WrapperGenerator(shared_ptr<unordered_map<string, 
                                   FunctionPrototype>> _prototypeMap,
                                   std::shared_ptr<std::unordered_map<std::string, 
                                   std::string>> _operationMap,
                                   string pathPrefix):
                                   prototypeMap(_prototypeMap),
                                   operationMap(_operationMap),
                                   vprofInternalOps({{MKNOD, true}, {READ, true},
                                                     {WRITE, true}, {PIPE, true},
                                                     {MSGGET, true}, {MSGSND, true},
                                                     {MSGRCV, true}}) {
    initOpToGenMap();

    headerFile.open(pathPrefix + "VProfEventWrappers.h");
    implementationFile.open(pathPrefix + "VProfEventWrappers.cpp");

    const string generatedFileMessage = 
        " ///////////////////////////////////////////////////// \n"
        " // Note that this file was generated by VProfiler. // \n"
        " // Please do not change the contents of this file! // \n"
        " ///////////////////////////////////////////////////// \n\n";

    headerFile << generatedFileMessage; 
    implementationFile << generatedFileMessage;
}

bool WrapperGenerator::isIPCOperation(const Operation op) const { 
    return vprofInternalOps.find(op) != vprofInternalOps.end();
}

void instrumentIPCFunction(std::string &functionName, FunctionPrototype &prototype) {
}

vector<string> WrapperGenerator::getFilenames() {
    vector<string> result;

    for (auto kv : *prototypeMap) {
        result.push_back(kv.second.filename);
    }

    sort(result.begin(), result.end());
    result.erase(unique(result.begin(), result.end()), result.end());
    return result;
}

void WrapperGenerator::GenerateHeader() {
    vector<string> includeNames = getFilenames();
    headerFile << "#ifndef VPROFEVENTWRAPPERS_H\n#define VPROFEVENTWRAPPERS_H\n";
    for (string &includeName : includeNames) {
        headerFile << "#include \"" + includeName + "\"\n";
    }
    headerFile << "#include \"trace_tool.h\"\n\n"
                  "#ifdef __cplusplus\nextern \"C\" {\n#endif";

    for (auto kv : *prototypeMap) {
        headerFile << kv.second.functionPrototype + ";\n\n";
    }

    headerFile << "#ifdef __cplusplus\n}\n#endif\n\n#endif";

    headerFile.close();
}

void WrapperGenerator::GenerateImplementations() {
    string operation;
    implementationFile << "#include \"VProfEventWrappers.h\"\n\n";

    for (auto kv : *prototypeMap) {
        operation = (*operationMap)[kv.first];
        
        implementationFile << kv.second.functionPrototype + " {\n\t";

        if (kv.second.returnType != "void") {
            implementationFile << kv.second.returnType + " result;\n\t";
        }

        operationToGenMap[operation]->GenerateWrapperPrologue(kv.first, kv.second);

        if (kv.second.returnType != "void") {
            implementationFile << "result = ";
        }

        implementationFile << kv.second.innerCallPrefix + "(";

        for (int i = 0, j = kv.second.paramVars.size(); i < j; i++) {
            implementationFile << kv.second.paramVars[i];

            if (i != (j - 1)) {
                implementationFile << ", ";
            }
        }

        implementationFile <<");\n\t";

        operationToGenMap[operation]->GenerateWrapperEpilogue(kv.first, kv.second);

        if (kv.second.returnType != "void") {
            implementationFile << "\treturn result;\n";
        }

        implementationFile << "}\n\n";
    }<commit_msg>Few changes before band<commit_after>#include "WrapperGenerator.h"

using namespace std;

void WrapperGenerator::initOpToGenMap() {
    shared_ptr<TracingInnerWrapperGenerator> traceGen;
    shared_ptr<CachingIPCInnerWrapperGenerator> cachingIPCGen;
    shared_ptr<NonCachingIPCInnerWrapperGenerator> nonCachingIPCGen;

    traceGen = make_shared<TracingInnerWrapperGenerator>(operationMap);
    cachingIPCGen = make_shared<CachingIPCInnerWrapperGenerator>();
    nonCachingIPCGen = make_shared<NonCachingIPCInnerWrapperGenerator>();

    operationToGenerator = WrapperGenMap({{"MUTEX_LOCK", traceGen}, 
                                          {"MUTEX_UNLOCK", traceGen},
                                          {"CV_WAIT", traceGen},
                                          {"CV_BROADCAST", traceGen},
                                          {"CV_SIGNAL", traceGen},
                                          {"QUEUE_ENQUEUE", traceGen},
                                          {"QUEUE_DEQUEUE", traceGen},
                                          {"MESSAGE_SEND", traceGen},
                                          {"MESSAGE_RECEIVE", traceGen},
                                          {"MKNOD", cachingIPCGen},
                                          {"OPEN", cachingIPCGen},
                                          {"PIPE", cachingIPCGen},
                                          {"MSGGET", cachingIPCGen},
                                          {"READ", nonCachingIPCGen},
                                          {"WRITE", nonCachingIPCGen},
                                          {"MSGRCV", nonCachingIPCGen},
                                          {"MSGSND", nonCachingIPCGen}});
}

WrapperGenerator::WrapperGenerator(shared_ptr<unordered_map<string, 
                                   FunctionPrototype>> _prototypeMap,
                                   std::shared_ptr<std::unordered_map<std::string, 
                                   std::string>> _operationMap,
                                   string pathPrefix):
                                   prototypeMap(_prototypeMap),
                                   operationMap(_operationMap) {
    initOpToGenMap();

    headerFile.open(pathPrefix + "VProfEventWrappers.h");
    implementationFile.open(pathPrefix + "VProfEventWrappers.cpp");

    const string generatedFileMessage = 
        " ///////////////////////////////////////////////////// \n"
        " // Note that this file was generated by VProfiler. // \n"
        " // Please do not change the contents of this file! // \n"
        " ///////////////////////////////////////////////////// \n\n";

    headerFile << generatedFileMessage; 
    implementationFile << generatedFileMessage;
}

vector<string> WrapperGenerator::getFilenames() {
    vector<string> result;

    for (auto kv : *prototypeMap) {
        result.push_back(kv.second.filename);
    }

    sort(result.begin(), result.end());
    result.erase(unique(result.begin(), result.end()), result.end());
    return result;
}

void WrapperGenerator::GenerateHeader() {
    vector<string> includeNames = getFilenames();
    headerFile << "#ifndef VPROFEVENTWRAPPERS_H\n#define VPROFEVENTWRAPPERS_H\n";
    for (string &includeName : includeNames) {
        headerFile << "#include \"" + includeName + "\"\n";
    }
    headerFile << "#include \"trace_tool.h\"\n\n"
                  "#ifdef __cplusplus\nextern \"C\" {\n#endif";

    for (auto kv : *prototypeMap) {
        headerFile << kv.second.functionPrototype + ";\n\n";
    }

    headerFile << "#ifdef __cplusplus\n}\n#endif\n\n#endif";

    headerFile.close();
}

void WrapperGenerator::GenerateImplementations() {
    string operation;
    implementationFile << "#include \"VProfEventWrappers.h\"\n\n";

    for (auto kv : *prototypeMap) {
        operation = (*operationMap)[kv.first];
        
        implementationFile << kv.second.functionPrototype + " {\n\t";

        if (kv.second.returnType != "void") {
            implementationFile << kv.second.returnType + " result;\n\t";
        }

        operationToGenerator[operation]->GenerateWrapperPrologue(kv.first, kv.second);

        if (kv.second.returnType != "void") {
            implementationFile << "result = ";
        }

        implementationFile << kv.second.innerCallPrefix + "(";

        for (int i = 0, j = kv.second.paramVars.size(); i < j; i++) {
            implementationFile << kv.second.paramVars[i];

            if (i != (j - 1)) {
                implementationFile << ", ";
            }
        }

        implementationFile <<");\n\t";

        operationToGenerator[operation]->GenerateWrapperEpilogue(kv.first, kv.second);

        if (kv.second.returnType != "void") {
            implementationFile << "\treturn result;\n";
        }

        implementationFile << "}\n\n";
    }
{'fraction_non_alphanumeric': 0.13781545741324921, 'fraction_numerical': 0.0003943217665615142, 'mean_word_length': 1.6648279485158919, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 67, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3194511', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2541, 'n_words': 444}