diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java index 0df4c0555df..85fcec9cf69 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/CIndex.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2009 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2010 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 @@ -99,12 +99,12 @@ public class CIndex implements IIndex { } else { List result = new ArrayList(); ILinkage[] linkages = Linkage.getIndexerLinkages(); - for (int j = 0; j < linkages.length; j++) { - if (filter.acceptLinkage(linkages[j])) { + for (ILinkage linkage : linkages) { + if (filter.acceptLinkage(linkage)) { IIndexFragmentBinding[][] fragmentBindings = new IIndexFragmentBinding[fPrimaryFragmentCount][]; for (int i = 0; i < fPrimaryFragmentCount; i++) { try { - IBinding[] part = fFragments[i].findBindings(patterns, isFullyQualified, retargetFilter(linkages[j], filter), monitor); + IBinding[] part = fFragments[i].findBindings(patterns, isFullyQualified, retargetFilter(linkage, filter), monitor); fragmentBindings[i] = new IIndexFragmentBinding[part.length]; System.arraycopy(part, 0, fragmentBindings[i], 0, part.length); } catch (CoreException e) { @@ -112,7 +112,7 @@ public class CIndex implements IIndex { fragmentBindings[i] = IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY; } } - ICompositesFactory factory = getCompositesFactory(linkages[j].getLinkageID()); + ICompositesFactory factory = getCompositesFactory(linkage.getLinkageID()); result.add(factory.getCompositeBindings(fragmentBindings)); } } @@ -126,12 +126,12 @@ public class CIndex implements IIndex { } else { List result = new ArrayList(); ILinkage[] linkages = Linkage.getIndexerLinkages(); - for (int j = 0; j < linkages.length; j++) { - if (filter.acceptLinkage(linkages[j])) { + for (ILinkage linkage : linkages) { + if (filter.acceptLinkage(linkage)) { IIndexFragmentBinding[][] fragmentBindings = new IIndexFragmentBinding[fPrimaryFragmentCount][]; for (int i = 0; i < fPrimaryFragmentCount; i++) { try { - IBinding[] part = fFragments[i].findMacroContainers(pattern, retargetFilter(linkages[j], filter), monitor); + IBinding[] part = fFragments[i].findMacroContainers(pattern, retargetFilter(linkage, filter), monitor); fragmentBindings[i] = new IIndexFragmentBinding[part.length]; System.arraycopy(part, 0, fragmentBindings[i], 0, part.length); } catch (CoreException e) { @@ -139,7 +139,7 @@ public class CIndex implements IIndex { fragmentBindings[i] = IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY; } } - ICompositesFactory factory = getCompositesFactory(linkages[j].getLinkageID()); + ICompositesFactory factory = getCompositesFactory(linkage.getLinkageID()); result.add(factory.getCompositeBindings(fragmentBindings)); } } @@ -156,8 +156,7 @@ public class CIndex implements IIndex { } if (bindings.length > 1) { ArrayList multi= new ArrayList(); - for (int i = 0; i < bindings.length; i++) { - IBinding b = bindings[i]; + for (IBinding b : bindings) { multi.addAll(Arrays.asList(findNames(b, flags))); } return multi.toArray(new IIndexName[multi.size()]); @@ -221,8 +220,7 @@ public class CIndex implements IIndex { BitSet linkages= new BitSet(); for (int i = 0; i < fPrimaryFragmentCount; i++) { IIndexFragmentFile[] candidates= fFragments[i].getFiles(location); - for (int j = 0; j < candidates.length; j++) { - IIndexFragmentFile candidate= candidates[j]; + for (IIndexFragmentFile candidate : candidates) { int linkage= candidate.getLinkageID(); if (!linkages.get(linkage) && candidate.hasContent()) { result.add(candidate); @@ -261,12 +259,11 @@ public class CIndex implements IIndex { public void findIncludedBy(List in, List out, int depth, HashSet handled) throws CoreException { List nextLevel= depth != 0 ? new LinkedList() : null; - for (Iterator it= in.iterator(); it.hasNext(); ) { - IIndexFragmentFile file = (IIndexFragmentFile) it.next(); + for (IIndexFile iIndexFile : in) { + IIndexFragmentFile file = (IIndexFragmentFile) iIndexFile; for (int j = 0; j < fPrimaryFragmentCount; j++) { IIndexInclude[] includedBy= fFragments[j].findIncludedBy(file); - for (int k = 0; k < includedBy.length; k++) { - IIndexInclude include = includedBy[k]; + for (IIndexInclude include : includedBy) { if (handled.add(include.getIncludedByLocation())) { out.add(include); if (depth != 0) { @@ -298,11 +295,10 @@ public class CIndex implements IIndex { private void findIncludes(List in, List out, int depth, HashSet handled) throws CoreException { List nextLevel= depth != 0 ? new LinkedList() : null; - for (Iterator it= in.iterator(); it.hasNext(); ) { - IIndexFragmentFile file = (IIndexFragmentFile) it.next(); + for (IIndexFile iIndexFile : in) { + IIndexFragmentFile file = (IIndexFragmentFile) iIndexFile; IIndexInclude[] includes= file.getIncludes(); - for (int k = 0; k < includes.length; k++) { - IIndexInclude include = includes[k]; + for (IIndexInclude include : includes) { IIndexFileLocation target= include.getIncludesLocation(); Object key= target != null ? (Object) target : include.getFullName(); if (handled.add(key)) { @@ -346,8 +342,8 @@ public class CIndex implements IIndex { public synchronized void releaseReadLock() { if (--fReadLock == 0) { - for (int i = 0; i < fFragments.length; i++) { - fFragments[i].releaseReadLock(); + for (IIndexFragment fFragment : fFragments) { + fFragment.releaseReadLock(); } } } @@ -357,8 +353,8 @@ public class CIndex implements IIndex { } public boolean hasWaitingReaders() { - for (int i= 0; i < fFragments.length; i++) { - if (fFragments[i].hasWaitingReaders()) { + for (IIndexFragment fFragment : fFragments) { + if (fFragment.hasWaitingReaders()) { return true; } } @@ -367,8 +363,8 @@ public class CIndex implements IIndex { public long getLastWriteAccess() { long result= 0; - for (int i = 0; i < fFragments.length; i++) { - result= Math.max(result, fFragments[i].getLastWriteAccess()); + for (IIndexFragment fFragment : fFragments) { + result= Math.max(result, fFragment.getLastWriteAccess()); } return result; } @@ -388,12 +384,12 @@ public class CIndex implements IIndex { List result = new ArrayList(); ILinkage[] linkages = Linkage.getIndexerLinkages(); monitor.beginTask(Messages.CIndex_FindBindingsTask_label, fFragments.length * linkages.length); - for (int j = 0; j < linkages.length; j++) { - if (filter.acceptLinkage(linkages[j])) { + for (ILinkage linkage : linkages) { + if (filter.acceptLinkage(linkage)) { IIndexFragmentBinding[][] fragmentBindings = new IIndexFragmentBinding[fPrimaryFragmentCount][]; for (int i = 0; i < fPrimaryFragmentCount; i++) { try { - IBinding[] part = fFragments[i].findBindings(names, retargetFilter(linkages[j], filter), new SubProgressMonitor(monitor, 1)); + IBinding[] part = fFragments[i].findBindings(names, retargetFilter(linkage, filter), new SubProgressMonitor(monitor, 1)); fragmentBindings[i] = new IIndexFragmentBinding[part.length]; System.arraycopy(part, 0, fragmentBindings[i], 0, part.length); } catch (CoreException e) { @@ -401,7 +397,7 @@ public class CIndex implements IIndex { fragmentBindings[i] = IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY; } } - ICompositesFactory factory = getCompositesFactory(linkages[j].getLinkageID()); + ICompositesFactory factory = getCompositesFactory(linkage.getLinkageID()); result.add(factory.getCompositeBindings(fragmentBindings)); } } @@ -460,8 +456,8 @@ public class CIndex implements IIndex { public IIndexFragmentBinding[] findEquivalentBindings(IBinding binding) throws CoreException { List result = new ArrayList(); - for (int i = 0; i < fFragments.length; i++) { - IIndexFragmentBinding adapted = fFragments[i].adaptBinding(binding); + for (IIndexFragment fFragment : fFragments) { + IIndexFragmentBinding adapted = fFragment.adaptBinding(binding); if (adapted != null) { result.add(adapted); } @@ -513,12 +509,12 @@ public class CIndex implements IIndex { } else { List result = new ArrayList(); ILinkage[] linkages = Linkage.getIndexerLinkages(); - for (int j = 0; j < linkages.length; j++) { - if (filter.acceptLinkage(linkages[j])) { + for (ILinkage linkage : linkages) { + if (filter.acceptLinkage(linkage)) { IIndexFragmentBinding[][] fragmentBindings = new IIndexFragmentBinding[fPrimaryFragmentCount][]; for (int i = 0; i < fPrimaryFragmentCount; i++) { try { - IBinding[] part = fFragments[i].findBindingsForPrefix(prefix, filescope, retargetFilter(linkages[j], filter), monitor); + IBinding[] part = fFragments[i].findBindingsForPrefix(prefix, filescope, retargetFilter(linkage, filter), monitor); fragmentBindings[i] = new IIndexFragmentBinding[part.length]; System.arraycopy(part, 0, fragmentBindings[i], 0, part.length); } catch (CoreException e) { @@ -526,7 +522,7 @@ public class CIndex implements IIndex { fragmentBindings[i] = IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY; } } - ICompositesFactory factory = getCompositesFactory(linkages[j].getLinkageID()); + ICompositesFactory factory = getCompositesFactory(linkage.getLinkageID()); result.add(factory.getCompositeBindings(fragmentBindings)); } } @@ -540,12 +536,12 @@ public class CIndex implements IIndex { } else { List result = new ArrayList(); ILinkage[] linkages = Linkage.getIndexerLinkages(); - for (int j = 0; j < linkages.length; j++) { - if (filter.acceptLinkage(linkages[j])) { + for (ILinkage linkage : linkages) { + if (filter.acceptLinkage(linkage)) { IIndexFragmentBinding[][] fragmentBindings = new IIndexFragmentBinding[fPrimaryFragmentCount][]; for (int i = 0; i < fPrimaryFragmentCount; i++) { try { - IBinding[] part = fFragments[i].findBindings(name, filescope, retargetFilter(linkages[j], filter), monitor); + IBinding[] part = fFragments[i].findBindings(name, filescope, retargetFilter(linkage, filter), monitor); fragmentBindings[i] = new IIndexFragmentBinding[part.length]; System.arraycopy(part, 0, fragmentBindings[i], 0, part.length); } catch (CoreException e) { @@ -553,7 +549,7 @@ public class CIndex implements IIndex { fragmentBindings[i] = IIndexFragmentBinding.EMPTY_INDEX_BINDING_ARRAY; } } - ICompositesFactory factory = getCompositesFactory(linkages[j].getLinkageID()); + ICompositesFactory factory = getCompositesFactory(linkage.getLinkageID()); result.add(factory.getCompositeBindings(fragmentBindings)); } } @@ -582,8 +578,7 @@ public class CIndex implements IIndex { HashSet allowedFiles= new HashSet(); try { IIndexMacro[] macros= fFragments[i].findMacros(name, isPrefix, caseSensitive, filter, new SubProgressMonitor(monitor, 1)); - for (int k = 0; k < macros.length; k++) { - IIndexMacro indexMacro = macros[k]; + for (IIndexMacro indexMacro : macros) { IIndexFile file= indexMacro.getFile(); if (!allowedFiles.contains(file)) { if (handledIFLs.add(file.getLocation())) { @@ -604,27 +599,27 @@ public class CIndex implements IIndex { public long getCacheHits() { long result= 0; - for (int i = 0; i < fFragments.length; i++) { - result+= fFragments[i].getCacheHits(); + for (IIndexFragment fFragment : fFragments) { + result+= fFragment.getCacheHits(); } return result; } public long getCacheMisses() { long result= 0; - for (int i = 0; i < fFragments.length; i++) { - result+= fFragments[i].getCacheMisses(); + for (IIndexFragment fFragment : fFragments) { + result+= fFragment.getCacheMisses(); } return result; } public void resetCacheCounters() { - for (int i = 0; i < fFragments.length; i++) { - fFragments[i].resetCacheCounters(); + for (IIndexFragment fFragment : fFragments) { + fFragment.resetCacheCounters(); } } - void clearResultCaches() { + protected void clearResultCache() { for (IIndexFragment frag : fFragments) { frag.clearResultCache(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndex.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndex.java index b125d44838e..a8f2c806cdf 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndex.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IWritableIndex.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2010 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 @@ -10,7 +10,6 @@ * Andrew Ferguson (Symbian) * Sergey Prigogin (Google) *******************************************************************************/ - package org.eclipse.cdt.internal.core.index; import java.util.Collection; @@ -151,4 +150,9 @@ public interface IWritableIndex extends IIndex { * Returns the size of the database in bytes. */ long getDatabaseSizeBytes(); + + /** + * Clears the result cache, caller needs to hold a write-lock. + */ + void clearResultCache(); } 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 ad7d877db3f..eef6cdb0c14 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2010 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 @@ -10,7 +10,6 @@ * Andrew Ferguson (Symbian) * Sergey Prigogin (Google) *******************************************************************************/ - package org.eclipse.cdt.internal.core.index; import java.util.Collection; @@ -134,11 +133,19 @@ public class WritableCIndex extends CIndex implements IWritableIndex { assert fIsWriteLocked: "No write lock to be released"; //$NON-NLS-1$ assert establishReadlockCount == getReadLockCount(): "Unexpected read lock is not allowed"; //$NON-NLS-1$ + // Bug 297641: Result cache of read only providers needs to be cleared. + if (establishReadlockCount == 0) { + clearResultCache(); + } + fIsWriteLocked= false; fWritableFragment.releaseWriteLock(establishReadlockCount, flush); - - // Bug 297641: Result cache of read only providers needs to be cleared. - clearResultCaches(); + } + + @Override + public void clearResultCache() { + assert fIsWriteLocked: "Need to hold a write lock to clear result caches"; //$NON-NLS-1$ + super.clearResultCache(); } public void flush() throws CoreException { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java index 56652441d7a..b687fe1a6d4 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/PDOM.java @@ -863,7 +863,10 @@ public class PDOM extends PlatformObject implements IPDOM { @SuppressWarnings("nls") public void releaseWriteLock(int establishReadLocks, boolean flush) { - clearResultCache(); + // When all locks are released we can clear the result cache. + if (establishReadLocks == 0) { + clearResultCache(); + } try { db.giveUpExclusiveLock(flush); } catch (CoreException e) { 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 e5abcf63fb6..f81cab32664 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2010 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 @@ -219,6 +219,11 @@ abstract public class PDOMWriter { } catch (AssertionError e) { th= e; } finally { + // When the caller holds a read-lock, the result cache of the index is never cleared. + // ==> Before releasing the lock for the last time in this ast, we clear the result cache. + if (readlockCount > 0 && i == ifls.length-1) { + index.clearResultCache(); + } lock.release(); } if (th != null) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPClassSpecialization.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPClassSpecialization.java index e7f77ef22bb..7a6e7046063 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPClassSpecialization.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/dom/cpp/PDOMCPPClassSpecialization.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2009 QNX Software Systems and others. + * Copyright (c) 2007, 2010 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 @@ -151,12 +151,14 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization implements } public void addBase(PDOMCPPBase base) throws CoreException { + getPDOM().removeCachedResult(record+PDOMCPPLinkage.CACHE_BASES); PDOMCPPBase firstBase = getFirstBase(); base.setNextBase(firstBase); setFirstBase(base); } public void removeBase(PDOMName pdomName) throws CoreException { + getPDOM().removeCachedResult(record+PDOMCPPLinkage.CACHE_BASES); PDOMCPPBase base= getFirstBase(); PDOMCPPBase predecessor= null; long nameRec= pdomName.getRecord();