Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 85a54be9b28ce3acdc9ad990bce6d21f6d6c3756 (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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*******************************************************************************
 * Copyright (c) 2007, 2009 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 * 	  IBM Corporation
 *******************************************************************************/ 
package org.eclipse.cdt.internal.core.indexer;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.internal.core.index.IWritableIndex;
import org.eclipse.cdt.internal.core.pdom.IndexerProgress;
import org.eclipse.cdt.internal.core.pdom.PDOMWriter;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * The base class for stand-alone index population tools.
 * 
 * <p>
 * This class is not thread safe.
 * </p>
 */
public abstract class StandaloneIndexer {
	
	/**
	 * Parser should not skip any references.
	 */
	public static final int SKIP_NO_REFERENCES= PDOMWriter.SKIP_NO_REFERENCES;
	
	/**
	 * Parser to skip all references.
	 */
	public static final int SKIP_ALL_REFERENCES= PDOMWriter.SKIP_ALL_REFERENCES; 
	
	/**
	 * Parser to skip implicit references.
	 */
	public static final int SKIP_IMPLICIT_REFERENCES= PDOMWriter.SKIP_IMPLICIT_REFERENCES;

	/**
	 * Parser to skip type references.
	 */
	public static final int SKIP_TYPE_REFERENCES= PDOMWriter.SKIP_TYPE_REFERENCES;
	
	/**
	 * Parser to skip type references.
	 */
	public static final int SKIP_MACRO_REFERENCES= PDOMWriter.SKIP_MACRO_REFERENCES;
	
	/**
	 * Constant for indicating to update all translation units.
	 */
	public final static int UPDATE_ALL= 0x1;

	/**
	 * Constant for indicating to update translation units if their timestamp
	 * has changed.
	 */
	public final static int UPDATE_CHECK_TIMESTAMPS= 0x2;
		
	/**
	 * Empty list.
	 */
	protected static final List<String> NO_TUS = Collections.emptyList();
	
	
	/**
	 * The IWritableIndex that stores all bindings and names.
	 */
	protected IWritableIndex fIndex;
	
	/**
	 * A flag that indiciates if all files (sources without config, headers not included)
	 * should be parsed.
	 */
	protected boolean fIndexAllFiles;
	
	/**
	 * Collection of valid file extensions for C/C++ source.
	 */
	protected Set<String> fValidSourceUnitNames;
	
	/**
	 * The IScannerInfo that provides include paths and defined symbols.
	 * Either a single scanner info or a IStandaloneScannerInfoProvider must
	 * be provided, but not both. If a single IScannerInfo object is provided
	 * it will always be used. Otherwise the provider will be used.
	 */
	@Deprecated
	protected IScannerInfo fScanner;
	
	/**
	 * Creates IScannerInfo objects from file paths, allows there
	 * to be separate scanner infos for specific files and folders.
	 */
	protected IStandaloneScannerInfoProvider fScannerInfoProvider;
	
	/**
	 * The ILanguageMapper that determines the ILanguage for a file.
	 */
	protected ILanguageMapper fMapper;
	
	/**
	 * The logger during parsing.
	 */
	protected IParserLogService fLog;
	
	/**
	 * A flag that indicates if all activities during indexing should be shown.
	 */
	protected boolean fShowActivity;
	
	/**
	 * A flag that indicates if any problems encountered during indexing.
	 * should be shown.
	 */
	protected boolean fShowProblems;
	
	/**
	 * A flag that indicates if statistics should be gathered during indexing.
	 */
	protected boolean fTraceStatistics;
	
	/**
	 * The type of references the parser should skip.
	 */
	protected int fSkipReferences = SKIP_NO_REFERENCES;
	
	/**
	 * The exclusion filter that skips over files that should not be indexed.
	 */
	protected FilenameFilter fExclusionFilter;
	
	/**
	 * Files to parse up front.
	 */
	protected String[] fFilesToParseUpFront = new String[0];
	
	protected int fUpdateOptions = UPDATE_ALL;
	
	private IndexerProgress fProgress = null;
	private volatile StandaloneIndexerTask fDelegate;
	
	private static FilenameFilter DEFAULT_FILTER = new FilenameFilter() {
		public boolean accept(File dir, String name) {
			return true;
		}
	};
	
	/**
	 * @deprecated Its better to provide a scanner info provider instead.
	 */
	@Deprecated
	public StandaloneIndexer(IWritableIndex index, boolean indexAllFiles,  
			                 ILanguageMapper mapper, IParserLogService log, IScannerInfo scanner) {
		fIndex = index;
		fIndexAllFiles = indexAllFiles;
		fMapper = mapper;
		fLog = log;	
		fScanner = scanner;
		fScannerInfoProvider = null;
	}
	
	
	public StandaloneIndexer(IWritableIndex index, boolean indexAllFiles,  
            ILanguageMapper mapper, IParserLogService log, IStandaloneScannerInfoProvider scannerProvider) {
		fIndex = index;
		fIndexAllFiles = indexAllFiles;
		fMapper = mapper;
		fLog = log;	
		fScanner = null;
		fScannerInfoProvider = scannerProvider;
	}
	

	public void setScannerInfoProvider(IStandaloneScannerInfoProvider provider) {
		fScannerInfoProvider = provider;
		fScanner = null;
	}
	
	/**
	 * Returns the index.
	 * @return the IWritable index the indexer is writing to
	 */
	public IWritableIndex getIndex() {
		return fIndex;
	}
	
	/**
	 * Returns true if all files (sources without config, headers not included)
	 * should be parsed.  Otherwise, this method returns false.
	 */
	public boolean getIndexAllFiles() {
		return fIndexAllFiles;
	}
	
	/**
	 * If true then all files will be indexed.
	 */
	public void setIndexAllFiles(boolean indexAllFiles) {
		fIndexAllFiles = indexAllFiles;
	}
	
	/**
	 * Returns the collection of valid file extensions for C/C++ source.
	 */
	public Set<String> getValidSourceUnitNames() {
		return fValidSourceUnitNames;
	}
	
	/**
	 * Sets the collection of valid file extensions for C/C++ source.
	 */
	public void setValidSourceUnitNames(Set<String> validSourceUnitNames) {
		fValidSourceUnitNames = validSourceUnitNames;
	}
	
	/**
	 * Returns the IScannerInfo that provides include paths and defined symbols.
	 * @deprecated Should probably be using a IStandaloneScannerInfoProvider instead and
	 * calling getScannerInfo(String).
	 */
	@Deprecated
	public IScannerInfo getScannerInfo() {
		return fScanner;
	}
	
	
	/**
	 * Returns the IScannerInfo for the given path.
	 * If the current instance was created with an IScannerInfo instead of
	 * an IScannerInfoProvider then the path will be ignored and
	 * that IScannerInfo will always be returned.
	 */
	public IScannerInfo getScannerInfo(String path) {
		if(fScanner != null)
			return fScanner;
		
		return fScannerInfoProvider.getScannerInformation(path);
	}


	/**
	 * Returns the IStandaloneScannerInfoProvider or null if one was not provided.
	 */
	public IStandaloneScannerInfoProvider getScannerInfoProvider() {
		return fScannerInfoProvider;
	}
	
	/**
	 * Returns the ILanguageMapper that determines the ILanguage for a file.
	 */
	public ILanguageMapper getLanguageMapper() {
		return fMapper;
	}
	
	
	public void setLanguageMapper(ILanguageMapper mapper) {
		fMapper = mapper;
	}
	
	/**
	 * Returns the logger.
	 */
	public IParserLogService getParserLog() {
		return fLog;
	}
	
	/**
	 * Returns true if indexing activities should be shown.
	 * Otherwise, this method returns false.
	 */
	public boolean getShowActivity() {
		return fShowActivity;
	}
	
	/**
	 * Tells indexer if indexing activities should be shown.
	 */
	public void setShowActivity(boolean showActivity) {
		fShowActivity = showActivity;
	}
	
	/**
	 * Returns true if problems during indexing should be shown.
	 * Otherwise, this method returns false.
	 */
	public boolean getShowProblems() {
		return fShowProblems;
	}
	
	/**
	 * Tells indexer if problems during indexing should be shown.
	 */
	public void setShowProblems(boolean showProblems) {
		fShowProblems = showProblems;
	}
	
	/**
	 * Returns true if statistics should be gathered during indexing.
	 * Otherwise, this method returns false..
	 */
	public boolean getTraceStatistics() {
		return fTraceStatistics;
	}
	
	/**
	 * Tells indexer if statistics should be gathered during indexing.
	 */
	public void setTraceStatistics(boolean traceStatistics) {
		fTraceStatistics = traceStatistics;
	}
	
	private IndexerProgress createProgress() {
		IndexerProgress progress= new IndexerProgress();
		progress.fTimeEstimate= 1000;
		return progress;
	}
	
	private void clearIndex() throws CoreException, InterruptedException {
		IWritableIndex index= getIndex();
		// First clear the pdom
		index.acquireWriteLock(0);
		try {
			index.clear();
		}
		finally {
			index.releaseWriteLock(0);
		}
	}
	
	/**
	 * Returns the progress information.
	 */
	public synchronized IndexerProgress getProgressInformation() {
		return fDelegate != null ? fDelegate.getProgressInformation() : fProgress;
	}
	
	/**
	 * Returns the update options specified.
	 */
	public int getUpdateOptions() {
		return fUpdateOptions;
	}
	
	/**
	 * Specifies the update options, whether all translation units should be updated or only the ones
	 * with timestamp changes.
	 * @param options
	 */
	public void setUpdateOptions (int options) {
		fUpdateOptions = options;
	}
		
	/**
	 * Clears the index and rebuild
	 * @param tus - directories/files to be added to index
	 * @param monitor
	 * @throws IOException
	 */
	public void rebuild(List<String> tus, IProgressMonitor monitor) throws IOException {
		fProgress = createProgress();
		
		try {
			clearIndex();
			fDelegate= createTask(getFilesAdded(tus), NO_TUS, NO_TUS);
			fDelegate.setUpdateFlags(fUpdateOptions);
			fDelegate.setParseUpFront();
			
			if (fDelegate != null) {
				fDelegate.run(monitor);
			}
		} catch (CoreException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
		}
	}
	
	/**
	 * Updates the index with changes.
	 * @param added - directories/files to be added to the index
	 * @param changed - files that have been changed
	 * @param removed - files to be removed from the index
	 * @param monitor
	 * @throws IOException
	 */
	public void handleDelta(List<String> added, List<String> changed, List<String> removed, IProgressMonitor monitor) throws IOException {
		fProgress= new IndexerProgress();
				
		fDelegate= createTask(getFilesAdded(added), changed, removed);
		if (fDelegate != null) {
			try {
				fDelegate.setUpdateFlags(fUpdateOptions);
				fDelegate.run(monitor);
			} catch (InterruptedException e) {
			}
		}
		
	}
	
	/**
	 * Returns files that are being added to the index, skipping over files that 
	 * should not be excluded.
	 * @param tus
	 * @return
	 */
	private List<String> getFilesAdded(List<String> tus) {
		List<String> added = new ArrayList<String>();
		
		FilenameFilter filter = getExclusionFilter();
		if (filter == null) {
			filter = DEFAULT_FILTER;
		}
		
		Iterator<String> iter = tus.iterator();
		while (iter.hasNext()) {
			String path = iter.next();
			File file = new File(path);
			if (file.isDirectory()) {
				String[] files = file.list(filter);
				for (String file2 : files) {
					added.add(file2);
				}
			}
			else {				
				if (filter.accept(file.getParentFile(), file.getName())) {
					added.add(path);
				}
			}
		}
		return added;
	}
	
	/**
	 * Creates a delegate standalone indexing task
	 */
	protected abstract StandaloneIndexerTask createTask(List<String> added, List<String> changed, List<String> removed);

	/**
	 * Return the type of references the parser should skip.
	 */
	public int getSkipReferences() {
		return fSkipReferences;
	}
	
	/**
	 * Sets the type of references the parser should skip.
	 * @param skipReferences
	 */
	public void setSkipReferences(int skipReferences) {
		fSkipReferences = skipReferences;
	}

	/**
	 * Returns an array of files that should be parsed up front.
	 */
	public String[] getFilesToParseUpFront() {
		return fFilesToParseUpFront;
	}
	
	/**
	 * Sets an array of files that should be parsed up front.
	 * @param filesToParseUpFront
	 */
	public void setFilesToParseUpFront(String[] filesToParseUpFront) {
		fFilesToParseUpFront = filesToParseUpFront;
	}
	
	/**
	 * Returns the exclusion filter for this indexer.
	 */
	public FilenameFilter getExclusionFilter() {
		return fExclusionFilter;
	}
	
	/**
	 * Sets the exclusion filter that tells the indexer to skip over 
	 * files that should not be indexed.
	 * @param exclusionFilter
	 */
	public void setExclusionFilter(FilenameFilter exclusionFilter) {
		fExclusionFilter = exclusionFilter;
	}
}

Back to the top