Skip to content

Commit

Permalink
Pull #5364: changed RequireThis kept track of the frame being examined
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed Dec 25, 2017
1 parent 0ab421d commit 36fdb1b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
Expand Up @@ -146,12 +146,12 @@ public class RequireThisCheck extends AbstractCheck {
TokenTypes.BXOR_ASSIGN, TokenTypes.BXOR_ASSIGN,
}).collect(Collectors.toSet())); }).collect(Collectors.toSet()));


/** Frame for the currently processed AST. */
private final Deque<AbstractFrame> current = new ArrayDeque<>();

/** Tree of all the parsed frames. */ /** Tree of all the parsed frames. */
private Map<DetailAST, AbstractFrame> frames; private Map<DetailAST, AbstractFrame> frames;


/** Frame for the currently processed AST. */
private AbstractFrame current;

/** Whether we should check fields usage. */ /** Whether we should check fields usage. */
private boolean checkFields = true; private boolean checkFields = true;
/** Whether we should check methods usage. */ /** Whether we should check methods usage. */
Expand Down Expand Up @@ -210,7 +210,7 @@ public int[] getAcceptableTokens() {
@Override @Override
public void beginTree(DetailAST rootAST) { public void beginTree(DetailAST rootAST) {
frames = new HashMap<>(); frames = new HashMap<>();
current = null; current.clear();


final Deque<AbstractFrame> frameStack = new LinkedList<>(); final Deque<AbstractFrame> frameStack = new LinkedList<>();
DetailAST curNode = rootAST; DetailAST curNode = rootAST;
Expand Down Expand Up @@ -241,7 +241,24 @@ public void visitToken(DetailAST ast) {
case TokenTypes.SLIST : case TokenTypes.SLIST :
case TokenTypes.METHOD_DEF : case TokenTypes.METHOD_DEF :
case TokenTypes.CTOR_DEF : case TokenTypes.CTOR_DEF :
current = frames.get(ast); current.push(frames.get(ast));
break;
default :
// do nothing
}
}

@Override
public void leaveToken(DetailAST ast) {
switch (ast.getType()) {
case TokenTypes.CLASS_DEF :
case TokenTypes.INTERFACE_DEF :
case TokenTypes.ENUM_DEF :
case TokenTypes.ANNOTATION_DEF :
case TokenTypes.SLIST :
case TokenTypes.METHOD_DEF :
case TokenTypes.CTOR_DEF :
current.pop();
break; break;
default : default :
// do nothing // do nothing
Expand Down Expand Up @@ -843,7 +860,7 @@ private AbstractFrame getMethodWithoutThis(DetailAST ast) {
* @return AbstractFrame containing declaration or null. * @return AbstractFrame containing declaration or null.
*/ */
private AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod) { private AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod) {
AbstractFrame frame = current; AbstractFrame frame = current.peek();


while (true) { while (true) {
frame = findFrame(frame, name, lookForMethod); frame = findFrame(frame, name, lookForMethod);
Expand All @@ -865,7 +882,7 @@ private AbstractFrame findClassFrame(DetailAST name, boolean lookForMethod) {
* @return AbstractFrame containing declaration or null. * @return AbstractFrame containing declaration or null.
*/ */
private AbstractFrame findFrame(DetailAST name, boolean lookForMethod) { private AbstractFrame findFrame(DetailAST name, boolean lookForMethod) {
return findFrame(current, name, lookForMethod); return findFrame(current.peek(), name, lookForMethod);
} }


/** /**
Expand Down Expand Up @@ -912,7 +929,7 @@ private static boolean isCompoundAssignToken(int tokenType) {
* @return the name of the nearest parent ClassFrame. * @return the name of the nearest parent ClassFrame.
*/ */
private String getNearestClassFrameName() { private String getNearestClassFrameName() {
AbstractFrame frame = current; AbstractFrame frame = current.peek();
while (frame.getType() != FrameType.CLASS_FRAME) { while (frame.getType() != FrameType.CLASS_FRAME) {
frame = frame.getParent(); frame = frame.getParent();
} }
Expand Down
Expand Up @@ -61,6 +61,8 @@ public void testIt() throws Exception {
"122:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue2240."), "122:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue2240."),
"134:9: " + getCheckMessage(MSG_METHOD, "foo", ""), "134:9: " + getCheckMessage(MSG_METHOD, "foo", ""),
"142:9: " + getCheckMessage(MSG_VARIABLE, "s", ""), "142:9: " + getCheckMessage(MSG_VARIABLE, "s", ""),
"167:16: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"167:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
}; };
verify(checkConfig, verify(checkConfig,
getPath("InputRequireThisEnumInnerClassesAndBugs.java"), getPath("InputRequireThisEnumInnerClassesAndBugs.java"),
Expand Down Expand Up @@ -99,6 +101,8 @@ public void testFieldsOnly() throws Exception {
"114:9: " + getCheckMessage(MSG_VARIABLE, "i", ""), "114:9: " + getCheckMessage(MSG_VARIABLE, "i", ""),
"122:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue2240."), "122:13: " + getCheckMessage(MSG_VARIABLE, "i", "Issue2240."),
"142:9: " + getCheckMessage(MSG_VARIABLE, "s", ""), "142:9: " + getCheckMessage(MSG_VARIABLE, "s", ""),
"167:16: " + getCheckMessage(MSG_VARIABLE, "a", ""),
"167:20: " + getCheckMessage(MSG_VARIABLE, "a", ""),
}; };
verify(checkConfig, verify(checkConfig,
getPath("InputRequireThisEnumInnerClassesAndBugs.java"), getPath("InputRequireThisEnumInnerClassesAndBugs.java"),
Expand Down
Expand Up @@ -150,3 +150,20 @@ public void method() {
} }
} }
} }
class NestedFrames {
int a = 0;

public int oneReturnInMethod2() {
for (int i = 0; i < 10; i++) {
int a = 1;
if (a != 2 && true) {
if (true | false) {
if (a - a != 0) {
a += 1;
}
}
}
}
return a + a * a;
}
}

0 comments on commit 36fdb1b

Please sign in to comment.