Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8bbacedb0c40a255efce48a07544331fe4641a25 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*******************************************************************************
 * Copyright (c) 2004, 2011 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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.xlc.core.scannerconfig;

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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.IMarkerGenerator;
import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector;
import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector2;
import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoConsoleParser;
import org.eclipse.cdt.make.internal.core.scannerconfig.util.TraceUtil;
import org.eclipse.cdt.make.internal.core.scannerconfig2.SCProfileInstance;
import org.eclipse.cdt.make.internal.core.scannerconfig2.ScannerConfigProfileManager;
import org.eclipse.cdt.make.internal.core.scannerconfig2.ScannerConfigProfile.BuildOutputProvider;
import org.eclipse.cdt.make.xlc.core.activator.Activator;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;

/**
 * @author crecoskie
 * 
 */
public abstract class AbstractXLCBuildOutputParser implements IScannerInfoConsoleParser {

	protected static final String[] COMPILER_INVOCATION = { "xlc", "xlC"//$NON-NLS-1$ //$NON-NLS-2$
	};
	protected static final String DASHIDASH = "-I-"; //$NON-NLS-1$
	protected static final String DASHI = "-I"; //$NON-NLS-1$
	protected static final String DASHD = "-D"; //$NON-NLS-1$

	protected IProject fProject;
	protected IScannerInfoCollector fCollector;
	protected IPath fWorkingDir;
	protected IMarkerGenerator fMarkerGenerator;
	protected XLCBuildOutputParserUtility fUtility;

	protected boolean fBMultiline = false;
	protected String fSMultiline = ""; //$NON-NLS-1$

	protected String[] fCompilerCommands = { "xlc", "xlC" }; //$NON-NLS-1$ //$NON-NLS-2$

	/**
	 * @return Returns the fProject.
	 */
	protected IProject getProject() {
		return fProject;
	}

	/**
	 * @return Returns the fCollector.
	 */
	protected IScannerInfoCollector getCollector() {
		return fCollector;
	}

	public void startup(IProject project, IScannerInfoCollector collector) {
		fProject = project;
		fCollector = collector;
		fCompilerCommands = computeCompilerCommands();
	}

	/**
	 * Returns array of additional compiler commands to look for
	 * 
	 * @return String[]
	 */
	protected String[] computeCompilerCommands() {
		if (fProject != null) {
			SCProfileInstance profileInstance = ScannerConfigProfileManager.getInstance().getSCProfileInstance(
					fProject, ScannerConfigProfileManager.NULL_PROFILE_ID);
			BuildOutputProvider boProvider = profileInstance.getProfile().getBuildOutputProviderElement();
			if (boProvider != null) {
				String compilerCommandsString = boProvider.getScannerInfoConsoleParser().getCompilerCommands();
				if (compilerCommandsString != null && compilerCommandsString.length() > 0) {
					String[] compilerCommands = compilerCommandsString.split(",\\s*"); //$NON-NLS-1$
					if (compilerCommands.length > 0) {
						String[] compilerInvocation = new String[COMPILER_INVOCATION.length + compilerCommands.length];
						System.arraycopy(COMPILER_INVOCATION, 0, compilerInvocation, 0, COMPILER_INVOCATION.length);
						System.arraycopy(compilerCommands, 0, compilerInvocation, COMPILER_INVOCATION.length,
								compilerCommands.length);
						return compilerInvocation;
					}
				}
			}
		}
		return COMPILER_INVOCATION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.cdt.make.core.scannerconfig.IScannerInfoConsoleParser#processLine
	 * (java.lang.String)
	 */
	public boolean processLine(String line) {
		boolean rc = false;
		int lineBreakPos = line.length() - 1;
		char[] lineChars = line.toCharArray();
		while (lineBreakPos >= 0 && Character.isWhitespace(lineChars[lineBreakPos])) {
			lineBreakPos--;
		}
		if (lineBreakPos >= 0) {
			if (lineChars[lineBreakPos] != '\\' || (lineBreakPos > 0 && lineChars[lineBreakPos - 1] == '\\')) {
				lineBreakPos = -1;
			}
		}
		// check for multiline commands (ends with '\')
		if (lineBreakPos >= 0) {
			fSMultiline += line.substring(0, lineBreakPos);
			fBMultiline = true;
			return rc;
		}
		if (fBMultiline) {
			line = fSMultiline + line;
			fBMultiline = false;
			fSMultiline = ""; //$NON-NLS-1$
		}
		line = line.trim();
		try{
			TraceUtil.outputTrace("XLCBuildOutputParser parsing line: [", line, "]"); //$NON-NLS-1$ //$NON-NLS-2$
		}catch(NoClassDefFoundError e){
			//no problem, as this may be called from a standalone indexer
		}
		// make\[[0-9]*\]: error_desc
		int firstColon = line.indexOf(':');
		String make = line.substring(0, firstColon + 1);
		if (firstColon != -1 && make.indexOf("make") != -1) { //$NON-NLS-1$
			boolean enter = false;
			String msg = line.substring(firstColon + 1).trim();
			if ((enter = msg.startsWith("Entering directory")) || //$NON-NLS-1$
					(msg.startsWith("Leaving directory"))) { //$NON-NLS-1$
				int s = msg.indexOf('`');
				int e = msg.indexOf('\'');
				if (s != -1 && e != -1) {
					String dir = msg.substring(s + 1, e);
					if (getUtility() != null) {
						getUtility().changeMakeDirectory(dir, getDirectoryLevel(line), enter);
					}
					return rc;
				}
			}
		}
		// call sublclass to process a single line
		return processSingleLine(line.trim());
	}

	protected synchronized XLCBuildOutputParserUtility getUtility() {
		if (fUtility == null)
			fUtility = new XLCBuildOutputParserUtility(fProject, fWorkingDir, fMarkerGenerator);

		return fUtility;
	}

	protected int getDirectoryLevel(String line) {
		int s = line.indexOf('[');
		int num = 0;
		if (s != -1) {
			int e = line.indexOf(']');
			String number = line.substring(s + 1, e).trim();
			try {
				num = Integer.parseInt(number);
			} catch (NumberFormatException exc) {
			}
		}
		return num;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.cdt.make.core.scannerconfig.IScannerInfoConsoleParser#shutdown
	 * ()
	 */
	public void shutdown() {
		if (getUtility() != null) {
			getUtility().reportProblems();
		}
		
		if(fCollector != null && fCollector instanceof IScannerInfoCollector2) {
			IScannerInfoCollector2 collector = (IScannerInfoCollector2) fCollector;
			try {
				collector.updateScannerConfiguration(null);
			} catch (CoreException e) {
				// TODO Auto-generated catch block
				Activator.log(e);
			}
		}
	}

	/**
	 * Tokenizes a line into an array of commands. Commands are separated by
	 * ';', '&&' or '||'. Tokens are separated by whitespace unless found inside
	 * of quotes, back-quotes, or double quotes. Outside of single-, double- or
	 * back-quotes a backslash escapes white-spaces, all quotes, the backslash,
	 * '&' and '|'. A backslash used for escaping is removed. Quotes other than
	 * the back-quote plus '&&', '||', ';' are removed, also.
	 * 
	 * @param line
	 *            to tokenize
	 * @return array of commands
	 */
	protected String[][] tokenize(String line, boolean escapeInsideDoubleQuotes) {
		ArrayList<String[]> commands = new ArrayList<String[]>();
		ArrayList<String> tokens = new ArrayList<String>();
		StringBuffer token = new StringBuffer();

		final char[] input = line.toCharArray();
		boolean nextEscaped = false;
		char currentQuote = 0;
		for (int i = 0; i < input.length; i++) {
			final char c = input[i];
			final boolean escaped = nextEscaped;
			nextEscaped = false;

			if (currentQuote != 0) {
				if (c == currentQuote) {
					if (escaped) {
						token.append(c);
					} else {
						if (c == '`') {
							token.append(c); // preserve back-quotes
						}
						currentQuote = 0;
					}
				} else {
					if (escapeInsideDoubleQuotes && currentQuote == '"' && c == '\\') {
						nextEscaped = !escaped;
						if (escaped) {
							token.append(c);
						}
					} else {
						if (escaped) {
							token.append('\\');
						}
						token.append(c);
					}
				}
			} else {
				switch (c) {
				case '\\':
					if (escaped) {
						token.append(c);
					} else {
						nextEscaped = true;
					}
					break;
				case '\'':
				case '"':
				case '`':
					if (escaped) {
						token.append(c);
					} else {
						if (c == '`') {
							token.append(c);
						}
						currentQuote = c;
					}
					break;
				case ';':
					if (escaped) {
						token.append(c);
					} else {
						endCommand(token, tokens, commands);
					}
					break;
				case '&':
				case '|':
					if (escaped || i + 1 >= input.length || input[i + 1] != c) {
						token.append(c);
					} else {
						i++;
						endCommand(token, tokens, commands);
					}
					break;

				default:
					if (Character.isWhitespace(c)) {
						if (escaped) {
							token.append(c);
						} else {
							endToken(token, tokens);
						}
					} else {
						if (escaped) {
							token.append('\\'); // for windows put backslash
												// back onto the token.
						}
						token.append(c);
					}
				}
			}
		}
		endCommand(token, tokens, commands);
		return commands.toArray(new String[commands.size()][]);
	}

	protected void endCommand(StringBuffer token, ArrayList<String> tokens, ArrayList<String[]> commands) {
		endToken(token, tokens);
		if (!tokens.isEmpty()) {
			commands.add(tokens.toArray(new String[tokens.size()]));
			tokens.clear();
		}
	}

	protected void endToken(StringBuffer token, ArrayList<String> tokens) {
		if (token.length() > 0) {
			tokens.add(token.toString());
			token.setLength(0);
		}
	}

	protected boolean processSingleLine(String line) {
		boolean rc = false;
		String[][] tokens = tokenize(line, true);
		for (int i = 0; i < tokens.length; i++) {
			String[] command = tokens[i];
			if (processCommand(command)) {
				rc = true;
			} else { // go inside quotes, if the compiler is called per wrapper
						// or shell script
				for (int j = 0; j < command.length; j++) {
					String[][] subtokens = tokenize(command[j], true);
					for (int k = 0; k < subtokens.length; k++) {
						String[] subcommand = subtokens[k];
						if (subcommand.length > 1) { // only proceed if there is
														// any additional info
							if (processCommand(subcommand)) {
								rc = true;
							}
						}
					}
				}
			}
		}
		return rc;
	}

	protected int findCompilerInvocation(String[] tokens) {
		for (int i = 0; i < tokens.length; i++) {
			final String token = tokens[i].toLowerCase();
			final int searchFromOffset = Math.max(token.lastIndexOf('/'), token.lastIndexOf('\\')) + 1;
			for (int j = 0; j < fCompilerCommands.length; j++) {
				if (token.indexOf(fCompilerCommands[j], searchFromOffset) != -1) {
					return i;
				}
			}
		}
		return -1;
	}

	public void startup(IProject project, IPath workingDirectory, IScannerInfoCollector collector,
			IMarkerGenerator markerGenerator) {
		fProject = project;
		fWorkingDir = workingDirectory;
		fCollector = collector;
		fMarkerGenerator = markerGenerator;

	}

	abstract protected boolean processCommand(String[] tokens);

	protected  List<String> getFileExtensionsList() {
		IContentTypeManager manager = Platform.getContentTypeManager();
		List<String> extensions = new LinkedList<String>();
		IContentType cSource = manager.getContentType(CCorePlugin.CONTENT_TYPE_CSOURCE);
		IContentType cppSource = manager.getContentType(CCorePlugin.CONTENT_TYPE_CXXSOURCE);

		String[] cExtensions = cSource.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
		String[] cppExtensions = cppSource.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);

		for (int k = 0; k < cExtensions.length; k++) {
			extensions.add("." + cExtensions[k]); //$NON-NLS-1$
		}

		for (int k = 0; k < cppExtensions.length; k++) {
			extensions.add("." + cppExtensions[k]); //$NON-NLS-1$
		}

		return extensions;
	}

	protected String[] getFileExtensions() {
		IContentTypeManager manager = Platform.getContentTypeManager();
		List<String> extensions = new LinkedList<String>();
		IContentType cSource = manager.getContentType(CCorePlugin.CONTENT_TYPE_CSOURCE);
		IContentType cppSource = manager.getContentType(CCorePlugin.CONTENT_TYPE_CXXSOURCE);

		String[] cExtensions = cSource.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);
		String[] cppExtensions = cppSource.getFileSpecs(IContentType.FILE_EXTENSION_SPEC);

		for (int k = 0; k < cExtensions.length; k++) {
			extensions.add("." + cExtensions[k]); //$NON-NLS-1$
		}

		for (int k = 0; k < cppExtensions.length; k++) {
			extensions.add("." + cppExtensions[k]); //$NON-NLS-1$
		}

		return extensions.toArray(new String[0]);
	}

}

Back to the top