1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Changed the interface to the Scanner to take a CodeReader object.

A CodeReader simply contains a char[] and the name of the file, if any,
that the char[] is the contents of. This leads us down the path of using
char[]s in the parser instead of Strings in an attempt to cut down on
our memory usage.
This commit is contained in:
Doug Schaefer 2004-06-04 21:01:48 +00:00
parent 98800badf4
commit a206e32d2a
47 changed files with 327 additions and 322 deletions

View file

@ -11,7 +11,6 @@
package org.eclipse.cdt.managedbuild.core.tests;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -23,6 +22,7 @@ import junit.framework.TestSuite;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
@ -1508,8 +1508,8 @@ public class ManagedBuildCoreTests extends TestCase {
IScannerInfo info = provider.getScannerInformation( project );
ISourceElementRequestor callback = new NullSourceElementRequestor();
IScanner scanner = ParserFactory.createScanner( new StringReader( "#include <header.h>\n int A::i = 1;" ),
"TEST", info, ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, callback, new NullLogService(), null);
IScanner scanner = ParserFactory.createScanner( new CodeReader( "#include <header.h>\n int A::i = 1;".toCharArray() ),
info, ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, callback, new NullLogService(), null);
IParser parser = ParserFactory.createParser( scanner, callback, ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, null );
assertTrue( parser.parse() );

View file

@ -117,6 +117,8 @@ import org.eclipse.core.runtime.Platform;
//Delete project
if (testProject.exists()){
try {
System.gc();
System.runFinalization();
testProject.delete(true,monitor);
} catch (CoreException e) {
fail(getMessage(e.getStatus()));
@ -470,8 +472,7 @@ import org.eclipse.core.runtime.Platform;
j++;
}
if (postDepTestModelLocal.length != postDepTestIncludes.length)
fail("Number of included files differs from model");
assertEquals(postDepTestModelLocal.length, postDepTestIncludes.length);
Arrays.sort(postDepTestModelLocal);
Arrays.sort(postDepTestIncludes);

View file

@ -89,6 +89,8 @@ public class IndexManagerTests extends TestCase {
//Delete project
if (testProject.exists()) {
try {
System.gc();
System.runFinalization();
testProject.delete(true, monitor);
} catch (CoreException e) {
fail(getMessage(e.getStatus()));
@ -239,6 +241,8 @@ public class IndexManagerTests extends TestCase {
* @param testProject
*/
private void safeDelete(IProject testProject) throws InterruptedException, CoreException {
System.gc();
System.runFinalization();
try {
testProject.delete(true, monitor);
} catch (CoreException e) {
@ -277,6 +281,9 @@ public class IndexManagerTests extends TestCase {
}
//Delete mail.cpp from the project, give some time to remove index
IResource resourceHdl = testProject.findMember("mail.cpp") ;
// Cleaning up file handles before delete
System.gc();
System.runFinalization();
resourceHdl.delete(true,monitor);
Thread.sleep(10000);
//See if the index is still there

View file

@ -175,6 +175,8 @@ public class BinaryTests extends TestCase {
* Called after every test case method.
*/
protected void tearDown() throws CoreException, InterruptedException {
System.gc();
System.runFinalization();
CProjectHelper.delete(testProject);
}

View file

@ -58,7 +58,7 @@ public class AutomatedTest extends AutomatedFramework {
String filePath = file.getCanonicalPath();
ParserLanguage language = ((String)natures.get( filePath )).equalsIgnoreCase("cpp") ? ParserLanguage.CPP : ParserLanguage.C; //$NON-NLS-1$
parser = ParserFactory.createParser( ParserFactory.createScanner( new InputStreamReader (stream), filePath, new ScannerInfo(), ParserMode.QUICK_PARSE, language, nullCallback, null, null ), nullCallback, ParserMode.QUICK_PARSE, language, null);
parser = ParserFactory.createParser( ParserFactory.createScanner(filePath, new ScannerInfo(), ParserMode.QUICK_PARSE, language, nullCallback, null, null ), nullCallback, ParserMode.QUICK_PARSE, language, null);
mapping = ParserFactory.createLineOffsetReconciler( new InputStreamReader( stream ) );

View file

@ -10,11 +10,11 @@
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.util.Iterator;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IQuickParseCallback;
import org.eclipse.cdt.core.parser.NullLogService;
@ -51,7 +51,7 @@ public class BaseASTTest extends TestCase
{
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
quickParseCallback = ParserFactory.createQuickParseCallback();
parser = ParserFactory.createParser( ParserFactory.createScanner( new StringReader( code ), "code", new ScannerInfo(), mode, lang, quickParseCallback, new NullLogService(), null), quickParseCallback, mode, lang, null ); //$NON-NLS-1$
parser = ParserFactory.createParser( ParserFactory.createScanner( new CodeReader(code.toCharArray()), new ScannerInfo(), mode, lang, quickParseCallback, new NullLogService(), null), quickParseCallback, mode, lang, null ); //$NON-NLS-1$
if( ! parser.parse() && throwExceptionOnError )
throw new ParserException("Parse failure"); //$NON-NLS-1$
return quickParseCallback.getCompilationUnit();

View file

@ -11,10 +11,9 @@
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
@ -47,7 +46,7 @@ public class BaseScannerTest extends TestCase {
protected void initializeScanner( String input, ParserMode mode, ISourceElementRequestor requestor ) throws ParserFactoryError
{
scanner= ParserFactory.createScanner( new StringReader(input),"TEXT", new ScannerInfo(), mode, ParserLanguage.CPP, requestor, null, null ); //$NON-NLS-1$
scanner= ParserFactory.createScanner( new CodeReader(input.toCharArray()), new ScannerInfo(), mode, ParserLanguage.CPP, requestor, null, null ); //$NON-NLS-1$
}
protected void initializeScanner(String input) throws ParserFactoryError

View file

@ -6,10 +6,10 @@
*/
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullLogService;
@ -67,7 +67,7 @@ public class CompleteParseASTSymbolIteratorTest extends CompleteParseBaseTest {
{
callback = new CompilationUnitCallback();
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), "test-code", new ScannerInfo(), //$NON-NLS-1$
ParserFactory.createScanner( new CodeReader(code.toCharArray()), new ScannerInfo(),
ParserMode.COMPLETE_PARSE, language, callback, new NullLogService(), null ), callback, ParserMode.COMPLETE_PARSE, language, null
);
if( ! parser.parse() && throwOnError ) throw new ParserException( "FAILURE"); //$NON-NLS-1$

View file

@ -10,8 +10,6 @@
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
@ -22,6 +20,7 @@ import java.util.Stack;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementCallbackDelegate;
@ -737,7 +736,7 @@ public class CompleteParseBaseTest extends TestCase
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath,workingCopies);
}
@ -796,7 +795,7 @@ public class CompleteParseBaseTest extends TestCase
{
callback = new FullParseCallback();
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), "test-code", new ScannerInfo(), //$NON-NLS-1$
ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), //$NON-NLS-1$
ParserMode.COMPLETE_PARSE, language, callback, new NullLogService(), null ), callback, ParserMode.COMPLETE_PARSE, language, null
);
if( ! parser.parse() && throwOnError ) throw new ParserException( "FAILURE"); //$NON-NLS-1$

View file

@ -10,10 +10,10 @@
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -51,8 +51,7 @@ public class CompletionParseBaseTest extends CompleteParseBaseTest {
parser =
ParserFactory.createParser(
ParserFactory.createScanner(
new StringReader(code),
"completion-test", //$NON-NLS-1$
new CodeReader(code.toCharArray()),
new ScannerInfo(),
ParserMode.COMPLETION_PARSE,
ParserLanguage.CPP,
@ -73,8 +72,7 @@ public class CompletionParseBaseTest extends CompleteParseBaseTest {
parser =
ParserFactory.createParser(
ParserFactory.createScanner(
new StringReader(code),
"completion-test", //$NON-NLS-1$
new CodeReader(code.toCharArray()),
new ScannerInfo(),
ParserMode.COMPLETION_PARSE,
lang,

View file

@ -1,11 +1,10 @@
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -28,7 +27,7 @@ public class ExprEvalTest extends TestCase {
public void runTest(String code, int expectedValue) throws Exception {
final NullSourceElementRequestor nullCallback = new NullSourceElementRequestor();
IExpressionParser parser = InternalParserUtil.createExpressionParser(ParserFactory.createScanner( new StringReader( code ), getClass().getName(), new ScannerInfo(), null, ParserLanguage.CPP, nullCallback, new NullLogService(), null ), ParserLanguage.CPP, null );
IExpressionParser parser = InternalParserUtil.createExpressionParser(ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), null, ParserLanguage.CPP, nullCallback, new NullLogService(), null ), ParserLanguage.CPP, null );
IASTExpression expression = parser.expression(null,null, null);
assertEquals(expectedValue, expression.evaluateExpression());
}

View file

@ -15,13 +15,13 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -242,7 +242,7 @@ public class FractionalAutomatedTest extends AutomatedFramework {
result = null;
ParserLanguage language = cppNature ? ParserLanguage.CPP : ParserLanguage.C;
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, new ScannerInfo(), ParserMode.QUICK_PARSE, language, nullCallback, new NullLogService(), null ), nullCallback, ParserMode.QUICK_PARSE, language, null );
ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), ParserMode.QUICK_PARSE, language, nullCallback, new NullLogService(), null ), nullCallback, ParserMode.QUICK_PARSE, language, null );
parser.parse();
} catch ( Exception e ){

View file

@ -10,10 +10,10 @@
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.IToken;
@ -36,10 +36,9 @@ public class PreprocessorConditionalTest extends BaseScannerTest
protected void initializeScanner(String input, Map definitions ) throws Exception
{
scanner= ParserFactory.createScanner( new StringReader(input),"TEXT", new ScannerInfo( definitions ), ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, nullSourceElementRequestor, null, null ); //$NON-NLS-1$
scanner= ParserFactory.createScanner( new CodeReader(input.toCharArray()), new ScannerInfo( definitions ), ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, nullSourceElementRequestor, null, null );
}
protected void evaluateConditionalsPositive( String conditional, Map definitions ) throws Exception
{

View file

@ -1156,7 +1156,7 @@ public class QuickParseASTTests extends BaseASTTest
String code = "#include <stdio.h>\n#define DEF VALUE\n"; //$NON-NLS-1$
IASTCompilationUnit tu = parse( code );
IASTCompilationUnit tu = parse( code.toString() );
assertFalse( tu.getDeclarations().hasNext());
Iterator inclusions = quickParseCallback.getInclusions();
Iterator macros = quickParseCallback.getMacros();
@ -2244,9 +2244,4 @@ public class QuickParseASTTests extends BaseASTTest
assertFalse( iter.hasNext() );
}
}
public void testBug61972() throws Exception
{
parse( "#define DEF1(A1) A1\n#define DEF2 DEF1(DEF2)\nDEF2;", true, false ); //$NON-NLS-1$
}
}

View file

@ -9,8 +9,7 @@
* IBM Rational Software - Initial API and implementation */
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -43,8 +42,7 @@ public class SelectionParseBaseTest extends CompleteParseBaseTest {
parser =
ParserFactory.createParser(
ParserFactory.createScanner(
new StringReader(code),
"completion-test", //$NON-NLS-1$
new CodeReader(code.toCharArray()),
new ScannerInfo(),
ParserMode.SELECTION_PARSE,
ParserLanguage.CPP,

View file

@ -1,19 +1,12 @@
/*
* Created on Jun 3, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package org.eclipse.cdt.core.parser.tests;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
@ -23,54 +16,43 @@ import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.internal.core.parser.QuickParseCallback;
/**
* @author Doug Schaefer
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
// A test that just calculates the speed of the parser
// Eventually, we'll peg a max time and fail the test if it exceeds it
public class SpeedTest extends TestCase {
public static void main(String[] args) {
try {
new SpeedTest().runTest(1);
new SpeedTest().test();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
public void test() throws Exception {
runTest(1);
}
public void runTest(int n) throws Exception {
for (int i = 0; i < n; ++i) {
System.gc();
String code =
"#include <windows.h>\n" +
"#include <stdio.h>\n" +
"#include <iostream>\n";
Reader reader = new StringReader(code);
IScannerInfo info = mingwScannerInfo(false);
//IScannerInfo info = msvcScannerInfo(quick);
testParse(reader, "text", false, info, ParserLanguage.CPP);
}
String code =
"#include <windows.h>\n" +
"#include <stdio.h>\n" +
"#include <iostream>\n";
CodeReader reader = new CodeReader(code.toCharArray());
IScannerInfo info = mingwScannerInfo(false);
//IScannerInfo info = msvcScannerInfo(quick);
testParse(reader, false, info, ParserLanguage.CPP);
}
/**
* @param path
* @param quick TODO
*/
protected void testParse(Reader reader, String path, boolean quick, IScannerInfo info, ParserLanguage lang) throws Exception {
protected void testParse(CodeReader reader, boolean quick, IScannerInfo info, ParserLanguage lang) throws Exception {
ParserMode mode = quick ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
IScanner scanner = ParserFactory.createScanner(reader, path, info, mode, lang, CALLBACK, null, Collections.EMPTY_LIST );
IScanner scanner = ParserFactory.createScanner(reader, info, mode, lang, CALLBACK, null, Collections.EMPTY_LIST );
IParser parser = ParserFactory.createParser( scanner, CALLBACK, mode, lang, null);
long startTime = System.currentTimeMillis();
long totalTime;
parser.parse();
totalTime = System.currentTimeMillis() - startTime;
System.out.println( "Resulting parse for " + path + " took " + totalTime + " millisecs"); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println( "Resulting parse took " + totalTime + " millisecs"); //$NON-NLS-1$ //$NON-NLS-2$
}
private static final QuickParseCallback CALLBACK = new QuickParseCallback();
@ -93,6 +75,7 @@ public class SpeedTest extends TestCase {
}
protected IScannerInfo mingwScannerInfo(boolean quick) {
// TODO It would be easier and more flexible if we used discovery for this
if( quick )
return new ScannerInfo();
Map definitions = new Hashtable();

View file

@ -13,11 +13,11 @@
*/
package org.eclipse.cdt.core.parser.tests;
import java.io.StringReader;
import java.util.Iterator;
import junit.framework.TestCase;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -59,7 +59,7 @@ public class StructuralParseTest extends TestCase {
{
callback = new StructuralParseCallback();
IParser parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), "test-code", new ScannerInfo(), //$NON-NLS-1$
ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), //$NON-NLS-1$
ParserMode.STRUCTURAL_PARSE, language, callback, new NullLogService(), null ),
callback, ParserMode.STRUCTURAL_PARSE, language, null
);

View file

@ -22,6 +22,7 @@ import java.util.StringTokenizer;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ILineOffsetReconciler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.NullLogService;
@ -281,7 +282,7 @@ public class TortureTest extends FractionalAutomatedTest {
ParserMode parserMode = quickParse ? ParserMode.QUICK_PARSE : ParserMode.COMPLETE_PARSE;
ParserLanguage language = cppNature ? ParserLanguage.CPP : ParserLanguage.C;
parser = ParserFactory.createParser(
ParserFactory.createScanner( new StringReader( code ), null, new ScannerInfo(), parserMode, language, nullCallback, new NullLogService(), null ), nullCallback, parserMode, language, null);
ParserFactory.createScanner( new CodeReader( code.toCharArray() ), new ScannerInfo(), parserMode, language, nullCallback, new NullLogService(), null ), nullCallback, parserMode, language, null);
mapping = ParserFactory.createLineOffsetReconciler( new StringReader( code ) );

View file

@ -11,8 +11,6 @@
package org.eclipse.cdt.core.search.tests;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import junit.framework.TestCase;
@ -34,7 +32,6 @@ import org.eclipse.core.runtime.Path;
*/
public class ParseTestOnSearchFiles extends TestCase
{
private FileInputStream fileIn;
private String name;
private String fullPathName;
/**
@ -56,13 +53,12 @@ public class ParseTestOnSearchFiles extends TestCase
name = "resources/search/classDecl.cpp";
File f = CTestPlugin.getDefault().getFileInPlugin(new Path(name));
fullPathName = f.getAbsolutePath();
fileIn = new FileInputStream(f);
}
public void testParseOfAndrewsFile() throws Exception
{
ISourceElementRequestor requestor = new NullSourceElementRequestor();
IScanner scanner = ParserFactory.createScanner( new InputStreamReader( fileIn ), fullPathName, new ScannerInfo(), ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, requestor, new NullLogService(), null );
IScanner scanner = ParserFactory.createScanner( fullPathName, new ScannerInfo(), ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, requestor, new NullLogService(), null );
IParser parser = ParserFactory.createParser( scanner, requestor, ParserMode.COMPLETE_PARSE, ParserLanguage.CPP, null );
assertTrue( parser.parse() );
}

View file

@ -110,6 +110,8 @@ public class CProjectHelper {
} catch (InterruptedException e1) {
} finally {
try {
System.gc();
System.runFinalization();
cproject.getProject().delete(true, true, null);
} catch (CoreException e2) {
Assert.fail(getMessage(e2.getStatus()));

View file

@ -11,13 +11,8 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.browser.cache;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@ -38,6 +33,7 @@ import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.DefaultProblemHandler;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
@ -54,7 +50,6 @@ import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.ASTClassKind;
import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
@ -86,6 +81,7 @@ import org.eclipse.cdt.core.parser.ast.IASTTemplateDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTTemplateInstantiation;
import org.eclipse.cdt.core.parser.ast.IASTTemplateParameterReference;
import org.eclipse.cdt.core.parser.ast.IASTTemplateSpecialization;
import org.eclipse.cdt.core.parser.ast.IASTTypedefDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTTypedefReference;
import org.eclipse.cdt.core.parser.ast.IASTUsingDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTUsingDirective;
@ -285,7 +281,7 @@ public class TypeParser implements ISourceElementRequestor {
return; // not C or C++
}
Reader reader = null;
CodeReader reader = null;
Object stackObject = null;
if (workingCopy != null) {
@ -358,46 +354,44 @@ public class TypeParser implements ISourceElementRequestor {
return projectLanguage;
}
private Reader createWorkingCopyReader(IWorkingCopy workingCopy) {
Reader reader = null;
private CodeReader createWorkingCopyReader(IWorkingCopy workingCopy) {
CodeReader reader = null;
IResource resource = workingCopy.getResource();
if (resource != null && resource.isAccessible()) {
char[] contents = workingCopy.getContents();
if (contents != null)
reader = new CharArrayReader(contents);
reader = new CodeReader(resource.getLocation().toOSString(), contents);
}
return reader;
}
private Reader createResourceReader(IResource resource) {
Reader reader = null;
private CodeReader createResourceReader(IResource resource) {
CodeReader reader = null;
if (resource.isAccessible() && resource instanceof IFile) {
IFile file = (IFile) resource;
try {
InputStream contents = file.getContents();
if (contents != null)
reader = new InputStreamReader(contents);
reader = new CodeReader(resource.getLocation().toOSString(), contents);
} catch (CoreException ex) {
ex.printStackTrace();
} catch (IOException e) {
}
}
return reader;
}
private Reader createFileReader(IPath path) {
Reader reader = null;
File file = path.toFile();
if (file != null) {
try {
reader = new FileReader(file);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
private CodeReader createFileReader(IPath path) {
CodeReader reader = null;
try {
reader = new CodeReader(path.toOSString());
} catch (IOException ex) {
ex.printStackTrace();
}
return reader;
}
private void parseContents(IPath realPath, IProject project, Reader reader, ParserLanguage language, IProgressMonitor progressMonitor) throws InterruptedException {
private void parseContents(IPath realPath, IProject project, CodeReader reader, ParserLanguage language, IProgressMonitor progressMonitor) throws InterruptedException {
IScannerInfo scanInfo = null;
if (project != null) {
@ -418,7 +412,7 @@ public class TypeParser implements ISourceElementRequestor {
try {
fProgressMonitor = progressMonitor;
IScanner scanner = ParserFactory.createScanner(reader, realPath.toOSString(), scanInfo,
IScanner scanner = ParserFactory.createScanner(reader, scanInfo,
ParserMode.STRUCTURAL_PARSE, language, this, ParserUtil.getScannerLogService(), null);
IParser parser = ParserFactory.createParser(scanner, this, ParserMode.STRUCTURAL_PARSE, language, ParserUtil.getParserLogService());
parser.parse();
@ -711,7 +705,7 @@ public class TypeParser implements ISourceElementRequestor {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath, workingCopies);
}

View file

@ -16,14 +16,13 @@ package org.eclipse.cdt.internal.core.search.indexing;
*/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICLogConstants;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
@ -103,10 +102,10 @@ public class SourceIndexer extends AbstractIndexer {
try
{
CodeReader reader = new CodeReader(resourceFile.getLocation().toOSString(), resourceFile.getContents());
BufferedInputStream inStream = new BufferedInputStream(resourceFile.getContents());
parser = ParserFactory.createParser(
ParserFactory.createScanner( new BufferedReader(new InputStreamReader(inStream)), resourceFile.getLocation().toOSString(), scanInfo, ParserMode.COMPLETE_PARSE, language, requestor, ParserUtil.getScannerLogService(), null ),
ParserFactory.createScanner(reader, scanInfo, ParserMode.COMPLETE_PARSE, language, requestor, ParserUtil.getScannerLogService(), null ),
requestor, ParserMode.COMPLETE_PARSE, language, ParserUtil.getParserLogService() );
} catch( ParserFactoryError pfe )
{

View file

@ -16,7 +16,6 @@ package org.eclipse.cdt.internal.core.search.indexing;
*/
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -26,6 +25,7 @@ import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserMode;
@ -566,7 +566,7 @@ public class SourceIndexerRequestor implements ISourceElementRequestor, IIndexCo
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath,workingCopies);
}

View file

@ -10,8 +10,6 @@
******************************************************************************/
package org.eclipse.cdt.internal.core.model;
import java.io.BufferedReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -23,6 +21,7 @@ import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.IProblemRequestor;
import org.eclipse.cdt.core.model.ITemplate;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IQuickParseCallback;
@ -163,13 +162,14 @@ public class CModelBuilder {
scanInfo = new ScannerInfo(buildScanInfo.getDefinedSymbols(), buildScanInfo.getIncludePaths());
}
}
CodeReader reader =
translationUnit.getUnderlyingResource() != null
? new CodeReader(translationUnit.getUnderlyingResource().getLocation().toOSString(), code.toCharArray())
: new CodeReader(code.toCharArray());
parser = ParserFactory.createParser(
ParserFactory.createScanner(
new BufferedReader( new StringReader( code ) ),
(translationUnit.getUnderlyingResource() != null ?
translationUnit.getUnderlyingResource().getLocation().toOSString() :
""), //$NON-NLS-1$
ParserFactory.createScanner(
reader,
scanInfo,
mode,
language,

View file

@ -10,29 +10,92 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.Reader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
/**
* @author jcamelon
*/
public class CodeReader {
public CodeReader( String filename, Reader reader )
{
this.reader = reader;
this.filename = filename;
}
private final Reader reader;
private final String filename;
public String getFilename()
{
return filename;
private static final String NOFILE = "<text>";
public final char[] buffer;
public final String filename;
// If you already have preloaded the buffer, e.g. working copy
public CodeReader(String filename, char[] buffer) {
this.filename = filename;
this.buffer = buffer;
}
// If you are just scanning a string
public CodeReader(char[] buffer) {
this(NOFILE, buffer);
}
public Reader getUnderlyingReader()
// If you are loading up a file normally
public CodeReader(String filename) throws IOException
{
return reader;
this.filename = filename;
FileInputStream stream = new FileInputStream(filename);
buffer = load(stream);
}
// If you have a handle on a stream to the file, e.g. IFile.getContents()
public CodeReader(String filename, InputStream stream) throws IOException {
this.filename = filename;
FileInputStream fstream =
(stream instanceof FileInputStream)
? (FileInputStream)stream
: new FileInputStream(filename);
buffer = load(fstream);
}
private char[] load(FileInputStream stream) throws IOException {
FileChannel channel = stream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect((int)channel.size());
channel.read(byteBuffer);
byteBuffer.rewind();
// TODO use the real encoding
CharBuffer charBuffer = Charset.forName("UTF-8").decode(byteBuffer);
if (charBuffer.hasArray())
return charBuffer.array();
else {
// Got to copy it out
char[] buff = new char[charBuffer.length()];
charBuffer.get(buff);
return buff;
}
}
private char[] xload(FileInputStream stream) throws IOException {
FileChannel channel = stream.getChannel();
MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
// TODO use the real encoding
CharBuffer charBuffer = Charset.forName("UTF-8").decode(map);
if (charBuffer.hasArray())
return charBuffer.array();
else {
// Got to copy it out
char[] buff = new char[charBuffer.length()];
charBuffer.get(buff);
return buff;
}
}
public boolean isFile() {
return filename != NOFILE;
}
}

View file

@ -10,7 +10,6 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.Reader;
import java.util.Iterator;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
@ -114,7 +113,7 @@ public interface ISourceElementRequestor {
* @param finalPath
* @return
*/
public Reader createReader(String finalPath, Iterator workingCopies );
public CodeReader createReader(String finalPath, Iterator workingCopies );
/**
* The parser asks the client if it wishes to time out
* in case it is taking more than the expected time.

View file

@ -1,7 +1,6 @@
package org.eclipse.cdt.core.parser;
import java.io.Reader;
import java.util.Iterator;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
@ -460,7 +459,7 @@ public class NullSourceElementRequestor implements ISourceElementRequestor
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return InternalParserUtil.createFileReader( finalPath );
}

View file

@ -10,7 +10,7 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Set;
@ -97,22 +97,22 @@ public class ParserFactory {
* @return
* @throws ParserFactoryError - erroneous input provided
*/
public static IScanner createScanner( Reader input, String fileName, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError
public static IScanner createScanner( CodeReader code, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError
{
if( input == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_READER );
Reader ourReader = input;
if( !(input instanceof BufferedReader ))
ourReader = new BufferedReader( input );
if( fileName == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_FILENAME );
if( config == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_CONFIG );
if( language == null ) throw new ParserFactoryError( ParserFactoryError.Kind.NULL_LANGUAGE );
IParserLogService logService = ( log == null ) ? createDefaultLogService() : log;
ParserMode ourMode = ( (mode == null )? ParserMode.COMPLETE_PARSE : mode );
ISourceElementRequestor ourRequestor = (( requestor == null) ? new NullSourceElementRequestor() : requestor );
IScanner s = new Scanner( ourReader, fileName, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies );
IScanner s = new Scanner( code, config, ourRequestor, ourMode, language, logService, extensionFactory.createScannerExtension(), workingCopies );
return s;
}
public static IScanner createScanner( String fileName, IScannerInfo config, ParserMode mode, ParserLanguage language, ISourceElementRequestor requestor, IParserLogService log, List workingCopies ) throws ParserFactoryError, IOException
{
return createScanner(new CodeReader(fileName), config, mode, language, requestor, log, workingCopies);
}
public static ILineOffsetReconciler createLineOffsetReconciler( Reader input )
{
return new LineOffsetReconciler( input );

View file

@ -10,12 +10,10 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.io.IOException;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -43,16 +41,13 @@ public class InternalParserUtil extends ParserFactory {
* @param finalPath
* @return
*/
public static Reader createFileReader(String finalPath) {
public static CodeReader createFileReader(String finalPath) {
File includeFile = new File(finalPath);
if (includeFile.exists() && includeFile.isFile())
{
//check and see
try {
return new BufferedReader( new FileReader( includeFile) );
} catch (FileNotFoundException fnf) {
return new CodeReader(finalPath);
} catch (IOException e) {
}
}
return null;

View file

@ -214,6 +214,7 @@ public abstract class Parser extends ExpressionParser implements IParser
if( e instanceof Exception )
logException( "translationUnit", (Exception) e ); //$NON-NLS-1$
failParse();
break;
}
}
compilationUnit.exitScope( requestor, astFactory.getReferenceManager() );

View file

@ -11,8 +11,7 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.Reader;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IScanner;
@ -112,9 +111,9 @@ public class ContextStack {
scanner.setScannerContext(sentinel);
}
public void updateInclusionContext(Reader reader, String filename, IASTInclusion inclusion, ISourceElementRequestor requestor) throws ContextException {
addInclusionFilename( filename );
ScannerContextInclusion context = new ScannerContextInclusion( reader, filename, inclusion, currentInclusionIndex - 1 );
public void updateInclusionContext(CodeReader code, IASTInclusion inclusion, ISourceElementRequestor requestor) throws ContextException {
addInclusionFilename( code.filename );
ScannerContextInclusion context = new ScannerContextInclusion( code, inclusion, currentInclusionIndex - 1 );
if( isCircularInclusion( context.getContextName() ) )
throw new ContextException( IProblem.PREPROCESSOR_CIRCULAR_INCLUSION );

View file

@ -189,7 +189,13 @@ public class GCCScannerExtension implements IScannerExtension {
// search through include paths
while (iter.hasNext()) {
String path = (String)iter.next();
duple = ScannerUtility.createReaderDuple( path, parsedDirective.getFilename(), iscanner.getClientRequestor(), iscanner.getWorkingCopies() );
String finalPath = ScannerUtility.createReconciledPath(path, parsedDirective.getFilename());
duple = (CodeReader)iscanner.getFileCache().get(finalPath);
if (duple == null) {
duple = ScannerUtility.createReaderDuple( finalPath, iscanner.getClientRequestor(), iscanner.getWorkingCopies() );
if (duple != null && duple.isFile())
iscanner.getFileCache().put(duple.filename, duple);
}
if( duple != null )
break;
}
@ -198,7 +204,7 @@ public class GCCScannerExtension implements IScannerExtension {
{
try
{
iscanner.getContextStack().updateInclusionContext(duple.getUnderlyingReader(), duple.getFilename(), inclusion, iscanner.getClientRequestor() );
iscanner.getContextStack().updateInclusionContext(duple, inclusion, iscanner.getClientRequestor() );
TraceUtil.outputTrace( iscanner.getLogService(), "GCCScannerExtension handling #include_next directive successfully pushed on new include file" ); //$NON-NLS-1$
}
catch (ContextException e1)

View file

@ -10,11 +10,11 @@
***********************************************************************/
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.Reader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IScannerInfo;
@ -30,6 +30,7 @@ import org.eclipse.cdt.internal.core.parser.scanner.ScannerUtility.InclusionPars
* @author jcamelon
*/
public interface IScannerData {
public Map getFileCache();
/**
* @return Returns the contextStack.
*/
@ -59,10 +60,6 @@ public interface IScannerData {
* @return Returns the problemFactory.
*/
public abstract IProblemFactory getProblemFactory();
/**
* @return Returns the filename.
*/
public abstract String getInitialFilename();
/**
* @return Returns the language.
*/
@ -74,7 +71,7 @@ public interface IScannerData {
/**
* @return Returns the reader.
*/
public abstract Reader getInitialReader();
public abstract CodeReader getInitialReader();
/**
* @return Returns the requestor.
*/

View file

@ -11,7 +11,7 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.Reader;
import org.eclipse.cdt.core.parser.CodeReader;
/**
* @author jcamelon
@ -30,8 +30,8 @@ public class LimitedScannerContext
* @param object
* @param offsetLimit
*/
public LimitedScannerContext(Scanner scanner, Reader reader, String string, int offsetLimit, int index ) {
super( reader, string, null, index );
public LimitedScannerContext(Scanner scanner, CodeReader code, int offsetLimit, int index ) {
super( code, null, index );
this.scanner = scanner;
limit = offsetLimit;
}

View file

@ -12,8 +12,6 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
@ -88,8 +86,7 @@ public final class Scanner implements IScanner, IScannerData {
private IASTFactory astFactory = null;
private ISourceElementRequestor requestor;
private ParserMode parserMode;
private final String filename;
private final Reader reader;
private final CodeReader reader;
private final ParserLanguage language;
protected IParserLogService log;
private final IProblemFactory problemFactory = new ScannerProblemFactory();
@ -109,6 +106,8 @@ public final class Scanner implements IScanner, IScannerData {
private boolean limitReached = false;
private IScannerContext currentContext;
private final Map fileCache = new HashMap(100);
public void setScannerContext(IScannerContext context) {
currentContext = context;
}
@ -137,13 +136,12 @@ public final class Scanner implements IScanner, IScannerData {
throw new ScannerException( problem );
}
Scanner( Reader reader, String filename, Map definitions, List includePaths, ISourceElementRequestor requestor, ParserMode mode, ParserLanguage language, IParserLogService log, IScannerExtension extension )
Scanner( CodeReader reader, Map definitions, List includePaths, ISourceElementRequestor requestor, ParserMode mode, ParserLanguage language, IParserLogService log, IScannerExtension extension )
{
String [] incs = (String [])includePaths.toArray(STRING_ARRAY);
this.log = log;
this.requestor = requestor;
this.parserMode = mode;
this.filename = filename;
this.reader = reader;
this.language = language;
this.originalConfig = new ScannerInfo( definitions, incs );
@ -152,15 +150,18 @@ public final class Scanner implements IScanner, IScannerData {
this.scannerExtension = extension;
this.definitions = definitions;
this.includePathNames = includePaths;
if (reader.isFile())
fileCache.put(reader.filename, reader);
setupBuiltInMacros();
}
public Scanner(Reader reader, String filename, IScannerInfo info, ISourceElementRequestor requestor, ParserMode parserMode, ParserLanguage language, IParserLogService log, IScannerExtension extension, List workingCopies ) {
public Scanner(CodeReader reader, IScannerInfo info, ISourceElementRequestor requestor, ParserMode parserMode, ParserLanguage language, IParserLogService log, IScannerExtension extension, List workingCopies ) {
this.log = log;
this.requestor = requestor;
this.parserMode = parserMode;
this.filename = filename;
this.reader = reader;
this.language = language;
this.originalConfig = info;
@ -169,6 +170,9 @@ public final class Scanner implements IScanner, IScannerData {
this.scannerExtension = extension;
this.astFactory = ParserFactory.createASTFactory( this, parserMode, language );
if (reader.isFile())
fileCache.put(reader.filename, reader);
TraceUtil.outputTrace(log, "Scanner constructed with the following configuration:"); //$NON-NLS-1$
TraceUtil.outputTrace(log, "\tPreprocessor definitions from IScannerInfo: "); //$NON-NLS-1$
@ -351,14 +355,13 @@ public final class Scanner implements IScanner, IScannerData {
private void setupInitialContext()
{
String resolvedFilename = filename == null ? TEXT : filename;
IScannerContext context = null;
try
{
if( offsetLimit == NO_OFFSET_LIMIT )
context = new ScannerContextTop(reader, resolvedFilename);
context = new ScannerContextTop(reader);
else
context = new LimitedScannerContext( this, reader, resolvedFilename, offsetLimit, 0 );
context = new LimitedScannerContext( this, reader, offsetLimit, 0 );
contextStack.pushInitialContext( context );
} catch( ContextException ce )
{
@ -586,7 +589,13 @@ public final class Scanner implements IScanner, IScannerData {
while (iter.hasNext()) {
String path = (String)iter.next();
duple = ScannerUtility.createReaderDuple( path, fileName, requestor, getWorkingCopies() );
String finalPath = ScannerUtility.createReconciledPath(path, fileName);
duple = (CodeReader)fileCache.get(finalPath);
if (duple == null) {
duple = ScannerUtility.createReaderDuple( finalPath, requestor, getWorkingCopies() );
if (duple != null && duple.isFile())
fileCache.put(duple.filename, duple);
}
if( duple != null )
break totalLoop;
}
@ -597,7 +606,13 @@ public final class Scanner implements IScanner, IScannerData {
}
else // local inclusion
{
duple = ScannerUtility.createReaderDuple( new File( currentContext.getContextName() ).getParentFile().getAbsolutePath(), fileName, requestor, getWorkingCopies() );
String finalPath = ScannerUtility.createReconciledPath(new File( currentContext.getContextName() ).getParentFile().getAbsolutePath(), fileName);
duple = (CodeReader)fileCache.get(finalPath);
if (duple == null) {
duple = ScannerUtility.createReaderDuple( finalPath, requestor, getWorkingCopies() );
if (duple != null && duple.isFile())
fileCache.put(duple.filename, duple);
}
if( duple != null )
break totalLoop;
useIncludePaths = true;
@ -612,7 +627,7 @@ public final class Scanner implements IScanner, IScannerData {
inclusion =
getASTFactory().createInclusion(
fileName,
duple.getFilename(),
duple.filename,
!useIncludePaths,
beginOffset,
startLine,
@ -627,8 +642,7 @@ public final class Scanner implements IScanner, IScannerData {
try
{
contextStack.updateInclusionContext(
duple.getUnderlyingReader(),
duple.getFilename(),
duple,
inclusion,
requestor);
}
@ -1557,6 +1571,7 @@ public final class Scanner implements IScanner, IScannerData {
strbuff.append( next );
if( !processUniversalCharacterName() )
return null;
c = getChar(false);
continue; // back to top of loop
}
ungetChar( next );
@ -2140,9 +2155,8 @@ public final class Scanner implements IScanner, IScannerData {
if( ! expression.trim().equals(EMPTY_STRING))
{
IScanner subScanner = new Scanner(
new StringReader(expression),
SCRATCH,
IScanner subScanner = new Scanner(
new CodeReader(expression.toCharArray()),
getTemporaryHashtable(),
Collections.EMPTY_LIST,
NULL_REQUESTOR,
@ -2588,9 +2602,8 @@ public final class Scanner implements IScanner, IScannerData {
strbuff.append(expression);
strbuff.append(';');
IScanner trial = new Scanner(
new StringReader(strbuff.toString()),
EXPRESSION,
IScanner trial = new Scanner(
new CodeReader(strbuff.toString().toCharArray()),
definitions,
includePathNames,
NULL_REQUESTOR,
@ -3591,17 +3604,10 @@ public final class Scanner implements IScanner, IScannerData {
return branches;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getInitialFilename()
*/
public String getInitialFilename() {
return filename;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner.IScannerData#getInitialReader()
*/
public Reader getInitialReader() {
public CodeReader getInitialReader() {
return reader;
}
@ -3639,4 +3645,8 @@ public final class Scanner implements IScanner, IScannerData {
public void setIncludePathNames(List includePathNames) {
this.includePathNames = includePathNames;
}
public Map getFileCache() {
return fileCache;
}
}

View file

@ -11,16 +11,13 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.IOException;
import java.io.Reader;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.ast.IASTInclusion;
public class ScannerContextInclusion implements IScannerContext
{
public static final int UNDO_BUFFER_SIZE = 4;
protected Reader reader;
private String filename;
public CodeReader code;
private IASTInclusion inc;
private final int index;
private int line;
@ -29,9 +26,8 @@ public class ScannerContextInclusion implements IScannerContext
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.IScannerContext#initialize(Reader, String, int, IASTInclusion)
*/
public ScannerContextInclusion(Reader r, String f, IASTInclusion i, int index) {
reader = r;
filename = f;
public ScannerContextInclusion(CodeReader code, IASTInclusion i, int index) {
this.code = code;
line = 1;
inc = i;
this.index = index;
@ -41,39 +37,34 @@ public class ScannerContextInclusion implements IScannerContext
public final String getContextName()
{
return filename;
return code.filename;
}
public int getOffset()
{
return offset - pos;
return offset;
}
public void close() {
try {
reader.close();
}
catch (IOException ie) {
}
}
protected int pos = 0;
protected int undo[] = new int[UNDO_BUFFER_SIZE];
public final void ungetChar(int c) {
undo[pos++] = c;
//TODO remove close and replace by releasing from file cache
}
public int getChar() {
if (pos > 0)
return undo[--pos];
try {
++offset;
int c = reader.read();
if ((char)c == '\n') line++;
return c;
}
catch (IOException e) {
return -1;
}
if (offset == code.buffer.length)
return -1;
int c = code.buffer[offset++];
if ((char)c == '\n') line++;
return c;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.parser.scanner.IScannerContext#ungetChar(int)
*/
public void ungetChar(int undo) {
--offset;
}
/**
* Returns the kind.
* @return int

View file

@ -11,12 +11,12 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.Reader;
import org.eclipse.cdt.core.parser.CodeReader;
public class ScannerContextTop extends ScannerContextInclusion
{
ScannerContextTop(Reader r, String f) {
super(r,f,null, 0);
ScannerContextTop(CodeReader code) {
super(code, null, 0);
}
public int getKind() {

View file

@ -10,7 +10,6 @@
******************************************************************************/package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.File;
import java.io.Reader;
import java.util.Iterator;
import java.util.Vector;
@ -67,13 +66,9 @@ public class ScannerUtility {
}
static CodeReader createReaderDuple( String path, String fileName, ISourceElementRequestor requestor, Iterator workingCopies )
static CodeReader createReaderDuple( String path, ISourceElementRequestor requestor, Iterator workingCopies )
{
String finalPath = createReconciledPath(path, fileName);
Reader r = requestor.createReader( finalPath, workingCopies );
if( r != null )
return new CodeReader( finalPath, r );
return null;
return requestor.createReader( path, workingCopies );
}
/**

View file

@ -14,10 +14,10 @@
package org.eclipse.cdt.internal.core.search.matching;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IParserLogService;
@ -179,8 +179,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(patternString),
"TEXT", //$NON-NLS-1$
new CodeReader(patternString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -250,8 +249,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(patternString),
"TEXT", //$NON-NLS-1$
new CodeReader(patternString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -292,8 +290,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(nameString),
"TEXT", //$NON-NLS-1$
new CodeReader(nameString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -351,8 +348,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(patternString),
"TEXT", //$NON-NLS-1$
new CodeReader(patternString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -403,8 +399,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(patternString),
"TEXT", //$NON-NLS-1$
new CodeReader(patternString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -429,8 +424,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(patternString),
"TEXT", //$NON-NLS-1$
new CodeReader(patternString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,
@ -464,8 +458,7 @@ public abstract class CSearchPattern implements ICSearchConstants, ICSearchPatte
try {
scanner =
ParserFactory.createScanner(
new StringReader(functionString),
"TEXT", //$NON-NLS-1$
new CodeReader(functionString.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,

View file

@ -13,11 +13,7 @@
*/
package org.eclipse.cdt.internal.core.search.matching;
import java.io.CharArrayReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
@ -27,6 +23,7 @@ import java.util.LinkedList;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IScanner;
@ -389,7 +386,7 @@ public class MatchLocator implements IMatchLocator{
if (!searchScope.encloses(pathString)) continue;
Reader reader = null;
CodeReader reader = null;
realPath = null;
IProject project = null;
@ -400,7 +397,7 @@ public class MatchLocator implements IMatchLocator{
if( workingCopy != null ){
currentResource = workingCopy.getResource();
if ( currentResource != null && currentResource.isAccessible() ) {
reader = new CharArrayReader( workingCopy.getContents() );
reader = new CodeReader(currentResource.getLocation().toOSString(), workingCopy.getContents());
realPath = currentResource.getLocation();
project = currentResource.getProject();
} else {
@ -413,7 +410,7 @@ public class MatchLocator implements IMatchLocator{
if( currentResource != null ){
if (currentResource.isAccessible() && currentResource instanceof IFile) {
IFile file = (IFile) currentResource;
reader = new InputStreamReader( file.getContents() );
reader = new CodeReader(currentResource.getLocation().toOSString(), file.getContents());
realPath = currentResource.getLocation();
project = file.getProject();
} else {
@ -422,16 +419,18 @@ public class MatchLocator implements IMatchLocator{
}
} catch ( CoreException e ){
continue;
} catch ( IOException e ) {
continue;
}
}
}
if( currentResource == null ) {
IPath path = new Path( pathString );
try {
IPath path = new Path( pathString );
currentPath = path;
reader = new FileReader( path.toFile() );
reader = new CodeReader(pathString);
realPath = currentPath;
} catch (FileNotFoundException e) {
} catch (IOException e) {
continue;
}
}
@ -456,7 +455,7 @@ public class MatchLocator implements IMatchLocator{
IParser parser = null;
try
{
IScanner scanner = ParserFactory.createScanner( reader, realPath.toOSString(), scanInfo, ParserMode.COMPLETE_PARSE, language, this, ParserUtil.getScannerLogService(), null );
IScanner scanner = ParserFactory.createScanner( reader, scanInfo, ParserMode.COMPLETE_PARSE, language, this, ParserUtil.getScannerLogService(), null );
parser = ParserFactory.createParser( scanner, this, ParserMode.COMPLETE_PARSE, language, ParserUtil.getParserLogService() );
}
catch( ParserFactoryError pfe )
@ -628,9 +627,10 @@ public class MatchLocator implements IMatchLocator{
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath,workingCopies);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#parserTimeout()
*/

View file

@ -10,9 +10,9 @@
******************************************************************************/
package org.eclipse.cdt.core;
import java.io.StringReader;
import java.util.StringTokenizer;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -222,8 +222,7 @@ public class CConventions {
// assuming that you are given a valid identifier
IToken token = null;
IScanner scanner = ParserFactory.createScanner(
new StringReader( name ),
"",
new CodeReader(name.toCharArray()),
new ScannerInfo(),
ParserMode.QUICK_PARSE,
ParserLanguage.CPP,

View file

@ -10,11 +10,7 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.IOException;
import java.util.Iterator;
import org.eclipse.cdt.core.model.IWorkingCopy;
@ -51,7 +47,7 @@ public class ParserUtil
return scannerLogService;
}
public static Reader createReader( String finalPath, Iterator workingCopies )
public static CodeReader createReader( String finalPath, Iterator workingCopies )
{
// check to see if the file which this path points to points to an
// IResource in the workspace
@ -65,27 +61,22 @@ public class ParserUtil
// check the working copy
if( workingCopies.hasNext() )
{
Reader r = findWorkingCopy( resultingResource, workingCopies );
if( r != null ) return r;
char[] buffer = findWorkingCopy( resultingResource, workingCopies );
if( buffer != null )
return new CodeReader(finalPath, buffer);
}
return createResourceReader(resultingResource);
return new CodeReader(finalPath, ((IFile)resultingResource).getContents());
}
}
catch( CoreException ce )
{
}
catch( IOException e )
{
}
return InternalParserUtil.createFileReader(finalPath);
}
/**
* @param resource
* @return
* @throws CoreException
*/
public static BufferedReader createResourceReader(IResource resource) throws CoreException {
return new BufferedReader( new InputStreamReader( new BufferedInputStream( ((IFile) resource).getContents() ) ) );
}
/**
* @param finalPath
* @return
@ -106,7 +97,7 @@ public class ParserUtil
* @param workingCopies
* @return
*/
protected static Reader findWorkingCopy(IResource resultingResource, Iterator workingCopies) {
protected static char[] findWorkingCopy(IResource resultingResource, Iterator workingCopies) {
if( parserLogService.isTracing() )
parserLogService.traceLog( "Attempting to find the working copy for " + resultingResource.getName() ); //$NON-NLS-1$
while( workingCopies.hasNext() )
@ -116,10 +107,9 @@ public class ParserUtil
IWorkingCopy copy = (IWorkingCopy) next;
if( copy.getResource().equals(resultingResource ))
{
CharArrayReader arrayReader = new CharArrayReader( copy.getContents() );
if( parserLogService.isTracing() )
parserLogService.traceLog( "Working copy found!!" ); //$NON-NLS-1$
return new BufferedReader( arrayReader );
return copy.getContents();
}
}
if( parserLogService.isTracing() )

View file

@ -9,17 +9,17 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.NullLogService;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.compare.IEditableContent;
@ -74,7 +74,7 @@ public class CStructureCreator implements IStructureCreator {
//are bugs while parsing C files, we might want to create a separate Structure
//compare for c files, but we'll never be completely right about .h files
IScanner scanner =
ParserFactory.createScanner(new StringReader(s), "code", new ScannerInfo(), ParserMode.QUICK_PARSE, ParserLanguage.CPP, builder, new NullLogService(), null); //$NON-NLS-1$
ParserFactory.createScanner(new CodeReader(s.toCharArray()), new ScannerInfo(), ParserMode.QUICK_PARSE, ParserLanguage.CPP, builder, new NullLogService(), null); //$NON-NLS-1$
IParser parser = ParserFactory.createParser(scanner, builder, ParserMode.QUICK_PARSE, ParserLanguage.CPP, ParserUtil.getParserLogService() );
parser.parse();
} catch (Exception e) {

View file

@ -11,9 +11,9 @@
package org.eclipse.cdt.internal.ui.compare;
import java.io.Reader;
import java.util.Iterator;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.DefaultProblemHandler;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
@ -340,7 +340,7 @@ public class SourceElementRequestorAdapter implements ISourceElementRequestor {
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath, workingCopies );
}

View file

@ -11,14 +11,12 @@
package org.eclipse.cdt.internal.ui.search.actions;
import java.io.CharArrayReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.io.IOException;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IScannerInfoProvider;
@ -98,20 +96,20 @@ public class SelectionParseAction extends Action {
}
IParser parser = null;
Reader reader = null;
CodeReader reader = null;
try {
if( workingCopy == null )
reader = new FileReader(resourceFile.getLocation().toFile());
reader = new CodeReader(resourceFile.getLocation().toOSString());
else
reader = new CharArrayReader( workingCopy.getContents() );
} catch (FileNotFoundException e) {
reader = new CodeReader(resourceFile.getLocation().toOSString(), workingCopy.getContents());
} catch (IOException e) {
e.printStackTrace();
}
try
{
parser = ParserFactory.createParser(
ParserFactory.createScanner( reader, resourceFile.getLocation().toOSString(), scanInfo, ParserMode.SELECTION_PARSE, language, new NullSourceElementRequestor(), ParserUtil.getScannerLogService(), null ),
ParserFactory.createScanner( reader, scanInfo, ParserMode.SELECTION_PARSE, language, new NullSourceElementRequestor(), ParserUtil.getScannerLogService(), null ),
new NullSourceElementRequestor(), ParserMode.SELECTION_PARSE, language, ParserUtil.getParserLogService() );
} catch( ParserFactoryError pfe ){}

View file

@ -10,9 +10,6 @@
**********************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
@ -25,6 +22,7 @@ import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IMacroDescriptor;
import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.IScanner;
@ -152,7 +150,7 @@ public class CompletionEngine implements RelevanceConstants {
IResource currentResource = sourceUnit.getResource();
IPath realPath = currentResource.getLocation();
IProject project = currentResource.getProject();
Reader reader = new BufferedReader( new CharArrayReader( sourceUnit.getContents() ));
CodeReader reader = new CodeReader(realPath.toOSString(), sourceUnit.getContents());
//Get the scanner info
IScannerInfo scanInfo = new ScannerInfo();
@ -170,7 +168,7 @@ public class CompletionEngine implements RelevanceConstants {
IScanner scanner = null;
try
{
scanner = ParserFactory.createScanner( reader, realPath.toOSString(), scanInfo, ParserMode.COMPLETION_PARSE, language, elementRequestor, ParserUtil.getScannerLogService(), Arrays.asList(CUIPlugin.getSharedWorkingCopies()) );
scanner = ParserFactory.createScanner( reader, scanInfo, ParserMode.COMPLETION_PARSE, language, elementRequestor, ParserUtil.getScannerLogService(), Arrays.asList(CUIPlugin.getSharedWorkingCopies()) );
parser = ParserFactory.createParser( scanner, elementRequestor, ParserMode.COMPLETION_PARSE, language, ParserUtil.getParserLogService() );
}
catch( ParserFactoryError pfe )

View file

@ -10,9 +10,9 @@
***********************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist;
import java.io.Reader;
import java.util.Iterator;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.NullSourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.utils.TimeOut;
@ -32,7 +32,7 @@ public class ContentAssistElementRequestor extends NullSourceElementRequestor im
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath, Iterator workingCopies) {
public CodeReader createReader(String finalPath, Iterator workingCopies) {
return ParserUtil.createReader(finalPath, workingCopies );
}