diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexIncludeTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexIncludeTest.java index 32a33e813b2..8f0f1de763e 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexIncludeTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexIncludeTest.java @@ -318,11 +318,15 @@ public class IndexIncludeTest extends IndexTestBase { // #include "resolved20070426.h" public void testFixedContext() throws Exception { - waitForIndexer(); TestScannerProvider.sIncludes= new String[]{fProject.getProject().getLocation().toOSString()}; String source= getContentsForTest(1)[0].toString(); IFile header= TestSourceReader.createFile(fProject.getProject(), "resolved20070426.h", ""); IFile s1= TestSourceReader.createFile(fProject.getProject(), "s1.cpp", source); + // make sure it is parsed in context + waitForIndexer(); + CCorePlugin.getIndexManager().reindex(fProject); + waitForIndexer(); + IFile s2= TestSourceReader.createFile(fProject.getProject(), "s2.cpp", source); TestSourceReader.waitUntilFileIsIndexed(fIndex, s2, INDEXER_WAIT_TIME); @@ -332,8 +336,12 @@ public class IndexIncludeTest extends IndexTestBase { assertNotNull(ifile); IIndexInclude[] includes= fIndex.findIncludedBy(ifile); assertEquals(2, includes.length); - assertEquals(s2.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); - assertEquals(s1.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + assertEquals(s1.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); + assertEquals(s2.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + + IIndexInclude context= ifile.getParsedInContext(); + assertNotNull(context); + assertEquals(s1.getFullPath().toString(), context.getIncludedByLocation().getFullPath()); } finally { fIndex.releaseReadLock(); @@ -348,8 +356,11 @@ public class IndexIncludeTest extends IndexTestBase { assertNotNull(ifile); IIndexInclude[] includes= fIndex.findIncludedBy(ifile); assertEquals(2, includes.length); - assertEquals(s2.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); - assertEquals(s1.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + assertEquals(s1.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); + assertEquals(s2.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + IIndexInclude context= ifile.getParsedInContext(); + assertNotNull(context); + assertEquals(s1.getFullPath().toString(), context.getIncludedByLocation().getFullPath()); } finally { fIndex.releaseReadLock(); @@ -364,8 +375,11 @@ public class IndexIncludeTest extends IndexTestBase { assertNotNull(ifile); IIndexInclude[] includes= fIndex.findIncludedBy(ifile); assertEquals(2, includes.length); - assertEquals(s2.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); - assertEquals(s1.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + assertEquals(s1.getFullPath().toString(), includes[0].getIncludedByLocation().getFullPath()); + assertEquals(s2.getFullPath().toString(), includes[1].getIncludedByLocation().getFullPath()); + IIndexInclude context= ifile.getParsedInContext(); + assertNotNull(context); + assertEquals(s1.getFullPath().toString(), context.getIncludedByLocation().getFullPath()); } finally { fIndex.releaseReadLock(); diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java index ba4f86dbeba..4370d5cfdd5 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/CoreModelUtil.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2002, 2006 QNX Software Systems and others. + * Copyright (c) 2002, 2007 QNX Software Systems 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 @@ -7,6 +7,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * Markus Schorn (Wind River Systems) *******************************************************************************/ package org.eclipse.cdt.core.model; @@ -17,6 +18,8 @@ import java.util.List; import java.util.Map; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.index.IIndexFileLocation; +import org.eclipse.cdt.core.index.IndexLocationFactory; import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; import org.eclipse.cdt.core.settings.model.ICProjectDescription; import org.eclipse.core.resources.IFile; @@ -569,6 +572,40 @@ public class CoreModelUtil { return null; } + /** + * Returns the translation unit for the location given or null. + * @throws CModelException + */ + public static ITranslationUnit findTranslationUnitForLocation(IIndexFileLocation ifl, ICProject preferredProject) throws CModelException { + String fullPath= ifl.getFullPath(); + if (fullPath != null) { + IResource file= ResourcesPlugin.getWorkspace().getRoot().findMember(fullPath); + if (file instanceof IFile) { + return findTranslationUnit((IFile) file); + } + return null; + } + IPath location= IndexLocationFactory.getAbsolutePath(ifl); + if (location != null) { + CoreModel coreModel = CoreModel.getDefault(); + ITranslationUnit tu= null; + if (preferredProject != null) { + tu= coreModel.createTranslationUnitFrom(preferredProject, location); + } + if (tu == null) { + ICProject[] projects= coreModel.getCModel().getCProjects(); + for (int i = 0; i < projects.length && tu == null; i++) { + ICProject project = projects[i]; + if (!project.equals(preferredProject)) { + tu= coreModel.createTranslationUnitFrom(project, location); + } + } + } + return tu; + } + return null; + } + /** * Returns the translation unit for the file given or null. */ @@ -654,10 +691,10 @@ public class CoreModelUtil { if(cfgDes!=null) { CoreModel core= CoreModel.getDefault(); - IProject[] cprojects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); + IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); - for (int i=0; inull. */ - void clearFile(IIndexFragmentFile file, IASTPreprocessorIncludeStatement[] newIncludes, IIndexFileLocation[] newIncludeLocations) throws CoreException; + void clearFile(IIndexFragmentFile file, Collection clearedContexts) throws CoreException; /** * Acquires a write lock, while giving up a certain amount of read locks. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndexFragment.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndexFragment.java index d27c96146c9..a42b339f2d2 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndexFragment.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndexFragment.java @@ -12,10 +12,12 @@ package org.eclipse.cdt.internal.core.index; +import java.util.Collection; + import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.index.IIndexFileLocation; +import org.eclipse.cdt.internal.core.index.IWritableIndex.IncludeInformation; import org.eclipse.core.runtime.CoreException; /** @@ -30,10 +32,11 @@ public interface IWritableIndexFragment extends IIndexFragment { /** * Clears the given file in the index. - * @param newIncludes list of includes to set - * @param destFiles list of file objects for the includes + * @param file a file to clear, must belong to this fragment. + * @param a collection that receives IndexFileLocation objects for files that + * had the cleared file as a context. */ - void clearFile(IIndexFragmentFile file, IASTPreprocessorIncludeStatement[] newIncludes, IIndexFragmentFile[] destFiles) throws CoreException; + void clearFile(IIndexFragmentFile file, Collection contextsRemoved) throws CoreException; /** * Creates a file object for the given location or returns an existing one. @@ -47,6 +50,7 @@ public interface IWritableIndexFragment extends IIndexFragment { * Adds an include to the given file. */ void addFileContent(IIndexFragmentFile sourceFile, + IncludeInformation[] includes, IASTPreprocessorMacroDefinition[] macros, IASTName[][] names) throws CoreException; /** diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/WritableCIndex.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/WritableCIndex.java index 160745cb13f..7bf171f3bc0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/WritableCIndex.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/WritableCIndex.java @@ -12,8 +12,9 @@ package org.eclipse.cdt.internal.core.index; +import java.util.Collection; + import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.core.runtime.CoreException; @@ -56,12 +57,19 @@ public class WritableCIndex extends CIndex implements IWritableIndex { } public void setFileContent(IIndexFragmentFile file, + IncludeInformation[] includes, IASTPreprocessorMacroDefinition[] macros, IASTName[][] names) throws CoreException { IIndexFragment indexFragment = file.getIndexFragment(); assert isWritableFragment(indexFragment); - ((IWritableIndexFragment) indexFragment).addFileContent(file, macros, names); + for (int i = 0; i < includes.length; i++) { + IncludeInformation ii= includes[i]; + if (ii.fLocation != null) { + ii.fTargetFile= addFile(ii.fLocation); + } + } + ((IWritableIndexFragment) indexFragment).addFileContent(file, includes, macros, names); } public void clear() throws CoreException { @@ -71,20 +79,11 @@ public class WritableCIndex extends CIndex implements IWritableIndex { } } - public void clearFile(IIndexFragmentFile file, - IASTPreprocessorIncludeStatement[] newIncludes, - IIndexFileLocation[] newIncludeLocations) throws CoreException { + public void clearFile(IIndexFragmentFile file, Collection clearedContexts) throws CoreException { IIndexFragment indexFragment = file.getIndexFragment(); assert isWritableFragment(indexFragment); - IIndexFragmentFile[] destFiles= new IIndexFragmentFile[newIncludes.length]; - for (int i = 0; i < newIncludes.length; i++) { - if (newIncludeLocations[i] != null) { - destFiles[i]= addFile(newIncludeLocations[i]); - } - } - - ((IWritableIndexFragment) indexFragment).clearFile(file, newIncludes, destFiles); + ((IWritableIndexFragment) indexFragment).clearFile(file, clearedContexts); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMWriter.java index a3c68044314..71be85e5ddf 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOMWriter.java @@ -12,10 +12,15 @@ package org.eclipse.cdt.internal.core.pdom; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; @@ -37,6 +42,8 @@ import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.internal.core.index.IIndexFragmentFile; import org.eclipse.cdt.internal.core.index.IWritableIndex; +import org.eclipse.cdt.internal.core.index.IndexFileLocation; +import org.eclipse.cdt.internal.core.index.IWritableIndex.IncludeInformation; import org.eclipse.cdt.internal.core.pdom.dom.PDOMASTAdapter; import org.eclipse.cdt.internal.core.pdom.indexer.IndexerASTVisitor; import org.eclipse.cdt.internal.core.pdom.indexer.IndexerStatistics; @@ -49,9 +56,6 @@ import org.eclipse.core.runtime.IProgressMonitor; * @since 4.0 */ abstract public class PDOMWriter { - private static final IASTPreprocessorIncludeStatement[] NO_INCLUDES = {}; - private static final IIndexFileLocation[] NO_LOCATIONS = {}; - public static int SKIP_ALL_REFERENCES= -1; public static int SKIP_TYPE_REFERENCES= 1; public static int SKIP_NO_REFERENCES= 0; @@ -117,7 +121,8 @@ abstract public class PDOMWriter { IProgressMonitor pm) throws InterruptedException, CoreException { final Map symbolMap= new HashMap(); try { - IIndexFileLocation[] orderedPaths= extractSymbols(ast, symbolMap, configHash); + HashSet contextIncludes= new HashSet(); + IIndexFileLocation[] orderedPaths= extractSymbols(ast, symbolMap, configHash, contextIncludes); for (int i=0; i*/ orderedIFLs= new LinkedHashSet/**/(); ArrayList/**/ iflStack= new ArrayList/**/(); - - + HashMap firstIncludePerTarget= new HashMap(); + final IIndexFileLocation astLocation = findLocation(ast.getFilePath()); IIndexFileLocation aboveStackIFL = astLocation; @@ -234,6 +240,11 @@ abstract public class PDOMWriter { if (include.isResolved()) { iflStack.add(nextIFL); aboveStackIFL= findLocation(include.getPath()); + if (include.isActive()) { + if (!firstIncludePerTarget.containsKey(aboveStackIFL)) { + firstIncludePerTarget.put(aboveStackIFL, include); + } + } } else if (include.isActive()) { reportProblem(include); @@ -248,6 +259,15 @@ abstract public class PDOMWriter { } } + // extract context includes + for (Iterator iterator = orderedIFLs.iterator(); iterator.hasNext();) { + IndexFileLocation loc = (IndexFileLocation) iterator.next(); + Object contextInclude= firstIncludePerTarget.get(loc); + if (contextInclude != null) { + contextIncludes.add(contextInclude); + } + } + // macros IASTPreprocessorMacroDefinition[] macros = ast.getMacroDefinitions(); for (int i2 = 0; i2 < macros.length; ++i2) { @@ -344,36 +364,37 @@ abstract public class PDOMWriter { return false; } - private IIndexFragmentFile addToIndex(IWritableIndex index, IIndexFileLocation location, Map symbolMap, int configHash) throws CoreException { - ArrayList[] lists= (ArrayList[]) symbolMap.get(location); - IASTPreprocessorIncludeStatement[] includes= NO_INCLUDES; - IASTPreprocessorMacroDefinition[] macros= null; - IASTName[][] names= null; - IIndexFileLocation[] includeLocations= NO_LOCATIONS; - if (lists != null) { - ArrayList list= lists[0]; - includes= (IASTPreprocessorIncludeStatement[]) list.toArray(new IASTPreprocessorIncludeStatement[list.size()]); - list= lists[1]; - macros= (IASTPreprocessorMacroDefinition[]) list.toArray(new IASTPreprocessorMacroDefinition[list.size()]); - list= lists[2]; - names= (IASTName[][]) list.toArray(new IASTName[list.size()][]); - - includeLocations = new IIndexFileLocation[includes.length]; - for(int i=0; i0, if unknown. - * @throws CoreException - * @since 4.0 - */ - protected int getScannerConfigurationHashcode(IIndexFileLocation location) throws CoreException { - return 0; - } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/TeamPDOMImportOperation.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/TeamPDOMImportOperation.java index ba503c98230..9f74e3b2194 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/TeamPDOMImportOperation.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/TeamPDOMImportOperation.java @@ -30,7 +30,6 @@ import java.util.zip.ZipFile; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOMIndexerTask; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IndexLocationFactory; import org.eclipse.cdt.core.model.CoreModel; @@ -65,9 +64,7 @@ public class TeamPDOMImportOperation implements IWorkspaceRunnable { private static final String PROJECT_VAR_REPLACEMENT_BEGIN = "\\${$1:"; //$NON-NLS-1$ private static final String PROJECT_VAR_REPLACEMENT_END = "}"; //$NON-NLS-1$ private static final String DOLLAR_OR_BACKSLASH_REPLACEMENT = "\\\\$0"; //$NON-NLS-1$ - private static final IASTPreprocessorIncludeStatement[] NO_INCLUDES = {}; private static final Pattern DOLLAR_OR_BACKSLASH_PATTERN= Pattern.compile("[\\$\\\\]"); //$NON-NLS-1$ - private static final IIndexFragmentFile[] NO_IDS = {}; private static final class FileAndChecksum { public ITranslationUnit fFile; @@ -268,7 +265,7 @@ public class TeamPDOMImportOperation implements IWorkspaceRunnable { IndexFileLocation ifl = (IndexFileLocation) i.next(); IIndexFragmentFile file= pdom.getFile(ifl); - pdom.clearFile(file, NO_INCLUDES, NO_IDS); + pdom.clearFile(file, null); } for (Iterator i = updateTimestamps.iterator(); i.hasNext();) { checkMonitor(monitor); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java index f93c9a00e81..bdabb5ec383 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/WritablePDOM.java @@ -15,18 +15,19 @@ package org.eclipse.cdt.internal.core.pdom; import java.io.File; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IIndexLocationConverter; import org.eclipse.cdt.internal.core.index.IIndexFragmentFile; import org.eclipse.cdt.internal.core.index.IWritableIndexFragment; +import org.eclipse.cdt.internal.core.index.IWritableIndex.IncludeInformation; import org.eclipse.cdt.internal.core.pdom.db.ChunkCache; import org.eclipse.cdt.internal.core.pdom.db.DBProperties; import org.eclipse.cdt.internal.core.pdom.db.IBTreeVisitor; @@ -52,19 +53,19 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment { } public void addFileContent(IIndexFragmentFile sourceFile, + IncludeInformation[] includes, IASTPreprocessorMacroDefinition[] macros, IASTName[][] names) throws CoreException { assert sourceFile.getIndexFragment() == this; PDOMFile pdomFile = (PDOMFile) sourceFile; + pdomFile.addIncludesTo(includes); pdomFile.addMacros(macros); pdomFile.addNames(names); } - public void clearFile(IIndexFragmentFile file, - IASTPreprocessorIncludeStatement[] newIncludes, IIndexFragmentFile[] destFiles) throws CoreException { + public void clearFile(IIndexFragmentFile file, Collection contextsRemoved) throws CoreException { assert file.getIndexFragment() == this; - assert newIncludes.length == destFiles.length; - ((PDOMFile) file).clear(newIncludes, destFiles); + ((PDOMFile) file).clear(contextsRemoved); } public void clear() throws CoreException { @@ -130,7 +131,7 @@ public class WritablePDOM extends PDOM implements IWritableIndexFragment { for(Iterator i = notConverted.iterator(); i.hasNext(); ) { PDOMFile file = (PDOMFile) i.next(); file.convertIncludersToUnresolved(); - file.clear(null, null); + file.clear(null); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java index f14a8cbd9dc..e84a571b23a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMFile.java @@ -13,13 +13,13 @@ package org.eclipse.cdt.internal.core.pdom.dom; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ast.IASTName; -import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IIndexInclude; @@ -29,6 +29,7 @@ import org.eclipse.cdt.core.index.IIndexName; import org.eclipse.cdt.internal.core.index.IIndexFragment; import org.eclipse.cdt.internal.core.index.IIndexFragmentFile; import org.eclipse.cdt.internal.core.index.IWritableIndexFragment; +import org.eclipse.cdt.internal.core.index.IWritableIndex.IncludeInformation; import org.eclipse.cdt.internal.core.pdom.PDOM; import org.eclipse.cdt.internal.core.pdom.WritablePDOM; import org.eclipse.cdt.internal.core.pdom.db.BTree; @@ -45,7 +46,6 @@ import org.eclipse.core.runtime.CoreException; * */ public class PDOMFile implements IIndexFragmentFile { - private final PDOM pdom; private final int record; @@ -165,6 +165,10 @@ public class PDOMFile implements IIndexFragmentFile { int rec = pdom.getDB().getInt(record + FIRST_INCLUDED_BY); return rec != 0 ? new PDOMInclude(pdom, rec) : null; } + + public IIndexInclude getParsedInContext() throws CoreException { + return getFirstIncludedBy(); + } public void setFirstIncludedBy(PDOMInclude includedBy) throws CoreException { int rec = includedBy != null ? includedBy.getRecord() : 0; @@ -235,63 +239,18 @@ public class PDOMFile implements IIndexFragmentFile { return result; } - public void clear(IASTPreprocessorIncludeStatement[] newIncludes, IIndexFragmentFile[] destFiles) throws CoreException { - int[] records= new int[destFiles.length]; - PDOMInclude[] oldDirectives= new PDOMInclude[destFiles.length]; - for (int i = 0; i < records.length; i++) { - PDOMFile destFile= (PDOMFile) destFiles[i]; - if (destFile != null) { - records[i]= destFile.getRecord(); - } - } - - // Remove the includes preserving the unchanged + public void clear(Collection contextsRemoved) throws CoreException { + // Remove the includes PDOMInclude include = getFirstInclude(); while (include != null) { PDOMInclude nextInclude = include.getNextInIncludes(); - final PDOMFile includes= (PDOMFile) include.getIncludes(); - if (includes != null) { - final int rec= includes.record; - int i; - for (i=0; i < records.length; i++) { - if (rec == records[i]) { - records[i]= 0; - oldDirectives[i]= include; - include.setNextInIncludes(null); - break; - } - } - if (i >= records.length) { - include.delete(); - } + if (contextsRemoved != null && include.getPrevInIncludedByRecord() == 0) { + contextsRemoved.add(include.getIncludesLocation()); } + include.delete(); include = nextInclude; } - setFirstInclude(null); - - PDOMInclude lastInclude= null; - for (int i = 0; i < newIncludes.length; i++) { - IASTPreprocessorIncludeStatement statement = newIncludes[i]; - PDOMFile targetFile= (PDOMFile) destFiles[i]; - PDOMInclude pdomInclude= oldDirectives[i]; - if (pdomInclude == null) { - pdomInclude= new PDOMInclude(pdom, statement, this, targetFile); - if (targetFile != null) { - assert targetFile.getIndexFragment() instanceof IWritableIndexFragment; - targetFile.addIncludedBy(pdomInclude); - } - } - else { - pdomInclude.update(statement); - } - if (lastInclude == null) { - setFirstInclude(pdomInclude); - } - else { - lastInclude.setNextInIncludes(pdomInclude); - } - lastInclude= pdomInclude; - } + setFirstInclude(include); // Delete all the macros in this file PDOMMacro macro = getFirstMacro(); @@ -318,16 +277,51 @@ public class PDOMFile implements IIndexFragmentFile { setFirstName(null); } - public void addIncludedBy(PDOMInclude include) throws CoreException { - PDOMInclude firstIncludedBy = getFirstIncludedBy(); - if (firstIncludedBy != null) { - include.setNextInIncludedBy(firstIncludedBy); - firstIncludedBy.setPrevInIncludedBy(include); + public void addIncludesTo(IncludeInformation[] includeInfos) throws CoreException { + assert getFirstInclude() == null; + + PDOMInclude lastInclude= null; + for (int i = 0; i < includeInfos.length; i++) { + final IncludeInformation info= includeInfos[i]; + final PDOMFile targetFile= (PDOMFile) info.fTargetFile; + + PDOMInclude pdomInclude = new PDOMInclude(pdom, info.fStatement, this, targetFile); + if (targetFile != null) { + assert targetFile.getIndexFragment() instanceof IWritableIndexFragment; + targetFile.addIncludedBy(pdomInclude, info.fIsContext); + } + if (lastInclude == null) { + setFirstInclude(pdomInclude); + } + else { + lastInclude.setNextInIncludes(pdomInclude); + } + lastInclude= pdomInclude; } - setFirstIncludedBy(include); } - + public void addIncludedBy(PDOMInclude include, boolean isContext) throws CoreException { + PDOMInclude firstIncludedBy = getFirstIncludedBy(); + if (firstIncludedBy != null) { + if (isContext) { + setFirstIncludedBy(include); + include.setNextInIncludedBy(firstIncludedBy); + firstIncludedBy.setPrevInIncludedBy(include); + } + else { + PDOMInclude secondIncludedBy= firstIncludedBy.getNextInIncludedBy(); + if (secondIncludedBy != null) { + include.setNextInIncludedBy(secondIncludedBy); + secondIncludedBy.setPrevInIncludedBy(include); + } + include.setPrevInIncludedBy(firstIncludedBy); + firstIncludedBy.setNextInIncludedBy(include); + } + } + else { + setFirstIncludedBy(include); + } + } public IIndexInclude[] getIncludes() throws CoreException { List result= new ArrayList(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMInclude.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMInclude.java index 6bf8e0fc7c9..8f392b2251b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMInclude.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/PDOMInclude.java @@ -72,20 +72,6 @@ public class PDOMInclude implements IIndexFragmentInclude { setIncludes(targetFile, name.toCharArray()); } - public void update(IASTPreprocessorIncludeStatement include) throws CoreException { - IASTName name= include.getName(); - IASTFileLocation loc= name.getFileLocation(); - // includes generated by -include or -macro don't have a location - if (loc != null) { - setNameOffsetAndLength(loc.getNodeOffset(), (short) loc.getNodeLength()); - } - else { - setNameOffsetAndLength(0, (short) 0); - } - - setFlag(encodeFlags(include, false)); - } - private byte encodeFlags(IASTPreprocessorIncludeStatement include, boolean unresolved) { byte flags= 0; if (include.isSystemInclude()) { @@ -187,9 +173,13 @@ public class PDOMInclude implements IIndexFragmentInclude { } public PDOMInclude getPrevInIncludedBy() throws CoreException { - int rec = pdom.getDB().getInt(record + INCLUDED_BY_PREV); + int rec = getPrevInIncludedByRecord(); return rec != 0 ? new PDOMInclude(pdom, rec) : null; } + + int getPrevInIncludedByRecord() throws CoreException { + return pdom.getDB().getInt(record + INCLUDED_BY_PREV); + } public void setPrevInIncludedBy(PDOMInclude include) throws CoreException { int rec = include != null ? include.getRecord() : 0; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/PDOMIndexerTask.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/PDOMIndexerTask.java index 0fbf2387cc2..61b63c9b4db 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/PDOMIndexerTask.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/PDOMIndexerTask.java @@ -25,7 +25,6 @@ import java.util.Map; 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.ast.IASTPreprocessorIncludeStatement; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndexFile; @@ -34,7 +33,6 @@ 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.AbstractLanguage; -import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModelUtil; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ILanguage; @@ -66,8 +64,6 @@ public abstract class PDOMIndexerTask extends PDOMWriter implements IPDOMIndexer private static final Object NO_CONTEXT = new Object(); private static final int MAX_ERRORS = 500; private static final String TRUE = "true"; //$NON-NLS-1$ - private static final IIndexFileLocation[] NO_LOCATIONS= {}; - private static final IASTPreprocessorIncludeStatement[] NO_INCLUDES= {}; private AbstractPDOMIndexer fIndexer; protected Map/**/ fContextMap = new HashMap/**/(); @@ -372,7 +368,7 @@ public abstract class PDOMIndexerTask extends PDOMWriter implements IPDOMIndexer } } - private ITranslationUnit findContext(IIndex index, IIndexFileLocation location) { + private ITranslationUnit findContext(IIndex index, final IIndexFileLocation location) { Object cachedContext= fContextMap.get(location); if (cachedContext != null) { return cachedContext == NO_CONTEXT ? null : (ITranslationUnit) cachedContext; @@ -381,19 +377,29 @@ public abstract class PDOMIndexerTask extends PDOMWriter implements IPDOMIndexer fContextMap.put(location, NO_CONTEXT); // prevent recursion IIndexFile pdomFile; try { + final ICProject project= getIndexer().getProject(); pdomFile = index.getFile(location); if (pdomFile != null) { - ICProject project= getIndexer().getProject(); + final IIndexInclude contextInclude= pdomFile.getParsedInContext(); + if (contextInclude != null) { + final IIndexFileLocation loc= contextInclude.getIncludedByLocation(); + ITranslationUnit context= getSourceUnit(project, loc); + if (context == null) { + context= findContext(index, loc); + } + if (context != null) { + fContextMap.put(location, context); + return context; + } + } + + // fallback to other includes IIndexInclude[] includedBy = index.findIncludedBy(pdomFile, IIndex.DEPTH_ZERO); for (int i = includedBy.length-1; i >=0; i--) { - IIndexInclude include = includedBy[i]; - IIndexFileLocation incLocation = include.getIncludedByLocation(); - ITranslationUnit context= null; - if (CoreModel.isValidSourceUnitName(project.getProject(), incLocation.getURI().toString())) { // FIXME - is this ok? - context = CoreModelUtil.findTranslationUnitForLocation(IndexLocationFactory.getAbsolutePath(incLocation), project); - } - else { - context= findContext(index, incLocation); + final IIndexFileLocation loc= includedBy[i].getIncludedByLocation(); + ITranslationUnit context= getSourceUnit(project, loc); + if (context == null) { + context= findContext(index, loc); } if (context != null) { fContextMap.put(location, context); @@ -407,6 +413,17 @@ public abstract class PDOMIndexerTask extends PDOMWriter implements IPDOMIndexer return null; } + private ITranslationUnit getSourceUnit(final ICProject project, final IIndexFileLocation location) + throws CoreException { + ITranslationUnit tu= CoreModelUtil.findTranslationUnitForLocation(location, getIndexer().getProject()); + if (tu != null) { + if (tu.isSourceUnit()) { + return tu; + } + } + return null; + } + /** * Conveninence method for subclasses, removes a translation unit from the index. * @since 4.0 @@ -416,7 +433,7 @@ public abstract class PDOMIndexerTask extends PDOMWriter implements IPDOMIndexer try { IIndexFragmentFile file = (IIndexFragmentFile) index.getFile(IndexLocationFactory.getIFL(tu)); if (file != null) - index.clearFile(file, NO_INCLUDES, NO_LOCATIONS); + index.clearFile(file, null); } finally { index.releaseWriteLock(readlocks); } diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index 193d2ddf88b..3a23b441cb1 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -361,8 +361,9 @@ CDTIndexer.nullindexer=No Indexer (search based features will not work correctly CDTIndexer.fastindexer=Fast C/C++ Indexer (recommended) IndexView.name=C/C++ Index -RebuildIndex.name=Rebuild -SyncIndex.name=Update with Modified Files +RebuildIndex.name=&Rebuild +SyncIndex.name=&Update with Modified Files +FreshenIndex.name=&Freshen All Files indexerPage.name = Indexer Page proposalFilter.name = Code Completion Proposal Filter diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 7f6fc4dc6ee..1c7c0f4f4c6 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -711,6 +711,11 @@ + - + - + @@ -748,7 +753,12 @@ - + + - + null. - * @throws CModelException - */ - public static ITranslationUnit findTranslationUnitForLocation(IIndexFileLocation ifl, ICProject preferredProject) throws CModelException { - String fullPath= ifl.getFullPath(); - if (fullPath != null) { - IResource file= ResourcesPlugin.getWorkspace().getRoot().findMember(fullPath); - if (file instanceof IFile) { - return findTranslationUnit((IFile) file); - } - return null; - } - IPath location= IndexLocationFactory.getAbsolutePath(ifl); - if (location != null) { - CoreModel coreModel = CoreModel.getDefault(); - ITranslationUnit tu= null; - if (preferredProject != null) { - tu= coreModel.createTranslationUnitFrom(preferredProject, location); - } - if (tu == null) { - ICProject[] projects= coreModel.getCModel().getCProjects(); - for (int i = 0; i < projects.length && tu == null; i++) { - ICProject project = projects[i]; - if (!project.equals(preferredProject)) { - tu= coreModel.createTranslationUnitFrom(project, location); - } - } - } - return tu; - } - return null; - } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBFile.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBFile.java index 7b9c1c08fe2..20a9b24256f 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBFile.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/includebrowser/IBFile.java @@ -18,11 +18,10 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.cdt.core.index.IIndexFileLocation; import org.eclipse.cdt.core.index.IndexLocationFactory; import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.CoreModelUtil; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.internal.corext.util.CModelUtil; - import org.eclipse.cdt.internal.ui.util.CoreUtility; public class IBFile { @@ -38,7 +37,7 @@ public class IBFile { public IBFile(ICProject preferredProject, IIndexFileLocation location) throws CModelException { fLocation= location; - fTU= CModelUtil.findTranslationUnitForLocation(location, preferredProject); + fTU= CoreModelUtil.findTranslationUnitForLocation(location, preferredProject); String name= fLocation.getURI().getPath(); fName= name.substring(name.lastIndexOf('/')+1); }