Skip to main content
summaryrefslogtreecommitdiffstats
blob: 79694ecff1a391a85b234b61755b5a0079afefa8 (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
/*******************************************************************************
* Copyright (c) 2016 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
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2.constexprevaluation;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerClause;
import org.eclipse.cdt.core.dom.parser.IScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPScannerExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.tests.ASTComparer;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.CStringValue;
import org.eclipse.cdt.internal.core.dom.parser.FloatingPointValue;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluationOwner;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor;
import org.eclipse.cdt.internal.index.tests.IndexBindingResolutionTestBase;

public class TestBase extends IndexBindingResolutionTestBase {
	private static final String TEST_CODE = "<testcode>";
	private static final IParserLogService NULL_LOG = new NullLogService();
	private static final ScannerInfo SCANNER_INFO = new ScannerInfo(getStdMap());

	private static Map<String, String> getStdMap() {
		Map<String, String> map = new HashMap<>();
		map.put("__SIZEOF_SHORT__", "2");
		map.put("__SIZEOF_INT__", "4");
		map.put("__SIZEOF_LONG__", "8");
		map.put("__SIZEOF_POINTER__", "8");
		return map;
	}

	protected void assertEvaluationEquals(boolean expectedValue) throws Exception {
		IValue value = getValue();
		Number num = value.numberValue();
		assertNotNull(num);
		assertEquals(expectedValue == false, num.longValue() == 0);
	}

	protected void assertEvaluationEquals(char expectedValue) throws Exception {
		IValue value = getValue();
		Number num = value.numberValue();
		assertNotNull(num);
		assertEquals(expectedValue, num.longValue());
	}

	protected void assertEvaluationEquals(long expectedValue) throws Exception {
		IValue value = getValue();
		Number num = value.numberValue();
		assertNotNull(num);
		assertEquals(expectedValue, num.longValue());
	}

	protected void assertEvaluationEquals(IValue expectedValue) throws Exception {
		IValue value = getValue();
		assertEquals(expectedValue, value);
	}

	protected void assertEvaluationEquals(String expectedValue) throws Exception {
		IValue value = getValue();
		assertInstance(value, CStringValue.class);
		CStringValue cstrValue = (CStringValue) value;
		assertEquals(expectedValue, cstrValue.cStringValue());
	}

	protected void assertEvaluationEquals(double expectedValue) throws Exception {
		IValue value = getValue();
		assertInstance(value, FloatingPointValue.class);
		FloatingPointValue floatingPointValue = (FloatingPointValue) value;
		assertEquals(expectedValue, floatingPointValue.numberValue().doubleValue(), 0.001);
	}

	private IValue getValue() throws Exception {
		ICPPASTInitializerClause point = getLastDeclarationInitializer();
		ICPPEvaluation evaluation = ((ICPPEvaluationOwner) point).getEvaluation();
		return evaluation.getValue(point);
	}

	protected ICPPASTInitializerClause getLastDeclarationInitializer() throws Exception {
		IASTTranslationUnit tu = strategy.getAst(0);
		IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) tu.getChildren()[tu.getChildren().length - 1];
		IASTEqualsInitializer initializer = (IASTEqualsInitializer) declaration.getDeclarators()[0].getInitializer();
		return (ICPPASTInitializerClause) initializer.getInitializerClause();
	}

	protected class NonIndexingTestStrategy implements ITestStrategy {
		private ICProject cproject;
		private StringBuilder[] testData;
		private IASTTranslationUnit ast;

		@Override
		public ICProject getCProject() {
			return cproject;
		}

		@Override
		public StringBuilder[] getTestData() {
			return testData;
		}

		@Override
		public int getAstCount() {
			return 1;
		}

		@Override
		public IASTTranslationUnit getAst(int index) {
			return ast;
		}

		@Override
		public StringBuilder getAstSource(int index) {
			return testData[0];
		}

		@Override
		public void setUp() throws Exception {
			CTestPlugin plugin = CTestPlugin.getDefault();
			StringBuilder[] builders = TestSourceReader.getContentsForTest(plugin.getBundle(), "parser", TestBase.this.getClass(), getName(), 2);
			if(builders.length == 2) {
				builders[0].append(builders[1].toString());
			}
			testData = new StringBuilder[] { builders[0] };
			ast = parse(testData[0].toString());
		}

		@Override
		public void tearDown() throws Exception {
		}

		@Override
		public IIndex getIndex() {
			return null;
		}

		@Override
		public boolean isCompositeIndex() {
			return false;
		}
	}

	protected static IASTTranslationUnit parse(String code) throws ParserException {
		IScanner scanner = createScanner(FileContent.create(TEST_CODE, code.toCharArray()), ParserLanguage.CPP, ParserMode.COMPLETE_PARSE, SCANNER_INFO);
		AbstractGNUSourceCodeParser parser = null;
		ICPPParserExtensionConfiguration config = new ANSICPPParserExtensionConfiguration();
		parser = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config, null);
		parser.setMaximumTrivialExpressionsInAggregateInitializers(Integer.MAX_VALUE);

		IASTTranslationUnit tu = parser.parse();
		assertTrue(tu.isFrozen());

		validateCopy(tu);

		if (parser.encounteredError()) {
			throw new ParserException("FAILURE"); //$NON-NLS-1$
		}

		assertEquals(CPPVisitor.getProblems(tu).length, 0);
		assertEquals(0, tu.getPreprocessorProblems().length);
		return tu;
	}

	private static IScanner createScanner(FileContent codeReader, ParserLanguage lang, ParserMode mode,
			IScannerInfo scannerInfo) {
		IScannerExtensionConfiguration configuration = GPPScannerExtensionConfiguration.getInstance(scannerInfo);
		IScanner scanner = new CPreprocessor(codeReader, scannerInfo, lang, NULL_LOG, configuration,
				IncludeFileContentProvider.getSavedFilesProvider());
		return scanner;
	}

	private static <T extends IASTNode> T validateCopy(T tu) {
		IASTNode copy = tu.copy();
		assertFalse(copy.isFrozen());
		ASTComparer.assertCopy(tu, copy);
		return (T) copy;
	}
}

Back to the top