From fe5aea4644ddc41bf03200b93786dbbd1d754fc7 Mon Sep 17 00:00:00 2001 From: Nathan Ridge Date: Tue, 29 Dec 2015 20:39:48 -0500 Subject: [PATCH] Bug 484824 - Disallow initializers in type-ids in C code This was already disalowed in C++ code previously. Change-Id: I17f0b19253394896a980e2bbeb610bbbf266d012 Signed-off-by: Nathan Ridge --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 12 ++++++++++++ .../internal/core/dom/parser/c/GNUCSourceParser.java | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 60e5a0bfbee..ef4211a2ee3 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -7569,4 +7569,16 @@ public class AST2Tests extends AST2TestBase { public void testAlignas_451082() throws Exception { parseAndCheckBindings(getAboveComment(), C); } + + // void foo(int waldo) { + // (waldo = 5) && waldo; + // } + public void testTypeIdWithEqualsInitializer_484824() throws Exception { + // Test that 'waldo = 5' is not parsed as a type-id, causing + // the entire expression to be parsed as a cast-expression. + // See also bug 471174, which is about the broader problem of + // binary && expressions with a parenthesized left operand + // being incorrectly parsed as cast-expressions. + parseAndCheckBindings(getAboveComment(), C); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java index 4e794266027..bc5eeef9edc 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/GNUCSourceParser.java @@ -144,7 +144,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser { @Override protected IASTInitializer optionalInitializer(IASTDeclarator dtor, DeclarationOptions options) throws EndOfFileException, BacktrackException { - if (LTcatchEOF(1) == IToken.tASSIGN) { + if (options.fAllowInitializer && LTcatchEOF(1) == IToken.tASSIGN) { final int offset= consume().getOffset(); IASTInitializerClause initClause = initClause(); IASTEqualsInitializer result= getNodeFactory().newEqualsInitializer(initClause);