<commit_before>/* Soot - a J*va Optimization Framework
 * Copyright (C) 2007 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package soot.jimple.toolkits.annotation.logic;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import soot.Unit;
import soot.UnitBox;
import soot.jimple.Stmt;
import soot.toolkits.graph.UnitGraph;

/**
 * A (natural) loop in Jimple. A back-edge (t,h) is a control-flog edge for which
 * h dominates t. In this case h is the header and the loop consists of all statements
 * s which reach t without passing through h. 
 *
 * @author <NAME>
 */
public class Loop {
    
    protected final Stmt header;
    protected final Stmt backJump;
    protected final List<Stmt> loopStatements;
    protected final UnitGraph g;
    protected Collection<Stmt> loopExists;

    /**
     * Creates a new loop. Expects that the last statement in the list is the loop head
     * and the second-last statement is the back-jump to the head. {@link LoopFinder} will
     * normally guarantee this.
     * @param header the loop header
     * @param loopStatements an ordered list of loop statements, ending with the header
     * @param g the unit graph according to which the loop exists
     */
    Loop(Stmt header, List<Stmt> loopStatements, UnitGraph g) {
        this.header = header;
        this.g = g;

        //put header to the top
        loopStatements.remove(header);
        loopStatements.add(0, header);
        
        //last statement
        this.backJump = loopStatements.get(loopStatements.size()-1);
        assert this.backJump.branches(); //must be a goto or if statement
        
        boolean branchesToHead = false;
        for (UnitBox box : this.backJump.getUnitBoxes()) {
            if(box.getUnit()==header) {
                branchesToHead = true;
                break;
            }
        }
        assert branchesToHead; //must branch back to the head

        this.loopStatements = loopStatements;
    }

    /**
     * @return the loop head
     */
    public Stmt getHead() {
        return header;
    }

    /**
     * Returns the statement that jumps back to the head, thereby constituing the loop.
     */<commit_msg>more fixes on loops<commit_after>/* Soot - a J*va Optimization Framework
 * Copyright (C) 2007 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package soot.jimple.toolkits.annotation.logic;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import soot.Unit;
import soot.jimple.Stmt;
import soot.toolkits.graph.UnitGraph;

/**
 * A (natural) loop in Jimple. A back-edge (t,h) is a control-flog edge for which
 * h dominates t. In this case h is the header and the loop consists of all statements
 * s which reach t without passing through h. 
 *
 * @author <NAME>
 */
public class Loop {
    
    protected final Stmt header;
    protected final Stmt backJump;
    protected final List<Stmt> loopStatements;
    protected final UnitGraph g;
    protected Collection<Stmt> loopExists;

    /**
     * Creates a new loop. Expects that the last statement in the list is the loop head
     * and the second-last statement is the back-jump to the head. {@link LoopFinder} will
     * normally guarantee this.
     * @param head the loop header
     * @param loopStatements an ordered list of loop statements, ending with the header
     * @param g the unit graph according to which the loop exists
     */
    Loop(Stmt head, List<Stmt> loopStatements, UnitGraph g) {
        this.header = head;
        this.g = g;

        //put header to the top
        loopStatements.remove(head);
        loopStatements.add(0, head);
        
        //last statement
        this.backJump = loopStatements.get(loopStatements.size()-1);
        
        assert g.getSuccsOf(this.backJump).contains(head); //must branch back to the head

        this.loopStatements = loopStatements;
    }

    /**
     * @return the loop head
     */
    public Stmt getHead() {
        return header;
    }

    /**
     * Returns the statement that jumps back to the head, thereby constituing the loop.
     */
{'fraction_non_alphanumeric': 0.07727108215707565, 'fraction_numerical': 0.007962359753890699, 'mean_word_length': 2.775273224043716, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3744031', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1493, 'n_words': 659}