<commit_before>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003-2005 University of Maryland
 * 
 * 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 edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.*;

import org.apache.bcel.classfile.Method;

public class FindNonShortCircuit extends BytecodeScanningDetector implements
		StatelessDetector {

	int stage1 = 0;
	int stage2 = 0;
	int distance = 0;
	int operator;

	boolean sawDanger = false;
	boolean sawDangerOld = false;

	private BugReporter bugReporter;

	public FindNonShortCircuit(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	OpcodeStack stack = new OpcodeStack();

	@Override
	public void visit(Method obj) {
		stack.resetForMethodEntry(this);
		stage1 = 0;
		stage2 = 0;
		distance = 1000000;
		sawDanger = sawDangerOld = true;
	}

	@Override
	public void sawOpcode(int seen) {
		stack.mergeJumps(this);
		// System.out.println(getPC() + " " + OPCODE_NAMES[seen] + " " + stage1 + " " + stage2);
		// System.out.println(stack);
		distance++;
		scanForBooleanValue(seen);
		scanForDanger(seen);
		scanForShortCircuit(seen);
		stack.sawOpcode(this, seen);
	}

	private void scanForDanger(int seen) {
		switch (seen) {
		case INVOKEINTERFACE:
		case INVOKESPECIAL:
		case INVOKEVIRTUAL:
		case INVOKESTATIC:
		case ARRAYLENGTH:
		case AALOAD:
		case BALOAD:
		case SALOAD:
		case CALOAD:
		case IALOAD:
		case LALOAD:
		case FALOAD:
		case DALOAD:
		case IDIV:
		case IREM:
		case GETFIELD:
			sawDanger = true;
			break;
		default:
			break;
		}

	}

	private void scanForShortCircuit(int seen) {
		switch (seen) {
		case IAND:
		case IOR:
			// System.out.println("Saw IOR or IAND at distance " + distance);
			OpcodeStack.Item item0 = stack.getStackItem(0);
			OpcodeStack.Item item1 = stack.getStackItem(1);
			System.out.println(item0);
			System.out.println(item1);
			if (item0.getConstant() == null && item1.getConstant() == null && distance < 4) {
				operator = seen;
				stage2 = 1;
			} else
				stage2 = 0;
			break;
		case IFEQ:
		case IFNE:
			if (stage2 == 1) {
				// System.out.println("Found nsc");
				reportBug();
			}
			stage2 = 0;
			break;
		case PUTFIELD:
		case PUTSTATIC:
		case IRETURN:
			if (operator == IAND && stage2 == 1) {
				reportBug();
			}
			stage2 = 0;
			break;
		default:
			stage2 = 0;
			break;
		}
	}

	private void reportBug() {<commit_msg>Oops. get rid of debugging output<commit_after>/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2003-2005 University of Maryland
 * 
 * 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 edu.umd.cs.findbugs.detect;

import edu.umd.cs.findbugs.*;

import org.apache.bcel.classfile.Method;

public class FindNonShortCircuit extends BytecodeScanningDetector implements
		StatelessDetector {

	int stage1 = 0;
	int stage2 = 0;
	int distance = 0;
	int operator;

	boolean sawDanger = false;
	boolean sawDangerOld = false;

	private BugReporter bugReporter;

	public FindNonShortCircuit(BugReporter bugReporter) {
		this.bugReporter = bugReporter;
	}

	OpcodeStack stack = new OpcodeStack();

	@Override
	public void visit(Method obj) {
		stack.resetForMethodEntry(this);
		stage1 = 0;
		stage2 = 0;
		distance = 1000000;
		sawDanger = sawDangerOld = true;
	}

	@Override
	public void sawOpcode(int seen) {
		stack.mergeJumps(this);
		// System.out.println(getPC() + " " + OPCODE_NAMES[seen] + " " + stage1 + " " + stage2);
		// System.out.println(stack);
		distance++;
		scanForBooleanValue(seen);
		scanForDanger(seen);
		scanForShortCircuit(seen);
		stack.sawOpcode(this, seen);
	}

	private void scanForDanger(int seen) {
		switch (seen) {
		case INVOKEINTERFACE:
		case INVOKESPECIAL:
		case INVOKEVIRTUAL:
		case INVOKESTATIC:
		case ARRAYLENGTH:
		case AALOAD:
		case BALOAD:
		case SALOAD:
		case CALOAD:
		case IALOAD:
		case LALOAD:
		case FALOAD:
		case DALOAD:
		case IDIV:
		case IREM:
		case GETFIELD:
			sawDanger = true;
			break;
		default:
			break;
		}

	}

	private void scanForShortCircuit(int seen) {
		switch (seen) {
		case IAND:
		case IOR:
			// System.out.println("Saw IOR or IAND at distance " + distance);
			OpcodeStack.Item item0 = stack.getStackItem(0);
			OpcodeStack.Item item1 = stack.getStackItem(1);
			if (item0.getConstant() == null && item1.getConstant() == null && distance < 4) {
				operator = seen;
				stage2 = 1;
			} else
				stage2 = 0;
			break;
		case IFEQ:
		case IFNE:
			if (stage2 == 1) {
				// System.out.println("Found nsc");
				reportBug();
			}
			stage2 = 0;
			break;
		case PUTFIELD:
		case PUTSTATIC:
		case IRETURN:
			if (operator == IAND && stage2 == 1) {
				reportBug();
			}
			stage2 = 0;
			break;
		default:
			stage2 = 0;
			break;
		}
	}

	private void reportBug() {
{'fraction_non_alphanumeric': 0.09905352480417755, 'fraction_numerical': 0.020887728459530026, 'mean_word_length': 3.4252707581227435, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5218309', 'n_tokens_mistral': 2391, 'n_tokens_neox': 2053, 'n_words': 645}