1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-16 20:55:44 +02:00

Bug 368045: Choose correct language for indexing files opened in editor.

This commit is contained in:
Markus Schorn 2012-01-11 12:19:38 +01:00
parent c55ca76edc
commit b68ecdc716
6 changed files with 90 additions and 42 deletions

View file

@ -22,6 +22,7 @@ import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IScannerInfo; import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.internal.core.index.IndexFileLocation; import org.eclipse.cdt.internal.core.index.IndexFileLocation;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil; import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.pdom.AbstractIndexerTask.UnusedHeaderStrategy;
import org.eclipse.cdt.internal.core.pdom.IndexerInputAdapter; import org.eclipse.cdt.internal.core.pdom.IndexerInputAdapter;
import org.eclipse.cdt.internal.core.pdom.indexer.FileExistsCache; import org.eclipse.cdt.internal.core.pdom.indexer.FileExistsCache;
import org.eclipse.cdt.utils.UNCPathConverter; import org.eclipse.cdt.utils.UNCPathConverter;
@ -178,7 +179,7 @@ public class StandaloneIndexerInputAdapter extends IndexerInputAdapter {
} }
@Override @Override
public AbstractLanguage[] getLanguages(Object tu, boolean bothForHeaders) { public AbstractLanguage[] getLanguages(Object tu, UnusedHeaderStrategy strat) {
ILanguage language = fIndexer.getLanguageMapper().getLanguage(tu.toString()); ILanguage language = fIndexer.getLanguageMapper().getLanguage(tu.toString());
if (language instanceof AbstractLanguage) { if (language instanceof AbstractLanguage) {
return new AbstractLanguage[] {(AbstractLanguage) language}; return new AbstractLanguage[] {(AbstractLanguage) language};

View file

@ -71,12 +71,12 @@ import org.eclipse.osgi.util.NLS;
* @since 5.0 * @since 5.0
*/ */
public abstract class AbstractIndexerTask extends PDOMWriter { public abstract class AbstractIndexerTask extends PDOMWriter {
protected static enum UnusedHeaderStrategy { public static enum UnusedHeaderStrategy {
skip, useDefaultLanguage, useAlternateLanguage, useBoth skip, useC, useCPP, useDefaultLanguage, useBoth
} }
private static final int MAX_ERRORS = 500; private static final int MAX_ERRORS = 500;
private static enum UpdateKind {REQUIRED_SOURCE, REQUIRED_HEADER, OTHER_HEADER} private static enum UpdateKind {REQUIRED_SOURCE, REQUIRED_HEADER, ONE_LINKAGE_HEADER, OTHER_HEADER}
private static class LinkageTask { private static class LinkageTask {
final int fLinkageID; final int fLinkageID;
private final Map<IIndexFileLocation, LocationTask> fLocationTasks; private final Map<IIndexFileLocation, LocationTask> fLocationTasks;
@ -87,13 +87,19 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
} }
boolean requestUpdate(IIndexFileLocation ifl, IIndexFragmentFile ifile, Object tu, boolean requestUpdate(IIndexFileLocation ifl, IIndexFragmentFile ifile, Object tu,
UpdateKind kind) { UpdateKind kind, Map<IIndexFileLocation, LocationTask> oneLinkageTasks) {
LocationTask locTask= fLocationTasks.get(ifl); LocationTask locTask= fLocationTasks.get(ifl);
if (locTask == null) { if (locTask == null) {
locTask= new LocationTask(); locTask= new LocationTask();
fLocationTasks.put(ifl, locTask); fLocationTasks.put(ifl, locTask);
} }
return locTask.requestUpdate(ifile, tu, kind); boolean result = locTask.requestUpdate(ifile, tu, kind);
// Store one-linkage tasks.
if (kind == UpdateKind.ONE_LINKAGE_HEADER && locTask.fVersionTasks.isEmpty())
oneLinkageTasks.put(ifl, locTask);
return result;
} }
LocationTask find(IIndexFileLocation ifl) { LocationTask find(IIndexFileLocation ifl) {
@ -282,6 +288,7 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
private List<LinkageTask> fRequestsPerLinkage= new ArrayList<LinkageTask>(); private List<LinkageTask> fRequestsPerLinkage= new ArrayList<LinkageTask>();
private Map<IIndexFile, IndexFileContent> fIndexContentCache= new LRUCache<IIndexFile, IndexFileContent>(500); private Map<IIndexFile, IndexFileContent> fIndexContentCache= new LRUCache<IIndexFile, IndexFileContent>(500);
private Map<IIndexFileLocation, IIndexFile[]> fIndexFilesCache= new LRUCache<IIndexFileLocation, IIndexFile[]>(5000); private Map<IIndexFileLocation, IIndexFile[]> fIndexFilesCache= new LRUCache<IIndexFileLocation, IIndexFile[]>(5000);
private Map<IIndexFileLocation, LocationTask> fOneLinkageTasks= new HashMap<IIndexFileLocation, AbstractIndexerTask.LocationTask>();
private Object[] fFilesToUpdate; private Object[] fFilesToUpdate;
private List<Object> fFilesToRemove = new ArrayList<Object>(); private List<Object> fFilesToRemove = new ArrayList<Object>();
@ -386,6 +393,8 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
* @return array of linkage IDs that shall be parsed * @return array of linkage IDs that shall be parsed
*/ */
protected int[] getLinkagesToParse() { protected int[] getLinkagesToParse() {
if (fIndexHeadersWithoutContext == UnusedHeaderStrategy.useCPP)
return PDOMManager.IDS_FOR_LINKAGES_TO_INDEX_C_FIRST;
return PDOMManager.IDS_FOR_LINKAGES_TO_INDEX; return PDOMManager.IDS_FOR_LINKAGES_TO_INDEX;
} }
@ -494,6 +503,11 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
final List<IIndexFileLocation> filesForLinkage = files.get(linkageID); final List<IIndexFileLocation> filesForLinkage = files.get(linkageID);
if (filesForLinkage != null) { if (filesForLinkage != null) {
parseLinkage(linkageID, filesForLinkage, monitor); parseLinkage(linkageID, filesForLinkage, monitor);
for (Iterator<LocationTask> it = fOneLinkageTasks.values().iterator(); it.hasNext();) {
LocationTask task = it.next();
if (task.isCompleted())
it.remove();
}
fIndexContentCache.clear(); fIndexContentCache.clear();
fIndexFilesCache.clear(); fIndexFilesCache.clear();
} }
@ -569,7 +583,7 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
final boolean forceAll= (fUpdateFlags & IIndexManager.UPDATE_ALL) != 0; final boolean forceAll= (fUpdateFlags & IIndexManager.UPDATE_ALL) != 0;
final boolean checkTimestamps= (fUpdateFlags & IIndexManager.UPDATE_CHECK_TIMESTAMPS) != 0; final boolean checkTimestamps= (fUpdateFlags & IIndexManager.UPDATE_CHECK_TIMESTAMPS) != 0;
final boolean checkFileContentsHash = (fUpdateFlags & IIndexManager.UPDATE_CHECK_CONTENTS_HASH) != 0; final boolean checkFileContentsHash = (fUpdateFlags & IIndexManager.UPDATE_CHECK_CONTENTS_HASH) != 0;
final boolean both = fIndexHeadersWithoutContext == UnusedHeaderStrategy.useBoth;
int count= 0; int count= 0;
int forceFirst= fForceNumberFiles; int forceFirst= fForceNumberFiles;
BitSet linkages= new BitSet(); BitSet linkages= new BitSet();
@ -585,28 +599,33 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
final IIndexFragmentFile[] indexFiles= fIndex.getWritableFiles(ifl); final IIndexFragmentFile[] indexFiles= fIndex.getWritableFiles(ifl);
final boolean isSourceUnit= fResolver.isSourceUnit(tu); final boolean isSourceUnit= fResolver.isSourceUnit(tu);
linkages.clear(); linkages.clear();
if (isRequiredInIndex(tu, ifl, isSourceUnit)) { final boolean regularContent = isRequiredInIndex(tu, ifl, isSourceUnit);
final boolean indexedUnconditionally = fResolver.isIndexedUnconditionally(ifl);
if (regularContent || indexedUnconditionally) {
// Headers or sources required with a specific linkage // Headers or sources required with a specific linkage
final UpdateKind updateKind = isSourceUnit ? UpdateKind.REQUIRED_SOURCE : UpdateKind.REQUIRED_HEADER; final UpdateKind updateKind = isSourceUnit ? UpdateKind.REQUIRED_SOURCE
AbstractLanguage[] langs= fResolver.getLanguages(tu, fIndexHeadersWithoutContext == UnusedHeaderStrategy.useBoth); : regularContent && both ? UpdateKind.REQUIRED_HEADER : UpdateKind.ONE_LINKAGE_HEADER;
for (AbstractLanguage lang : langs) { if (regularContent || indexFiles.length == 0) {
int linkageID = lang.getLinkageID(); AbstractLanguage[] langs= fResolver.getLanguages(tu, fIndexHeadersWithoutContext);
boolean foundInLinkage = false; for (AbstractLanguage lang : langs) {
for (int i = 0; i < indexFiles.length; i++) { int linkageID = lang.getLinkageID();
IIndexFragmentFile ifile = indexFiles[i]; boolean foundInLinkage = false;
if (ifile != null && ifile.getLinkageID() == linkageID && ifile.hasContent()) { for (int i = 0; i < indexFiles.length; i++) {
foundInLinkage = true; IIndexFragmentFile ifile = indexFiles[i];
indexFiles[i]= null; // Take the file. if (ifile != null && ifile.getLinkageID() == linkageID && ifile.hasContent()) {
boolean update= force || isModified(checkTimestamps, checkFileContentsHash, ifl, tu, ifile); foundInLinkage = true;
if (update && requestUpdate(linkageID, ifl, ifile, tu, updateKind)) { indexFiles[i]= null; // Take the file.
count++; boolean update= force || isModified(checkTimestamps, checkFileContentsHash, ifl, tu, ifile);
linkages.set(linkageID); if (update && requestUpdate(linkageID, ifl, ifile, tu, updateKind)) {
count++;
linkages.set(linkageID);
}
} }
} }
} if (!foundInLinkage && requestUpdate(linkageID, ifl, null, tu, updateKind)) {
if (!foundInLinkage && requestUpdate(linkageID, ifl, null, tu, updateKind)) { linkages.set(linkageID);
linkages.set(linkageID); count++;
count++; }
} }
} }
} }
@ -615,7 +634,7 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
for (IIndexFragmentFile ifile : indexFiles) { for (IIndexFragmentFile ifile : indexFiles) {
if (ifile != null) { if (ifile != null) {
IIndexInclude ctx= ifile.getParsedInContext(); IIndexInclude ctx= ifile.getParsedInContext();
if (ctx == null && !fResolver.isIndexedUnconditionally(ifile.getLocation())) { if (ctx == null && !indexedUnconditionally) {
iFilesToRemove.add(ifile); iFilesToRemove.add(ifile);
count++; count++;
} else { } else {
@ -656,10 +675,6 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
if (fIndexHeadersWithoutContext != UnusedHeaderStrategy.skip) if (fIndexHeadersWithoutContext != UnusedHeaderStrategy.skip)
return true; return true;
// File required because it is open in the editor.
if (fResolver.isIndexedUnconditionally(ifl))
return true;
// Source file // Source file
if (isSourceUnit) { if (isSourceUnit) {
if (fIndexFilesWithoutConfiguration || fResolver.isFileBuildConfigured(tu)) if (fIndexFilesWithoutConfiguration || fResolver.isFileBuildConfigured(tu))
@ -689,7 +704,7 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
private boolean requestUpdate(int linkageID, IIndexFileLocation ifl, IIndexFragmentFile ifile, Object tu, UpdateKind kind) { private boolean requestUpdate(int linkageID, IIndexFileLocation ifl, IIndexFragmentFile ifile, Object tu, UpdateKind kind) {
LinkageTask fileMap= createRequestMap(linkageID); LinkageTask fileMap= createRequestMap(linkageID);
return fileMap.requestUpdate(ifl, ifile, tu, kind); return fileMap.requestUpdate(ifl, ifile, tu, kind, fOneLinkageTasks);
} }
private LinkageTask createRequestMap(int linkageID) { private LinkageTask createRequestMap(int linkageID) {
@ -712,11 +727,13 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
@Override @Override
protected void reportFileWrittenToIndex(FileInAST file, IIndexFragmentFile ifile) throws CoreException { protected void reportFileWrittenToIndex(FileInAST file, IIndexFragmentFile ifile) throws CoreException {
final FileContentKey fck = file.fFileContentKey; final FileContentKey fck = file.fFileContentKey;
final IIndexFileLocation location = fck.getLocation();
boolean wasCounted= false; boolean wasCounted= false;
UpdateKind kind= UpdateKind.OTHER_HEADER; UpdateKind kind= UpdateKind.OTHER_HEADER;
LinkageTask map = findRequestMap(fck.getLinkageID()); LinkageTask map = findRequestMap(fck.getLinkageID());
LocationTask locTask= null;
if (map != null) { if (map != null) {
LocationTask locTask = map.find(fck.getLocation()); locTask = map.find(location);
if (locTask != null) { if (locTask != null) {
kind= locTask.fKind; kind= locTask.fKind;
FileVersionTask v = locTask.findVersion(ifile); FileVersionTask v = locTask.findVersion(ifile);
@ -733,6 +750,21 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
} }
fIndexContentCache.remove(ifile); fIndexContentCache.remove(ifile);
fIndexFilesCache.remove(file.fFileContentKey.getLocation()); fIndexFilesCache.remove(file.fFileContentKey.getLocation());
LocationTask task= fOneLinkageTasks.remove(location);
if (task != null && task != locTask) {
if (task.fKind == UpdateKind.ONE_LINKAGE_HEADER && !task.isCompleted()) {
task.fKind= UpdateKind.OTHER_HEADER;
if (task.isCompleted()) {
if (!wasCounted) {
kind= UpdateKind.ONE_LINKAGE_HEADER;
wasCounted= true;
} else {
reportFile(wasCounted, UpdateKind.ONE_LINKAGE_HEADER);
}
}
}
}
reportFile(wasCounted, kind); reportFile(wasCounted, kind);
} }
@ -989,7 +1021,7 @@ public abstract class AbstractIndexerTask extends PDOMWriter {
} }
private AbstractLanguage getLanguage(Object tu, int linkageID) { private AbstractLanguage getLanguage(Object tu, int linkageID) {
for (AbstractLanguage language : fResolver.getLanguages(tu, true)) { for (AbstractLanguage language : fResolver.getLanguages(tu, UnusedHeaderStrategy.useBoth)) {
if (language.getLinkageID() == linkageID) { if (language.getLinkageID() == linkageID) {
return language; return language;
} }

View file

@ -71,7 +71,7 @@ public abstract class IndexerInputAdapter extends ASTFilePathResolver {
/** /**
* Obtains the languages the input file should be parsed with. * Obtains the languages the input file should be parsed with.
*/ */
public abstract AbstractLanguage[] getLanguages(Object tu, boolean bothForHeaders); public abstract AbstractLanguage[] getLanguages(Object tu, AbstractIndexerTask.UnusedHeaderStrategy strat);
/** /**
* Obtains the scanner configuration for the input file. * Obtains the scanner configuration for the input file.

View file

@ -136,6 +136,9 @@ public class PDOMManager implements IWritableIndexManager, IListener {
public static final int[] IDS_FOR_LINKAGES_TO_INDEX = { public static final int[] IDS_FOR_LINKAGES_TO_INDEX = {
ILinkage.CPP_LINKAGE_ID, ILinkage.C_LINKAGE_ID, ILinkage.FORTRAN_LINKAGE_ID ILinkage.CPP_LINKAGE_ID, ILinkage.C_LINKAGE_ID, ILinkage.FORTRAN_LINKAGE_ID
}; };
public static final int[] IDS_FOR_LINKAGES_TO_INDEX_C_FIRST = {
ILinkage.C_LINKAGE_ID, ILinkage.CPP_LINKAGE_ID, ILinkage.FORTRAN_LINKAGE_ID
};
private final LinkedList<ICProject> fProjectQueue= new LinkedList<ICProject>(); private final LinkedList<ICProject> fProjectQueue= new LinkedList<ICProject>();
private final PDOMSetupJob fSetupJob; private final PDOMSetupJob fSetupJob;

View file

@ -24,6 +24,7 @@ import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.index.IWritableIndex; import org.eclipse.cdt.internal.core.index.IWritableIndex;
import org.eclipse.cdt.internal.core.index.IWritableIndexManager; import org.eclipse.cdt.internal.core.index.IWritableIndexManager;
import org.eclipse.cdt.internal.core.model.CProject;
import org.eclipse.cdt.internal.core.pdom.AbstractIndexerTask; import org.eclipse.cdt.internal.core.pdom.AbstractIndexerTask;
import org.eclipse.cdt.internal.core.pdom.ITodoTaskUpdater; import org.eclipse.cdt.internal.core.pdom.ITodoTaskUpdater;
import org.eclipse.cdt.internal.core.pdom.IndexerProgress; import org.eclipse.cdt.internal.core.pdom.IndexerProgress;
@ -80,10 +81,11 @@ public abstract class PDOMIndexerTask extends AbstractIndexerTask implements IPD
boolean i1= checkProperty(IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_DEFAULT_LANG); boolean i1= checkProperty(IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_DEFAULT_LANG);
boolean i2= checkProperty(IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_ALTERNATE_LANG); boolean i2= checkProperty(IndexerPreferences.KEY_INDEX_UNUSED_HEADERS_WITH_ALTERNATE_LANG);
UnusedHeaderStrategy strategy; UnusedHeaderStrategy strategy;
if (i1) { if (i1 == i2) {
strategy= i2 ? UnusedHeaderStrategy.useBoth : UnusedHeaderStrategy.useDefaultLanguage; strategy = i1 ? UnusedHeaderStrategy.useBoth : UnusedHeaderStrategy.skip;
} else { } else {
strategy= i2 ? UnusedHeaderStrategy.useAlternateLanguage: UnusedHeaderStrategy.skip; strategy = i1 == CProject.hasCCNature(getProject().getProject())
? UnusedHeaderStrategy.useCPP : UnusedHeaderStrategy.useC;
} }
setIndexHeadersWithoutContext(strategy); setIndexHeadersWithoutContext(strategy);
} else { } else {

View file

@ -30,6 +30,7 @@ import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.ScannerInfo; import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.CCoreInternals; import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil; import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.pdom.AbstractIndexerTask.UnusedHeaderStrategy;
import org.eclipse.cdt.internal.core.pdom.IndexerInputAdapter; import org.eclipse.cdt.internal.core.pdom.IndexerInputAdapter;
import org.eclipse.cdt.internal.core.resources.PathCanonicalizationStrategy; import org.eclipse.cdt.internal.core.resources.PathCanonicalizationStrategy;
import org.eclipse.cdt.utils.UNCPathConverter; import org.eclipse.cdt.utils.UNCPathConverter;
@ -193,7 +194,7 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter {
} }
@Override @Override
public AbstractLanguage[] getLanguages(Object tuo, boolean bothForHeaders) { public AbstractLanguage[] getLanguages(Object tuo, UnusedHeaderStrategy strategy) {
if (tuo instanceof PotentialTranslationUnit) { if (tuo instanceof PotentialTranslationUnit) {
if (fLangC != null) { if (fLangC != null) {
if (fLangCpp != null) { if (fLangCpp != null) {
@ -211,14 +212,23 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter {
try { try {
ILanguage lang= tu.getLanguage(); ILanguage lang= tu.getLanguage();
if (lang instanceof AbstractLanguage) { if (lang instanceof AbstractLanguage) {
if (bothForHeaders && tu.isHeaderUnit()) { final boolean both = strategy == UnusedHeaderStrategy.useBoth;
final boolean useC = strategy == UnusedHeaderStrategy.useC;
final boolean useCpp = strategy == UnusedHeaderStrategy.useCPP;
if ((both || useC || useCpp) && tu.isHeaderUnit()) {
String filename= tu.getElementName(); String filename= tu.getElementName();
if (filename.indexOf('.') >= 0) { if (filename.indexOf('.') >= 0) {
final String contentTypeId= tu.getContentTypeId(); final String contentTypeId= tu.getContentTypeId();
if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CXXHEADER) && fLangC != null) { if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CXXHEADER) && fLangC != null) {
return new AbstractLanguage[] {(AbstractLanguage) lang, fLangC}; if (both)
return new AbstractLanguage[] {(AbstractLanguage) lang, fLangC};
if (useC)
return new AbstractLanguage[] {fLangC};
} else if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CHEADER) && fLangCpp != null) { } else if (contentTypeId.equals(CCorePlugin.CONTENT_TYPE_CHEADER) && fLangCpp != null) {
return new AbstractLanguage[] {(AbstractLanguage) lang, fLangCpp}; if (both)
return new AbstractLanguage[] {(AbstractLanguage) lang, fLangCpp};
if (useCpp)
return new AbstractLanguage[] {fLangCpp};
} }
} }
} }