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

org.eclipse.cdt.core

Fixed Bug 43051 : Search: cannot specify relative search paths
	Fixed Bug 45140 : refactor IScanner to allow use of Readers of IResource

org.eclipse.cdt.core.tests
org.eclipse.cdt.ui
	Updates for new ISourceElementRequestor interface updates.
This commit is contained in:
John Camelon 2004-02-25 22:18:51 +00:00
parent d3890d72b5
commit f070a52e93
14 changed files with 233 additions and 57 deletions

View file

@ -1,3 +1,6 @@
2004-02-25 John Camelon
Updates for new ISourceElementRequestor interface updates.
2004-02-25 John Camelon
Added ScannerTestCase::testGerman().

View file

@ -10,6 +10,7 @@
***********************************************************************/
package org.eclipse.cdt.core.parser.tests;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
@ -30,6 +31,7 @@ import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserFactoryError;
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.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
@ -672,6 +674,13 @@ public class CompleteParseBaseTest extends TestCase
processReference( reference );
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath) {
return ParserUtil.createReader(finalPath);
}
}
protected Iterator getNestedScopes( IASTCodeScope scope )

View file

@ -13,7 +13,7 @@ import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.internal.core.parser.IExpressionParser;
import org.eclipse.cdt.internal.core.parser.InternalParserFactory;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
public class ExprEvalTest extends TestCase {
@ -28,7 +28,7 @@ public class ExprEvalTest extends TestCase {
public void runTest(String code, int expectedValue) throws Exception {
final NullSourceElementRequestor nullCallback = new NullSourceElementRequestor();
IExpressionParser parser = InternalParserFactory.createExpressionParser(ParserFactory.createScanner( new StringReader( code ), getClass().getName(), new ScannerInfo(), null, ParserLanguage.CPP, nullCallback, new NullLogService() ), ParserLanguage.CPP, null );
IExpressionParser parser = InternalParserUtil.createExpressionParser(ParserFactory.createScanner( new StringReader( code ), getClass().getName(), new ScannerInfo(), null, ParserLanguage.CPP, nullCallback, new NullLogService() ), ParserLanguage.CPP, null );
IASTExpression expression = parser.expression(null);
assertEquals(expectedValue, expression.evaluateExpression());
}

View file

@ -15,12 +15,14 @@ package org.eclipse.cdt.internal.core.search.indexing;
* @author bgheorgh
*/
import java.io.Reader;
import java.util.LinkedList;
import org.eclipse.cdt.core.parser.DefaultProblemHandler;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
@ -476,4 +478,10 @@ public class SourceIndexerRequestor implements ISourceElementRequestor, IIndexCo
private IASTInclusion peekInclude(){
return currentInclude;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath) {
return ParserUtil.createReader(finalPath);
}
}

View file

@ -1,3 +1,7 @@
2004-02-25 John Camelon
Fixed Bug 43051 : Search: cannot specify relative search paths
Fixed Bug 45140 : refactor IScanner to allow use of Readers of IResource
2004-02-25 John Camelon
Updated Scanner to allow for invalid identifier names despite C++'s best efforts at maintaining its honour.

View file

@ -0,0 +1,38 @@
/**********************************************************************
* Copyright (c) 2002-2004 IBM Canada 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:
* IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.Reader;
/**
* @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;
}
public Reader getUnderlyingReader()
{
return reader;
}
}

View file

@ -10,6 +10,8 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.Reader;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
@ -102,4 +104,10 @@ public interface ISourceElementRequestor {
public void exitNamespaceDefinition( IASTNamespaceDefinition namespaceDefinition );
public void exitInclusion( IASTInclusion inclusion );
public void exitCompilationUnit( IASTCompilationUnit compilationUnit );
/**
* @param finalPath
* @return
*/
public Reader createReader(String finalPath);
}

View file

@ -1,5 +1,7 @@
package org.eclipse.cdt.core.parser;
import java.io.Reader;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
@ -31,6 +33,7 @@ import org.eclipse.cdt.core.parser.ast.IASTUsingDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTUsingDirective;
import org.eclipse.cdt.core.parser.ast.IASTVariable;
import org.eclipse.cdt.core.parser.ast.IASTVariableReference;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
public class NullSourceElementRequestor implements ISourceElementRequestor
@ -449,4 +452,11 @@ public class NullSourceElementRequestor implements ISourceElementRequestor
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath) {
return InternalParserUtil.createFileReader( finalPath );
}
}

View file

@ -10,6 +10,11 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.ParserFactory;
@ -19,7 +24,7 @@ import org.eclipse.cdt.core.parser.ParserLanguage;
/**
* @author jcamelon
*/
public class InternalParserFactory extends ParserFactory {
public class InternalParserUtil extends ParserFactory {
public static IExpressionParser createExpressionParser( IScanner scanner, ParserLanguage language, IParserLogService log ) throws ParserFactoryError
{
@ -29,4 +34,21 @@ public class InternalParserFactory extends ParserFactory {
return new ExpressionParser( scanner, language, logService );
}
/**
* @param finalPath
* @return
*/
public static Reader createFileReader(String finalPath) {
File includeFile = new File(finalPath);
if (includeFile.exists() && includeFile.isFile())
{
//check and see
try {
return new FileReader( includeFile);
} catch (FileNotFoundException fnf) {
}
}
return null;
}
}

View file

@ -11,8 +11,6 @@
package org.eclipse.cdt.internal.core.parser.scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
@ -28,6 +26,7 @@ import java.util.StringTokenizer;
import java.util.Vector;
import org.eclipse.cdt.core.parser.BacktrackException;
import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.Directives;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IMacroDescriptor;
@ -54,8 +53,8 @@ import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTFactory;
import org.eclipse.cdt.core.parser.ast.IASTInclusion;
import org.eclipse.cdt.core.parser.extension.IScannerExtension;
import org.eclipse.cdt.internal.core.parser.*;
import org.eclipse.cdt.internal.core.parser.InternalParserFactory;
import org.eclipse.cdt.internal.core.parser.IExpressionParser;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.ast.ASTCompletionNode;
import org.eclipse.cdt.internal.core.parser.token.KeywordSets;
import org.eclipse.cdt.internal.core.parser.token.Token;
@ -309,6 +308,7 @@ public class Scanner implements IScanner {
buffer.append( tokenizer.nextToken() );
}
file = new File( buffer.toString() );
path = buffer.toString();
}
if( file.exists() && file.isDirectory() )
@ -496,9 +496,7 @@ public class Scanner implements IScanner {
protected void handleInclusion(String fileName, boolean useIncludePaths, int beginOffset, int startLine, int nameOffset, int nameLine, int endOffset, int endLine ) throws ScannerException {
FileReader inclusionReader = null;
String newPath = null;
CodeReader duple = null;
totalLoop: for( int i = 0; i < 2; ++i )
{
if( useIncludePaths ) // search include paths for this file
@ -509,65 +507,33 @@ public class Scanner implements IScanner {
while (iter.hasNext()) {
String path = (String)iter.next();
File pathFile = new File(path);
//TODO assert pathFile.isDirectory();
StringBuffer buffer = new StringBuffer( pathFile.getPath() );
buffer.append( File.separatorChar );
buffer.append( fileName );
newPath = buffer.toString();
//TODO remove ".." and "." segments
File includeFile = new File(newPath);
if (includeFile.exists() && includeFile.isFile()) {
try {
inclusionReader = new FileReader(includeFile);
break totalLoop;
} catch (FileNotFoundException fnf) {
continue;
}
}
duple = createReaderDuple( path, fileName );
if( duple != null )
break totalLoop;
}
if (inclusionReader == null )
if (duple == null )
handleProblem( IProblem.PREPROCESSOR_INCLUSION_NOT_FOUND, fileName, beginOffset, false, true );
}
else // local inclusion
{
String currentFilename = scannerData.getContextStack().getCurrentContext().getFilename();
File currentIncludeFile = new File( currentFilename );
String parentDirectory = currentIncludeFile.getParentFile().getAbsolutePath();
currentIncludeFile = null;
StringBuffer buffer = new StringBuffer( parentDirectory );
buffer.append( File.separatorChar );
buffer.append( fileName );
newPath = buffer.toString();
//TODO remove ".." and "." segments
File includeFile = new File( newPath );
if (includeFile.exists() && includeFile.isFile()) {
try {
inclusionReader = new FileReader(includeFile);
break totalLoop;
} catch (FileNotFoundException fnf) {
useIncludePaths = true;
continue totalLoop;
}
}
else
{
useIncludePaths = true;
continue totalLoop;
}
duple = createReaderDuple( new File( scannerData.getContextStack().getCurrentContext().getFilename() ).getParentFile().getAbsolutePath(), fileName );
if( duple != null )
break totalLoop;
useIncludePaths = true;
continue totalLoop;
}
}
if (inclusionReader != null) {
if (duple!= null) {
IASTInclusion inclusion = null;
try
{
inclusion =
scannerData.getASTFactory().createInclusion(
fileName,
newPath,
duple.getFilename(),
!useIncludePaths,
beginOffset,
startLine,
@ -581,7 +547,7 @@ public class Scanner implements IScanner {
try
{
scannerData.getContextStack().updateContext(inclusionReader, newPath, ScannerContext.ContextKind.INCLUSION, inclusion, scannerData.getClientRequestor() );
scannerData.getContextStack().updateContext(duple.getUnderlyingReader(), duple.getFilename(), ScannerContext.ContextKind.INCLUSION, inclusion, scannerData.getClientRequestor() );
}
catch (ContextException e1)
{
@ -2283,7 +2249,7 @@ public class Scanner implements IScanner {
EXPRESSION,
new ScannerInfo( scannerData.getDefinitions(), scannerData.getOriginalConfig().getIncludePaths()),
ParserMode.QUICK_PARSE, scannerData.getLanguage(), new NullSourceElementRequestor(), nullLogService );
parser = InternalParserFactory.createExpressionParser(trial, scannerData.getLanguage(), nullLogService);
parser = InternalParserUtil.createExpressionParser(trial, scannerData.getLanguage(), nullLogService);
} catch( ParserFactoryError pfe )
{
handleInternalError();
@ -3129,4 +3095,53 @@ public class Scanner implements IScanner {
return ( scannerData.getContextStack().getCurrentContext() == scannerData.getContextStack().getTopContext() );
}
protected CodeReader createReaderDuple( String path, String fileName )
{
File pathFile = new File(path);
//TODO assert pathFile.isDirectory();
StringBuffer newPathBuffer = new StringBuffer( pathFile.getPath() );
newPathBuffer.append( File.separatorChar );
newPathBuffer.append( fileName );
//remove ".." and "." segments
String finalPath = reconcilePath( newPathBuffer.toString() );
Reader r = scannerData.getClientRequestor().createReader( finalPath );
if( r != null )
return new CodeReader( finalPath, r );
return null;
}
/**
* @param string
* @return
*/
private String reconcilePath(String originalPath ) {
if( originalPath == null ) return null;
String [] segments = originalPath.split( "[/\\\\]" );
if( segments.length == 1 ) return originalPath;
Vector results = new Vector();
for( int i = 0; i < segments.length; ++i )
{
String segment = segments[i];
if( segment.equals( ".") ) continue;
if( segment.equals("..") )
{
if( results.size() > 0 )
results.removeElementAt( results.size() - 1 );
}
else
results.add( segment );
}
StringBuffer buffer = new StringBuffer();
Iterator i = results.iterator();
while( i.hasNext() )
{
buffer.append( (String)i.next() );
if( i.hasNext() )
buffer.append( File.separatorChar );
}
scannerData.getLogService().traceLog( "Path has been reduced to " + buffer.toString());
return buffer.toString();
}
}

View file

@ -593,4 +593,11 @@ public class MatchLocator implements ISourceElementRequestor, ICSearchConstants
public static void verbose(String log) {
System.out.println("(" + Thread.currentThread() + ") " + log);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath) {
return ParserUtil.createReader(finalPath);
}
}

View file

@ -10,8 +10,20 @@
***********************************************************************/
package org.eclipse.cdt.core.parser;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.eclipse.cdt.internal.core.model.IDebugLogConstants;
import org.eclipse.cdt.internal.core.parser.InternalParserUtil;
import org.eclipse.cdt.internal.core.parser.ParserLogService;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
/**
* @author jcamelon
@ -34,4 +46,31 @@ public class ParserUtil
public static IParserLogService getScannerLogService() {
return scannerLogService;
}
public static Reader createReader( String finalPath )
{
// check to see if the file which this path points to points to an
// IResource in the workspace
try
{
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path( finalPath );
if( workspace.getRoot().getLocation().isPrefixOf( path ) )
path = path.removeFirstSegments(workspace.getRoot().getLocation().segmentCount() );
IResource result = workspace.getRoot().findMember(path);
if( result != null && result.getType() == IResource.FILE )
{
BufferedInputStream bufferedStream = new BufferedInputStream( ((IFile) result).getContents() );
InputStreamReader reader = new InputStreamReader( bufferedStream );
return reader;
}
}
catch( CoreException ce )
{
}
return InternalParserUtil.createFileReader(finalPath);
}
}

View file

@ -1,3 +1,6 @@
2004-02-25 John Camelon
Updates for new ISourceElementRequestor interface updates.
2004-02-24 Alain Magloire
New constructor for ShowInCView.

View file

@ -11,10 +11,13 @@
package org.eclipse.cdt.internal.ui.compare;
import java.io.Reader;
import org.eclipse.cdt.core.parser.DefaultProblemHandler;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ISourceElementRequestor;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ast.IASTASMDefinition;
import org.eclipse.cdt.core.parser.ast.IASTAbstractTypeSpecifierDeclaration;
import org.eclipse.cdt.core.parser.ast.IASTClassReference;
@ -331,4 +334,11 @@ public class SourceElementRequestorAdapter implements ISourceElementRequestor {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.ISourceElementRequestor#createReader(java.lang.String)
*/
public Reader createReader(String finalPath) {
return ParserUtil.createReader(finalPath);
}
}