Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab436e7104f7e46f311428b92fa7cf43b0b2c00c (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**********************************************************************
 * Copyright (c) 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 * IBM - Initial API and implementation
 **********************************************************************/
package org.eclipse.cdt.make.internal.core.scannerconfig;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.MakeProjectNature;
import org.eclipse.cdt.make.core.scannerconfig.IExternalScannerInfoProvider;
import org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo;
import org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector;
import org.eclipse.cdt.make.core.scannerconfig.ScannerConfigBuilder;
import org.eclipse.cdt.make.core.scannerconfig.ScannerConfigUtil;
import org.eclipse.cdt.make.core.scannerconfig.IDiscoveredPathManager.IDiscoveredPathInfo;
import org.eclipse.cdt.make.internal.core.MakeMessages;
import org.eclipse.cdt.make.internal.core.scannerconfig.util.CygpathTranslator;
import org.eclipse.cdt.make.internal.core.scannerconfig.util.TraceUtil;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;


/**
 * Singleton object that collects scanner config updates from ScannerInfoParser
 * and updates scanner config when the project's build is done.
 *
 * @author vhirsl
 */
public class ScannerInfoCollector implements IScannerInfoCollector { 

	// Singleton
	private static ScannerInfoCollector instance = new ScannerInfoCollector();
	private Map discoveredIncludes; 
	private Map discoveredSymbols;
	private Map discoveredTSO;	// target specific options
	// cumulative values
	private Map sumDiscoveredIncludes; 
	private Map sumDiscoveredSymbols;
//	private Map sumDiscoveredTSO;	// target specific options
	
	private IProject currentProject;	// project being built
	
	private ScannerInfoCollector() {
		discoveredIncludes = new HashMap();
		discoveredSymbols = new HashMap();
		discoveredTSO = new HashMap();
		
		sumDiscoveredIncludes = new HashMap();
		sumDiscoveredSymbols = new HashMap();
//		sumDiscoveredTSO = new HashMap();
	}
	
	public static ScannerInfoCollector getInstance() {
		return instance;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector#contributeToScannerConfig(org.eclipse.core.resources.IResource, java.util.List, java.util.List, java.util.Map)
	 */
	public void contributeToScannerConfig(IResource resource, List includes, List symbols, Map extraInfo) {
		IProject project;
		if (resource == null || (project = resource.getProject()) == null) {
			TraceUtil.outputError("IScannerInfoCollector.contributeToScannerConfig : ", "resource or project is null"); //$NON-NLS-1$ //$NON-NLS-2$
			return;
		}
		try {
			if (project.hasNature(MakeProjectNature.NATURE_ID) && // limits to StandardMake projects
					(project.hasNature(CProjectNature.C_NATURE_ID) ||
					 project.hasNature(CCProjectNature.CC_NATURE_ID))) { 

				String projectName = project.getName();
				contribute(projectName, discoveredIncludes, includes, true);
				contribute(projectName, discoveredSymbols, symbols, false);
				contribute(projectName, 
						   discoveredTSO, 
						   (extraInfo == null) ? null : (List) extraInfo.get(IScannerInfoCollector.TARGET_SPECIFIC_OPTION),
						   false);
			}
		} 
		catch (CoreException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param project
	 * @param discovered symbols | includes | targetSpecificOptions
	 * @param delta symbols | includes | targetSpecificOptions
	 * @param ordered - to preserve order or append at the end
	 * @return true if there is a change in discovered symbols | includes | targetSpecificOptions
	 */
	private boolean contribute(String projectName, Map discovered, List delta, boolean ordered) {
		if (delta == null || delta.isEmpty())
			return false;
		List projectDiscovered = (List) discovered.get(projectName);
		if (projectDiscovered == null) {
			projectDiscovered = new ArrayList(delta);
			discovered.put(projectName, projectDiscovered);
			return true;
		}
		return addItemsWithOrder(delta, projectDiscovered, ordered);
	}

	/**
	 * Adds new items to the already accumulated ones preserving order
	 *  
	 * @param includes - items to be added
	 * @param sumIncludes - previously accumulated items
	 * @param ordered - to preserve order or append at the end
	 * @return boolean - true if added
	 */
	private boolean addItemsWithOrder(List includes, List sumIncludes, boolean ordered) {
		boolean addedIncludes = false;
		int prev = sumIncludes.size() - 1;	// index of previously added/found contribution in already discovered list
		for (Iterator i = includes.iterator(); i.hasNext(); ) {
			String item = (String) i.next();
			if (!sumIncludes.contains(item)) {
				sumIncludes.add(prev + 1, item);
				addedIncludes = true;
			}
			prev = ordered ? sumIncludes.indexOf(item) : sumIncludes.size() - 1;
		}
		return addedIncludes;
	}

	/**
	 * @param project
	 * @param monitor
	 */
	private void updateScannerConfig(IProject project, IProgressMonitor monitor) throws CoreException {
		IDiscoveredPathInfo pathInfo = MakeCorePlugin.getDefault().getDiscoveryManager().getDiscoveredInfo(project);
		monitor.beginTask(MakeMessages.getString("ScannerInfoCollector.Processing"), 100); //$NON-NLS-1$
		if (pathInfo != null) {
			String projectName = project.getName();
			monitor.subTask(MakeMessages.getString("ScannerInfoCollector.Processing")); //$NON-NLS-1$
			if (scannerConfigNeedsUpdate(pathInfo)) {
				monitor.worked(50);
				monitor.subTask(MakeMessages.getString("ScannerInfoCollector.Updating") + projectName); //$NON-NLS-1$
				try {
					// update scanner configuration
					MakeCorePlugin.getDefault().getDiscoveryManager().updateDiscoveredInfo(pathInfo);
					monitor.worked(50);
				} catch (CoreException e) {
					MakeCorePlugin.log(e);
				}
			}
		}
		monitor.done();
	}

	/**
	 * Compare discovered include paths and symbol definitions with the ones from scanInfo.
	 * 
	 * @param scanInfo
	 * @param projectName
	 * @return
	 */
	private boolean scannerConfigNeedsUpdate(IDiscoveredPathInfo discPathInfo) {
		List includes = (List) discoveredIncludes.get(discPathInfo.getProject().getName());
		List symbols = (List) discoveredSymbols.get(discPathInfo.getProject().getName());
		
		boolean addedIncludes = includePathsNeedUpdate(discPathInfo, includes);
		boolean addedSymbols = definedSymbolsNeedUpdate(discPathInfo, symbols);
		
		return (addedIncludes | addedSymbols);
	}

	/**
	 * Compare include paths with already discovered.
	 * 
	 * @param discPathInfo
	 * @param projectName
	 * @param includes
	 * @return
	 */
	private boolean includePathsNeedUpdate(IDiscoveredPathInfo discPathInfo, List includes) {
		boolean addedIncludes = false;
		String projectName = discPathInfo.getProject().getName();
		if (includes != null) {
			// Step 1. Add discovered scanner config to the existing discovered scanner config 
			// add the includes from the latest discovery
			List sumIncludes = (List) sumDiscoveredIncludes.get(projectName);
			if (sumIncludes == null) {
				sumIncludes = new ArrayList(includes);
				sumDiscoveredIncludes.put(projectName, sumIncludes);
				addedIncludes = true;
			}
			else {
				addedIncludes = addItemsWithOrder(includes, sumIncludes, true);
			}
			// try to translate cygpaths to absolute paths
			List finalSumIncludes = translateIncludePaths(sumIncludes);
			
			// Step 2. Get project's scanner config
			LinkedHashMap persistedIncludes = discPathInfo.getIncludeMap();
	
			// Step 3. Merge scanner config from steps 1 and 2
			// order is important, use list to preserve it
			ArrayList persistedKeyList = new ArrayList(persistedIncludes.keySet());
			addedIncludes = addItemsWithOrder(finalSumIncludes, persistedKeyList, true);
			
			LinkedHashMap newPersistedIncludes;
			if (addedIncludes) {
				newPersistedIncludes = new LinkedHashMap(persistedKeyList.size());
				for (Iterator i = persistedKeyList.iterator(); i.hasNext(); ) {
					String include = (String) i.next();
					if (persistedIncludes.containsKey(include)) {
						newPersistedIncludes.put(include, persistedIncludes.get(include));
					}
					else {
						newPersistedIncludes.put(include, 
								((new Path(include)).toFile().exists()) ? Boolean.FALSE : Boolean.TRUE);
					}
				}
			}
			else {
				newPersistedIncludes = persistedIncludes;
			}
			
			// Step 4. Set resulting scanner config
			discPathInfo.setIncludeMap(newPersistedIncludes);
			
			// Step 5. Invalidate discovered include paths
			discoveredIncludes.put(projectName, null);
		}
		return addedIncludes;
	}

	/**
	 * Compare symbol definitions with already discovered.
	 * 
	 * @param discPathInfo
	 * @param projectName
	 * @param symbols
	 * @return
	 */
	private boolean definedSymbolsNeedUpdate(IDiscoveredPathInfo discPathInfo, List symbols) {
		boolean addedSymbols = false;
		String projectName = discPathInfo.getProject().getName();
		if (symbols != null) {
			// Step 1. Add discovered scanner config to the existing discovered scanner config 
			// add the symbols from the latest discovery
			Map sumSymbols = (Map) sumDiscoveredSymbols.get(projectName);
			if (sumSymbols == null) {
				sumSymbols = new LinkedHashMap();
				sumDiscoveredSymbols.put(projectName, sumSymbols);
			}
			addedSymbols = ScannerConfigUtil.scAddSymbolsList2SymbolEntryMap(sumSymbols, symbols, false);
			
			// Step 2. Get project's scanner config
			LinkedHashMap persistedSymbols = discPathInfo.getSymbolMap();
			
			// Step 3. Merge scanner config from steps 1 and 2
			LinkedHashMap candidateSymbols = new LinkedHashMap(persistedSymbols);
			addedSymbols |= ScannerConfigUtil.scAddSymbolEntryMap2SymbolEntryMap(candidateSymbols, sumSymbols);
			
			// Step 4. Set resulting scanner config
			discPathInfo.setSymbolMap(candidateSymbols);
			
			// Step 5. Invalidate discovered symbol definitions
			discoveredSymbols.put(projectName, null);
		}
		return addedSymbols;
	}

	/**
	 * @param sumIncludes
	 * @return
	 */
	private List translateIncludePaths(List sumIncludes) {
		List translatedIncludePaths = new ArrayList();
		for (Iterator i = sumIncludes.iterator(); i.hasNext(); ) {
			String includePath = (String) i.next();
			IPath realPath = new Path(includePath);
			if (!realPath.toFile().exists()) {
				String translatedPath = includePath;
				if (Platform.getOS().equals(Platform.OS_WIN32)) {
					translatedPath = new CygpathTranslator(currentProject, includePath).run();
				}
				if (translatedPath != null) {
					if (!translatedPath.equals(includePath)) {
						// Check if the translated path exists
						IPath transPath = new Path(translatedPath);
						if (transPath.toFile().exists()) {
							translatedIncludePaths.add(translatedPath);
						}
						else {
							// TODO VMIR for now add even if it does not exist
							translatedIncludePaths.add(translatedPath);
						}
					}
					else {
						// TODO VMIR for now add even if it does not exist
						translatedIncludePaths.add(translatedPath);
					}
				}
				else {
					TraceUtil.outputError("CygpathTranslator unable to translate path: ",//$NON-NLS-1$
							includePath);
				}
			}
			else {
				translatedIncludePaths.add(includePath);
			}
		}
		return translatedIncludePaths;
	}

	/**
	 * Call ESI provider to get scanner info
	 *
	 * @param project
	 * @param tso
	 * @param monitor
	 */
	private void getProviderScannerInfo(final IProject project, 
										final List tso, 
										final IProgressMonitor monitor) {
		// get IScannerConfigBuilderInfo
		IScannerConfigBuilderInfo info;
		try {
			info = MakeCorePlugin.createScannerConfigBuildInfo(
					project, ScannerConfigBuilder.BUILDER_ID);
		}
		catch (CoreException e) {
			info = MakeCorePlugin.createScannerConfigBuildInfo(
					MakeCorePlugin.getDefault().getPluginPreferences(), 
					ScannerConfigBuilder.BUILDER_ID, false);
		}
		if (info.isESIProviderCommandEnabled()) {
			final IScannerConfigBuilderInfo buildInfo = info;
			final IExternalScannerInfoProvider esiProvider = MakeCorePlugin.getDefault().
				getExternalScannerInfoProvider(MakeCorePlugin.DEFAULT_EXTERNAL_SI_PROVIDER_ID);
			if (esiProvider != null) {
				ISafeRunnable runnable = new ISafeRunnable() {
					public void run() {
						esiProvider.invokeProvider(monitor, project, buildInfo, tso, ScannerInfoCollector.getInstance());
					}
		
					public void handleException(Throwable exception) {
						MakeCorePlugin.log(exception);
					}
				};
				Platform.run(runnable);
			}
		}
	}

	/**
	 * @param project
	 * @param monitor
	 */
	public synchronized void updateScannerConfiguration(IProject project, IProgressMonitor monitor) throws CoreException {
		currentProject = project;
		String projectName = project.getName();
		// check TSO for the project
		monitor.beginTask("", 100); //$NON-NLS-1$
		getProviderScannerInfo(project, (List) discoveredTSO.get(projectName), new SubProgressMonitor(monitor, 60));
		updateScannerConfig(project, new SubProgressMonitor(monitor, 40));

		// delete discovered scanner config
		discoveredIncludes.put(projectName, null);
		discoveredSymbols.put(projectName, null);
		discoveredTSO.put(projectName, null);
	}

	/**
	 * Delete all discovered paths for the project
	 * 
	 * @param project
	 */
	public void deleteAllPaths(IProject project) {
		if (project != null) {
			sumDiscoveredIncludes.put(project.getName(), null);
		}
	}

	/**
	 * Delete all discovered symbols for the project
	 * 
	 * @param project
	 */
	public void deleteAllSymbols(IProject project) {
		if (project != null) {
			sumDiscoveredSymbols.put(project.getName(), null);
		}
	}

	/**
	 * Delete a specific include path
	 * 
	 * @param project
	 * @param path
	 */
	public void deletePath(IProject project, String path) {
		if (project != null) {
			List sumIncludes = (List) sumDiscoveredIncludes.get(project.getName());
			if (sumIncludes != null) {
				sumIncludes.remove(path);
			}
		}
	}

	/**
	 * Delete a specific symbol definition
	 * 
	 * @param project
	 * @param path
	 */
	public void deleteSymbol(IProject project, String symbol) {
		if (project != null) {
			Map sumSymbols = (Map) sumDiscoveredSymbols.get(project.getName());
			if (sumSymbols != null) {
				// remove it from the Map of SymbolEntries 
				ScannerConfigUtil.removeSymbolEntryValue(symbol, sumSymbols);
			}
		}
	}
}

Back to the top