1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 06:45:43 +02:00

Patch for Devin Steffler.

Code Reader Cache for new parser framework.
This commit is contained in:
John Camelon 2005-03-22 17:00:43 +00:00
parent 4bc76bbea7
commit 6759040739
17 changed files with 866 additions and 15 deletions

View file

@ -0,0 +1,58 @@
/*******************************************************************************
* Copyright (c) 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.tests.FileBasePluginTest;
import org.eclipse.core.resources.IFile;
/**
* @author dsteffle
*/
public class CDOMBaseTest extends FileBasePluginTest {
public CDOMBaseTest(String name, Class className) {
super(name, className);
}
protected IASTTranslationUnit parse(String fileName, String contents) {
IFile file = null;
try {
file = importFile(fileName, contents);
} catch (Exception e) {
e.printStackTrace();
}
return parse(file);
}
protected IASTTranslationUnit parse(IFile file) {
IASTTranslationUnit tu = null;
try {
tu = CDOM.getInstance().getASTService().getTranslationUnit(
file,
CDOM.getInstance().getCodeReaderFactory(
CDOM.PARSE_SAVED_RESOURCES));
} catch (UnsupportedDialectException e) {
e.printStackTrace();
}
return tu;
}
}

View file

@ -0,0 +1,143 @@
/**********************************************************************
* Copyright (c) 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.core.parser.tests.ast2;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.CodeReaderCache;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
/**
* @author dsteffle
*/
public class CodeReaderCacheTest extends CDOMBaseTest {
public CodeReaderCacheTest(String name) {
super(name, CodeReaderCacheTest.class);
}
public static Test suite() {
TestSuite suite = new TestSuite( CodeReaderCacheTest.class );
suite.addTest( new CodeReaderCacheTest("cleanupProject") ); //$NON-NLS-1$
return suite;
}
private class UpdateFileJob extends Job {
private IFile file = null;
private String fileName = null;
private String code = null;
public UpdateFileJob(String name, IFile file, String fileName, String code) {
super(name);
this.file = file;
this.fileName = fileName;
this.code = code;
}
protected IStatus run(IProgressMonitor monitor) {
while(!monitor.isCanceled()) {
try {
file = importFile(fileName, code);
} catch (Exception e) {
}
}
return Status.OK_STATUS;
}
public IFile getFile() {
return file;
}
}
// THIS MUST BE RUN FIRST IN THIS TEST
public void testSetCacheSize() {
ICodeReaderCache cache = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES).getCodeReaderCache();
// update the size of the cache... must be done for the first test since other test suites use 0MB cache size
assertTrue(cache instanceof CodeReaderCache);
((CodeReaderCache)cache).setCacheSize(CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
}
public void testSimpleCacheFunctionality() {
StringBuffer code = new StringBuffer();
code.append("int x;"); //$NON-NLS-1$
IFile file = null;
try {
file = importFile("test.c", code.toString()); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
parse(file);
ICodeReaderCache cache = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES).getCodeReaderCache();
CodeReader reader = cache.get(file.getLocation().toOSString());
assertNotNull(reader);
assertEquals(cache.getCurrentSpace(), 1);
assertEquals(String.valueOf(reader.filename), file.getLocation().toOSString());
cache.remove(String.valueOf(reader.filename));
assertEquals(cache.getCurrentSpace(), 0);
}
public void testResourceChangedUpdate() {
boolean hasPassed = false;
StringBuffer code = new StringBuffer();
code.append("int x;"); //$NON-NLS-1$
ICodeReaderCache cache = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES).getCodeReaderCache();
IFile file = null;
try {
file = importFile("test.c", code.toString()); //$NON-NLS-1$
} catch (Exception e) {
e.printStackTrace();
}
// start a new job that repeatedly updates the file...
UpdateFileJob job = new UpdateFileJob("updater", file, "test.c", code.toString()); //$NON-NLS-1$ //$NON-NLS-2$
job.schedule();
while(!hasPassed) {
if (file != null) {
parse(file);
}
try {
Thread.sleep(1000); // give the updater thread some time to update the resource
file = job.getFile();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (cache.getCurrentSpace() == 0) // item was properly removed by the updater thread
hasPassed = true;
}
job.cancel();
}
// THIS MUST BE RUN LAST IN THIS TEST
public void testClearCache() {
ICodeReaderCache cache = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES).getCodeReaderCache();
// now that the
assertTrue(cache instanceof CodeReaderCache);
((CodeReaderCache)cache).setCacheSize(0);
}
}

View file

@ -35,6 +35,7 @@ public class DOMParserTestSuite extends TestCase {
suite.addTestSuite( AST2UtilTests.class ); suite.addTestSuite( AST2UtilTests.class );
suite.addTestSuite( AST2UtilOldTests.class ); suite.addTestSuite( AST2UtilOldTests.class );
suite.addTestSuite( AST2SelectionParseTest.class ); suite.addTestSuite( AST2SelectionParseTest.class );
suite.addTestSuite( CodeReaderCacheTest.class );
return suite; return suite;
} }

View file

@ -64,6 +64,16 @@ public abstract class OverflowingLRUCache extends LRUCache {
* Inital load factor of one third. * Inital load factor of one third.
*/ */
protected double fLoadFactor = 0.333; protected double fLoadFactor = 0.333;
/**
* Creates an OverflowingLRUCache with default sizes.
*
* This is required to create a cache with a hash map that is not
* dependent on the initial size of the cache (i.e. if the cache is
* relative to size of entries and not the number of entries).
*/
public OverflowingLRUCache() {
super();
}
/** /**
* Creates a OverflowingLRUCache. * Creates a OverflowingLRUCache.
* @param size Size limit of cache. * @param size Size limit of cache.

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.core.dom; package org.eclipse.cdt.core.dom;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
/** /**
* This is the interface that an AST Service uses to delegate the construction * This is the interface that an AST Service uses to delegate the construction
@ -42,4 +43,10 @@ public interface ICodeReaderFactory {
* @return CodeReader for contents at that path. * @return CodeReader for contents at that path.
*/ */
public CodeReader createCodeReaderForInclusion(String path); public CodeReader createCodeReaderForInclusion(String path);
/**
* Returns the ICodeReaderCache used for this ICodeReaderFacotry.
* @return the ICodeReaderCache used for this ICodeReaderFacotry
*/
public ICodeReaderCache getCodeReaderCache();
} }

View file

@ -0,0 +1,299 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corporation and others. All rights
* reserved. This program and the accompanying materials are made available
* under the terms of the Common Public License v0.5 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors: Rational Software - Initial API and implementation
******************************************************************************/
package org.eclipse.cdt.core.parser;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator;
import org.eclipse.cdt.internal.core.util.ILRUCacheable;
import org.eclipse.cdt.internal.core.util.LRUCache;
import org.eclipse.cdt.internal.core.util.OverflowingLRUCache;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
/**
* This is the CodeReaderBuffer used to cache CodeReaders for the ICodeReaderFactory
* when working with saved copies (primarily SavedCodeReaderFactory).
*
* @author dsteffle
*/
public class CodeReaderCache implements ICodeReaderCache {
/**
* The string used to identify this CodeReaderCache. Mainly used for preferences.
*/
public static final String CODE_READER_BUFFER="CODE_READER_CACHE"; //$NON-NLS-1$
/**
* The default size of the cache in MB.
*/
public static final int DEFAULT_CACHE_SIZE_IN_MB = 64;
/**
* A String value of the default size of the cache.
*/
public static final String DEFAULT_CACHE_SIZE_IN_MB_STRING = String.valueOf(DEFAULT_CACHE_SIZE_IN_MB);
private static final int MB_TO_KB_FACTOR = 1024;
private CodeReaderLRUCache cache = null; // the actual cache
private class UpdateCodeReaderCacheListener implements IResourceChangeListener {
ICodeReaderCache cache = null;
/**
* Create the UpdateCodeReaderCacheListener used to dispatch events to remove CodeReaders
* from the cache when a resource is changed and detected in Eclipse.
* @param cache
*/
public UpdateCodeReaderCacheListener(ICodeReaderCache cache) {
this.cache = cache;
}
private class RemoveCacheJob extends Job {
private static final String REMOVE_CACHE = "Remove Cache"; //$NON-NLS-1$
ICodeReaderCache cache = null;
IResourceChangeEvent event = null;
/**
* Create a RemoveCacheJob used to run as a separate thread to remove a CodeReader from the cache.
* @param cache
* @param event
* @param mutex
*/
public RemoveCacheJob(ICodeReaderCache cache, IResourceChangeEvent event) {
super(REMOVE_CACHE);
this.cache = cache;
this.event = event;
}
protected IStatus run(IProgressMonitor monitor) {
String key = null;
if (event.getSource() instanceof IWorkspace && event.getDelta() != null) {
IResourceDelta[] projects = event.getDelta().getAffectedChildren();
for(int i=0; i<projects.length; i++) {
if (projects[i].getResource().getType() == IResource.PROJECT) {
IResourceDelta[] files = projects[i].getAffectedChildren();
for(int j=0; j<files.length; j++) {
if (files[j].getResource() instanceof IFile && ((IFile)files[j].getResource()).getLocation() != null) {
key = ((IFile)files[j].getResource()).getLocation().toOSString();
}
}
}
}
}
if (key != null && cache != null)
cache.remove(key);
return Status.OK_STATUS;
}
}
/**
* Identifies when a resource was chaned and schedules a new RemoveCacheJob.
*/
public void resourceChanged(IResourceChangeEvent event) {
if (cache instanceof CodeReaderCache)
new RemoveCacheJob(cache, event).schedule();
}
}
/**
* Creates a CodeReaderCache and sets the size of the CodeReaderCache in MB. Creating a new
* CodeReaderCache also adds an UpdateCodeReaderCacheListener to the workspace so that when
* a resource is changed then the CodeReader for that resource is removed from this cache.
*
* @param size initial size of the CodeReaderCache in terms of MB
*/
public CodeReaderCache(int size) {
cache = new CodeReaderLRUCache(size * MB_TO_KB_FACTOR);
if (ResourcesPlugin.getWorkspace() != null)
ResourcesPlugin.getWorkspace().addResourceChangeListener(new UpdateCodeReaderCacheListener(this));
}
/**
* Get a CodeReader from the cache. The key is the char[] filename of the CodeReader to retrieve.
* @param key the path of the CodeReader to retrieve
* @return
*/
public synchronized CodeReader get(String key) {
CodeReader ret = null;
if (cache.getSpaceLimit() > 0)
ret = cache.get(key);
// not in the cache
if (ret == null) {
ret = ParserUtil.createReader(key, EmptyIterator.EMPTY_ITERATOR);
if (cache.getSpaceLimit() > 0)
put(ret);
}
return ret;
}
/**
* Put a CodeReader into the Cache.
* @param key
* @param value
* @return
*/
private synchronized CodeReader put(CodeReader value) {
if (value==null) return null;
return cache.put(String.valueOf(value.filename), value);
}
/**
* Sets the max cache size of this cache in terms of MB.
* @param size
*/
public void setCacheSize(int size) {
cache.setSpaceLimit(size * MB_TO_KB_FACTOR);
}
/**
* This class is a wrapper/implementor class for OverflowingLRUCache.
*
* It uses CodeReaderCacheEntry (which implements ILRUCacheable) to specify that the size of
* the cache should be relative to the size of the entries and not the number of entries.
*
* @author dsteffle
*
*/
private class CodeReaderLRUCache extends OverflowingLRUCache {
/**
* This is a wrapper for entries to put into the OverflowingLRUCache (required to determine the
* size of entries relative to the CodeReader's file size).
*
* Although the size of the CodeReaderCache is specified in terms of MB, the actual granularity of
* the cache is KB.
*
* @author dsteffle
*
*/
private class CodeReaderCacheEntry implements ILRUCacheable {
private static final double CHAR_TO_KB_FACTOR = 1024;
CodeReader reader = null;
int size = 0; // used to specify the size of the CodeReader in terms of KB
public CodeReaderCacheEntry(CodeReader value) {
this.reader = value;
size = (int)Math.ceil(reader.buffer.length / CHAR_TO_KB_FACTOR); // get the size of the file in terms of KB
}
public int getCacheFootprint() {
return size;
}
public CodeReader getCodeReader() {
return reader;
}
}
/**
* Creates a new CodeReaderLRUCache with a specified initial maximum size.
* @param size the maximum size of the cache in terms of MB
*/
public CodeReaderLRUCache(int size) {
super(); // need to initialize the LRUCache with super() so that the size of the hashtable isn't relative to the size in MB
this.setSpaceLimit(size);
}
// must be overloaded, required to remove entries from the cache
protected boolean close(LRUCacheEntry entry) {
Object obj = remove(entry._fKey);
if (obj != null)
return true;
return false;
}
protected LRUCache newInstance(int size, int overflow) {
return null;
}
/**
* Removes an entry from the cache and returns the entry that was removed if found.
* Otherwise null is returned.
* @param key
* @return
*/
public CodeReader remove(String key) {
Object removed = removeKey(key);
if (removed instanceof CodeReaderCacheEntry)
return ((CodeReaderCacheEntry)removed).getCodeReader();
return null;
}
/**
* Puts a CodeReader into the cache by wrapping it with a CodeReaderCacheEntry first.
* This way the proper size of the element in the cache can be determined
* via the CodeReaderCacheEntry.
* @param key
* @param value
* @return
*/
public CodeReader put(Object key, CodeReader value) {
Object entry = new CodeReaderCacheEntry(value);
Object ret = put(key, entry);
if (ret instanceof CodeReaderCacheEntry)
return ((CodeReaderCacheEntry)ret).getCodeReader();
return null;
}
/**
* Retrieves a CodeReader from the cache corresponding to the path specified by the key.
* @param key
* @return
*/
public CodeReader get(String key) {
Object obj = peek(key);
if (obj instanceof CodeReaderCacheEntry)
return ((CodeReaderCacheEntry)obj).getCodeReader();
return null;
}
}
/**
* Removes the CodeReader from the cache corresponding to the path specified by the key and
* returns the CodeReader that was removed. If no CodeReader is removed then null is returned.
* @param key
*/
public synchronized CodeReader remove(String key) {
return cache.remove(key);
}
/**
* Returns the current size of the cache. For the CodeReaderCache this is in MB.
* @return
*/
public int getCurrentSpace() {
return cache.getCurrentSpace();
}
}

View file

@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corporation and others. All rights
* reserved. This program and the accompanying materials are made available
* under the terms of the Common Public License v0.5 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors: Rational Software - Initial API and implementation
******************************************************************************/
package org.eclipse.cdt.core.parser;
import java.util.Iterator;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator;
/**
* This is an empty implementation of the ICodeReaderCache interface. It is used to implement a
* cache for the interface that isn't actually a cache, but rather always creates new CodeReaders
* everytime a CodeReader is retrieved.
*
* This cache is not optimized to be run from within Eclipse (i.e. it ignores IResources).
*
* @author dsteffle
*/
public class EmptyCodeReaderCache implements ICodeReaderCache {
/**
* Creates a new CodeReader and returns it.
* @param key
* @return
*/
public CodeReader get(String key) {
CodeReader ret = null;
ret = InternalParserUtil.createFileReader(key);
return ret;
}
/**
* This provides support for PartialWorkingCopyCodeReaderFactory.
* @param finalPath
* @param workingCopies
* @return
*/
public CodeReader createReader( String finalPath, Iterator workingCopies ) {
return ParserUtil.createReader(finalPath, workingCopies);
}
/**
* Returns null.
* @param key
* @return
*/
public CodeReader remove(String key) {
return null;
}
/**
* Returns 0.
*/
public int getCurrentSpace() {
return 0;
}
}

View file

@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2005 Rational Software Corporation and others. All rights
* reserved. This program and the accompanying materials are made available
* under the terms of the Common Public License v0.5 which accompanies this
* distribution, and is available at http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors: Rational Software - Initial API and implementation
******************************************************************************/
package org.eclipse.cdt.core.parser;
/**
* This is the interface to a cache for CodeReaders.
*
* For thread safety the implementations of this interface must ensure that their methods are thread safe.
*
* @author dsteffle
*/
public interface ICodeReaderCache {
/**
* Retrieves the CodeReader corresponding to the key specified that represents the
* path for that CodeReader. If no CodeReader is found in the cache then a new CodeReader
* is created for the path and then returned.
*
* @param key the path corresponding to the CodeReader, generally:
* fileToParse.getLocation().toOSString()
* @return the CodeReader corresponding to the path specified by the key
*/
public CodeReader get(String key);
/**
* Used to remove the CodeReader corresponding to the path specified by key from the cache.
*
* @param key the path of the CodeReader to be removed
* @return the removed CodeReader or null if not found
*/
public CodeReader remove(String key);
/**
* Returns the amount of space that the cache is using.
* The units are relative to the implementation of the cache. It could be
* the total number of objects in the cache, or the total space the cache is
* using in MB.
*
* @return
*/
public int getCurrentSpace();
}

View file

@ -12,7 +12,9 @@ package org.eclipse.cdt.internal.core.parser.scanner2;
import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil; import org.eclipse.cdt.core.parser.CodeReaderCache;
import org.eclipse.cdt.core.parser.EmptyCodeReaderCache;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
/** /**
* @author jcamelon * @author jcamelon
@ -20,9 +22,11 @@ import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
public class FileCodeReaderFactory implements ICodeReaderFactory { public class FileCodeReaderFactory implements ICodeReaderFactory {
private static FileCodeReaderFactory instance; private static FileCodeReaderFactory instance;
private ICodeReaderCache cache = null;
private FileCodeReaderFactory() private FileCodeReaderFactory(ICodeReaderCache cache)
{ {
this.cache = cache;
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -36,14 +40,14 @@ public class FileCodeReaderFactory implements ICodeReaderFactory {
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String)
*/ */
public CodeReader createCodeReaderForTranslationUnit(String path) { public CodeReader createCodeReaderForTranslationUnit(String path) {
return InternalParserUtil.createFileReader(path); return cache.get(path);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/ */
public CodeReader createCodeReaderForInclusion(String path) { public CodeReader createCodeReaderForInclusion(String path) {
return InternalParserUtil.createFileReader(path); return cache.get(path);
} }
/** /**
@ -51,8 +55,12 @@ public class FileCodeReaderFactory implements ICodeReaderFactory {
*/ */
public static FileCodeReaderFactory getInstance() { public static FileCodeReaderFactory getInstance() {
if( instance == null ) if( instance == null )
instance = new FileCodeReaderFactory(); instance = new FileCodeReaderFactory(new EmptyCodeReaderCache());
return instance; return instance;
} }
public ICodeReaderCache getCodeReaderCache() {
return cache;
}
} }

View file

@ -17,7 +17,8 @@ import org.eclipse.cdt.core.browser.IWorkingCopyProvider;
import org.eclipse.cdt.core.dom.CDOM; import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.parser.EmptyCodeReaderCache;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator; import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator;
/** /**
@ -27,12 +28,14 @@ public class PartialWorkingCopyCodeReaderFactory
implements ICodeReaderFactory { implements ICodeReaderFactory {
private final IWorkingCopyProvider provider; private final IWorkingCopyProvider provider;
private ICodeReaderCache cache = null;
/** /**
* @param provider * @param provider
*/ */
public PartialWorkingCopyCodeReaderFactory(IWorkingCopyProvider provider) { public PartialWorkingCopyCodeReaderFactory(IWorkingCopyProvider provider) {
this.provider = provider; this.provider = provider;
cache = new EmptyCodeReaderCache();
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -46,14 +49,14 @@ public class PartialWorkingCopyCodeReaderFactory
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String)
*/ */
public CodeReader createCodeReaderForTranslationUnit(String path) { public CodeReader createCodeReaderForTranslationUnit(String path) {
return ParserUtil.createReader( path, createWorkingCopyIterator() ); return ((EmptyCodeReaderCache)cache).createReader( path, createWorkingCopyIterator() );
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/ */
public CodeReader createCodeReaderForInclusion(String path) { public CodeReader createCodeReaderForInclusion(String path) {
return ParserUtil.createReader( path, EmptyIterator.EMPTY_ITERATOR ); return cache.get( path );
} }
/** /**
@ -64,4 +67,11 @@ public class PartialWorkingCopyCodeReaderFactory
return Arrays.asList( provider.getWorkingCopies() ).iterator(); return Arrays.asList( provider.getWorkingCopies() ).iterator();
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#getCodeReaderCache()
*/
public ICodeReaderCache getCodeReaderCache() {
return cache;
}
} }

View file

@ -10,17 +10,20 @@
**********************************************************************/ **********************************************************************/
package org.eclipse.cdt.internal.core.dom; package org.eclipse.cdt.internal.core.dom;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.CDOM; import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.ICodeReaderFactory; import org.eclipse.cdt.core.dom.ICodeReaderFactory;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.parser.CodeReaderCache;
import org.eclipse.cdt.internal.core.parser.ast.EmptyIterator; import org.eclipse.cdt.core.parser.ICodeReaderCache;
/** /**
* @author jcamelon * @author jcamelon
*/ */
public class SavedCodeReaderFactory implements ICodeReaderFactory { public class SavedCodeReaderFactory implements ICodeReaderFactory {
private ICodeReaderCache cache = null;
public static SavedCodeReaderFactory getInstance() public static SavedCodeReaderFactory getInstance()
{ {
return instance; return instance;
@ -30,6 +33,16 @@ public class SavedCodeReaderFactory implements ICodeReaderFactory {
private SavedCodeReaderFactory() private SavedCodeReaderFactory()
{ {
int size=0;
if (CCorePlugin.getDefault() == null || CCorePlugin.getDefault().getPluginPreferences() == null)
size = CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB;
else
size = CCorePlugin.getDefault().getPluginPreferences().getInt(CodeReaderCache.CODE_READER_BUFFER);
if (size >= 0)
cache = new CodeReaderCache(size);
else
cache = new CodeReaderCache(CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#getUniqueIdentifier() * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#getUniqueIdentifier()
@ -42,14 +55,20 @@ public class SavedCodeReaderFactory implements ICodeReaderFactory {
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForTranslationUnit(java.lang.String)
*/ */
public CodeReader createCodeReaderForTranslationUnit(String path) { public CodeReader createCodeReaderForTranslationUnit(String path) {
return ParserUtil.createReader( path, EmptyIterator.EMPTY_ITERATOR ); return cache.get(path);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String) * @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/ */
public CodeReader createCodeReaderForInclusion(String path) { public CodeReader createCodeReaderForInclusion(String path) {
return ParserUtil.createReader( path, EmptyIterator.EMPTY_ITERATOR ); return cache.get(path);
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ICodeReaderFactory#createCodeReaderForInclusion(java.lang.String)
*/
public ICodeReaderCache getCodeReaderCache() {
return cache;
}
} }

View file

@ -222,6 +222,7 @@ HideMacrodirective.description= Hides Macro directives
WorkInProgress.name=Work In Progress WorkInProgress.name=Work In Progress
CDTSearch.name=Search CDTSearch.name=Search
CDTParser.name=Parser
CDTIndexerProperty.name=C/C++ Indexer CDTIndexerProperty.name=C/C++ Indexer

View file

@ -617,6 +617,11 @@
class="org.eclipse.cdt.internal.ui.preferences.CFileTypesPreferencePage" class="org.eclipse.cdt.internal.ui.preferences.CFileTypesPreferencePage"
id="org.eclipse.cdt.ui.preferences.CFileTypesPreferences"> id="org.eclipse.cdt.ui.preferences.CFileTypesPreferences">
</page> </page>
<page
class="org.eclipse.cdt.internal.ui.preferences.CParserPreferencePage"
category="org.eclipse.cdt.ui.preferences.CPluginPreferencePage"
name="%CDTParser.name"
id="org.eclipse.cdt.ui.preferences.CParserPreferencePage"/>
<page <page
name="%appearancePrefName" name="%appearancePrefName"
category="org.eclipse.cdt.ui.preferences.CPluginPreferencePage" category="org.eclipse.cdt.ui.preferences.CPluginPreferencePage"

View file

@ -0,0 +1,167 @@
/**********************************************************************
Copyright (c) 2005 IBM Rational Software and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html
Contributors:
IBM Rational Software - Initial Contribution
**********************************************************************/
package org.eclipse.cdt.internal.ui.preferences;
import java.util.ArrayList;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.parser.CodeReaderCache;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
/**
* @author dsteffle
*/
public class CParserPreferencePage extends PreferencePage implements
IWorkbenchPreferencePage {
protected OverlayPreferenceStore fOverlayStore;
private Text bufferTextControl;
public CParserPreferencePage(){
setPreferenceStore(CUIPlugin.getDefault().getPreferenceStore());
fOverlayStore = createOverlayStore();
}
private OverlayPreferenceStore createOverlayStore() {
ArrayList overlayKeys = new ArrayList();
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, CodeReaderCache.CODE_READER_BUFFER));
OverlayPreferenceStore.OverlayKey[] keys = new OverlayPreferenceStore.OverlayKey[overlayKeys.size()];
overlayKeys.toArray(keys);
return new OverlayPreferenceStore(getPreferenceStore(), keys);
}
/* (non-Javadoc)
* @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
*/
protected Control createContents(Composite parent) {
fOverlayStore.load();
fOverlayStore.start();
initializeDialogUnits(parent);
Composite result= new Composite(parent, SWT.NONE);
GridLayout layout= new GridLayout();
layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
layout.marginWidth= 0;
layout.verticalSpacing= convertVerticalDLUsToPixels(10);
layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
result.setLayout(layout);
Group bufferGroup= new Group(result, SWT.NONE);
bufferGroup.setLayout(new GridLayout());
bufferGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
bufferGroup.setText(PreferencesMessages.getString("CBufferPreferences.CodeReaderBuffer.CodeReaderBufferGroup")); //$NON-NLS-1$
bufferTextControl = (Text) addTextField( bufferGroup, PreferencesMessages.getString("CBufferPreferences.CodeReaderBuffer.Size"),6,0); //$NON-NLS-1$ //$NON-NLS-2$
initialize();
return result;
}
private void initialize(){
bufferTextControl.setText(fOverlayStore.getString(CodeReaderCache.CODE_READER_BUFFER));
}
/* (non-Javadoc)
* @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
*/
public void init(IWorkbench workbench) {
// TODO Auto-generated method stub
}
private Control addTextField(Composite composite, String label, int textLimit, int indentation) {
Label labelControl = new Label(composite, SWT.NONE);
labelControl.setText(label);
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
gd.horizontalIndent = indentation;
labelControl.setLayoutData(gd);
Text textControl = new Text(composite, SWT.BORDER | SWT.SINGLE);
gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
gd.widthHint = convertWidthInCharsToPixels(textLimit + 1);
textControl.setLayoutData(gd);
textControl.setTextLimit(textLimit);
return textControl;
}
/*
* @see IPreferencePage#performOk()
*/
public boolean performOk() {
Preferences prefs = CCorePlugin.getDefault().getPluginPreferences();
String bufferSize = bufferTextControl.getText();
fOverlayStore.setValue(CodeReaderCache.CODE_READER_BUFFER, bufferSize);
prefs.setValue(CodeReaderCache.CODE_READER_BUFFER, bufferSize);
ICodeReaderCache cache = CDOM.getInstance().getCodeReaderFactory(CDOM.PARSE_SAVED_RESOURCES).getCodeReaderCache();
if (cache instanceof CodeReaderCache) {
try {
// Check the string number
int size = Integer.parseInt(bufferSize);
if (size >= 0) {
((CodeReaderCache)cache).setCacheSize(size);
} else {
((CodeReaderCache)cache).setCacheSize(CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
prefs.setValue(CodeReaderCache.CODE_READER_BUFFER, CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
fOverlayStore.setValue(CodeReaderCache.CODE_READER_BUFFER, CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
}
} catch (NumberFormatException ex){
((CodeReaderCache)cache).setCacheSize(CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
prefs.setValue(CodeReaderCache.CODE_READER_BUFFER, CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
fOverlayStore.setValue(CodeReaderCache.CODE_READER_BUFFER, CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB);
}
}
fOverlayStore.propagate();
CCorePlugin.getDefault().savePluginPreferences();
return true;
}
/**
* @param store
*/
public static void initDefaults(IPreferenceStore store) {
store.setDefault(CodeReaderCache.CODE_READER_BUFFER,CodeReaderCache.DEFAULT_CACHE_SIZE_IN_MB_STRING);
}
/*
* @see PreferencePage#performDefaults()
*/
protected void performDefaults() {
fOverlayStore.loadDefaults();
initialize();
super.performDefaults();
}
}

View file

@ -181,6 +181,10 @@ CSearchPreferences.ExternalSearchLinks.Invisible=Invisible
CSearchPreferences.IndexerTimeout.IndexerTimeoutGroup=Indexer Timeout CSearchPreferences.IndexerTimeout.IndexerTimeoutGroup=Indexer Timeout
CSearchPreferences.IndexerTimeout.Timeout=Timeout (ms) CSearchPreferences.IndexerTimeout.Timeout=Timeout (ms)
#Buffer Preferences
CBufferPreferences.CodeReaderBuffer.CodeReaderBufferGroup=CodeReader Buffer Size
CBufferPreferences.CodeReaderBuffer.Size=Size (MB)
#Open Type Preferences #Open Type Preferences
CEditorPreferencePage.Navigation.OpenType=Open Type CEditorPreferencePage.Navigation.OpenType=Open Type
CEditorPreferencePage.Navigation.CacheTypesInBackground=Cache types in background CEditorPreferencePage.Navigation.CacheTypesInBackground=Cache types in background

View file

@ -20,6 +20,7 @@ import org.eclipse.cdt.core.dom.IASTServiceProvider.UnsupportedDialectException;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.model.IWorkingCopy; import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ICodeReaderCache;
import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.internal.ui.text.CParameterListValidator; import org.eclipse.cdt.internal.ui.text.CParameterListValidator;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -73,6 +74,10 @@ public class CCompletionProcessor2 implements IContentAssistProcessor {
public int getUniqueIdentifier() { public int getUniqueIdentifier() {
return 99; return 99;
} }
public ICodeReaderCache getCodeReaderCache() {
// TODO this is useless
return null;
}
} }
); );
long stopTime = System.currentTimeMillis(); long stopTime = System.currentTimeMillis();

View file

@ -13,6 +13,7 @@ package org.eclipse.cdt.ui;
import org.eclipse.cdt.internal.ui.cview.CView; import org.eclipse.cdt.internal.ui.cview.CView;
import org.eclipse.cdt.internal.ui.preferences.BuildConsolePreferencePage; import org.eclipse.cdt.internal.ui.preferences.BuildConsolePreferencePage;
import org.eclipse.cdt.internal.ui.preferences.CEditorPreferencePage; import org.eclipse.cdt.internal.ui.preferences.CEditorPreferencePage;
import org.eclipse.cdt.internal.ui.preferences.CParserPreferencePage;
import org.eclipse.cdt.internal.ui.preferences.CPluginPreferencePage; import org.eclipse.cdt.internal.ui.preferences.CPluginPreferencePage;
import org.eclipse.cdt.internal.ui.preferences.CodeAssistPreferencePage; import org.eclipse.cdt.internal.ui.preferences.CodeAssistPreferencePage;
import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
@ -37,6 +38,7 @@ public class CUIPreferenceInitializer extends AbstractPreferenceInitializer {
CPluginPreferencePage.initDefaults(store); CPluginPreferencePage.initDefaults(store);
BuildConsolePreferencePage.initDefaults(store); BuildConsolePreferencePage.initDefaults(store);
CView.initDefaults(store); CView.initDefaults(store);
CParserPreferencePage.initDefaults(store);
CEditorPreferencePage.initDefaults(store); CEditorPreferencePage.initDefaults(store);
CodeAssistPreferencePage.initDefaults(store); CodeAssistPreferencePage.initDefaults(store);