1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-30 21:55:31 +02:00

Bug 535196: [C++17] Support *this in lambda capture

Implementation and tests.

Change-Id: If32911514eb62078215b5f06be12289fa571e9a3
Signed-off-by: Hansruedi Patzen <hansruedi.patzen@hsr.ch>
This commit is contained in:
Hansruedi Patzen 2018-05-28 15:03:00 +02:00
parent eb197ea3c7
commit f65fa5b7c5
6 changed files with 105 additions and 4 deletions

View file

@ -17,6 +17,7 @@ import org.eclipse.cdt.core.parser.tests.ast2.cxx14.GenericLambdaTests;
import org.eclipse.cdt.core.parser.tests.ast2.cxx14.InitCaptureTests;
import org.eclipse.cdt.core.parser.tests.ast2.cxx14.ReturnTypeDeductionTests;
import org.eclipse.cdt.core.parser.tests.ast2.cxx14.VariableTemplateTests;
import org.eclipse.cdt.core.parser.tests.ast2.cxx17.LambdaExpressionTests;
import org.eclipse.cdt.core.parser.tests.ast2.cxx17.TemplateAutoTests;
import org.eclipse.cdt.core.parser.tests.prefix.CompletionTestSuite;
@ -69,6 +70,7 @@ public class DOMParserTestSuite extends TestCase {
suite.addTestSuite(InitCaptureTests.class);
// C++17 tests
suite.addTest(TemplateAutoTests.suite());
suite.addTestSuite(LambdaExpressionTests.class);
return suite;
}
}

View file

@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright (c) 2018 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences 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
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Hansruedi Patzen (IFS)
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2.cxx17;
import org.eclipse.cdt.core.parser.tests.ast2.AST2CPPTestBase;
/**
* AST tests for C++17 lambda changes.
*/
public class LambdaExpressionTests extends AST2CPPTestBase {
// struct S {
// void foo() {
// [*this] { }();
// }
// };
public void testLambdaCaptures_535196_1() throws Exception {
parseAndCheckBindings();
}
// struct S {
// void bar() {
// }
// void foo() {
// [*this] { bar(); }();
// }
// };
public void testLambdaCaptures_535196_2() throws Exception {
parseAndCheckBindings();
}
// struct S {
// void bar(int k) {
// }
// void foo() {
// [m = 3, *this] { bar(m); }();
// }
// };
public void testLambdaCaptures_535196_3() throws Exception {
parseAndCheckBindings();
}
}

View file

@ -83,17 +83,30 @@ public class ExpressionWriterTest extends TestCase {
@Test
public void testWriteLambdaExpressionThisCaptureNoDeclarator() {
ICPPASTLambdaExpression lambda = getEmptyLambdaExpression();
lambda.addCapture(new CPPASTCapture());
CPPASTCapture thisCapture = new CPPASTCapture();
thisCapture.setIsByReference(true);
lambda.addCapture(thisCapture);
lambda.accept(visitor);
String expected = "[this] {" + BR + " return 7;" + BR + "}" + BR;
Assert.assertEquals(expected, visitor.toString());
}
@Test
public void testWriteLambdaExpressionStarThisCaptureNoDeclarator() {
ICPPASTLambdaExpression lambda = getEmptyLambdaExpression();
lambda.addCapture(new CPPASTCapture());
lambda.accept(visitor);
String expected = "[*this] {" + BR + " return 7;" + BR + "}" + BR;
Assert.assertEquals(expected, visitor.toString());
}
@Test
public void testWriteLambdaExpressionMixedCaptureNoDeclarator() {
ICPPASTLambdaExpression lambda = getEmptyLambdaExpression();
lambda.setCaptureDefault(ICPPASTLambdaExpression.CaptureDefault.BY_COPY);
lambda.addCapture(new CPPASTCapture());
CPPASTCapture thisCapture = new CPPASTCapture();
thisCapture.setIsByReference(true);
lambda.addCapture(thisCapture);
ICPPASTCapture x = new CPPASTCapture(), y = new CPPASTCapture();
x.setIdentifier(new CPPASTName(new char[] { 'x' }));
x.setIsByReference(true);
@ -158,7 +171,9 @@ public class ExpressionWriterTest extends TestCase {
y.setIsByReference(true);
lambda.addCapture(x);
lambda.addCapture(y);
lambda.addCapture(new CPPASTCapture());
CPPASTCapture thisCapture = new CPPASTCapture();
thisCapture.setIsByReference(true);
lambda.addCapture(thisCapture);
f.setMutable(true);
f.setTrailingReturnType(new CPPASTTypeId(INT, new CPPASTDeclarator(NO_NAME)));
f.addExceptionSpecificationTypeId(new CPPASTTypeId(INT, new CPPASTDeclarator(NO_NAME)));

View file

@ -216,4 +216,28 @@ constexpr long double operator ""_deg(long double deg)
int main()
{
double x = 90.0_deg;
}
}
//!Star this lambda capture
//%CPP
struct S
{
void foo()
{
[*this] {
}
;
}
};
//!This lambda capture
//%CPP
struct S
{
void foo()
{
[this] {
}
;
}
};

View file

@ -2122,11 +2122,18 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
switch (LT(1)) {
case IToken.t_this:
result.setIsByReference(true);
return setRange(result, offset, consume().getEndOffset());
case IToken.tAMPER:
consume();
referenceCapture = true;
break;
case IToken.tSTAR:
if (LT(2) == IToken.t_this) {
consume();
return setRange(result, offset, consume().getEndOffset());
}
break;
}
final IASTName identifier = identifier();

View file

@ -570,6 +570,9 @@ public class ExpressionWriter extends NodeWriter{
private void writeCapture(ICPPASTCapture capture) {
if (capture.capturesThisPointer()) {
if (!capture.isByReference()) {
scribe.print(STAR_OP);
}
scribe.print(THIS);
} else {
if (capture instanceof ICPPASTInitCapture) {