mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-06 01:06:01 +02:00
fix bug 74069: [Parser][IProblem] if ( ) if ( ) else if ( ) causes unnecessary warning marker
This commit is contained in:
parent
bd562b731b
commit
8e22cf635e
2 changed files with 68 additions and 42 deletions
|
@ -2300,5 +2300,29 @@ public class CompleteParseASTTest extends CompleteParseBaseTest
|
|||
assertEquals( IProblem.SYNTAX_ERROR, problem.getID() );
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5883,48 +5883,50 @@ public class Parser implements IParserData, IParser
|
|||
cleanupLastToken();
|
||||
return;
|
||||
case IToken.t_if :
|
||||
consume(IToken.t_if);
|
||||
consume(IToken.tLPAREN);
|
||||
IToken start = LA(1);
|
||||
boolean passedCondition = true;
|
||||
try {
|
||||
condition(scope);
|
||||
consume(IToken.tRPAREN);
|
||||
} catch (BacktrackException b) {
|
||||
//if the problem has no offset info, make a new one that does
|
||||
if( b.getProblem() != null && b.getProblem().getSourceLineNumber() == -1 ){
|
||||
IProblem p = b.getProblem();
|
||||
IProblem p2 = problemFactory.createProblem( p.getID(), start.getOffset(),
|
||||
lastToken != null ? lastToken.getEndOffset() : start.getEndOffset(),
|
||||
start.getLineNumber(), p.getOriginatingFileName(),
|
||||
p.getArguments() != null ? p.getArguments().toCharArray() : null,
|
||||
p.isWarning(), p.isError() );
|
||||
b.initialize( p2 );
|
||||
}
|
||||
failParse(b);
|
||||
failParseWithErrorHandling();
|
||||
passedCondition = false;
|
||||
}
|
||||
|
||||
if( passedCondition ){
|
||||
if (LT(1) != IToken.tLBRACE)
|
||||
singleStatementScope(scope);
|
||||
else
|
||||
statement(scope);
|
||||
}
|
||||
|
||||
if (LT(1) == IToken.t_else) {
|
||||
consume(IToken.t_else);
|
||||
if (LT(1) == IToken.t_if) {
|
||||
//an else if, return and get the rest of the else if as
|
||||
// the next statement instead of recursing
|
||||
cleanupLastToken();
|
||||
return;
|
||||
} else if (LT(1) != IToken.tLBRACE)
|
||||
singleStatementScope(scope);
|
||||
else
|
||||
statement(scope);
|
||||
}
|
||||
if_loop: while( true ){
|
||||
consume(IToken.t_if);
|
||||
consume(IToken.tLPAREN);
|
||||
IToken start = LA(1);
|
||||
boolean passedCondition = true;
|
||||
try {
|
||||
condition(scope);
|
||||
consume(IToken.tRPAREN);
|
||||
} catch (BacktrackException b) {
|
||||
//if the problem has no offset info, make a new one that does
|
||||
if( b.getProblem() != null && b.getProblem().getSourceLineNumber() == -1 ){
|
||||
IProblem p = b.getProblem();
|
||||
IProblem p2 = problemFactory.createProblem( p.getID(), start.getOffset(),
|
||||
lastToken != null ? lastToken.getEndOffset() : start.getEndOffset(),
|
||||
start.getLineNumber(), p.getOriginatingFileName(),
|
||||
p.getArguments() != null ? p.getArguments().toCharArray() : null,
|
||||
p.isWarning(), p.isError() );
|
||||
b.initialize( p2 );
|
||||
}
|
||||
failParse(b);
|
||||
failParseWithErrorHandling();
|
||||
passedCondition = false;
|
||||
}
|
||||
|
||||
if( passedCondition ){
|
||||
if (LT(1) != IToken.tLBRACE)
|
||||
singleStatementScope(scope);
|
||||
else
|
||||
statement(scope);
|
||||
}
|
||||
|
||||
if (LT(1) == IToken.t_else) {
|
||||
consume(IToken.t_else);
|
||||
if (LT(1) == IToken.t_if) {
|
||||
//an else if, don't recurse, just loop and do another if
|
||||
cleanupLastToken();
|
||||
continue if_loop;
|
||||
} else if (LT(1) != IToken.tLBRACE)
|
||||
singleStatementScope(scope);
|
||||
else
|
||||
statement(scope);
|
||||
}
|
||||
break if_loop;
|
||||
}
|
||||
cleanupLastToken();
|
||||
return;
|
||||
case IToken.t_switch :
|
||||
|
|
Loading…
Add table
Reference in a new issue