Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 63fb951fef94c2c1dfe452c8f59e53316d38702d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
* Copyright (c) 2016 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2.cxx14.constexpr;

import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPExecution;

import junit.framework.TestSuite;

public class FunctionTests extends TestBase {
	public static class NonIndexing extends FunctionTests {
		public NonIndexing() {
			setStrategy(new NonIndexingTestStrategy());
		}

		public static TestSuite suite() {
			return suite(NonIndexing.class);
		}
	}

	public static class SingleProject extends FunctionTests {
		public SingleProject() {
			setStrategy(new SinglePDOMTestStrategy(true, false));
		}

		public static TestSuite suite() {
			return suite(SingleProject.class);
		}
	}

	//	struct S {
	//		int x;
	//	};
	//	constexpr int g(S const& s) {
	//		return s.x;
	//	}
	//	constexpr int f() {
	//		S s{5};
	//		return g(s);
	//	}

	//	constexpr int x = f();
	public void testAccessMemberOfCompositeParameter() throws Exception {
		assertEvaluationEquals(5);
	}

	// constexpr int function(int n) { return n > 0 ? n + function(n-1) : n; }

	// constexpr int x = function(10);
	public void testRecursion() throws Exception {
		assertEvaluationEquals(55);
	}

	// constexpr int helper(int n) {
	//   int m = 5;
	//   return m + n;
	// }
	// constexpr int function() {
	//   int value = helper(5);
	//   return value + helper(5);
	// }

	// constexpr int x = function();
	public void testEvaluationOfConstexprFunctionCalls() throws Exception {
		assertEvaluationEquals(20);
	}

	//	constexpr int g(int i) {
	//		i++;
	//		return i;
	//	}
	//
	//	constexpr auto f() {
	//		int a = 3;
	//		int b = g(a);
	//      b++;
	//		return a;
	//	}

	//	constexpr auto x = f();
	public void testFunctionReturnValueIsCopiedAndNotReferenced() throws Exception {
		assertEvaluationEquals(3);
	}

	//	constexpr void incr(int x) {
	//		x = x + 1;
	//	}
	//	constexpr int f() {
	//		int a { 5 };
	//		incr(a);
	//		return a;
	//	}

	//	constexpr auto x = f();
	public void testPassingIntByValue() throws Exception {
		assertEvaluationEquals(5);
	}

	// constexpr void incr(int &x) {
	// 	x++;
	// }
	// constexpr int f() {
	// 	int a { 5 };
	// 	incr(a);
	// 	return a;
	// }

	//	constexpr auto x = f();
	public void testPassingIntByReference1() throws Exception {
		assertEvaluationEquals(6);
	}

	// constexpr void incr(int &x, int &y) {
	//  x++;
	//  y++;
	// }
	// constexpr int f() {
	//  int a { 5 };
	//  incr(a, a);
	//  return a;
	// }

	// constexpr auto x = f();
	public void testPassingIntByReference2() throws Exception {
		assertEvaluationEquals(7);
	}

	//	struct S {
	//		int x;
	//	};
	//	constexpr void g(S s) {
	//		s.x++;
	//	}
	//	constexpr int f() {
	//		S s{5};
	//		g(s);
	//		return s.x;
	//	}

	//	constexpr int x = f();
	public void testPassingCompositeByValue() throws Exception {
		assertEvaluationEquals(5);
	}

	// 	struct Point { int x, y; };
	//	constexpr void incr(Point &point) {
	//		point.x++;
	//	}
	//	constexpr int f() {
	//		Point p{ 2, 4 };
	//		incr(p);
	//		return p.x;
	//	}

	//	constexpr auto x = f();
	public void testPassingCompositeByReference() throws Exception {
		assertEvaluationEquals(3);
	}

	//	constexpr int a[2][2] { { 1, 2 }, { 3, 4 } };
	//	constexpr int const * g() {
	//		return a[0];
	//	}
	//	constexpr int f() {
	//		return g()[1];
	//	}

	//	constexpr auto x = f();
	public void testPointerReturnValue() throws Exception {
		assertEvaluationEquals(2);
	}

	//	int const y { 5 };
	//	constexpr int const & g() {
	//		return y;
	//	}
	//	constexpr int f() {
	//		return g() + 1;
	//	}

	//	constexpr auto x = f();
	public void testReferenceReturnValue() throws Exception {
		assertEvaluationEquals(6);
	}

	//	constexpr void side_effect(int array[], int length) {
	//		for (int i = 0; i < length; ++i) {
	//			array[i]++;
	//		}
	//	}
	//	constexpr int f() {
	//		int array[4] { 1, 2, 3, 4 };
	//		side_effect(array, 4);
	//		return array[0];
	//	}

	//	constexpr auto x = f();
	public void testSideEffectsOnArrayParameter() throws Exception {
		assertEvaluationEquals(2);
	}

	//	constexpr int f(int a) {
	//		{
	//			int a = 5;
	//			return a;
	//		}
	//		return a;
	//	}

	//	constexpr int x = f(10);
	public void testBlockScopeValueLookup1() throws Exception {
		assertEvaluationEquals(5);
	}

	//	constexpr int f(int a) {
	//		{
	//			int a = 5;
	//		}
	//		return a;
	//	}

	//	constexpr int x = f(10);
	public void testBlockScopeValueLookup2() throws Exception {
		assertEvaluationEquals(10);
	}

	//	char foo();
	//	constexpr int almost = sizeof(foo());

	//	constexpr int x = almost;
	public void testSizeofCallToRegularFunction() throws Exception {
		assertEvaluationEquals(1);
	}

	//	int f() {
	//		return 5;
	//	}

	//	int x = f();
	public void testNonConstexprFunctionDoesntStoreBodyExecution() throws Exception {
		IASTInitializerClause clause = getLastDeclarationInitializer();
		IASTFunctionCallExpression funcExpr = (IASTFunctionCallExpression) clause;
		IASTIdExpression idExpr = (IASTIdExpression) funcExpr.getFunctionNameExpression();
		ICPPFunction function = (ICPPFunction) idExpr.getName().resolveBinding();
		ICPPExecution bodyExec = CPPFunction.getFunctionBodyExecution(function);
		assertNull(bodyExec);
	}

	//	// Empty header file

	//	struct A {
	//	  A() {}
	//	  A& m(int p) {
	//	    return *this;
	//	  }
	//	};
	//
	//	A a = A()
	//	    .m(1).m(2).m(3).m(4).m(5).m(6).m(7).m(8).m(9).m(10)
	//	    .m(11).m(12).m(13).m(14).m(15).m(16).m(17).m(18).m(19).m(20)
	//	    .m(21).m(22).m(23).m(24).m(25).m(26).m(27).m(28).m(29).m(30);
	public void testLongCallChain_505606() throws Exception {
	}
}

Back to the top