1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

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>
This commit is contained in:
Hansruedi Patzen 2018-05-17 14:15:49 +02:00 committed by Thomas Corbat
parent 45279d5ffc
commit 278979870c
8 changed files with 68 additions and 12 deletions

View file

@ -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();
}
}

View file

@ -159,3 +159,8 @@ int main()
}
;
}
//!static_asserts with and without message
//%CPP
static_assert(true, "Should always pass");
static_assert(sizeof (int) == 4);

View file

@ -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();
}

View file

@ -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);

View file

@ -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;

View file

@ -736,6 +736,11 @@ public class CPPNodeFactory extends NodeFactory implements ICPPNodeFactory {
return new CPPASTStaticAssertionDeclaration(condition, message);
}
@Override
public ICPPASTStaticAssertDeclaration newStaticAssertion(IASTExpression condition) {
return newStaticAssertion(condition, null);
}
@Override
public ICPPASTSwitchStatement newSwitchStatement() {
return new CPPASTSwitchStatement();

View file

@ -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();
consume(IToken.tRPAREN);
endOffset= consume(IToken.tSEMI).getEndOffset();
message = stringLiteral();
}
ICPPASTStaticAssertDeclaration assertion = getNodeFactory().newStaticAssertion(e, message);
if (LT(1) != IToken.tEOC) {
consume(IToken.tRPAREN);
endOffset = consume(IToken.tSEMI).getEndOffset();
}
ICPPASTStaticAssertDeclaration assertion = getNodeFactory().newStaticAssertion(e, lit);
return setRange(assertion, offset, endOffset);
}

View file

@ -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();
}
}