1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Bug 440231 - NullPointerException with Synthetic if-elseif-statement

Added null checks to the ASTWriter.doNodesHaveSameOffset and a test case
reproducing the problem.

Change-Id: I538806cf32683f5689dbf582b7a7dcb6615a899e
Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
Reviewed-on: https://git.eclipse.org/r/30334
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Thomas Corbat 2014-07-23 16:44:08 +02:00
parent b740ab50f8
commit 0544d1d000
2 changed files with 77 additions and 1 deletions

View file

@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2014 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Thomas Corbat (IFS) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class NestedElseifStatementTest extends ChangeGeneratorTest {
public NestedElseifStatementTest(){
super("Append Nested Elseif Statement"); //$NON-NLS-1$
}
@Override
protected void setUp() throws Exception {
source = "void foo(bool cond1, bool cond2) {\n}\n"; //$NON-NLS-1$
expectedSource = "void foo(bool cond1, bool cond2) {\n\tif (cond1) {\n\t} else if (cond2) {\n\t}\n}\n"; //$NON-NLS-1$
super.setUp();
}
@Override
protected ASTVisitor createModificator(final ASTModificationStore modStore) {
return new ASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTCompoundStatement) {
IASTCompoundStatement compound = (IASTCompoundStatement) statement;
INodeFactory factory = statement.getTranslationUnit().getASTNodeFactory();
IASTIdExpression elseIfCondition = factory.newIdExpression(factory.newName("cond2".toCharArray()));
IASTStatement elseIfThen = factory.newCompoundStatement();
IASTIfStatement elseIfStatement = factory.newIfStatement(elseIfCondition, elseIfThen, null);
IASTIdExpression ifCondition = factory.newIdExpression(factory.newName("cond1".toCharArray()));
IASTStatement ifThen = factory.newCompoundStatement();
IASTIfStatement ifStatement = factory.newIfStatement(ifCondition, ifThen, elseIfStatement);
ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, compound, ifStatement, null);
modStore.storeModification(null, modification);
}
return PROCESS_ABORT;
}
};
}
public static Test suite() {
return new NestedElseifStatementTest();
}
}

View file

@ -15,6 +15,7 @@ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
@ -197,6 +198,9 @@ public class ASTWriter {
* expansion.
*/
private static boolean doNodesHaveSameOffset(IASTNode node1, IASTNode node2) {
return node1.getFileLocation().getNodeOffset() == node2.getFileLocation().getNodeOffset();
IASTFileLocation fileLocation1 = node1.getFileLocation();
IASTFileLocation fileLocation2 = node2.getFileLocation();
return fileLocation1 != null && fileLocation2 != null &&
fileLocation1.getNodeOffset() == fileLocation2.getNodeOffset();
}
}