Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1bf928bd0a09b7657e6b4989620d0652d174e897 (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
/*******************************************************************************
 * Copyright (c) 2009,2011 QNX Software Systems
 * 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:
 *     QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.test;

import java.io.IOException;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.eclipse.cdt.codan.core.CodanRuntime;
import org.eclipse.cdt.codan.core.model.IChecker;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemReporter;
import org.eclipse.cdt.codan.core.model.IRunnableInEditorChecker;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
import org.eclipse.cdt.core.dom.parser.c.ANSICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration;
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
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.ast2.AST2BaseTest;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;

/**
 * TODO: add description
 */
@SuppressWarnings({ "nls", "restriction" })
public abstract class CodanFastCxxAstTestCase extends TestCase {
	protected IASTTranslationUnit tu;

	protected String getAboveComment() {
		return getContents(1)[0].toString();
	}

	protected StringBuffer[] getContents(int sections) {
		try {
			CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
			return TestSourceReader.getContentsForTest(plugin == null ? null : plugin.getBundle(), "src", getClass(), getName(), sections);
		} catch (IOException e) {
			fail(e.getMessage());
			return null;
		}
	}

	public boolean isCpp() {
		return false;
	}
	private static final NullLogService NULL_LOG = new NullLogService();

	/**
	 * @return
	 * 
	 */
	public IASTTranslationUnit parse(String code) {
		return parse(code, isCpp() ? ParserLanguage.CPP : ParserLanguage.C, true);
	}

	protected IASTTranslationUnit parse(String code, ParserLanguage lang, boolean gcc) {
		FileContent codeReader = FileContent.create("code.c", code.toCharArray());
		IScannerInfo scannerInfo = new ScannerInfo();
		IScanner scanner = AST2BaseTest.createScanner(codeReader, lang, ParserMode.COMPLETE_PARSE, scannerInfo);
		ISourceCodeParser parser2 = null;
		if (lang == ParserLanguage.CPP) {
			ICPPParserExtensionConfiguration config = null;
			if (gcc)
				config = new GPPParserExtensionConfiguration();
			else
				config = new ANSICPPParserExtensionConfiguration();
			parser2 = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config);
		} else {
			ICParserExtensionConfiguration config = null;
			if (gcc)
				config = new GCCParserExtensionConfiguration();
			else
				config = new ANSICParserExtensionConfiguration();
			parser2 = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config);
		}
		IASTTranslationUnit tu = parser2.parse();
		if (parser2.encounteredError() && !hasCodeErrors())
			fail("PARSE FAILURE");
		if (!hasCodeErrors()) {
			if (lang == ParserLanguage.C) {
				IASTProblem[] problems = CVisitor.getProblems(tu);
				assertEquals(problems.length, 0);
			} else if (lang == ParserLanguage.CPP) {
				IASTProblem[] problems = CPPVisitor.getProblems(tu);
				assertEquals(problems.length, 0);
			}
		}
		this.tu = tu;
		return tu;
	}

	/**
	 * Override if any of code that test tried to parse has errors, otherwise
	 * parse method would assert
	 * 
	 * @return
	 */
	protected boolean hasCodeErrors() {
		return false;
	}

	public class ProblemInstance {
		String id;
		IProblemLocation loc;
		Object[] args;

		/**
		 * @param id
		 * @param loc
		 * @param args
		 */
		public ProblemInstance(String id, IProblemLocation loc, Object[] args) {
			this.id = id;
			this.loc = loc;
			this.args = args;
		}
	}
	private ArrayList<ProblemInstance> codanproblems = new ArrayList<CodanFastCxxAstTestCase.ProblemInstance>();

	void runCodan(String code) {
		tu = parse(code);
		runCodan(tu);
	}

	void runCodan(IASTTranslationUnit tu) {
		IProblemReporter problemReporter = CodanRuntime.getInstance().getProblemReporter();
		CodanRuntime.getInstance().setProblemReporter(new IProblemReporter() {
			public void reportProblem(String problemId, IProblemLocation loc, Object... args) {
				codanproblems.add(new ProblemInstance(problemId, loc, args));
			}
		});
		try {
			IChecker checker = getChecker();
			((IRunnableInEditorChecker) checker).processModel(tu);
		} finally {
			CodanRuntime.getInstance().setProblemReporter(problemReporter);
		}
	}

	/**
	 * @return
	 */
	public abstract IChecker getChecker();

	protected int line2offset(int linePar, String code) throws IOException {
		byte[] bytes = code.getBytes();
		int line = 1;
		for (int j = 0; j < bytes.length; j++) {
			byte c = bytes[j];
			if (line >= linePar)
				return j;
			if (c == '\n')
				line++;
		}
		return 0;
	}
}

Back to the top