<commit_before>//=======================================================================
// Copyright <NAME> 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <string>
#include <unordered_map>

#include <boost/variant.hpp>

#include "tac/BasicBlockExtractor.hpp"
#include "tac/Program.hpp"

using namespace eddic;

namespace {


} //end of anonymous namespace

void tac::BasicBlockExtractor::extract(tac::Program& program) const {
    std::unordered_map<std::string, std::shared_ptr<BasicBlock>> labels;

    for(auto& function : program.functions){
        auto current = function->newBasicBlock();

        bool nextIsLeader = false;

        for(auto& statement : function->getStatements()){
            if(auto* ptr = boost::get<std::string>(&statement)){
                function->newBasicBlock();

                labels[*ptr] = function->currentBasicBlock();

                nextIsLeader = false;
            } else {
                if(nextIsLeader){
                    function->newBasicBlock();
                    nextIsLeader = false;
                }

                if(boost::get<tac::IfFalse>(&statement) || boost::get<tac::Return>(&statement) || boost::get<tac::Goto>(&statement)){
                    nextIsLeader = true;
                }

                function->currentBasicBlock()->add(statement);
            }
        }

        function->getStatements().clear();
    }
}
<commit_msg>After basic block extraction, set the basic block reference into conditional statements<commit_after>//=======================================================================
// Copyright <NAME> 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================

#include <string>
#include <unordered_map>

#include <boost/variant.hpp>

#include "tac/BasicBlockExtractor.hpp"
#include "tac/Program.hpp"

using namespace eddic;

void tac::BasicBlockExtractor::extract(tac::Program& program) const {
    for(auto& function : program.functions){
        std::unordered_map<std::string, std::shared_ptr<BasicBlock>> labels;
        
        function->newBasicBlock();

        bool nextIsLeader = false;

        //First separate the statements into basic blocks
        for(auto& statement : function->getStatements()){
            if(auto* ptr = boost::get<std::string>(&statement)){
                function->newBasicBlock();

                labels[*ptr] = function->currentBasicBlock();

                nextIsLeader = false;
            } else {
                if(nextIsLeader){
                    function->newBasicBlock();
                    nextIsLeader = false;
                }

                if(boost::get<tac::IfFalse>(&statement) || boost::get<tac::Return>(&statement) || boost::get<tac::Goto>(&statement)){
                    nextIsLeader = true;
                }

                function->currentBasicBlock()->add(statement);
            }
        }

        //Then, replace all the the labels by reference to basic blocks
        for(auto& block : function->getBasicBlocks()){
            for(auto& statement : block->statements){
                if(auto* ptr = boost::get<tac::IfFalse>(&statement)){
                   ptr->block = labels[ptr->label];
                } else if(auto* ptr = boost::get<tac::Goto>(&statement)){
                   ptr->block = labels[ptr->label];
                }
            }
        }

        function->getStatements().clear();
    }
}

{'fraction_non_alphanumeric': 0.2, 'fraction_numerical': 0.005298013245033113, 'mean_word_length': 2.4930619796484734, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7636646', 'n_tokens_mistral': 1032, 'n_tokens_neox': 950, 'n_words': 193}