Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDeclaration of CtLocalVariable does not find declarations in for loops #868

Closed
msteinbeck opened this issue Sep 29, 2016 · 1 comment
Closed

Comments

@msteinbeck
Copy link
Contributor

I have the following (stripped version) code:

package net.sf.jabref.collab;

import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import net.sf.jabref.*;
import net.sf.jabref.groups.*;
import net.sf.jabref.imports.*;


public class ChangeScanner extends Thread {


    /**
     * Finds the entry in neu best fitting the specified entry in old. If no entries get a score
     * above zero, an entry is still returned.
     * @param old EntrySorter
     * @param neu EntrySorter
     * @param index int
     * @return BibtexEntry
     */
    private BibtexEntry bestFit(EntrySorter old, EntrySorter neu, int index) {
        double comp = -1;
        int found = 0;
        loop: for (int i=0; i<neu.getEntryCount(); i++) {
            double res = Util.compareEntriesStrictly(old.getEntryAt(index),
            neu.getEntryAt(i));
            if (res > comp) {
                comp = res;
                found = i;
            }
            if (comp > 1)
                break loop;
        }
        return neu.getEntryAt(found);
    }

}

As you can see, i is a for loop variable that is referenced multiple times, for example by neu.getEntryAt(i). I'm using a CtScanner to find declarations that are not referenced, also known as unused code. Nevertheless, if my visitor visits any variable reference referencing i, getDeclaration() is not able to i's declaration. The reason for this error is obvious, CtLocalVariableReferenceImpl traverses all parent blocks one after another and checks whether a statement of the current examined block is a CtLocalVariable and, if true, whether the variable is the one we are looking for. In case of a CtForLoop (let's call it loop), this code is missing loop's declarations (i in our example). One solution to fix this error is to check whether a statement is a for loop (or any other statements allowing to declare variables in a new scope-lambdas maybe). However, you may have a better idea that is less brittle.

@monperrus
Copy link
Collaborator

One solution to fix this error is to check whether a statement is a for loop (or any other statements allowing to declare variables in a new scope-lambdas maybe).

Seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants