Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 45dbabb92603037cef978214843972a5bb01cc76 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*******************************************************************************
 * Copyright (c) 2008, 2013 Wind River Systems, Inc. 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
 *
 * Contributors:
 *    Markus Schorn - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;

import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTProblemExpression;
import org.eclipse.cdt.core.dom.ast.IASTProblemStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.parser.ParserLanguage;

import junit.framework.TestSuite;

/**
 * Testcases related to recovery from invalid syntax.
 */
public class FaultToleranceTests extends AST2TestBase {

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

	public FaultToleranceTests() {
		super();
	}

	public FaultToleranceTests(String name) {
		super(name);
	}

	// typedef int tint;
	// struct X {
	//    int a;
	// }
	// tint b;
	public void testCompositeTypeWithoutSemi() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTCompositeTypeSpecifier def = getCompositeType(tu, 1);
			IASTProblemDeclaration pdecl = getDeclaration(tu, 2);
			IASTSimpleDeclaration sdecl = getDeclaration(tu, 3);
		}
	}

	// typedef int tint;
	// struct X {
	//    int a;
	// } c
	// tint b;
	public void testCompositeTypeWithDtorWithoutSemi() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTSimpleDeclaration sdecl = getDeclaration(tu, 1);
			assertInstance(sdecl.getDeclSpecifier(), IASTCompositeTypeSpecifier.class);
			assertEquals(1, sdecl.getDeclarators().length);
			IASTProblemDeclaration pdecl = getDeclaration(tu, 2);
			sdecl = getDeclaration(tu, 3);
		}
	}

	// typedef int tint;
	// int a
	// tint b;
	public void testVariableWithoutSemi() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTSimpleDeclaration sdecl = getDeclaration(tu, 1);
			assertEquals("int a", sdecl.getRawSignature());
			IASTProblemDeclaration pdecl = getDeclaration(tu, 2);
			sdecl = getDeclaration(tu, 3);
		}
	}

	// typedef int tint;
	// int a()
	// tint b;
	public void testPrototypeWithoutSemi() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTSimpleDeclaration sdecl = getDeclaration(tu, 1);
			assertEquals("int a()", sdecl.getRawSignature());
			IASTProblemDeclaration pdecl = getDeclaration(tu, 2);
			sdecl = getDeclaration(tu, 3);
		}
	}

	// void f() {
	//   int a= 1
	// 	 f()
	// }
	public void testExpressionWithoutSemi_314593() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTFunctionDefinition fdef = getDeclaration(tu, 0);
			IASTStatement stmt = getStatement(fdef, 0);
			assertEquals("int a= 1", stmt.getRawSignature());
			IASTProblemStatement pstmt = getStatement(fdef, 1);
			stmt = getStatement(fdef, 2);
			assertEquals("f()", stmt.getRawSignature());
			pstmt = getStatement(fdef, 3);
		}
	}

	// struct X {
	//   int a;
	public void testIncompleteCompositeType() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTCompositeTypeSpecifier comp = getCompositeType(tu, 0);
			IASTProblemDeclaration pdecl = getDeclaration(tu, 1);

			IASTSimpleDeclaration sdecl = getDeclaration(comp, 0);
		}
	}

	// void func() {
	//   int a;
	public void testIncompleteFunctionDefinition() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTFunctionDefinition fdef = getDeclaration(tu, 0);
			IASTProblemDeclaration pdecl = getDeclaration(tu, 1);

			IASTDeclarationStatement sdecl = getStatement(fdef, 0);
		}
	}

	// namespace ns {
	//   int a;
	public void testIncompleteNamespace() throws Exception {
		final String comment = getAboveComment();
		IASTTranslationUnit tu = parse(comment, ParserLanguage.CPP, false, false);
		ICPPASTNamespaceDefinition ns = getDeclaration(tu, 0);
		IASTProblemDeclaration pdecl = getDeclaration(tu, 1);

		IASTSimpleDeclaration sdecl = getDeclaration(ns, 0);
	}

	// extern "C" {
	//   int a;
	public void testIncompleteLinkageSpec() throws Exception {
		final String comment = getAboveComment();
		IASTTranslationUnit tu = parse(comment, ParserLanguage.CPP, false, false);
		ICPPASTLinkageSpecification ls = getDeclaration(tu, 0);
		IASTProblemDeclaration pdecl = getDeclaration(tu, 1);

		IASTSimpleDeclaration sdecl = getDeclaration(ls, 0);
	}

	// void test() {
	//    int a= offsetof(struct mystruct, singlechar);
	// }
	public void testRangeOfProblemNode_Bug238151() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTFunctionDefinition fdef = getDeclaration(tu, 0);
			IASTProblemStatement pdecl = getStatement(fdef, 0);
			assertEquals("int a= offsetof(struct mystruct, singlechar);", pdecl.getRawSignature());
		}
	}

	// int f(){
	//    if( 12 A )
	//       return -1;
	//    int v;
	// }
	public void testProblemInIfExpression_Bug100321() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTFunctionDefinition fdef = getDeclaration(tu, 0);
			IASTIfStatement ifstmt = getStatement(fdef, 0);
			assertInstance(ifstmt.getConditionExpression(), IASTProblemExpression.class);
			assertEquals("12 A", ifstmt.getConditionExpression().getRawSignature());
			assertInstance(ifstmt.getThenClause(), IASTReturnStatement.class);
		}
	}

	// _MYMACRO_ myType foo();
	// _MYMACRO_ myType foo() {}
	// extern void foo2() _MYMACRO_;
	public void testUndefinedMacrosInFunctionDeclarations_Bug234085() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTProblemDeclaration pd = getDeclaration(tu, 0);
			assertEquals("_MYMACRO_", pd.getRawSignature());
			IASTSimpleDeclaration sdecl = getDeclaration(tu, 1);
			assertEquals("myType foo();", sdecl.getRawSignature());

			pd = getDeclaration(tu, 2);
			assertEquals("_MYMACRO_", pd.getRawSignature());
			IASTFunctionDefinition fdef = getDeclaration(tu, 3);
			assertEquals("myType foo() {}", fdef.getRawSignature());

			sdecl = getDeclaration(tu, 4);
			assertEquals("extern void foo2()", sdecl.getRawSignature());
			pd = getDeclaration(tu, 5); // the missing semicolon

			if (lang == ParserLanguage.CPP) {
				pd = getDeclaration(tu, 6);
				assertEquals("_MYMACRO_;", pd.getRawSignature());
			} else {
				sdecl = getDeclaration(tu, 6);
				assertEquals("_MYMACRO_;", sdecl.getRawSignature());
			}
		}
	}

	// enum _T { I J, K }; // missing comma
	// int i;
	public void testEnumProblem() throws Exception {
		final String comment = getAboveComment();
		for (ParserLanguage lang : ParserLanguage.values()) {
			IASTTranslationUnit tu = parse(comment, lang, false, false);
			IASTSimpleDeclaration e = getDeclaration(tu, 0);
			IASTProblemDeclaration p = getDeclaration(tu, 1);
			assertEquals("J, K };", p.getRawSignature());
			IASTSimpleDeclaration s = getDeclaration(tu, 2);
			assertEquals("int i;", s.getRawSignature());
		}
	}

	// class A {
	//    enum _T { I J, K }; // missing comma
	//    int i;
	// };
	public void testEnumError_Bug72685() throws Exception {
		final String comment = getAboveComment();
		IASTTranslationUnit tu = parse(comment, ParserLanguage.CPP, false, false);
		IASTCompositeTypeSpecifier ct = getCompositeType(tu, 0);
		IASTSimpleDeclaration e = getDeclaration(ct, 0);
		IASTProblemDeclaration p = getDeclaration(ct, 1);
		assertEquals("J, K };", p.getRawSignature());
		IASTSimpleDeclaration s = getDeclaration(ct, 2);
		assertEquals("int i;", s.getRawSignature());
	}

	//	#define XX() .
	//	int c;
	//	XX(
	//	);
	//	int d;
	public void testErrorRecovery_273759() throws Exception {
		IASTTranslationUnit tu = parse(getAboveComment(), ParserLanguage.C, false, false);
		IASTSimpleDeclaration s = getDeclaration(tu, 0);
		IASTProblemDeclaration p = getDeclaration(tu, 1);
		s = getDeclaration(tu, 2);

		tu = parse(getAboveComment(), ParserLanguage.CPP, false, false);
		s = getDeclaration(tu, 0);
		p = getDeclaration(tu, 1);
		s = getDeclaration(tu, 2);
	}

	//	    TINT* f(TINT* p) {
	//	    TINT* f1(TINT* p) {
	//	    TINT* f2(TINT* p) {
	//	    TINT* f3(TINT* p) {
	//	    TINT* f4(TINT* p) {
	//	    TINT* f5(TINT* p) {
	//	    TINT* f6(TINT* p) {
	//	    TINT* f7(TINT* p) {
	//	    TINT* f8(TINT* p) {
	//	    TINT* f9(TINT* p) {
	//	    TINT* f10(TINT* p) {
	//	    TINT* f11(TINT* p) {
	//	    TINT* f12(TINT* p) {
	//	    TINT* f13(TINT* p) {
	//	    TINT* f14(TINT* p) {
	//	    TINT* f15(TINT* p) {
	//	    TINT* f16(TINT* p) {
	//	    TINT* f17(TINT* p) {
	//	    TINT* f18(TINT* p) {
	//	    TINT* f19(TINT* p) {
	//	    TINT* f20(TINT* p) {
	//	    TINT* f21(TINT* p) {
	//	    TINT* f22(TINT* p) {
	//	    TINT* f23(TINT* p) {
	//	    TINT* f24(TINT* p) {
	//	    TINT* f25(TINT* p) {
	//	    TINT* f26(TINT* p) {
	//	    TINT* f27(TINT* p) {
	//	    TINT* f28(TINT* p) {
	//	    TINT* f29(TINT* p) {
	//	    }
	public void testPerformanceIssue_364108() throws Exception {
		final String comment = getAboveComment();
		parse(comment, ParserLanguage.CPP, false, false);
		parse(comment, ParserLanguage.C, false, false);
	}
}

Back to the top