Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cdc9991da77fa55b495eecbc97717299a7f0a7d9 (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
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/*
 * Created on Sept 30, 2004
 */
package org.eclipse.cdt.core.parser.tests;

import java.io.StringWriter;
import java.io.Writer;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParseError;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.cdt.core.parser.ast.IASTMacro;
import org.eclipse.cdt.core.parser.ast.IASTVariable;

/**
 * @author dsteffle
 */
public class ScannerParserLoopTest extends FileBasePluginTest {
	private static final int NUMBER_ITERATIONS = 30000;

	public ScannerParserLoopTest(String name) {
		super(name, ScannerParserLoopTest.class);
	}

	public static Test suite() {
		TestSuite suite = new TestSuite(ScannerParserLoopTest.class);
		suite.addTest(new ScannerParserLoopTest("cleanupProject")); //$NON-NLS-1$
		return suite;
	}

	// test scanner cancel()
	public void testBug72611A() throws Exception {
		Writer writer = new StringWriter();

		for (int i = 0; i < NUMBER_ITERATIONS; i++) {
			writer.write("#define A");
			writer.write(String.valueOf(i));
			writer.write(" B");
			writer.write(String.valueOf(i));
			writer.write("\n");
			writer.write("#define B");
			writer.write(String.valueOf(i));
			writer.write(" C");
			writer.write(String.valueOf(i));
			writer.write("\n");
			writer.write("#define C");
			writer.write(String.valueOf(i));
			writer.write(" D");
			writer.write(String.valueOf(i));
			writer.write("\n");
		}

		runCancelTest(writer);
	}

	// test parser cancel()
	public void testBug72611B() throws Exception {
		Writer writer = new StringWriter();

		for (int i = 0; i < NUMBER_ITERATIONS; i++) {
			writer.write("int a");
			writer.write(String.valueOf(i));
			writer.write("; // comment\n");
		}

		runCancelTest(writer);
	}

	private void runCancelTest(Writer writer) throws Exception {
		IFile file = importFile("code.cpp", writer.toString()); //$NON-NLS-1$

		try {
			TimeoutCallback callback = new TimeoutCallback();
			IParser parser = ParserFactory.createParser(ParserFactory
					.createScanner(new CodeReader(file.getRawLocation()
							.toString()), new ScannerInfo(), //$NON-NLS-1$
							ParserMode.COMPLETE_PARSE, ParserLanguage.CPP,
							callback, new NullLogService(), null), callback,
					ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, null);

			callback.setParser(parser);
			parser.parse();

			assertTrue(false); // fail if parse succeeds before being cancelled
		} catch (ParseError pe) { // expected
			assertEquals(pe.getErrorKind(),
					ParseError.ParseErrorKind.TIMEOUT_OR_CANCELLED);
		}
	}

	private static class TimeoutCallback extends NullSourceElementRequestor
			implements ISourceElementRequestor {
		private IParser parser;
		private boolean timerStarted = false;

		public void setParser(IParser parser) {
			this.parser = parser;
		}

		public void acceptMacro(IASTMacro macro) {
			parser.cancel();
		}
		
		public void acceptVariable(IASTVariable variable) {
			parser.cancel();
		}
	}
}

Back to the top