Skip to main content
summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorHansruedi Patzen2018-05-17 08:15:49 -0400
committerThomas Corbat2018-06-01 10:22:37 -0400
commit278979870c35e7eb8c11a7acefd692b0c5e9079b (patch)
treef4a8403154949a36f8fd6f4668ba618d79b1cf47 /core
parent45279d5ffc88c4c9345c9b5c156f962053ff5189 (diff)
downloadorg.eclipse.cdt-278979870c35e7eb8c11a7acefd692b0c5e9079b.tar.gz
org.eclipse.cdt-278979870c35e7eb8c11a7acefd692b0c5e9079b.tar.xz
org.eclipse.cdt-278979870c35e7eb8c11a7acefd692b0c5e9079b.zip
Bug 534808 - static_assert without message not recognized (C++17)
Fix and test Change-Id: I785ecfd0715f1fa8ff86b87bd01d16ac6d5d5da2 Signed-off-by: Hansruedi Patzen <hansruedi.patzen@hsr.ch> Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java5
-rw-r--r--core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclarationTestSource.awts5
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTStaticAssertDeclaration.java3
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java5
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTStaticAssertionDeclaration.java9
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java5
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java16
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java30
8 files changed, 67 insertions, 11 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
index d8b931c484..0402f2bcab 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
@@ -12705,4 +12705,9 @@ public class AST2CPPTests extends AST2CPPTestBase {
// does not throw an exception.
var.getInitialValue();
}
+
+ //static_assert(true);
+ public void testStaticAssertWithoutMessage_534808() throws Exception {
+ parseAndCheckBindings();
+ }
}
diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclarationTestSource.awts b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclarationTestSource.awts
index 9958fc479c..206c80cece 100644
--- a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclarationTestSource.awts
+++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterDeclarationTestSource.awts
@@ -159,3 +159,8 @@ int main()
}
;
}
+
+//!static_asserts with and without message
+//%CPP
+static_assert(true, "Should always pass");
+static_assert(sizeof (int) == 4);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTStaticAssertDeclaration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTStaticAssertDeclaration.java
index a7e44084b9..678572f992 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTStaticAssertDeclaration.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPASTStaticAssertDeclaration.java
@@ -33,7 +33,8 @@ public interface ICPPASTStaticAssertDeclaration extends IASTDeclaration {
IASTExpression getCondition();
/**
- * Returns the message of the assertion, or potentially <code>null</code> when using content assist.
+ * Returns the message of the assertion. Potentially <code>null</code> when message is omitted
+ * or using content assist.
*/
ICPPASTLiteralExpression getMessage();
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java
index 1abf334965..659b040f4d 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/cpp/ICPPNodeFactory.java
@@ -333,6 +333,11 @@ public interface ICPPNodeFactory extends INodeFactory {
*/
public ICPPASTStaticAssertDeclaration newStaticAssertion(IASTExpression condition, ICPPASTLiteralExpression message);
+ /**
+ * @since 6.5
+ */
+ public ICPPASTStaticAssertDeclaration newStaticAssertion(IASTExpression condition);
+
public ICPPASTSwitchStatement newSwitchStatement();
public ICPPASTSwitchStatement newSwitchStatement(IASTDeclaration controller, IASTStatement body);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTStaticAssertionDeclaration.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTStaticAssertionDeclaration.java
index 030c0030b3..1ffcc0d54e 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTStaticAssertionDeclaration.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPASTStaticAssertionDeclaration.java
@@ -23,6 +23,15 @@ public class CPPASTStaticAssertionDeclaration extends ASTNode implements ICPPAST
private IASTExpression fCondition;
private final ICPPASTLiteralExpression fMessage;
+ /**
+ * Constructor for C++17 static_assert with only a condition.
+ *
+ * @param condition The condition of the static assertion
+ */
+ public CPPASTStaticAssertionDeclaration(IASTExpression condition) {
+ this(condition, null);
+ }
+
public CPPASTStaticAssertionDeclaration(IASTExpression condition, ICPPASTLiteralExpression message) {
fCondition= condition;
fMessage= message;
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java
index e2d0ab790c..a95a3ebb0c 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPNodeFactory.java
@@ -737,6 +737,11 @@ public class CPPNodeFactory extends NodeFactory implements ICPPNodeFactory {
}
@Override
+ public ICPPASTStaticAssertDeclaration newStaticAssertion(IASTExpression condition) {
+ return newStaticAssertion(condition, null);
+ }
+
+ @Override
public ICPPASTSwitchStatement newSwitchStatement() {
return new CPPASTSwitchStatement();
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
index f4564c30b1..b0404798d4 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/GNUCPPSourceParser.java
@@ -2308,21 +2308,25 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
/**
* static_assert-declaration:
- static_assert (constant-expression , string-literal ) ;
+ * static_assert (constant-expression);
+ * OR
+ * static_assert (constant-expression , string-literal);
*/
private ICPPASTStaticAssertDeclaration staticAssertDeclaration() throws EndOfFileException, BacktrackException {
int offset= consume(IToken.t_static_assert).getOffset();
consume(IToken.tLPAREN);
IASTExpression e= constantExpression();
int endOffset= calculateEndOffset(e);
- ICPPASTLiteralExpression lit= null;
- if (LT(1) != IToken.tEOC) {
+ ICPPASTLiteralExpression message = null;
+ if (LT(1) == IToken.tCOMMA) {
consume(IToken.tCOMMA);
- lit= stringLiteral();
+ message = stringLiteral();
+ }
+ ICPPASTStaticAssertDeclaration assertion = getNodeFactory().newStaticAssertion(e, message);
+ if (LT(1) != IToken.tEOC) {
consume(IToken.tRPAREN);
- endOffset= consume(IToken.tSEMI).getEndOffset();
+ endOffset = consume(IToken.tSEMI).getEndOffset();
}
- ICPPASTStaticAssertDeclaration assertion = getNodeFactory().newStaticAssertion(e, lit);
return setRange(assertion, offset, endOffset);
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
index bca52929c1..7d7b611186 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
@@ -34,8 +34,10 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTStaticAssertDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateSpecialization;
@@ -56,10 +58,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
* @author Emanuel Graf IFS
*/
public class DeclarationWriter extends NodeWriter {
- private static final String ASM_END = ")"; //$NON-NLS-1$
- private static final String ASM_START = "asm("; //$NON-NLS-1$
+ private static final char OPEN_PAREN = '(';
+ private static final char CLOSE_PAREN = ')';
+ private static final String ASM_START = "asm" + OPEN_PAREN; //$NON-NLS-1$
private static final String TEMPLATE_DECLARATION = "template<"; //$NON-NLS-1$
- private static final String TEMPLATE_SPECIALIZATION = "template <> "; //$NON-NLS-1$
+ private static final String TEMPLATE_SPECIALIZATION = "template<> "; //$NON-NLS-1$
+ private static final char COMMA = ',';
private boolean printSemicolon;
public DeclarationWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
@@ -103,6 +107,8 @@ public class DeclarationWriter extends NodeWriter {
writeVisibilityLabel((ICPPASTVisibilityLabel) declaration);
} else if (declaration instanceof ICPPASTAliasDeclaration) {
writeAliasDeclaration((ICPPASTAliasDeclaration) declaration);
+ } else if (declaration instanceof ICPPASTStaticAssertDeclaration) {
+ writeStaticAssertDeclaration((ICPPASTStaticAssertDeclaration)declaration);
}
writeTrailingComments(declaration, addNewLine);
@@ -272,7 +278,7 @@ public class DeclarationWriter extends NodeWriter {
private void writeASMDeclatation(IASTASMDeclaration asmDeclaration) {
scribe.print(ASM_START);
scribe.print(asmDeclaration.getAssembly());
- scribe.print(ASM_END);
+ scribe.print(CLOSE_PAREN);
printSemicolon();
}
@@ -378,4 +384,20 @@ public class DeclarationWriter extends NodeWriter {
printSemicolon();
}
+
+ private void writeStaticAssertDeclaration(ICPPASTStaticAssertDeclaration staticAssertDeclaration) {
+ scribe.print(Keywords.STATIC_ASSERT);
+ scribe.print(OPEN_PAREN);
+ staticAssertDeclaration.getCondition().accept(visitor);
+
+ ICPPASTLiteralExpression message = staticAssertDeclaration.getMessage();
+ if (message != null) {
+ scribe.print(COMMA);
+ scribe.printSpace();
+ message.accept(visitor);
+ }
+
+ scribe.print(CLOSE_PAREN);
+ scribe.printSemicolon();
+ }
}

Back to the top