
package com.togethersoft.modules.qa.audit.ExmAu1;

import com.togethersoft.openapi.sci.*;

import com.togethersoft.modules.qa.api.*;
import com.togethersoft.modules.qa.utils.*;

public class ExmAu1 extends AuditPluginImpl {

    // loop nesting level
    private int loopNestingLvl = 0;

    // plugin entry point
    public boolean run(SciObject obj) {

        // make AuditPluginImpl process only operations, not attributes
        // it isn't neccesarry but speeds up operation
        procGoal = PROC_OPERS;

        loopNestingLvl = 0;

        // let AuditPluginImpl process object
        process(obj);

        return true;
    }

    // Plugin title - would be displayed in the plugins list
    public String getTitle() {
        return "Audit example: avoid declaring variables inside loops";
    }

    // group name - used for grouping plugins
    public String getGroupName () {
        return "Samples";
    }

	protected void processLoopStmt (SciLoopStatement ls)  {
        loopNestingLvl++;
        // avoid processing all but code block
        processCodeBlock(ls.getCodeBlock());
        loopNestingLvl--;
    }

    protected void processDeclStmt (SciDeclarationStatement ds)  {
        if (loopNestingLvl > 0) {
            // currMbr is a protected variable of class SciMember in PluginImpl class
            // it contains reference to the operation or attribute
            // that is being processed now
            AuditViolations.add(this, currMbr, ds, "Declaration of variable within loop");
        }
    }
}
