Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVivian Kong2012-05-02 11:53:17 -0400
committerVivian Kong2012-05-02 11:53:17 -0400
commit6cf930a564e90af43f8c5aadaacf986883c0d1c7 (patch)
tree374d2734f5fd42638e7622ab889b4013037acdbb
parenta2174db422c6bc30e06a63f162df30ca214951a2 (diff)
downloadorg.eclipse.cdt-6cf930a564e90af43f8c5aadaacf986883c0d1c7.tar.gz
org.eclipse.cdt-6cf930a564e90af43f8c5aadaacf986883c0d1c7.tar.xz
org.eclipse.cdt-6cf930a564e90af43f8c5aadaacf986883c0d1c7.zip
Bug 378189 - LR parser mistaken for-loop initializer as a declaration
-rw-r--r--lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/c99/C99BuildASTParserAction.java43
-rw-r--r--lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/cpp/CPPBuildASTParserAction.java42
2 files changed, 81 insertions, 4 deletions
diff --git a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/c99/C99BuildASTParserAction.java b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/c99/C99BuildASTParserAction.java
index 698b16a457..5f588b5f6d 100644
--- a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/c99/C99BuildASTParserAction.java
+++ b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/c99/C99BuildASTParserAction.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2009 IBM Corporation and others.
+ * Copyright (c) 2006, 2012 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
@@ -23,9 +23,11 @@ import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
@@ -58,6 +60,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICNodeFactory;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
+import org.eclipse.cdt.core.dom.lrparser.ISecondaryParser;
import org.eclipse.cdt.core.dom.lrparser.action.BuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.ISecondaryParserFactory;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
@@ -86,6 +89,8 @@ public class C99BuildASTParserAction extends BuildASTParserAction {
/** Used to create the AST node objects */
protected final ICNodeFactory nodeFactory;
+ private final ISecondaryParserFactory parserFactory;
+
/**
* @param parser
* @param orderedTerminalSymbols When an instance of this class is created for a parser
@@ -95,6 +100,7 @@ public class C99BuildASTParserAction extends BuildASTParserAction {
super(parser, astStack, nodeFactory, parserFactory);
this.nodeFactory = nodeFactory;
+ this.parserFactory = parserFactory;
this.tokenMap = new TokenMap(C99Parsersym.orderedTerminalSymbols, parser.getOrderedTerminalSymbols());
}
@@ -585,6 +591,41 @@ public class C99BuildASTParserAction extends BuildASTParserAction {
ParserUtil.setOffsetAndLength(initializer, offset(name), length(name));
}
+ //initializer could be an expression or a declaration
+ int TK_SC = TK_SemiColon;
+ IASTExpressionStatement expressionStatement = null;
+ if(initializer instanceof IASTDeclarationStatement) {
+ IASTDeclarationStatement declarationStatement = (IASTDeclarationStatement) initializer;
+ List<IToken> expressionTokens = stream.getRuleTokens();
+
+ //find the first semicolon
+ int end_pos = -1;
+ for(int i = 0, n = expressionTokens.size(); i < n; i++) {
+ if(tokenMap.mapKind(expressionTokens.get(i).getKind()) == TK_SC) {
+ end_pos = i;
+ break;
+ }
+ }
+
+ if (end_pos != -1) {
+ expressionTokens = expressionTokens.subList(2, end_pos);
+
+ ISecondaryParser<IASTExpression> expressionParser = parserFactory.getExpressionParser(stream, properties);
+ IASTExpression expr1 = runSecondaryParser(expressionParser, expressionTokens);
+
+ if(expr1 != null) { // the parse may fail
+ expressionStatement = nodeFactory.newExpressionStatement(expr1);
+ setOffsetAndLength(expressionStatement);
+ }
+ }
+
+ if(expressionStatement == null)
+ initializer = declarationStatement;
+ else {
+ initializer = createAmbiguousStatement(expressionStatement, declarationStatement);
+ setOffsetAndLength(initializer);
+ }
+ }
if(node != null)
ParserUtil.setOffsetAndLength(initializer, offset(node), length(node));
diff --git a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/cpp/CPPBuildASTParserAction.java b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/cpp/CPPBuildASTParserAction.java
index 3a4c71e29f..35f58f474b 100644
--- a/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/cpp/CPPBuildASTParserAction.java
+++ b/lrparser/org.eclipse.cdt.core.lrparser/src/org/eclipse/cdt/core/dom/lrparser/action/cpp/CPPBuildASTParserAction.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2010 IBM Corporation and others.
+ * Copyright (c) 2006, 2012 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
@@ -27,17 +27,18 @@ import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
+import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
-import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
@@ -56,6 +57,7 @@ import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAmbiguousTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
@@ -92,7 +94,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.lrparser.ISecondaryParser;
import org.eclipse.cdt.core.dom.lrparser.LPGTokenAdapter;
import org.eclipse.cdt.core.dom.lrparser.action.BuildASTParserAction;
@@ -681,6 +682,41 @@ public class CPPBuildASTParserAction extends BuildASTParserAction {
ParserUtil.setOffsetAndLength(initializer, offset(name), length(name));
}
+ int TK_SC = TK_SemiColon;
+ IASTExpressionStatement expressionStatement = null;
+ if(initializer instanceof IASTDeclarationStatement) {
+ IASTDeclarationStatement declarationStatement = (IASTDeclarationStatement) initializer;
+ List<IToken> expressionTokens = stream.getRuleTokens();
+
+ //find the first semicolon
+ int end_pos = -1;
+ for(int i = 0, n = expressionTokens.size(); i < n; i++) {
+ if(tokenMap.mapKind(expressionTokens.get(i).getKind()) == TK_SC) {
+ end_pos = i;
+ break;
+ }
+ }
+
+ if (end_pos != -1) {
+ expressionTokens = expressionTokens.subList(2, end_pos);
+
+ ISecondaryParser<IASTExpression> expressionParser = parserFactory.getExpressionParser(stream, properties);
+ IASTExpression expr1 = runSecondaryParser(expressionParser, expressionTokens);
+
+ if(expr1 != null) { // the parse may fail
+ expressionStatement = nodeFactory.newExpressionStatement(expr1);
+ setOffsetAndLength(expressionStatement);
+ }
+ }
+
+ if(expressionStatement == null)
+ initializer = declarationStatement;
+ else {
+ initializer = createAmbiguousStatement(declarationStatement, expressionStatement);
+ setOffsetAndLength(initializer);
+ }
+ }
+
IASTForStatement forStat;
if(condition instanceof IASTExpression)

Back to the top