diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java index 72458b7ce0f..bb6d1dbf0e7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java @@ -14,7 +14,6 @@ package org.eclipse.cdt.internal.core.index; import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; @@ -26,6 +25,7 @@ import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.index.IIndex; import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexInclude; +import org.eclipse.cdt.core.index.IIndexMacro; import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.ICodeReaderCache; @@ -33,6 +33,7 @@ import org.eclipse.cdt.core.parser.IMacro; import org.eclipse.cdt.core.parser.IScanner; import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.internal.core.parser.scanner2.ObjectStyleMacro; +import org.eclipse.cdt.internal.core.pdom.dom.PDOMMacro; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; @@ -46,7 +47,6 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { private final IIndex index; private Map fileInfoCache = new HashMap(); // filename, fileInfo private List usedMacros = new ArrayList(); - private Collection fPathCollector; private static final char[] EMPTY_CHARS = new char[0]; @@ -55,6 +55,7 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { private FileInfo() {} public IIndexFile fFile= null; public IMacro[] fMacros= null; +// public FileInfo[] fFileInfos= null; private boolean fRequested= false; public boolean isRequested() { @@ -75,9 +76,6 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { } public CodeReader createCodeReaderForTranslationUnit(String path) { - if (fPathCollector != null) { - fPathCollector.add(path); - } return ParserUtil.createReader(path, null); } @@ -108,7 +106,13 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { FileInfo fi = (FileInfo) iter.next(); if (fi.fMacros == null) { assert fi.fFile != null; - fi.fMacros= fi.fFile.getMacros(); + IIndexMacro[] macros= fi.fFile.getMacros(); + IMacro[] converted= new IMacro[macros.length]; + for (int i = 0; i < macros.length; i++) { + IIndexMacro macro = macros[i]; + converted[i]= ((PDOMMacro)macro).getMacro(); + } + fi.fMacros= converted; } for (int i = 0; i < fi.fMacros.length; ++i) { scanner.addDefinition(fi.fMacros[i]); @@ -126,9 +130,6 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { // still try to parse the file. } - if (fPathCollector != null) { - fPathCollector.add(canonicalPath); - } return ParserUtil.createReader(canonicalPath, null); } @@ -187,8 +188,4 @@ public class IndexBasedCodeReaderFactory implements ICodeReaderFactory { public FileInfo createFileInfo(ITranslationUnit tu) throws CoreException { return createInfo(tu.getLocation().toOSString(), null); } - - public void setPathCollector(Collection paths) { - fPathCollector= paths; - } } 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 db7afcf2603..d1da4ad501b 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 @@ -14,13 +14,11 @@ package org.eclipse.cdt.internal.core.pdom.indexer; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; -import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; -import java.util.Set; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.IPDOMIndexerTask; @@ -137,6 +135,53 @@ public abstract class PDOMIndexerTask implements IPDOMIndexerTask { } } + protected void parseTUs(Collection sources, Collection headers, IProgressMonitor monitor) throws CoreException, InterruptedException { + // sources first + Iterator iter; + for (iter = sources.iterator(); iter.hasNext();) { + if (monitor.isCanceled()) + return; + ITranslationUnit tu = (ITranslationUnit) iter.next(); + String path = tu.getLocation().toOSString(); + if (needToUpdate(path)) { + parseTU(tu, monitor); + } + } + + // headers with context + for (iter = headers.iterator(); iter.hasNext();) { + if (monitor.isCanceled()) + return; + ITranslationUnit tu = (ITranslationUnit) iter.next(); + String path = tu.getLocation().toOSString(); + if (!needToUpdate(path)) { + iter.remove(); + } + else { + ITranslationUnit context= findContext(getIndex(), path); + if (context != null) { + parseTU(context, monitor); + } + } + } + + // headers without context + if (getIndexAllFiles()) { + for (iter = headers.iterator(); iter.hasNext();) { + if (monitor.isCanceled()) + return; + ITranslationUnit tu = (ITranslationUnit) iter.next(); + String path = tu.getLocation().toOSString(); + if (!needToUpdate(path)) { + iter.remove(); + } + else { + parseTU(tu, monitor); + } + } + } + } + protected void parseTU(ITranslationUnit tu, IProgressMonitor pm) throws CoreException, InterruptedException { IPath path= tu.getPath(); try { @@ -246,40 +291,101 @@ public abstract class PDOMIndexerTask implements IPDOMIndexerTask { return fCompletedSources; } - /** - * Extracts a map of names from the ast and returns an array defining the order in - * which the symbols should be added to the index. - * @since 4.0 - */ - protected String[] extractSymbols(IASTTranslationUnit ast, Set legalPaths, final LinkedHashMap symbolMap) { - // includes + + protected void addSymbols(IASTTranslationUnit ast, IProgressMonitor pm) throws InterruptedException, CoreException { + final Map symbolMap= new HashMap(); + + String[] orderedPaths= extractSymbols(ast, symbolMap); + for (int i=0; i= 0; --i) { + for (int i= 0; i < includes.length; i++) { IASTPreprocessorIncludeStatement include = includes[i]; IASTFileLocation sourceLoc = include.getFileLocation(); - String path= sourceLoc != null ? sourceLoc.getFileName() : astFilePath; // command-line includes - if (legalPaths.contains(path)) { - prepareInMap(symbolMap, path); - addToMap(symbolMap, 0, path, include); + String newPath= sourceLoc != null ? sourceLoc.getFileName() : astFilePath; // command-line includes + while (!stack.isEmpty() && !currentPath.equals(newPath)) { + if (needToUpdate(currentPath)) { + prepareInMap(symbolMap, currentPath); + orderedIncludes.add(currentPath); + } + currentPath= (String) stack.remove(stack.size()-1); } - path= include.getPath(); - if (legalPaths.contains(path)) { - prepareInMap(symbolMap, path); + if (needToUpdate(newPath)) { + prepareInMap(symbolMap, newPath); + addToMap(symbolMap, 0, newPath, include); + } + stack.add(currentPath); + currentPath= include.getPath(); + } + stack.add(currentPath); + while (!stack.isEmpty()) { + currentPath= (String) stack.remove(stack.size()-1); + if (needToUpdate(currentPath)) { + prepareInMap(symbolMap, currentPath); + orderedIncludes.add(currentPath); } } - if (legalPaths.contains(astFilePath)) { - prepareInMap(symbolMap, ast.getFilePath()); - } - + // macros IASTPreprocessorMacroDefinition[] macros = ast.getMacroDefinitions(); - for (int i = 0; i < macros.length; ++i) { - IASTPreprocessorMacroDefinition macro = macros[i]; + for (int i2 = 0; i2 < macros.length; ++i2) { + IASTPreprocessorMacroDefinition macro = macros[i2]; IASTFileLocation sourceLoc = macro.getFileLocation(); if (sourceLoc != null) { // skip built-ins and command line macros - String path = sourceLoc.getFileName(); - addToMap(symbolMap, 1, path, macro); + String path2 = sourceLoc.getFileName(); + addToMap(symbolMap, 1, path2, macro); } } @@ -292,17 +398,16 @@ public abstract class PDOMIndexerTask implements IPDOMIndexerTask { } } }); - - Collection temp= symbolMap.keySet(); - int size= temp.size(); - String[] result= new String[temp.size()]; - for (Iterator iter = temp.iterator(); iter.hasNext();) { - result[--size]= (String) iter.next(); - } - return result; + return (String[]) orderedIncludes.toArray(new String[orderedIncludes.size()]); } - private void addToMap(HashMap map, int idx, String path, Object thing) { + protected abstract IWritableIndex getIndex(); + protected abstract int getReadlockCount(); + protected abstract boolean needToUpdate(String path) throws CoreException; + protected abstract boolean postAddToIndex(String path, IIndexFile file) throws CoreException; + + + private void addToMap(Map map, int idx, String path, Object thing) { List[] lists= (List[]) map.get(path); if (lists != null) lists[idx].add(thing); @@ -316,20 +421,7 @@ public abstract class PDOMIndexerTask implements IPDOMIndexerTask { return false; } - protected void prepareIndexInsertion(String path, Map symbolMap) { - ArrayList[] arrayLists = ((ArrayList[]) symbolMap.get(path)); - - // reverse the includes - Collections.reverse(arrayLists[0]); - - // resolve the names - ArrayList names= arrayLists[2]; - for (int j=0; j