Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb2186e42c1f8b4274c1e6051a66fa4958c2f6a0 (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
/*******************************************************************************
 * Copyright (c) 2007, 2012 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *	   Markus Schorn - initial API and implementation
 *	   Sergey Prigogin (Google)
******************************************************************************/ 
package org.eclipse.cdt.internal.core.pdom.indexer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMIndexer;
import org.eclipse.cdt.core.dom.IPDOMIndexerTask;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.core.parser.scanner.CPreprocessor;
import org.eclipse.cdt.internal.core.parser.scanner.IncludeSearchPath;
import org.eclipse.cdt.internal.core.parser.scanner.IncludeSearchPathElement;
import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility;
import org.eclipse.cdt.internal.core.pdom.IndexerProgress;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.osgi.util.NLS;

/**
 * A task for updating an index, suitable for all indexers.
 */
public class PDOMUpdateTask implements IPDOMIndexerTask {
	private static final ITranslationUnit[] NO_TUS = {};
	
	private final IPDOMIndexer fIndexer;
	private final int fUpdateOptions;
	private final IndexerProgress fProgress;
	private volatile IPDOMIndexerTask fDelegate;
	private ArrayList<ICElement> fFilesAndFolders;

	public PDOMUpdateTask(IPDOMIndexer indexer, int updateOptions) {
		fIndexer= indexer;
		fUpdateOptions= updateOptions;
		fProgress= createProgress();
	}

	private IndexerProgress createProgress() {
		IndexerProgress progress= new IndexerProgress();
		progress.fTimeEstimate= 1000;
		return progress;
	}

	@Override
	public IPDOMIndexer getIndexer() {
		return fIndexer;
	}

	@Override
	public void run(IProgressMonitor monitor) throws InterruptedException {
		monitor.subTask(NLS.bind(Messages.PDOMIndexerTask_collectingFilesTask, 
				fIndexer.getProject().getElementName()));

		ICProject project= fIndexer.getProject();
		if (project.getProject().isOpen()) {
			try {
				if (!IPDOMManager.ID_NO_INDEXER.equals(fIndexer.getID())) {
					createDelegate(project, monitor);
				}
			} catch (CoreException e) {
				CCorePlugin.log(e);
			} 
		}
		
		if (fDelegate != null) {
			fDelegate.run(monitor);
		}
	}
	
	private void createDelegate(ICProject project, IProgressMonitor monitor)
			throws CoreException, InterruptedException {
		HashSet<ITranslationUnit> set= new HashSet<ITranslationUnit>();
		if ((fUpdateOptions & (IIndexManager.UPDATE_ALL | IIndexManager.UPDATE_CHECK_TIMESTAMPS)) != 0) {
			TranslationUnitCollector collector= new TranslationUnitCollector(set, set, monitor);
			boolean haveProject= false;
			if (fFilesAndFolders == null) {
				project.accept(collector);
			} else {
				for (ICElement elem : fFilesAndFolders) {
					if (elem.getElementType() == ICElement.C_PROJECT) {
						haveProject= true;
					}
					elem.accept(collector);
				}
			}
			if (haveProject && (fUpdateOptions & IIndexManager.UPDATE_EXTERNAL_FILES_FOR_PROJECT) != 0) {
				final String projectPrefix= project.getProject().getFullPath().toString() + IPath.SEPARATOR;
				IIndex index= CCorePlugin.getIndexManager().getIndex(project);
				index.acquireReadLock();
				try {
					IIndexFile[] files= index.getAllFiles();
					for (IIndexFile indexFile : files) {
						IIndexFileLocation floc= indexFile.getLocation();
						final String fullPath = floc.getFullPath();
						if (fullPath == null || !fullPath.startsWith(projectPrefix)) {
							ITranslationUnit tu = getTranslationUnit(floc, project);
							if (tu != null) {
								set.add(tu);
							}
						}
					}
				} finally {
					index.releaseReadLock();
				}
			}
		}

		if ((fUpdateOptions & IIndexManager.UPDATE_UNRESOLVED_INCLUDES) != 0) {
			IIndex index= CCorePlugin.getIndexManager().getIndex(project);
			index.acquireReadLock();
			try {
				// Files that were indexed with I/O errors.
				IIndexFile[] files= index.getDefectiveFiles();
				for (IIndexFile file : files) {
					ITranslationUnit tu = getTranslationUnit(file.getLocation(), project);
					if (tu != null) {
						set.add(tu);
					}
				}

				// Files with unresolved includes.
				files= index.getFilesWithUnresolvedIncludes();
				if (files.length > 0) {
					ProjectIndexerInputAdapter inputAdapter = new ProjectIndexerInputAdapter(project, true);
			        ProjectIndexerIncludeResolutionHeuristics includeResolutionHeuristics =
			        		new ProjectIndexerIncludeResolutionHeuristics(project.getProject(), inputAdapter);
			        for (IIndexFile file : files) {
						ITranslationUnit tu = getTranslationUnit(file.getLocation(), project);
						if (tu != null) {
							IScannerInfo scannerInfo = tu.getScannerInfo(true);
							if (canResolveUnresolvedInclude(file, scannerInfo, includeResolutionHeuristics)) {
								set.add(tu);
							}
						}
					}
				}
			} finally {
				index.releaseReadLock();
			}
		}

		ITranslationUnit[] tus= set.toArray(new ITranslationUnit[set.size()]);
		IPDOMIndexerTask delegate= fIndexer.createTask(NO_TUS, tus, NO_TUS);
		if (delegate instanceof PDOMIndexerTask) {
			final PDOMIndexerTask task = (PDOMIndexerTask) delegate;
			task.setUpdateFlags(fUpdateOptions);
		}
		setDelegate(delegate);
	}

	private ITranslationUnit getTranslationUnit(IIndexFileLocation location, ICProject project) {
		IPath path= IndexLocationFactory.getAbsolutePath(location);
		if (path == null)
			return null;
		ITranslationUnit tu= CoreModel.getDefault().createTranslationUnitFrom(project, path);
		if (tu != null) {
			final String fullPath = location.getFullPath();
			if (fullPath != null) {
				if (tu instanceof ExternalTranslationUnit) {
					IResource file= ResourcesPlugin.getWorkspace().getRoot().findMember(fullPath);
					if (file instanceof IFile) {
						((ExternalTranslationUnit) tu).setResource((IFile) file);
					}
				}
			}
		}
		return tu;
	}

	private static boolean canResolveUnresolvedInclude(IIndexFile file, IScannerInfo scannerInfo,
			ProjectIndexerIncludeResolutionHeuristics includeResolutionHeuristics) {
		try {
			String filePath = IndexLocationFactory.getAbsolutePath(file.getLocation()).toOSString();
			long fileReadTime = file.getSourceReadTime();
			IncludeSearchPath includeSearchPath =
					CPreprocessor.configureIncludeSearchPath(new File(filePath).getParentFile(), scannerInfo);
			for (IIndexInclude include : file.getIncludes()) {
				if (!include.isResolved() && include.isActive() &&
						canResolveInclude(include, filePath, fileReadTime, includeSearchPath, includeResolutionHeuristics)) {
					return true;
				}
			}
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return false;
	}

	private static boolean canResolveInclude(IIndexInclude include, String currentFile, long timestamp,
			IncludeSearchPath includeSearchPath,
			ProjectIndexerIncludeResolutionHeuristics includeResolutionHeuristics) throws CoreException {
		String includeName = include.getFullName();
        String filePath = CPreprocessor.getAbsoluteInclusionPath(includeName, currentFile);
        if (filePath != null && fileIsNotOlderThanTimestamp(filePath, timestamp)) {
        	return true;
        }

        if (currentFile != null && !include.isSystemInclude() && !includeSearchPath.isInhibitUseOfCurrentFileDirectory()) {
            // Check to see if we find a match in the current directory
    		final File currentDir= new File(currentFile).getParentFile();
    		if (currentDir != null) {
        		filePath = ScannerUtility.createReconciledPath(currentDir.getAbsolutePath(), includeName);
        		if (!filePath.equals(currentFile) && fileIsNotOlderThanTimestamp(filePath, timestamp)) {
        			return true;
        		}
    		}
        }

        // Unlike CPreprocessor.findInclusion we are searching include path from the beginning.
        // This simplification may produce false positives, but by checking file modification time
        // we guarantee that any false positive won't be produced again when this task runs
        // next time.
        for (IncludeSearchPathElement path : includeSearchPath.getElements()) {
        	if (!include.isSystemInclude() || !path.isForQuoteIncludesOnly()) {
        		filePath = path.getLocation(includeName);
        		if (filePath != null && fileIsNotOlderThanTimestamp(filePath, timestamp)) {
        			return true;
        		}
        	}
        }
        if (includeResolutionHeuristics != null) {
        	filePath= includeResolutionHeuristics.findInclusion(includeName, currentFile);
    		if (filePath != null && fileIsNotOlderThanTimestamp(filePath, timestamp)) {
    			return true;
    		}
        }

        return false;
	}

	/**
	 * Returns true if the file exists and is not older than the given timestamp.
	 */
	private static boolean fileIsNotOlderThanTimestamp(String filename, long timestamp) {
		// We are subtracting 1 second from the timestamp to account for limited precision
		// of File.lastModified() method and possible skew between clocks on a multi-CPU
		// system. This may produce false positives, but they are pretty harmless.
		return new File(filename).lastModified() >= timestamp - 1000;
	}

	private synchronized void setDelegate(IPDOMIndexerTask delegate) {
		fDelegate= delegate;
	}

	@Override
	public synchronized IndexerProgress getProgressInformation() {
		return fDelegate != null ? fDelegate.getProgressInformation() : fProgress;
	}

	@Override
	public synchronized boolean acceptUrgentTask(IPDOMIndexerTask task) {
		return fDelegate != null && fDelegate.acceptUrgentTask(task);
	}

	public void setTranslationUnitSelection(List<? extends ICElement> filesAndFolders) {
		fFilesAndFolders= new ArrayList<>(filesAndFolders);
	}

	@Override
	public void cancel() {
		if (fDelegate != null)
			fDelegate.cancel();
	}
}

Back to the top