Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c3e4caf2b12fbba21740f3de2e359e87d19e3c0c (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
/*******************************************************************************
 *  Copyright (c) 2006, 2010 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
 *******************************************************************************/
package org.eclipse.cdt.core.lrparser.tests;

import java.util.ArrayList;
import java.util.List;

import junit.framework.AssertionFailedError;

import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.ast2.AST2BaseTest;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.core.runtime.CoreException;

/**
 * Utility methods for parsing test code using the C99 LPG parser.
 * 
 * @author Mike Kucera
 */
@SuppressWarnings({"restriction", "nls"})
public class ParseHelper {
	
	static int testsRun = 0;
	
	
	
	static protected class NameResolver extends ASTVisitor {
		{
			shouldVisitNames = true;
		}
		
		public List<IASTName> nameList = new ArrayList<IASTName>();
		public List<String> problemBindings = new ArrayList<String>();
		public int numNullBindings = 0;
		
		
		@Override
		public int visit(IASTName name) {
			nameList.add(name);
			IBinding binding = name.resolveBinding();
			if (binding instanceof IProblemBinding) {
				// Suppress assertion that would be thrown for computing string representation
				// of template-ids. The flag will be reset by BaseTestCase.setUp().
				CPPASTNameBase.sAllowNameComputation= true;
				problemBindings.add(name.toString());
			}
			if (binding == null)
				numNullBindings++;
			return PROCESS_CONTINUE;
		}
		
		public IASTName getName(int idx) {
			if(idx < 0 || idx >= nameList.size())
				return null;
			return nameList.get(idx);
		}
		
		public int size() { 
			return nameList.size(); 
		}
	}
	
	
	
	public static class Options {
		
		boolean checkSyntaxProblems = true;
		boolean checkPreprocessorProblems = true;
		boolean checkBindings = false;
		
		int expectedProblemBindings;
		String[] problems;
		boolean skipTrivialInitializers;
		
		public Options setCheckSyntaxProblems(boolean checkSyntaxProblems) {
			this.checkSyntaxProblems = checkSyntaxProblems;
			return this;
		}
		public Options setCheckBindings(boolean checkBindings) {
			this.checkBindings = checkBindings;
			return this;
		}
		public Options setCheckPreprocessorProblems(boolean checkPreprocessorProblems) {
			this.checkPreprocessorProblems = checkPreprocessorProblems;
			return this;
		}
		public Options setExpectedProblemBindings(int expectedProblemBindings) {
			this.expectedProblemBindings = expectedProblemBindings;
			return this;
		}
		public Options setProblems(String[] problems) {
			this.problems = problems;
			setExpectedProblemBindings(problems.length);
			setCheckBindings(true);
			return this;
		}
		public Options setSkipTrivialInitializers(boolean skipTrivialInitializers) {
			this.skipTrivialInitializers = skipTrivialInitializers;
			return this;
		}
		
	}
	
	
	public static IASTTranslationUnit parse(String code, ILanguage lang, boolean expectNoProblems) {
		Options options = new Options().setCheckSyntaxProblems(expectNoProblems).setCheckPreprocessorProblems(expectNoProblems);
		return parse(code.toCharArray(), lang, options);
	}

	public static IASTTranslationUnit parse(String code, ILanguage lang, Options options) {
		return parse(code.toCharArray(), lang, options);
	}
	
	
	public static IASTTranslationUnit parse(char[] code, ILanguage lang, Options options) {
			
		return parse(FileContent.create(AST2BaseTest.TEST_CODE, code), lang, new ScannerInfo(), null, options);
	}
	

	/**
	 * TODO thats WAY too many parameters, need to use a parameter object, need to refactor the
	 * DOM parser test suite so that its a lot cleaner.
	 * @Deprecated
	 */
	public static IASTTranslationUnit parse(CodeReader codeReader, ILanguage language, IScannerInfo scanInfo, 
			                                ICodeReaderFactory fileCreator, Options options) {
		testsRun++;
		
		IASTTranslationUnit tu;
		try {
			int languageOptions = 0;
			if(options.skipTrivialInitializers)
				languageOptions |= ILanguage.OPTION_SKIP_TRIVIAL_EXPRESSIONS_IN_AGGREGATE_INITIALIZERS;
			
			tu = language.getASTTranslationUnit(codeReader, scanInfo, fileCreator, null, languageOptions, ParserUtil.getParserLogService());
		} catch (CoreException e) {
			throw new AssertionFailedError(e.toString());
		}

		// should parse correctly first before we look at the bindings
        if(options.checkSyntaxProblems) {
        	
        	// this should work for C++ also, CVisitor.getProblems() and CPPVisitor.getProblems() are exactly the same code!
			if (CVisitor.getProblems(tu).length != 0) { 
				throw new AssertionFailedError(" CVisitor has AST Problems " ); 
			}
        }
        
        if(options.checkPreprocessorProblems) {
			if (tu.getPreprocessorProblems().length != 0) {
				throw new AssertionFailedError(language.getName() + " TranslationUnit has Preprocessor Problems " );
			}
        }

        // resolve all bindings
		if (options.checkBindings) {
			NameResolver res = new NameResolver();
	        tu.accept( res );
			if(res.problemBindings.size() != options.expectedProblemBindings)
				throw new AssertionFailedError("Expected " + options.expectedProblemBindings + " problem(s), encountered " + res.problemBindings.size());
			
			if(options.problems != null) {
				for(int i = 0; i < options.problems.length; i++) {
					String expected = options.problems[i];
					String actual = res.problemBindings.get(i);
					if(!expected.equals(actual))
						throw new AssertionFailedError(String.format("Problem binding not equal, expected: %s, got: %s", expected, actual));
				}
			}
		}
		
		return tu;
	}
	
	/**
	 * TODO thats WAY too many parameters, need to use a parameter object, need to refactor the
	 * DOM parser test suite so that its a lot cleaner.
	 */
	public static IASTTranslationUnit parse(FileContent fileContent, ILanguage language, IScannerInfo scanInfo, 
			IncludeFileContentProvider fileContentProvider, Options options) {
		testsRun++;
		
		IASTTranslationUnit tu;
		try {
			int languageOptions = 0;
			if(options.skipTrivialInitializers)
				languageOptions |= ILanguage.OPTION_SKIP_TRIVIAL_EXPRESSIONS_IN_AGGREGATE_INITIALIZERS;
			
			tu = language.getASTTranslationUnit(fileContent, scanInfo, fileContentProvider, null, languageOptions, ParserUtil.getParserLogService());
		} catch (CoreException e) {
			throw new AssertionFailedError(e.toString());
		}

		// should parse correctly first before we look at the bindings
        if(options.checkSyntaxProblems) {
        	
        	// this should work for C++ also, CVisitor.getProblems() and CPPVisitor.getProblems() are exactly the same code!
			if (CVisitor.getProblems(tu).length != 0) { 
				throw new AssertionFailedError(" CVisitor has AST Problems " ); 
			}
        }
        
        if(options.checkPreprocessorProblems) {
			if (tu.getPreprocessorProblems().length != 0) {
				throw new AssertionFailedError(language.getName() + " TranslationUnit has Preprocessor Problems " );
			}
        }

        // resolve all bindings
		if (options.checkBindings) {
			NameResolver res = new NameResolver();
	        tu.accept( res );
			if(res.problemBindings.size() != options.expectedProblemBindings)
				throw new AssertionFailedError("Expected " + options.expectedProblemBindings + " problem(s), encountered " + res.problemBindings.size());
			
			if(options.problems != null) {
				for(int i = 0; i < options.problems.length; i++) {
					String expected = options.problems[i];
					String actual = res.problemBindings.get(i);
					if(!expected.equals(actual))
						throw new AssertionFailedError(String.format("Problem binding not equal, expected: %s, got: %s", expected, actual));
				}
			}
		}
		
		return tu;
	}

	
	public static IASTTranslationUnit commentParse(String code, ILanguage language) {
		
		IASTTranslationUnit tu;
		try {
			tu = language.getASTTranslationUnit(FileContent.create(AST2BaseTest.TEST_CODE, code.toCharArray()), new ScannerInfo(), null, null, ILanguage.OPTION_ADD_COMMENTS, ParserUtil.getParserLogService());
		} catch (CoreException e) {
			throw new AssertionFailedError(e.toString());
		}
		return tu;
	}
	
	public static IASTCompletionNode getCompletionNode(String code, ILanguage lang) {
		int offset = code.length();
		if(offset > 0 && '\n' == code.charAt(offset-1)){
			offset--;
		}
		return getCompletionNode(code, lang, offset);
	}
	
	
	public static IASTCompletionNode getCompletionNode(String code, ILanguage language, int offset) {
		
		try {
			return language.getCompletionNode(FileContent.create(AST2BaseTest.TEST_CODE, code.toCharArray()), new ScannerInfo(), null, null, ParserUtil.getParserLogService(), offset);
		} catch (CoreException e) {
			throw new RuntimeException(e);
		}
	}

}

Back to the top