From fe49cf75df1f923a64b05cc111fd23470258ec25 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 22 Feb 2007 12:52:47 +0000 Subject: [PATCH] Fix for 171520, node length of type-id expression --- .../parser/tests/ast2/AST2TemplateTests.java | 9 +++++- .../tests/ast2/CompleteParser2Tests.java | 12 ++++++-- .../parser/tests/ast2/DOMLocationTests.java | 10 +++++-- .../parser/tests/ast2/DOMParserTestSuite.java | 8 +++--- .../parser/AbstractGNUSourceCodeParser.java | 28 +++++++++++++------ 5 files changed, 48 insertions(+), 19 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java index da509067150..a9e67bc2dc8 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2TemplateTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005 IBM Corporation and others. + * Copyright (c) 2005, 2007 IBM Corporation 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 @@ -7,12 +7,15 @@ * * Contributors: * IBM - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ /* * Created on Mar 11, 2005 */ package org.eclipse.cdt.core.parser.tests.ast2; +import junit.framework.TestSuite; + import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; @@ -68,6 +71,10 @@ public class AST2TemplateTests extends AST2BaseTest { super(name); } + public static TestSuite suite() { + return suite(AST2TemplateTests.class); + } + public void testBasicClassTemplate() throws Exception { IASTTranslationUnit tu = parse( "template class A{ T t; };", ParserLanguage.CPP ); //$NON-NLS-1$ CPPNameCollector col = new CPPNameCollector(); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java index b994fcb6956..313c1cd83fa 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2005 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation 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 @@ -7,6 +7,7 @@ * * Contributors: * IBM - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; @@ -15,7 +16,7 @@ import java.io.Writer; import java.util.ArrayList; import java.util.List; -import junit.framework.TestCase; +import junit.framework.TestSuite; import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; @@ -68,6 +69,7 @@ import org.eclipse.cdt.core.parser.NullLogService; import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.core.parser.ParserMode; import org.eclipse.cdt.core.parser.ScannerInfo; +import org.eclipse.cdt.core.testplugin.util.BaseTestCase; import org.eclipse.cdt.internal.core.dom.parser.ISourceCodeParser; import org.eclipse.cdt.internal.core.dom.parser.c.ANSICParserExtensionConfiguration; import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor; @@ -90,7 +92,7 @@ import org.eclipse.core.runtime.Platform; /** * @author jcamelon */ -public class CompleteParser2Tests extends TestCase { +public class CompleteParser2Tests extends BaseTestCase { private static final NullLogService NULL_LOG = new NullLogService(); @@ -100,6 +102,10 @@ public class CompleteParser2Tests extends TestCase { super(name); } + public static TestSuite suite() { + return suite(CompleteParser2Tests.class); + } + static private class CPPNameCollector extends CPPASTVisitor { { shouldVisitNames = true; diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java index 5604285e85a..3a68cf4845e 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation 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 @@ -11,6 +11,8 @@ *******************************************************************************/ package org.eclipse.cdt.core.parser.tests.ast2; +import junit.framework.TestSuite; + import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.IASTCastExpression; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; @@ -75,6 +77,10 @@ public class DOMLocationTests extends AST2BaseTest { setName(name); } + public static TestSuite suite() { + return suite(DOMLocationTests.class); + } + public void testBaseCase() throws ParserException { for (ParserLanguage p = ParserLanguage.C; p != null; p = (p == ParserLanguage.C) ? ParserLanguage.CPP : null) { @@ -671,7 +677,7 @@ public class DOMLocationTests extends AST2BaseTest { assertSoleLocation(problems[0], buffer.indexOf("X"), "X".length() ); } - public void _testBug171520() throws Exception { + public void testBug171520() throws Exception { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=171520 StringBuffer buffer = new StringBuffer(); buffer.append("int i = sizeof(int);"); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java index 782e8084ee7..0c09a497294 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2006 IBM Corporation and others. + * Copyright (c) 2004, 2007 IBM Corporation 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 @@ -28,10 +28,10 @@ public class DOMParserTestSuite extends TestCase { suite.addTest( AST2Tests.suite() ); suite.addTestSuite( GCCTests.class ); suite.addTestSuite( AST2CPPTests.class ); - suite.addTestSuite( AST2TemplateTests.class ); + suite.addTest( AST2TemplateTests.suite() ); suite.addTestSuite( QuickParser2Tests.class ); - suite.addTestSuite( CompleteParser2Tests.class ); - suite.addTestSuite( DOMLocationTests.class ); + suite.addTest( CompleteParser2Tests.suite() ); + suite.addTest( DOMLocationTests.suite() ); suite.addTestSuite( DOMLocationMacroTests.class ); suite.addTest( DOMLocationInclusionTests.suite() ); suite.addTestSuite( AST2KnRTests.class ); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java index 03faef2f645..6ed751465ff 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/AbstractGNUSourceCodeParser.java @@ -2080,6 +2080,11 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { protected IASTNode[] parseTypeIdOrUnaryExpression( boolean typeIdWithParentheses) throws EndOfFileException { + return parseTypeIdOrUnaryExpression(typeIdWithParentheses, new int[1]); + } + + protected IASTNode[] parseTypeIdOrUnaryExpression( + boolean typeIdWithParentheses, int[] endoffset) throws EndOfFileException { IASTTypeId typeId = null; IASTExpression unaryExpression = null; IToken typeIdLA = null, unaryExpressionLA = null; @@ -2094,13 +2099,18 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { switch (LT(1)) { case IToken.tRPAREN: case IToken.tEOC: - consume(); + endoffset[0]= consume().getEndOffset(); break; default: typeId = null; } } - if (typeId != null) typeIdLA = LA(1); + if (typeId != null) { + typeIdLA = LA(1); + if (!typeIdWithParentheses) { + endoffset[0]= calculateEndOffset(typeId); + } + } } } catch (BacktrackException e) { typeId = null; @@ -2123,6 +2133,7 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { backup(unaryExpressionLA); result = new IASTNode[1]; result[0] = unaryExpression; + endoffset[0]= calculateEndOffset(unaryExpression); return result; } if (unaryExpression != null && typeId != null @@ -2141,27 +2152,26 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser { protected IASTExpression parseSizeofExpression() throws BacktrackException, EndOfFileException { int startingOffset = consume().getOffset(); // t_sizeof - IASTNode[] choice = parseTypeIdOrUnaryExpression(true); + int[] endoffset= new int[] {0}; + IASTNode[] choice = parseTypeIdOrUnaryExpression(true, endoffset); switch (choice.length) { case 1: - int lastOffset = calculateEndOffset(choice[0]); if (choice[0] instanceof IASTExpression) return buildUnaryExpression(IASTUnaryExpression.op_sizeof, - (IASTExpression) choice[0], startingOffset, lastOffset); + (IASTExpression) choice[0], startingOffset, endoffset[0]); else if (choice[0] instanceof IASTTypeId) return buildTypeIdExpression(IASTTypeIdExpression.op_sizeof, - (IASTTypeId) choice[0], startingOffset, lastOffset); + (IASTTypeId) choice[0], startingOffset, endoffset[0]); throwBacktrack(LA(1)); break; case 2: - lastOffset = calculateEndOffset(choice[0]); IASTAmbiguousExpression ambExpr = createAmbiguousExpression(); IASTExpression e1 = buildTypeIdExpression( IASTTypeIdExpression.op_sizeof, (IASTTypeId) choice[0], - startingOffset, lastOffset); + startingOffset, endoffset[0]); IASTExpression e2 = buildUnaryExpression( IASTUnaryExpression.op_sizeof, (IASTExpression) choice[1], - startingOffset, lastOffset); + startingOffset, endoffset[0]); ambExpr.addExpression(e1); e1.setParent(ambExpr); e1.setPropertyInParent(IASTAmbiguousExpression.SUBEXPRESSION);