1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 09:46:02 +02:00

fix bug 74069: [Parser][IProblem] if (…) if (…) else if (…) causes unnecessary warning marker

This commit is contained in:
Andrew Niefer 2004-10-21 16:15:45 +00:00
parent bd562b731b
commit 8e22cf635e
2 changed files with 68 additions and 42 deletions

View file

@ -2300,5 +2300,29 @@ public class CompleteParseASTTest extends CompleteParseBaseTest
assertEquals( IProblem.SYNTAX_ERROR, problem.getID() ); assertEquals( IProblem.SYNTAX_ERROR, problem.getID() );
assertFalse( i.hasNext() ); assertFalse( i.hasNext() );
} }
public void testBug74069() throws Exception{
Writer writer = new StringWriter();
writer.write( "int f() { \n"); //$NON-NLS-1$
writer.write( " int a, b, c; \n"); //$NON-NLS-1$
writer.write( " if( a < b ) \n"); //$NON-NLS-1$
writer.write( " if( b < c ) \n"); //$NON-NLS-1$
writer.write( " return b; \n"); //$NON-NLS-1$
writer.write( " else if ( a < c ) \n"); //$NON-NLS-1$
writer.write( " return c; \n"); //$NON-NLS-1$
writer.write( " else \n"); //$NON-NLS-1$
writer.write( " return a; \n"); //$NON-NLS-1$
writer.write( " else if( a < c ) \n"); //$NON-NLS-1$
writer.write( " return a; \n"); //$NON-NLS-1$
writer.write( " else if( b < c ) \n"); //$NON-NLS-1$
writer.write( " return c; \n"); //$NON-NLS-1$
writer.write( " else \n"); //$NON-NLS-1$
writer.write( " return b; \n"); //$NON-NLS-1$
writer.write( "} \n"); //$NON-NLS-1$
parse( writer.toString() );
Iterator i = callback.getProblems();
assertFalse( i.hasNext() );
}
} }

View file

@ -5883,48 +5883,50 @@ public class Parser implements IParserData, IParser
cleanupLastToken(); cleanupLastToken();
return; return;
case IToken.t_if : case IToken.t_if :
consume(IToken.t_if); if_loop: while( true ){
consume(IToken.tLPAREN); consume(IToken.t_if);
IToken start = LA(1); consume(IToken.tLPAREN);
boolean passedCondition = true; IToken start = LA(1);
try { boolean passedCondition = true;
condition(scope); try {
consume(IToken.tRPAREN); condition(scope);
} catch (BacktrackException b) { consume(IToken.tRPAREN);
//if the problem has no offset info, make a new one that does } catch (BacktrackException b) {
if( b.getProblem() != null && b.getProblem().getSourceLineNumber() == -1 ){ //if the problem has no offset info, make a new one that does
IProblem p = b.getProblem(); if( b.getProblem() != null && b.getProblem().getSourceLineNumber() == -1 ){
IProblem p2 = problemFactory.createProblem( p.getID(), start.getOffset(), IProblem p = b.getProblem();
lastToken != null ? lastToken.getEndOffset() : start.getEndOffset(), IProblem p2 = problemFactory.createProblem( p.getID(), start.getOffset(),
start.getLineNumber(), p.getOriginatingFileName(), lastToken != null ? lastToken.getEndOffset() : start.getEndOffset(),
p.getArguments() != null ? p.getArguments().toCharArray() : null, start.getLineNumber(), p.getOriginatingFileName(),
p.isWarning(), p.isError() ); p.getArguments() != null ? p.getArguments().toCharArray() : null,
b.initialize( p2 ); p.isWarning(), p.isError() );
} b.initialize( p2 );
failParse(b); }
failParseWithErrorHandling(); failParse(b);
passedCondition = false; failParseWithErrorHandling();
} passedCondition = false;
}
if( passedCondition ){
if (LT(1) != IToken.tLBRACE) if( passedCondition ){
singleStatementScope(scope); if (LT(1) != IToken.tLBRACE)
else singleStatementScope(scope);
statement(scope); else
} statement(scope);
}
if (LT(1) == IToken.t_else) {
consume(IToken.t_else); if (LT(1) == IToken.t_else) {
if (LT(1) == IToken.t_if) { consume(IToken.t_else);
//an else if, return and get the rest of the else if as if (LT(1) == IToken.t_if) {
// the next statement instead of recursing //an else if, don't recurse, just loop and do another if
cleanupLastToken(); cleanupLastToken();
return; continue if_loop;
} else if (LT(1) != IToken.tLBRACE) } else if (LT(1) != IToken.tLBRACE)
singleStatementScope(scope); singleStatementScope(scope);
else else
statement(scope); statement(scope);
} }
break if_loop;
}
cleanupLastToken(); cleanupLastToken();
return; return;
case IToken.t_switch : case IToken.t_switch :