diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java index 5221e73ee34..1fd09ee8dae 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ParserTestSuite.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2002,2003 Rational Software Corporation and others. + * Copyright (c) 2002,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 @@ -16,6 +16,7 @@ import junit.framework.TestSuite; import org.eclipse.cdt.core.model.tests.CModelElementsTests; import org.eclipse.cdt.core.model.tests.StructuralCModelElementsTests; +import org.eclipse.cdt.core.parser.tests.ast2.DOMGCCParserExtensionTestSuite; import org.eclipse.cdt.core.parser.tests.ast2.DOMParserTestSuite; import org.eclipse.cdt.core.parser.tests.scanner2.IncludeTest; import org.eclipse.cdt.core.parser.tests.scanner2.ObjectMapTest; @@ -55,6 +56,7 @@ public class ParserTestSuite extends TestCase { suite.addTest( ScannerParserLoopTest.suite() ); suite.addTest( GCCParserExtensionTestSuite.suite() ); suite.addTest( DOMParserTestSuite.suite() ); + suite.addTest( DOMGCCParserExtensionTestSuite.suite() ); return suite; } } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMFileBasePluginTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMFileBasePluginTest.java new file mode 100644 index 00000000000..4781bbe5786 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMFileBasePluginTest.java @@ -0,0 +1,155 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ + +/* + * Created on Sept 28, 2004 + */ +package org.eclipse.cdt.core.parser.tests.ast2; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.core.testplugin.FileManager; +import org.eclipse.cdt.internal.core.index.sourceindexer.SourceIndexer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +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.NullProgressMonitor; + +/** + * @author dsteffle + */ +public class DOMFileBasePluginTest extends TestCase { + static NullProgressMonitor monitor; + static IWorkspace workspace; + static IProject project; + static FileManager fileManager; + static int numProjects = 0; + static Class className; + static ICProject cPrj; + + private void initialize(Class aClassName){ + if( CCorePlugin.getDefault() != null && CCorePlugin.getDefault().getCoreModel() != null){ + //(CCorePlugin.getDefault().getCoreModel().getIndexManager()).reset(); + monitor = new NullProgressMonitor(); + + workspace = ResourcesPlugin.getWorkspace(); + + try { + cPrj = CProjectHelper.createCCProject("ParserTestProject", "bin"); //$NON-NLS-1$ //$NON-NLS-2$ + + project = cPrj.getProject(); + project.setSessionProperty(SourceIndexer.activationKey,new Boolean(false)); + + // ugly + if (className == null || !className.equals(aClassName)) { + className = aClassName; + numProjects++; + } + } catch ( CoreException e ) { + /*boo*/ + } + if (project == null) + throw new NullPointerException("Unable to create project"); //$NON-NLS-1$ + + //Create file manager + fileManager = new FileManager(); + } + } + + public DOMFileBasePluginTest(String name, Class className) + { + super(name); + initialize(className); + } + + public void cleanupProject() throws Exception { + numProjects--; + + try{ + if (numProjects == 0) { + project.delete( true, false, monitor ); + project = null; + } + } catch( Throwable e ){ + /*boo*/ + } + } + + protected void tearDown() throws Exception { + if( project == null || !project.exists() ) + return; + + IResource [] members = project.members(); + for( int i = 0; i < members.length; i++ ){ + if( members[i].getName().equals( ".project" ) || members[i].getName().equals( ".cdtproject" ) ) //$NON-NLS-1$ //$NON-NLS-2$ + continue; + try{ + members[i].delete( false, monitor ); + } catch( Throwable e ){ + /*boo*/ + } + } + } + + // below can be used to work with large files (too large for memory) +// protected IFile importFile(String fileName) throws Exception { +// IFile file = cPrj.getProject().getFile(fileName); +// if (!file.exists()) { +// try{ +// FileInputStream fileIn = new FileInputStream( +// CTestPlugin.getDefault().getFileInPlugin(new Path("resources/parser/" + fileName))); +// file.create(fileIn,false, monitor); +// } catch (CoreException e) { +// e.printStackTrace(); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// } +// +// return file; +// } + + protected IFolder importFolder(String folderName) throws Exception { + IFolder folder = project.getProject().getFolder(folderName); + + //Create file input stream + if( !folder.exists() ) + folder.create( false, false, monitor ); + + return folder; + } + public IFile importFile(String fileName, String contents ) throws Exception{ + //Obtain file handle + IFile file = project.getProject().getFile(fileName); + + InputStream stream = new ByteArrayInputStream( contents.getBytes() ); + //Create file input stream + if( file.exists() ) + file.setContents( stream, false, false, monitor ); + else + file.create( stream, false, monitor ); + + fileManager.addFile(file); + + return file; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCParserExtensionTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCParserExtensionTestSuite.java new file mode 100644 index 00000000000..8b639b672ee --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCParserExtensionTestSuite.java @@ -0,0 +1,32 @@ +/********************************************************************** + * Copyright (c) 2004, 2005 IBM Canada Ltd. 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.tests.ast2; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * @author jcamelon + * + */ +public class DOMGCCParserExtensionTestSuite extends TestCase { + + public static Test suite() { + TestSuite suite= new TestSuite(DOMGCCParserExtensionTestSuite.class.getName()); +// suite.addTestSuite( GCCScannerExtensionsTest.class ); +// suite.addTestSuite( GCCQuickParseExtensionsTest.class ); +// suite.addTestSuite( GCCCompleteParseExtensionsTest.class ); + suite.addTestSuite( DOMGCCSelectionParseExtensionsTest.class); + return suite; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCSelectionParseExtensionsTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCSelectionParseExtensionsTest.java new file mode 100644 index 00000000000..da0711d7cca --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMGCCSelectionParseExtensionsTest.java @@ -0,0 +1,38 @@ +/********************************************************************** + * Copyright (c) 2002, 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: + * IBM Rational Software - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.core.parser.tests.ast2; + +import java.io.StringWriter; +import java.io.Writer; + +import org.eclipse.cdt.core.dom.ast.IASTNode; + +/** + * @author jcamelon + * + */ +public class DOMGCCSelectionParseExtensionsTest extends DOMSelectionParseBaseTest { + + public DOMGCCSelectionParseExtensionsTest(String name) { + super(name, DOMGCCSelectionParseExtensionsTest.class); + } + + public void testBug43021() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "extern int johnc(__const char *__restrict __format, ...);\n" ); //$NON-NLS-1$ + writer.write( "void m() {johnc(\"HI\");}" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "{johnc") + 1; //$NON-NLS-1$ + IASTNode node = parse( code, startIndex, startIndex + 5 ); + assertNotNull( node ); + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java index 988ebcb4779..9b613fb6f85 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java @@ -1,5 +1,5 @@ /********************************************************************** - * Copyright (c) 2004 IBM Corporation and others. + * Copyright (c) 2004,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 @@ -42,9 +42,9 @@ public class DOMParserTestSuite extends TestCase { suite.addTestSuite( AST2CPPSpecFailingTest.class ); suite.addTestSuite( AST2CSpecTest.class ); suite.addTestSuite( AST2CSpecFailingTest.class ); + suite.addTestSuite( DOMSelectionParseTest.class ); suite.addTest( CompletionTestSuite.suite() ); return suite; } - } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseBaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseBaseTest.java new file mode 100644 index 00000000000..9147efa4fa4 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseBaseTest.java @@ -0,0 +1,74 @@ +/********************************************************************** + * Copyright (c) 2002-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: + * IBM Rational Software - Initial API and implementation */ +package org.eclipse.cdt.core.parser.tests.ast2; + +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.search.DOMSearchUtil; +import org.eclipse.core.resources.IFile; + +/** + * @author johnc + * + */ +public class DOMSelectionParseBaseTest extends DOMFileBasePluginTest { + + public DOMSelectionParseBaseTest(String name, Class className) { + super(name, className); + } + + protected IASTNode parse(String code, int offset1, int offset2) throws Exception { + return parse( code, offset1, offset2, true ); + } + + /** + * @param code + * @param offset1 + * @param offset2 + * @param b + * @return + */ + protected IASTNode parse(String code, int offset1, int offset2, boolean expectedToPass) throws Exception { + IFile file = importFile("temp.cpp", code); //$NON-NLS-1$ + IASTName[] names = DOMSearchUtil.getSelectedNamesFrom(file, offset1, offset2 - offset1); + + if (!expectedToPass) return null; + + if (names.length == 0) { + assertFalse(true); + } else { + return names[0]; + } + + return null; + } + + protected IASTNode parse(IFile file, int offset1, int offset2, boolean expectedToPass) throws Exception { + IASTName[] names = DOMSearchUtil.getSelectedNamesFrom(file, offset1, offset2 - offset1); + + if (!expectedToPass) return null; + + if (names.length == 0) { + assertFalse(true); + } else { + return names[0]; + } + + return null; + } + + protected IASTName[] getDeclarationOffTU(IASTName name) { + return name.getTranslationUnit().getDeclarations(name.resolveBinding()); + } + + protected IASTName[] getReferencesOffTU(IASTName name) { + return name.getTranslationUnit().getReferences(name.resolveBinding()); + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java new file mode 100644 index 00000000000..ce1c119eed2 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMSelectionParseTest.java @@ -0,0 +1,1591 @@ +/********************************************************************** + * Copyright (c) 2002,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: + * IBM Rational Software - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.core.parser.tests.ast2; + +import java.io.StringWriter; +import java.io.Writer; + +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.dom.ast.IEnumeration; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.ILabel; +import org.eclipse.cdt.core.dom.ast.IMacroBinding; +import org.eclipse.cdt.core.dom.ast.IParameter; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.internal.core.dom.parser.ASTNode; +import org.eclipse.cdt.internal.core.model.CProject; +import org.eclipse.core.resources.IFile; + +/** + * @author dsteffle + */ +public class DOMSelectionParseTest extends DOMSelectionParseBaseTest { + + public DOMSelectionParseTest(String name) { + super(name, DOMSelectionParseTest.class); + } + + public void testBaseCase_VariableReference() throws Exception + { + String code = "void f() { int x; x=3; }"; //$NON-NLS-1$ + int offset1 = code.indexOf( "x=" ); //$NON-NLS-1$ + int offset2 = code.indexOf( '='); + IASTNode node = parse( code, offset1, offset2 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "x" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 15); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBaseCase_FunctionReference() throws Exception + { + String code = "int x(){x( );}"; //$NON-NLS-1$ + int offset1 = code.indexOf( "x( " ); //$NON-NLS-1$ + int offset2 = code.indexOf( "( )"); //$NON-NLS-1$ + IASTNode node = parse( code, offset1, offset2 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "x" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBaseCase_Error() throws Exception + { + String code = "int x() { y( ) }"; //$NON-NLS-1$ + int offset1 = code.indexOf( "y( " ); //$NON-NLS-1$ + int offset2 = code.indexOf( "( )"); //$NON-NLS-1$ + assertNull( parse( code, offset1, offset2, false )); + } + + public void testBaseCase_FunctionDeclaration() throws Exception + { + String code = "int x(); x( );"; //$NON-NLS-1$ + int offset1 = code.indexOf( "x()" ); //$NON-NLS-1$ + int offset2 = code.indexOf( "()"); //$NON-NLS-1$ + IASTNode node = parse( code, offset1, offset2 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "x" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBaseCase_FunctionDeclaration2() throws Exception + { + String code = "int printf( const char *, ... ); "; //$NON-NLS-1$ + int offset1 = code.indexOf( "printf" ); //$NON-NLS-1$ + int offset2 = code.indexOf( "( const"); //$NON-NLS-1$ + IASTNode node = parse( code, offset1, offset2 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "printf" ); //$NON-NLS-1$ + } + + public void testBaseCase_VariableDeclaration() throws Exception + { + String code = "int x = 3;"; //$NON-NLS-1$ + int offset1 = code.indexOf( "x" ); //$NON-NLS-1$ + int offset2 = code.indexOf( " ="); //$NON-NLS-1$ + IASTNode node = parse( code, offset1, offset2 ); + assertNotNull( node ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + } + + public void testBaseCase_Parameter() throws Exception + { + String code = "int main( int argc ) { int x = argc; }"; //$NON-NLS-1$ + int offset1 = code.indexOf( "argc;" ); //$NON-NLS-1$ + int offset2 = code.indexOf( ";" ); //$NON-NLS-1$ + IASTNode node = parse( code, offset1, offset2 ); + assertNotNull( node ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "argc" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "argc" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 14); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBug57898() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Gonzo { public: void playHorn(); };\n" ); //$NON-NLS-1$ + writer.write( "void Gonzo::playHorn() { return; }\n" ); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) { Gonzo gonzo; gonzo.playHorn(); }\n" ); //$NON-NLS-1$ + String code = writer.toString(); + for( int i = 0; i < 3; ++i ) + { + int start = -1, stop = -1; + switch( i ) + { + case 0: + start = code.indexOf( "void playHorn") + 5; //$NON-NLS-1$ + break; + case 1: + start = code.indexOf( "::playHorn") + 2; //$NON-NLS-1$ + break; + case 2: + start = code.indexOf( ".playHorn") + 1; //$NON-NLS-1$ + break; + } + stop = start + 8; + IASTNode node = parse( code, start, stop ); + assertNotNull( node ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "playHorn" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "playHorn" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 28); + assertEquals( ((ASTNode)decls[0]).getLength(), 8); + } + } + + public void testConstructorDestructorDeclaration() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Gonzo { Gonzo(); ~Gonzo(); };"); //$NON-NLS-1$ + String code = writer.toString(); + int offset = code.indexOf( " Gonzo()") + 1; //$NON-NLS-1$ + IASTNode node = parse( code, offset, offset + 5 ); + assertNotNull( node ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "Gonzo" ); //$NON-NLS-1$ + + offset = code.indexOf( "~Gonzo"); //$NON-NLS-1$ + node = parse( code, offset, offset + 6 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "~Gonzo" ); //$NON-NLS-1$ + } + + public void testBug60264() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "namespace Muppets { int i; }\n" ); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) { Muppets::i = 1; }\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int index = code.indexOf( "Muppets::"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 7 ); + assertNotNull( node ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPNamespace ); + assertEquals( ((IASTName)node).toString(), "Muppets" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Muppets" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 10); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + + index = code.indexOf( "e Muppets") + 2; //$NON-NLS-1$ + node = parse( code, index, index + 7 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPNamespace ); + assertEquals( ((IASTName)node).toString(), "Muppets" ); //$NON-NLS-1$ + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Muppets" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 10); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + } + + public void testBug61613() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Foo { // ** (A) **\n" ); //$NON-NLS-1$ + writer.write( " public:\n" ); //$NON-NLS-1$ + writer.write( "Foo() {};\n" ); //$NON-NLS-1$ + writer.write( "};\n" ); //$NON-NLS-1$ + writer.write( "int \n" ); //$NON-NLS-1$ + writer.write( "main(int argc, char **argv) {\n" ); //$NON-NLS-1$ + writer.write( "Foo foo; // ** (B) **\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int index = code.indexOf( "class Foo") + 6; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 3 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "Foo" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Foo" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 6); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug60038() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Gonzo {\n"); //$NON-NLS-1$ + writer.write( "public:\n"); //$NON-NLS-1$ + writer.write( "Gonzo( const Gonzo & other ){}\n"); //$NON-NLS-1$ + writer.write( "Gonzo() {}\n"); //$NON-NLS-1$ + writer.write( "~Gonzo(){}\n"); //$NON-NLS-1$ + writer.write( "};\n"); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write( " Gonzo * g = new Gonzo();\n"); //$NON-NLS-1$ + writer.write( " Gonzo * g2 = new Gonzo( *g );\n"); //$NON-NLS-1$ + writer.write( " g->~Gonzo();\n"); //$NON-NLS-1$ + writer.write( " return (int) g2;\n"); //$NON-NLS-1$ + writer.write( "}\n"); //$NON-NLS-1$ + String code = writer.toString(); + for( int i = 0; i < 3; ++i ) + { + int startOffset = 0, endOffset = 0; + switch( i ) + { + case 0: + startOffset = code.indexOf( "new Gonzo()") + 4; //$NON-NLS-1$ + endOffset = startOffset + 5; + break; + case 1: + startOffset = code.indexOf( "new Gonzo( ") + 4; //$NON-NLS-1$ + endOffset = startOffset + 5; + break; + default: + startOffset = code.indexOf( "->~") + 2; //$NON-NLS-1$ + endOffset = startOffset + 6; + } + IASTNode node = parse( code, startOffset, endOffset ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + IBinding binding = ((IASTName)node).resolveBinding(); + IASTName[] decls = null; + switch( i ) + { + case 0: + assertTrue( binding instanceof ICPPConstructor ); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Gonzo" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 53); + assertEquals( ((ASTNode)decls[0]).getLength(), 5); + break; + case 1: + assertTrue( binding instanceof ICPPConstructor ); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Gonzo" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 22); + assertEquals( ((ASTNode)decls[0]).getLength(), 5); + break; + default: + assertFalse( binding instanceof ICPPConstructor ); + String name = ((IASTName)node).toString(); + assertEquals( name.indexOf("~"), 0); //$NON-NLS-1$ + assertEquals( name.indexOf("Gonzo"), 1); //$NON-NLS-1$ + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "~Gonzo" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 64); + assertEquals( ((ASTNode)decls[0]).getLength(), 6); + break; + + } + } + } + + public void testMethodReference() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Sample { public:\n"); //$NON-NLS-1$ + writer.write( " int getAnswer() const;\n"); //$NON-NLS-1$ + writer.write( "};\n"); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$ + writer.write( " Sample * s = new Sample();\n" ); //$NON-NLS-1$ + writer.write( " return s->getAnswer();\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "->getAnswer") + 2; //$NON-NLS-1$ + IASTNode node = parse( code, startIndex, startIndex+9); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "getAnswer" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "getAnswer" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 29); + assertEquals( ((ASTNode)decls[0]).getLength(), 9); + } + + public void testConstructorDefinition() throws Exception + { + String code = "class ABC { public: ABC(); }; ABC::ABC(){}"; //$NON-NLS-1$ + int startIndex = code.indexOf( "::ABC") + 2; //$NON-NLS-1$ + IASTNode node = parse( code, startIndex, startIndex + 3 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "ABC" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "ABC" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 20); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug63966() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "void foo(int a) {}\n" ); //$NON-NLS-1$ + writer.write( "void foo(long a) {}\n" ); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$ + writer.write( "foo(1); \n }" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "foo(1)"); //$NON-NLS-1$ + parse( code, startIndex, startIndex + 3 ); + } + + public void testBug66744() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "enum EColours { RED, GREEN, BLUE }; \n" ); //$NON-NLS-1$ + writer.write( "void foo() { EColours color = GREEN; } \n" ); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "EColours color"); //$NON-NLS-1$ + parse( code, startIndex, startIndex + 8 ); + } + + + + public void testBug68527() throws Exception + { + Writer writer = new StringWriter(); + writer.write("struct X;\n"); //$NON-NLS-1$ + writer.write("struct X anA;"); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "X anA"); //$NON-NLS-1$ + parse( code, startIndex, startIndex + 1 ); + } + + public void testBug60407() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "struct ZZZ { int x, y, z; };\n" ); //$NON-NLS-1$ + writer.write( "typedef struct ZZZ _FILE;\n" ); //$NON-NLS-1$ + writer.write( "typedef _FILE FILE;\n" ); //$NON-NLS-1$ + writer.write( "static void static_function(FILE * lcd){}\n" ); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$ + writer.write( "FILE * file = 0;\n" ); //$NON-NLS-1$ + writer.write( "static_function( file );\n" ); //$NON-NLS-1$ + writer.write( "return 0;\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "static_function( file )"); //$NON-NLS-1$ + parse( code, startIndex, startIndex + "static_function".length() ); //$NON-NLS-1$ + } + + public void testBug61800() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class B {};\n"); //$NON-NLS-1$ + writer.write( "class ABCDEF {\n"); //$NON-NLS-1$ + writer.write( " static B stInt; };\n"); //$NON-NLS-1$ + writer.write( "B ABCDEF::stInt = 5;\n"); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "::stInt") + 2; //$NON-NLS-1$ + + IASTNode node = parse( code, startIndex, startIndex+ 5 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "stInt" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "stInt" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 37); + assertEquals( ((ASTNode)decls[0]).getLength(), 5); + } + + public void testBug68739() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "int fprintf( int *, const char *, ... ); \n" ); //$NON-NLS-1$ + writer.write( "void boo( int * lcd ) { \n" ); //$NON-NLS-1$ + writer.write( " /**/fprintf( lcd, \"%c%s 0x%x\", ' ', \"bbb\", 2 ); \n" ); //$NON-NLS-1$ + writer.write( "} \n" ); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "/**/fprintf") + 4; //$NON-NLS-1$ + + IASTNode node = parse( code, startIndex, startIndex+ 7 ); + + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "fprintf" ); //$NON-NLS-1$ + } + + public void testBug72818() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "union Squaw { int x; double u; };\n" ); //$NON-NLS-1$ + writer.write( "int main(int argc, char **argv) {\n" ); //$NON-NLS-1$ + writer.write( "return sizeof( Squaw );\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "sizeof( ") + "sizeof( ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode node = parse( code, startIndex, startIndex + 5 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "Squaw" ); //$NON-NLS-1$ + assertEquals( ((ICPPClassType)((IASTName)node).resolveBinding()).getKey(), ICPPClassType.k_union ); + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Squaw" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 6); + assertEquals( ((ASTNode)decls[0]).getLength(), 5); + } + + public void test72220() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "const int FOUND_ME = 1;\n" ); //$NON-NLS-1$ + writer.write( "class Test{\n" ); //$NON-NLS-1$ + writer.write( "public:\n" ); //$NON-NLS-1$ + writer.write( "const int findCode() const;\n" ); //$NON-NLS-1$ + writer.write( "};\n" ); //$NON-NLS-1$ + writer.write( "const int Test::findCode() const {\n" ); //$NON-NLS-1$ + writer.write( "return FOUND_ME;\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int startIndex = code.indexOf( "return ") + "return ".length(); //$NON-NLS-1$ //$NON-NLS-2$ + IASTNode node = parse( code, startIndex, startIndex + 8 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "FOUND_ME" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "FOUND_ME" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 10); + assertEquals( ((ASTNode)decls[0]).getLength(), 8); + } + + public void testBug72721() throws Exception{ + Writer writer = new StringWriter(); + writer.write(" class ABC { public: ABC(int); }; \n"); //$NON-NLS-1$ + writer.write("void f() { \n"); //$NON-NLS-1$ + writer.write(" int j = 1; \n"); //$NON-NLS-1$ + writer.write(" new ABC( j + 1 ); \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "ABC(" ); //$NON-NLS-1$ + IASTNode node = parse( code, startIndex, startIndex + 3 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "ABC" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "ABC" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 21); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug72372() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace B { \n"); //$NON-NLS-1$ + writer.write(" class SD_02 { void f_SD(); }; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + writer.write("using namespace B; \n"); //$NON-NLS-1$ + writer.write("void SD_02::f_SD(){} \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( ":f_SD" ); //$NON-NLS-1$ + IASTNode node = parse( code, startIndex + 1, startIndex + 5 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "f_SD" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "f_SD" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 71); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + public void testBug72372_2() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace A { \n"); //$NON-NLS-1$ + writer.write(" namespace B { \n"); //$NON-NLS-1$ + writer.write(" void f_SD(); \n"); //$NON-NLS-1$ + writer.write(" } \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + writer.write("namespace C { \n"); //$NON-NLS-1$ + writer.write(" using namespace A; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + writer.write("void C::B::f_SD(){} \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( ":f_SD" ); //$NON-NLS-1$ + IASTNode node = parse( code, startIndex + 1, startIndex + 5 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "f_SD" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "f_SD" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 109); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBug72713() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "class Deck{ void initialize(); }; \n"); //$NON-NLS-1$ + writer.write( "void Deck::initialize(){} \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( ":initialize" ); //$NON-NLS-1$ + IASTNode node = parse( code, startIndex + 1, startIndex + 11 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "initialize" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "initialize" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 17); + assertEquals( ((ASTNode)decls[0]).getLength(), 10); + } + + public void testBug72712() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "class B{ public: B(); }; void f(){ B* b; b = new B(); }" ); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "new B" ) + 4; //$NON-NLS-1$ + + IASTNode node = parse( code, startIndex, startIndex + 1 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "B" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "B" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 17); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug72712_2() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "class A {}; \n"); //$NON-NLS-1$ + writer.write( "class B{ public: B( A* ); }; \n"); //$NON-NLS-1$ + writer.write( "void f(){ B* b; b = new B( (A*)0 ); } \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "(A*)" ) + 1; //$NON-NLS-1$ + + IASTNode node = parse( code, startIndex, startIndex + 1 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "A" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "A" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 6); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug72814() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "namespace N{ \n"); //$NON-NLS-1$ + writer.write( " template < class T > class AAA { T _t; };\n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + writer.write( "N::AAA a; \n"); //$NON-NLS-1$ + + String code = writer.toString(); + int startIndex = code.indexOf( "AAA" ); //$NON-NLS-1$ + IASTNode node = parse( code, startIndex, startIndex + 3 ); + + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "AAA" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "AAA" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 75); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + + node = parse( code, startIndex, startIndex + 8 ); + + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "AAA" ); //$NON-NLS-1$ + decls = getDeclarationOffTU((IASTName)node); + // TODO raised bug 92632 for below +// assertEquals(decls.length, 1); +// assertEquals( decls[0].toString(), "AAA" ); //$NON-NLS-1$ +// assertEquals( ((ASTNode)decls[0]).getOffset(), 15); +// assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug72710() throws Exception + { + Writer writer = new StringWriter(); + writer.write( "class Card{\n" ); //$NON-NLS-1$ + writer.write( " Card( int rank );\n" ); //$NON-NLS-1$ + writer.write( " int rank;\n" ); //$NON-NLS-1$ + writer.write( "};\n" ); //$NON-NLS-1$ + writer.write( "Card::Card( int rank ) {\n" ); //$NON-NLS-1$ + writer.write( "this->rank = rank;\n" ); //$NON-NLS-1$ + writer.write( "}\n" ); //$NON-NLS-1$ + String code = writer.toString(); + int index = code.indexOf( "this->rank") + 6; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 36); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + + public void testBug75731() throws Exception + { + Writer writer = new StringWriter(); + writer.write("int rank() {\n"); //$NON-NLS-1$ + writer.write("return 5;\n}\n"); //$NON-NLS-1$ + writer.write("class Card{\n"); //$NON-NLS-1$ + writer.write("private:\n"); //$NON-NLS-1$ + writer.write("Card( int rank );\n"); //$NON-NLS-1$ + writer.write("int rank;\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("int getRank();\n};\n"); //$NON-NLS-1$ + writer.write("Card::Card( int rank )\n{\n"); //$NON-NLS-1$ + writer.write("this->rank = ::rank();\n"); //$NON-NLS-1$ + writer.write("this->rank = this->rank;\n"); //$NON-NLS-1$ + writer.write("this->rank = rank;\n"); //$NON-NLS-1$ + writer.write("this->rank = Card::rank;\n"); //$NON-NLS-1$ + writer.write("this->rank = getRank();\n}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf( "int rank() {") + 4; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "class Card{") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 31); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "Card( int rank );"); //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 46); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "Card( int rank );") + 10; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 56); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "int rank;") + 4; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "int getRank();") + 4; //$NON-NLS-1$ + node = parse( code, index, index + 7 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "getRank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "getRank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 86); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + + index = code.indexOf( "Card::Card( int rank )"); //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPClassType ); + assertEquals( ((IASTName)node).toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 31); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "Card::Card( int rank )") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "Card" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 46); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "Card::Card( int rank )") + 16; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 56); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = ::rank();") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = ::rank();") + 15; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IFunction ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = this->rank;") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = this->rank;") + 19; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = rank;") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = rank;") + 13; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 2); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 56); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = Card::rank;") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = Card::rank;") + 19; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = getRank();") + 6; //$NON-NLS-1$ + node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "rank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 68); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + + index = code.indexOf( "this->rank = getRank();") + 13; //$NON-NLS-1$ + node = parse( code, index, index + 7 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "getRank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "getRank" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 86); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + } + + public void testBug77989() throws Exception { + Writer writer = new StringWriter(); + writer.write("namespace N { /* A */\n"); //$NON-NLS-1$ + writer.write("class C{};\n}\n"); //$NON-NLS-1$ + writer.write("using namespace N; /* B */\n"); //$NON-NLS-1$ + writer.write("N::C c; /* C */\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf( "using namespace N;") + 16; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPNamespace ); + assertEquals( ((IASTName)node).toString(), "N" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "N" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 10); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug78435() throws Exception { + Writer writer = new StringWriter(); + writer.write("int itself; //A\n"); //$NON-NLS-1$ + writer.write("void f(int itself){} //B\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf( "void f(int itself){}") + 11; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 6 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "itself" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "itself" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 36); + assertEquals( ((ASTNode)decls[0]).getLength(), 6); + } + + public void testBug78231A() throws Exception { + Writer writer = new StringWriter(); + writer.write("struct Base {\n"); //$NON-NLS-1$ + writer.write("int Data; // 1\n"); //$NON-NLS-1$ + writer.write("struct Data; // 2\n};\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf("struct Data;") + 7; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICompositeType ); + assertEquals( ((IASTName)node).toString(), "Data" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Data" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 36); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBug78231B() throws Exception { + Writer writer = new StringWriter(); + writer.write("int Data;\n"); //$NON-NLS-1$ + writer.write("struct Base {\n"); //$NON-NLS-1$ + writer.write("int Data; // 1\n"); //$NON-NLS-1$ + writer.write("struct Data; // 2\n};\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf("struct Data;") + 7; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICompositeType ); + assertEquals( ((IASTName)node).toString(), "Data" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)node).getOffset(), index); + } + + public void testBug64326() throws Exception { + Writer writer = new StringWriter(); + writer.write("class foo {\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("foo() {}\n"); //$NON-NLS-1$ + writer.write("int bar;\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("int\n"); //$NON-NLS-1$ + writer.write("main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("foo* f;\n"); //$NON-NLS-1$ + writer.write("f->bar = 1; // ** (A) **\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + int index = code.indexOf("f->bar") + 3; //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 3 ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "bar" ); //$NON-NLS-1$ + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "bar" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 33); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug92605() throws Exception { + Writer writer = new StringWriter(); + writer.write("#define UINT32 unsigned int\n"); //$NON-NLS-1$ + writer.write("#define HANDLE unsigned int**\n"); //$NON-NLS-1$ + writer.write("// ...\n"); //$NON-NLS-1$ + writer.write("void foo()\n"); //$NON-NLS-1$ + writer.write("{\n"); //$NON-NLS-1$ + writer.write("UINT32 u; // line A\n"); //$NON-NLS-1$ + writer.write("HANDLE h; // line B\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + String code = writer.toString(); + + int index = code.indexOf("UINT32 u;"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 6, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IMacroBinding ); + assertEquals( ((IASTName)node).toString(), "UINT32" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "UINT32" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 8); + assertEquals( ((ASTNode)decls[0]).getLength(), 6); + } + + public void testBug79877() throws Exception { + Writer writer = new StringWriter(); + writer.write("int Func2() {\n"); //$NON-NLS-1$ + writer.write("int i;\n"); //$NON-NLS-1$ + writer.write("i = Func1();\n"); //$NON-NLS-1$ + writer.write("return(0);\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + String code = writer.toString(); + IFile test1 = importFile("test1.c", code); //$NON-NLS-1$ + + writer.write("int Func1(void) {\n"); //$NON-NLS-1$ + writer.write("return(10);\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + importFile("test2.c", writer.toString()); //$NON-NLS-1$ + + int index = code.indexOf("Func1"); //$NON-NLS-1$ + IASTNode node = parse( test1, index, index + 5, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICExternalBinding ); + assertEquals( ((IASTName)node).toString(), "Func1" ); //$NON-NLS-1$ + + ICElement[] scope = new ICElement[1]; + scope[0] = new CProject(null, project); + +// // TODO need to register to an index and wait for it to finish before this test will work +// +// Set matches = SearchEngine.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), (IASTName)node, CSearchPattern.DECLARATIONS); +// assertEquals(matches.size(), 1); + } + + public void testBug78114() throws Exception { + Writer writer = new StringWriter(); + writer.write("class Point{ //line C\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("Point(): xCoord(0){}\n"); //$NON-NLS-1$ + writer.write("Point(int x){} //line B \n"); //$NON-NLS-1$ + writer.write("private: \n"); //$NON-NLS-1$ + writer.write("int xCoord; \n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("int main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("Point &p2 = *(new Point(10)); // line A\n"); //$NON-NLS-1$ + writer.write("return (0);\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("Point(10)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 5, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "Point" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "Point" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 53); + assertEquals( ((ASTNode)decls[0]).getLength(), 5); + } + + public void testBug73398() throws Exception { + Writer writer = new StringWriter(); + writer.write("int joo=4;\n"); //$NON-NLS-1$ + writer.write("#define koo 4\n"); //$NON-NLS-1$ + writer.write("int main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("return (koo);\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("koo);"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 3, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IMacroBinding ); + assertEquals( ((IASTName)node).toString(), "koo" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "koo" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 19); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug() throws Exception { + Writer writer = new StringWriter(); + writer.write("class Point{ \n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("Point(): xCoord(0){}\n"); //$NON-NLS-1$ + writer.write(" Point& operator=(const Point &rhs){return *this;} // line A\n"); //$NON-NLS-1$ + writer.write("private: \n"); //$NON-NLS-1$ + writer.write("int xCoord; \n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("static const Point zero;\n"); //$NON-NLS-1$ + writer.write("int main(int argc, char **argv) { \n"); //$NON-NLS-1$ + writer.write(" Point *p2 = new Point();\n"); //$NON-NLS-1$ + writer.write("p2->operator=(zero); // line B\n"); //$NON-NLS-1$ + writer.write("return (0); \n"); //$NON-NLS-1$ + writer.write(" }\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("operator=(zero)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 9, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "operator =" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "operator =" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 51); + assertEquals( ((ASTNode)decls[0]).getLength(), 9); + } + + public void testBug80826() throws Exception { + Writer writer = new StringWriter(); + writer.write("void swapImpl(int& a, int& b) {/*...*/} // line C\n"); //$NON-NLS-1$ + writer.write("#define swap(a,b) (swapImpl(a,b)) // line B\n"); //$NON-NLS-1$ + writer.write("// ...\n"); //$NON-NLS-1$ + writer.write("void foo(int x, int y)\n"); //$NON-NLS-1$ + writer.write("{\n"); //$NON-NLS-1$ + writer.write("swap(x,y); // line A\n"); //$NON-NLS-1$ + writer.write(" //...\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("swap(x,y);"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IMacroBinding ); + assertEquals( ((IASTName)node).toString(), "swap" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "swap" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 58); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBug78389() throws Exception { + Writer writer = new StringWriter(); + writer.write("class A{\n"); //$NON-NLS-1$ + writer.write("void method1(){} //line A\n"); //$NON-NLS-1$ + writer.write("void method1(int i){} //line B\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("void f(){\n"); //$NON-NLS-1$ + writer.write("A a; \n"); //$NON-NLS-1$ + writer.write("a.method1(3); // F3 on method1 in this line should highlight method1 on line B\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("method1(3)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 7, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "method1" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "method1" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 41); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + } + + public void testBug78625() throws Exception { + Writer writer = new StringWriter(); + writer.write("class A{ \n"); //$NON-NLS-1$ + writer.write("public: A(int i){} \n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("class B: A{\n"); //$NON-NLS-1$ + writer.write("B():A(2) {} //line 5\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("A(2)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPConstructor ); + assertEquals( ((IASTName)node).toString(), "A" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "A" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 29); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug78656() throws Exception { + Writer writer = new StringWriter(); + writer.write("class A{\n"); //$NON-NLS-1$ + writer.write("public: int method1(){} //line 2\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("void f() {\n"); //$NON-NLS-1$ + writer.write("A a;\n"); //$NON-NLS-1$ + writer.write("int i=a.method1(); //line 6\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("method1();"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 7, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "method1" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "method1" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 21); + assertEquals( ((ASTNode)decls[0]).getLength(), 7); + } + + public void testBug79965() throws Exception { + Writer writer = new StringWriter(); + writer.write("int i = 2, half_i = i / 2;\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("i / 2"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "i" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "i" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug64326A() throws Exception { + Writer writer = new StringWriter(); + writer.write("class foo {\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("foo() {}\n"); //$NON-NLS-1$ + writer.write("void bar() {}\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("int\n"); //$NON-NLS-1$ + writer.write("main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("foo* f;\n"); //$NON-NLS-1$ + writer.write("f->bar(); // ** (A) **\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("bar();"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 3, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "bar" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "bar" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 34); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug64326B() throws Exception { + Writer writer = new StringWriter(); + writer.write("class foo {\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("foo() {}\n"); //$NON-NLS-1$ + writer.write("int bar;\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("int \n"); //$NON-NLS-1$ + writer.write("main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("foo* f;\n"); //$NON-NLS-1$ + writer.write("f->bar = 1; // ** (A) **\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("bar = "); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 3, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPField ); + assertEquals( ((IASTName)node).toString(), "bar" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "bar" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 33); + assertEquals( ((ASTNode)decls[0]).getLength(), 3); + } + + public void testBug43128A() throws Exception { + Writer writer = new StringWriter(); + writer.write("void foo()\n"); //$NON-NLS-1$ + writer.write("{\n"); //$NON-NLS-1$ + writer.write("int x=3;\n"); //$NON-NLS-1$ + writer.write(" // ...\n"); //$NON-NLS-1$ + writer.write("x++;\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("x++"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "x" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 17); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug43128B() throws Exception { + Writer writer = new StringWriter(); + writer.write("int\n"); //$NON-NLS-1$ + writer.write("main(int argc, char **argv) {\n"); //$NON-NLS-1$ + writer.write("int x = argc;\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("argc;"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 4, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IParameter ); + assertEquals( ((IASTName)node).toString(), "argc" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "argc" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 13); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBug43128C() throws Exception { + Writer writer = new StringWriter(); + writer.write("int j;\n"); //$NON-NLS-1$ + writer.write("int foo(int x) \n"); //$NON-NLS-1$ + writer.write("{ \n"); //$NON-NLS-1$ + writer.write("int y;\n"); //$NON-NLS-1$ + writer.write("x = 4;\n"); //$NON-NLS-1$ + writer.write("j = 5;\n"); //$NON-NLS-1$ + writer.write("y = 6;\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("x ="); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "x" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "x" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 19); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + + index = code.indexOf("j ="); //$NON-NLS-1$ + node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "j" ); //$NON-NLS-1$ + + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "j" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 4); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + + index = code.indexOf("y ="); //$NON-NLS-1$ + node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "y" ); //$NON-NLS-1$ + + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "y" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 30); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug86504() throws Exception { + Writer writer = new StringWriter(); + writer.write("class C { };\n"); //$NON-NLS-1$ + writer.write("void f(int(C)) { } // void f(int (*fp)(C c)) { }\n"); //$NON-NLS-1$ + writer.write("// not: void f(int C);\n"); //$NON-NLS-1$ + writer.write("int g(C);\n"); //$NON-NLS-1$ + writer.write("void foo() {\n"); //$NON-NLS-1$ + writer.write("f(g); // openDeclarations on g causes StackOverflowError\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("g); "); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPFunction ); + assertEquals( ((IASTName)node).toString(), "g" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "g" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 89); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBug79811() throws Exception { + Writer writer = new StringWriter(); + writer.write("enum E{E0};\n"); //$NON-NLS-1$ + writer.write("void f() {\n"); //$NON-NLS-1$ + writer.write("enum E{E1};\n"); //$NON-NLS-1$ + writer.write("E e; //this one is incorrectly found\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + writer.write("E f; //ok\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("E{E0}"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IEnumeration ); + assertEquals( ((IASTName)node).toString(), "E" ); //$NON-NLS-1$ + + IASTName[] decls = getReferencesOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "E" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 76); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + public void testBugLabelWithMacro() throws Exception { + Writer writer = new StringWriter(); + writer.write("#define UINT32 unsigned int\n"); //$NON-NLS-1$ + writer.write("#define HANDLE unsigned int**\n"); //$NON-NLS-1$ + writer.write("void foo()\n"); //$NON-NLS-1$ + writer.write("{\n"); //$NON-NLS-1$ + writer.write("UINT32 u;\n"); //$NON-NLS-1$ + writer.write("HANDLE h;\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + writer.write("int foo2() {\n"); //$NON-NLS-1$ + writer.write("test:\n"); //$NON-NLS-1$ + writer.write("goto test;\n"); //$NON-NLS-1$ + writer.write("return foo();\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("HANDLE h"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 6, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IMacroBinding ); + assertEquals( ((IASTName)node).toString(), "HANDLE" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "HANDLE" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 36); + assertEquals( ((ASTNode)decls[0]).getLength(), 6); + + index = code.indexOf("test;"); //$NON-NLS-1$ + node = parse( code, index, index + 4, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ILabel ); + assertEquals( ((IASTName)node).toString(), "test" ); //$NON-NLS-1$ + + decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "test" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 132); + assertEquals( ((ASTNode)decls[0]).getLength(), 4); + } + + public void testBugMethodDef() throws Exception { + Writer writer = new StringWriter(); + writer.write("class tetrahedron {\n"); //$NON-NLS-1$ + writer.write("private:\n"); //$NON-NLS-1$ + writer.write("int color;\n"); //$NON-NLS-1$ + writer.write("public:\n"); //$NON-NLS-1$ + writer.write("/* Methods */\n"); //$NON-NLS-1$ + writer.write("void setColor(int c) \n"); //$NON-NLS-1$ + writer.write("{color = c < 0 ? 0 : c;};\n"); //$NON-NLS-1$ + writer.write("void set();\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + writer.write("void tetrahedron::set() {\n"); //$NON-NLS-1$ + writer.write("int color;\n"); //$NON-NLS-1$ + writer.write("setColor(color);\n"); //$NON-NLS-1$ + writer.write("}\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("setColor(color)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 8, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof ICPPMethod ); + assertEquals( ((IASTName)node).toString(), "setColor" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "setColor" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 67); + assertEquals( ((ASTNode)decls[0]).getLength(), 8); + + IASTName[] refs = getReferencesOffTU((IASTName)node); + assertEquals(refs.length, 1); + assertEquals( refs[0].toString(), "setColor" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)refs[0]).getOffset(), 162); + assertEquals( ((ASTNode)refs[0]).getLength(), 8); + } + + public void testBug86698() throws Exception { + Writer writer = new StringWriter(); + writer.write("struct C;\n"); //$NON-NLS-1$ + writer.write("void no_opt(C*);\n"); //$NON-NLS-1$ + writer.write("struct C {\n"); //$NON-NLS-1$ + writer.write("int c;\n"); //$NON-NLS-1$ + writer.write("C() : c(0) { no_opt(this); }\n"); //$NON-NLS-1$ + writer.write("};\n"); //$NON-NLS-1$ + + String code = writer.toString(); + + int index = code.indexOf("c(0)"); //$NON-NLS-1$ + IASTNode node = parse( code, index, index + 1, true ); + assertTrue( node instanceof IASTName ); + assertTrue( ((IASTName)node).resolveBinding() instanceof IVariable ); + assertEquals( ((IASTName)node).toString(), "c" ); //$NON-NLS-1$ + + IASTName[] decls = getDeclarationOffTU((IASTName)node); + assertEquals(decls.length, 1); + assertEquals( decls[0].toString(), "c" ); //$NON-NLS-1$ + assertEquals( ((ASTNode)decls[0]).getOffset(), 42); + assertEquals( ((ASTNode)decls[0]).getLength(), 1); + } + + +} + diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IEntryResult.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IEntryResult.java index 29b5bc37a03..20ed3b3cde2 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IEntryResult.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/IEntryResult.java @@ -34,5 +34,13 @@ public interface IEntryResult { * offset array */ public int[][] getOffsetLengths(); + + /** + * Returns the simple name for this IEntryResult. + * + * ex: + * typeDecl/V/foo/namespace returns "foo" + */ + public String extractSimpleName(); } diff --git a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/cindexstorage/EntryResult.java b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/cindexstorage/EntryResult.java index fd75d81e77b..7e78c3708db 100644 --- a/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/cindexstorage/EntryResult.java +++ b/core/org.eclipse.cdt.core/index/org/eclipse/cdt/internal/core/index/cindexstorage/EntryResult.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2003 IBM Corporation and others. + * Copyright (c) 2000, 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 @@ -12,6 +12,7 @@ package org.eclipse.cdt.internal.core.index.cindexstorage; import org.eclipse.cdt.internal.core.CharOperation; import org.eclipse.cdt.internal.core.index.IEntryResult; +import org.eclipse.cdt.internal.core.index.IIndex; public class EntryResult implements IEntryResult { @@ -86,9 +87,45 @@ public String toString(){ public int[][] getOffsets() { return offsets; } +/* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.index.IEntryResult#getOffsetLengths() + */ public int[][] getOffsetLengths() { return offsetLengths; } +/* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.index.IEntryResult#extractSimpleName() + */ +public String extractSimpleName() { + return EntryResult.extractSimpleName(getWord()); +} +/* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.index.IEntryResult#extractSimpleName(char[]) + */ +public static String extractSimpleName(char[] word) { + String aWord = new String(word); + String longName=null; + + String typeDecl = Index.getDescriptionOf(IIndex.TYPE, IIndex.ANY, IIndex.DECLARATION); + String typeDef = Index.getDescriptionOf(IIndex.TYPE, IIndex.ANY, IIndex.DEFINITION); + String typeRef = Index.getDescriptionOf(IIndex.TYPE, IIndex.ANY, IIndex.REFERENCE); + + if (aWord.indexOf(typeDecl) == 0) { + longName = aWord.substring(aWord.indexOf(IndexerOutput.SEPARATOR, typeDecl.length())+1); + } else if (aWord.indexOf(typeRef) == 0) { + longName = aWord.substring(aWord.indexOf(IndexerOutput.SEPARATOR, typeRef.length())+1); + } else if (aWord.indexOf(typeDef) == 0) { + longName = aWord.substring(aWord.indexOf(IndexerOutput.SEPARATOR, typeDef.length())+1); + } else { + longName = aWord.substring(aWord.indexOf(IndexerOutput.SEPARATOR)+1); + } + + int sepPos=longName.indexOf(IndexerOutput.SEPARATOR); + if (sepPos>=0) + return aWord.substring(0, longName.indexOf(IndexerOutput.SEPARATOR)); + else + return longName; +} } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java index b000a4602a6..a7067b0566c 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ICElement.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2004 QNX Software Systems and others. + * Copyright (c) 2000, 2005 QNX Software Systems 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 @@ -193,6 +193,12 @@ public interface ICElement extends IAdaptable { * struct C; */ static final int C_UNION_DECLARATION = 87; + + /** + * An unknown ICElement. Mainly used to determine what elements are not yet implemented. + * i.e. the new DOM Parser supports open declaration on labels, while the old parser did not + */ + static final int C_UNKNOWN_DECLARATION = 88; /** * Modifier indicating a class constructor diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java index 6ca9aca8bd5..bef796512c0 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java @@ -374,4 +374,63 @@ public class ASTTypeUtil { return getType(type); } + /** + * This can be used to invoke the IType's isConst() if it has an isConst() method. + * This returns the result of that invoked isConst() method. + * It is a convenience function so that the structure of IType does not need + * to be known to determine if the IType is const or not. + * + * Note: false is returned if no isConst() method is found + * + * @param type + * @return + */ + public static boolean isConst(IType type) { + if (type instanceof IQualifierType) { + try { + return ((IQualifierType)type).isConst(); + } catch (DOMException e) { + return false; + } + } else if (type instanceof ITypeContainer) { + try { + return isConst(((ITypeContainer)type).getType()); + } catch (DOMException e) { + return false; + } + } else if (type instanceof IArrayType) { + try { + return isConst(((IArrayType)type).getType()); + } catch (DOMException e) { + return false; + } + } else if (type instanceof ICPPReferenceType) { + try { + return isConst(((ICPPReferenceType)type).getType()); + } catch (DOMException e) { + return false; + } + } else if (type instanceof IFunctionType) { + try { + return isConst(((IFunctionType)type).getReturnType()); + } catch (DOMException e) { + return false; + } + } else if (type instanceof IPointerType) { + try { + return isConst(((IPointerType)type).getType()); + } catch (DOMException e) { + return false; + } + } else if (type instanceof ITypedef) { + try { + return isConst(((ITypedef)type).getType()); + } catch (DOMException e) { + return false; + } + } else { + return false; + } + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java index 40397a4140e..552637fca60 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/CharArrayUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004 IBM Corporation and others. + * Copyright (c) 2004,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 @@ -212,7 +212,7 @@ public class CharArrayUtils { for( int i = 0; i < array.length; i++ ){ if( toBeFound[j] == array[i] ){ if( ++j == toBeFound.length ) - return i - j; + return i - j + 1; } else j = 0; } diff --git a/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java new file mode 100644 index 00000000000..f6c253e9ec2 --- /dev/null +++ b/core/org.eclipse.cdt.core/search/org/eclipse/cdt/core/search/DOMSearchUtil.java @@ -0,0 +1,330 @@ +/******************************************************************************* + * Copyright (c) 2000, 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.search; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.ICLogConstants; +import org.eclipse.cdt.core.dom.CDOM; +import org.eclipse.cdt.core.dom.IASTServiceProvider; +import org.eclipse.cdt.core.dom.ast.ASTVisitor; +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.dom.ast.IEnumeration; +import org.eclipse.cdt.core.dom.ast.IEnumerator; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IMacroBinding; +import org.eclipse.cdt.core.dom.ast.ITypedef; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias; +import org.eclipse.cdt.core.filetype.ICFileType; +import org.eclipse.cdt.core.filetype.ICFileTypeConstants; +import org.eclipse.cdt.core.parser.ParseError; +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.parser.util.ArrayUtil; +import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; +import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; +import org.eclipse.cdt.internal.core.search.matching.CSearchPattern; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; + +/** + * Utility class to have commonly used algorithms in one place for searching with the DOM. + * + * @author dsteffle + */ +public class DOMSearchUtil { + private static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0]; + private static final IASTName[] EMPTY_NAME_LIST = BLANK_NAME_ARRAY; + private static final Set EMPTY_MATCHES = new HashSet(0); + + /** + * This is a convenience method that uses the SearchEngine to find declarations, references, or both that correspond + * to the IASTName searchName found in the index. + * + * @param scope is used to limit the scope that SearchEngine searches the index against + * @param searchName is the IASTName whose delcarations/references are sought after + * @param limitTo used to specify whether to get declarations, references, or both, one of: + * ( CSearchPattern.DECLARATION | CSearchPattern.REFERENCES | CSearchPattern.ALL_OCCURRENCES ) + * @return + */ + public static Set getMatchesFromSearchEngine(ICSearchScope scope, IASTName searchName, LimitTo limitTo) { + SearchEngine engine = new SearchEngine(); + BasicSearchResultCollector results = new BasicSearchResultCollector(); + + ICSearchPattern pattern = createPattern(searchName.resolveBinding(), limitTo, true); + + try { + engine.search(CCorePlugin.getWorkspace(), pattern, scope, results, false); + } catch (InterruptedException e) { + return EMPTY_MATCHES; + } + + return results.getSearchResults(); + } + + private static CSearchPattern createPattern( IBinding binding, LimitTo limitTo, boolean caseSensitive) { + // build the SearchFor/pattern based on the IBinding + SearchFor searchFor = createSearchFor(binding); + if (binding instanceof IFunction) { + searchFor = ICSearchConstants.FUNCTION; + } else if (binding instanceof ICPPNamespace || binding instanceof ICPPNamespaceAlias) { + searchFor = ICSearchConstants.NAMESPACE; + } else if (binding instanceof ICPPField) { + searchFor = ICSearchConstants.FIELD; + } else if (binding instanceof IEnumerator) { + searchFor = ICSearchConstants.ENUMTOR; + } else if (binding instanceof ICPPMethod) { + searchFor = ICSearchConstants.METHOD; + } else if (binding instanceof IMacroBinding) { + searchFor = ICSearchConstants.MACRO; + } else if (binding instanceof ITypedef) { + searchFor = ICSearchConstants.TYPEDEF; + } else if (binding instanceof IVariable) { + searchFor = ICSearchConstants.VAR; + } else if (binding instanceof ICPPClassType) { + searchFor = ICSearchConstants.CLASS; + } else if (binding instanceof ICompositeType) { + try { + switch(((ICompositeType)binding).getKey()) { + case ICompositeType.k_struct: + searchFor = ICSearchConstants.CLASS_STRUCT; + break; + case ICompositeType.k_union: + searchFor = ICSearchConstants.UNION; + break; + } + } catch (DOMException e) { + searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR; + } + } else if (binding instanceof IEnumeration) { + searchFor = ICSearchConstants.ENUM; + } else { + searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR; + } + + return CSearchPattern.createPattern(binding.getName(), searchFor, limitTo, ICSearchConstants.EXACT_MATCH, caseSensitive); + } + + private static SearchFor createSearchFor( IBinding binding ) { + SearchFor searchFor = null; + if (binding instanceof IFunction) { + searchFor = ICSearchConstants.FUNCTION; + } else if (binding instanceof ICPPNamespace || binding instanceof ICPPNamespaceAlias) { + searchFor = ICSearchConstants.NAMESPACE; + } else if (binding instanceof ICPPField) { + searchFor = ICSearchConstants.FIELD; + } else if (binding instanceof IEnumerator) { + searchFor = ICSearchConstants.ENUMTOR; + } else if (binding instanceof ICPPMethod) { + searchFor = ICSearchConstants.METHOD; + } else if (binding instanceof IMacroBinding) { + searchFor = ICSearchConstants.MACRO; + } else if (binding instanceof ITypedef) { + searchFor = ICSearchConstants.TYPEDEF; + } else if (binding instanceof IVariable) { + searchFor = ICSearchConstants.VAR; + } else if (binding instanceof ICPPClassType) { + searchFor = ICSearchConstants.CLASS; + } else if (binding instanceof ICompositeType) { + try { + switch(((ICompositeType)binding).getKey()) { + case ICompositeType.k_struct: + searchFor = ICSearchConstants.CLASS_STRUCT; + break; + case ICompositeType.k_union: + searchFor = ICSearchConstants.UNION; + break; + } + } catch (DOMException e) { + searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR; + } + } else if (binding instanceof IEnumeration) { + searchFor = ICSearchConstants.ENUM; + } else { + searchFor = ICSearchConstants.UNKNOWN_SEARCH_FOR; + } + return searchFor; + } + + /** + * This is used to get a List of selected names in an IFile based on the offset and length into that IFile. + * + * ex: IFile contains: int foo; + * then getSelectedNamesFrom(file, 4, 3) will return the IASTName corresponding to foo + * + * @param file the IFile whose selection + * @param offset + * @param length + * @return + */ + public static IASTName[] getSelectedNamesFrom(IFile file, int offset, int length) { + IASTNode node = null; + IASTTranslationUnit tu = null; + try { + tu = CDOM.getInstance().getASTService().getTranslationUnit( + file, + CDOM.getInstance().getCodeReaderFactory( + CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + } catch (IASTServiceProvider.UnsupportedDialectException e) { + return EMPTY_NAME_LIST; + } + try{ + node = tu.selectNodeForLocation(file.getRawLocation().toOSString(), offset, length); + } + catch (ParseError er){} + catch ( VirtualMachineError vmErr){ + if (vmErr instanceof OutOfMemoryError){ + org.eclipse.cdt.internal.core.model.Util.log(null, "Open Declarations Out Of Memory error: " + vmErr.getMessage() + " on File: " + file.getName(), ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$ + } + } + catch (Exception ex){} + + finally{ + if (node == null){ + return EMPTY_NAME_LIST; + } + } + + if (node instanceof IASTName) { + IASTName[] results = new IASTName[1]; + results[0] = (IASTName)node; + return results; + } else { + ASTVisitor collector = null; + if (getLanguageFromFile(file) == ParserLanguage.CPP) { + collector = new CPPNameCollector(); + } else { + collector = new CNameCollector(); + } + + node.accept( collector ); + + List names = null; + if (collector instanceof CPPNameCollector) { + names = ((CPPNameCollector)collector).nameList; + } else { + names = ((CNameCollector)collector).nameList; + } + + IASTName[] results = new IASTName[names.size()]; + for(int i=0; i= nameList.size() ) + return null; + return (IASTName) nameList.get( idx ); + } + public int size() { return nameList.size(); } + } + + /** + * The CNameCollector used to get IASTNames from an IASTNode. + * + * @author dsteffle + */ + static public class CNameCollector extends CASTVisitor { + { + shouldVisitNames = true; + } + public List nameList = new ArrayList(); + public int visit( IASTName name ){ + nameList.add( name ); + return PROCESS_CONTINUE; + } + public IASTName getName( int idx ){ + if( idx < 0 || idx >= nameList.size() ) + return null; + return (IASTName) nameList.get( idx ); + } + public int size() { return nameList.size(); } + } +} diff --git a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java index ab97e51ed51..6e111d2a60a 100644 --- a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java +++ b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java @@ -216,6 +216,10 @@ public class DOMAST extends ViewPart { return tu; } + + public ParserLanguage getTULanguage() { + return lang; + } public void dispose() { } @@ -1084,7 +1088,7 @@ public class DOMAST extends ViewPart { private class DisplaySearchResultAction extends Action { protected void displayNames(IASTName[] names, String queryLabel, String pattern) { - DOMQuery job = new DOMQuery(names, queryLabel, pattern); + DOMDisplaySearchNames job = new DOMDisplaySearchNames(names, queryLabel, pattern); NewSearchUI.activateSearchResultView(); NewSearchUI.runQueryInBackground(job); } @@ -1094,7 +1098,7 @@ public class DOMAST extends ViewPart { private static final String IASTPROBLEM = "IASTProblem"; //$NON-NLS-1$ private static final String PROBLEMS_FOUND = "Problems Found"; //$NON-NLS-1$ protected void displayProblems(IASTProblem[] problems, String queryLabel, String pattern) { - DOMQuery job = new DOMQuery(problems, queryLabel, pattern); + DOMDisplaySearchNames job = new DOMDisplaySearchNames(problems, queryLabel, pattern); NewSearchUI.activateSearchResultView(); NewSearchUI.runQueryInBackground(job); } diff --git a/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMDisplaySearchNames.java b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMDisplaySearchNames.java new file mode 100644 index 00000000000..afd46a0035c --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMDisplaySearchNames.java @@ -0,0 +1,259 @@ +/********************************************************************** + * Copyright (c) 2005 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.ui.tests.DOMAST; + +import org.eclipse.cdt.core.browser.PathUtil; +import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTProblem; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.ICompositeType; +import org.eclipse.cdt.core.dom.ast.IEnumeration; +import org.eclipse.cdt.core.dom.ast.IEnumerator; +import org.eclipse.cdt.core.dom.ast.IField; +import org.eclipse.cdt.core.dom.ast.IFunction; +import org.eclipse.cdt.core.dom.ast.IMacroBinding; +import org.eclipse.cdt.core.dom.ast.ITypedef; +import org.eclipse.cdt.core.dom.ast.IVariable; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.parser.ParserUtil; +import org.eclipse.cdt.core.search.BasicSearchMatch; +import org.eclipse.cdt.core.search.IMatch; +import org.eclipse.cdt.internal.ui.search.CSearchQuery; +import org.eclipse.cdt.internal.ui.search.CSearchResult; +import org.eclipse.cdt.internal.ui.search.NewSearchResultCollector; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubProgressMonitor; +import org.eclipse.search.ui.ISearchQuery; +import org.eclipse.search.ui.ISearchResult; + +/** + * This is used for the DOM AST Viewer only... it requires that the names to be displayed + * in the search view were already found elsewhere. + * @author dsteffle + */ +public class DOMDisplaySearchNames extends CSearchQuery implements ISearchQuery { + + private static final String BLANK_STRING = ""; //$NON-NLS-1$ + private CSearchResult _result; + private IASTNode[] nodes = null; + private String queryLabel = null; + + /** + * + */ + public DOMDisplaySearchNames(IASTNode[] nodes, String queryLabel, String pattern) { + super(CUIPlugin.getWorkspace(), pattern, false, null, null, null, queryLabel, null); + this.nodes = nodes; + this.queryLabel = queryLabel; + } + + /* (non-Javadoc) + * @see org.eclipse.search.ui.ISearchQuery#run(org.eclipse.core.runtime.IProgressMonitor) + */ + public IStatus run(IProgressMonitor monitor) + throws OperationCanceledException { + + final CSearchResult textResult= (CSearchResult) getSearchResult(); + + IProgressMonitor mainSearchPM= new SubProgressMonitor(monitor, 1000); + NewSearchResultCollector collector = new NewSearchResultCollector(textResult, mainSearchPM); + + collector.aboutToStart(); + + for (int i=0; i 0) { + Iterator itr = matches.iterator(); + while(itr.hasNext()) { + Object next = itr.next(); + if (next instanceof IMatch) { + try { + collector.acceptMatch((IMatch)next); + } catch (CoreException e) { + // don't do anything if the match wasn't accepted + } + } + } + } else { // only search against the DOM if the index failed to get results... i.e. don't want duplicates + IASTName[] foundNames = DOMSearchUtil.getNamesFromDOM(searchName, limitTo); + + for (int i=0; i 1) { // too many names selected + operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE); + return; + } + + foundName = (IASTName)names.get(0); } - - CSearchQuery job = createSearchQuery(node); + + LimitTo limitTo = getLimitTo(); + ICSearchScope searchScope = null; + searchScope = getScope(); + + CSearchQuery job = FindAction.createSearchQueryForName(foundName, limitTo, searchScope); if (job == null) return; @@ -171,10 +286,37 @@ public abstract class FindAction extends SelectionParseAction { NewSearchUI.runQueryInBackground(job); } + + private class ParseWithProgress implements IRunnableWithProgress + { + private IFile file=null; + private IASTTranslationUnit tu = null; + + public ParseWithProgress(IFile file) { + this.file = file; + } + + public void run(IProgressMonitor monitor) { + try { + tu = CDOM.getInstance().getASTService().getTranslationUnit( + file, + CDOM.getInstance().getCodeReaderFactory( + CDOM.PARSE_WORKING_COPY_WHENEVER_POSSIBLE)); + } catch (IASTServiceProvider.UnsupportedDialectException e) { + operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE); + return; + } + } + + public IASTTranslationUnit getTu() { + return tu; + } + }; - abstract protected String getScopeDescription(); + abstract protected String getScopeDescription(); abstract protected ICSearchScope getScope(); abstract protected LimitTo getLimitTo(); + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java index 1c2269a4634..e1d802aadf1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsAction.java @@ -11,12 +11,15 @@ package org.eclipse.cdt.internal.ui.search.actions; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.search.ICSearchConstants; import org.eclipse.cdt.core.search.ICSearchScope; import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; +import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.search.CSearchMessages; +import org.eclipse.core.resources.IProject; import org.eclipse.ui.IWorkbenchSite; @@ -51,10 +54,10 @@ public class FindDeclarationsAction extends FindAction { setToolTipText(tooltip); } /* (non-Javadoc) - * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope() + * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope(org.eclipse.core.resources.IProject) */ protected ICSearchScope getScope() { - return SearchEngine.createWorkspaceScope(); + return SearchEngine.createWorkspaceScope(); } /* (non-Javadoc) * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScopeDescription() diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsInWorkingSetAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsInWorkingSetAction.java index 8220abbbddf..f9c60d700d9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsInWorkingSetAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindDeclarationsInWorkingSetAction.java @@ -17,6 +17,7 @@ import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.search.CSearchMessages; import org.eclipse.cdt.internal.ui.search.CSearchScopeFactory; import org.eclipse.cdt.internal.ui.search.CSearchUtil; +import org.eclipse.core.resources.IProject; import org.eclipse.ui.IWorkbenchSite; import org.eclipse.ui.IWorkingSet; @@ -71,7 +72,7 @@ public class FindDeclarationsInWorkingSetAction extends FindAction { return scopeDescription; } /* (non-Javadoc) - * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope() + * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope(org.eclipse.core.resources.IProject) */ protected ICSearchScope getScope() { IWorkingSet[] workingSets= null; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsAction.java index d23ebd86985..cde50aca4a2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsAction.java @@ -10,12 +10,15 @@ ******************************************************************************/ package org.eclipse.cdt.internal.ui.search.actions; +import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.search.ICSearchConstants; import org.eclipse.cdt.core.search.ICSearchScope; import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; +import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.search.CSearchMessages; +import org.eclipse.core.resources.IProject; import org.eclipse.ui.IWorkbenchSite; @@ -64,11 +67,11 @@ public class FindRefsAction extends FindAction { return CSearchMessages.getString("WorkspaceScope"); //$NON-NLS-1$ } /* (non-Javadoc) - * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope() + * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope(org.eclipse.core.resources.IProject) */ - protected ICSearchScope getScope() { - return SearchEngine.createWorkspaceScope(); - } + protected ICSearchScope getScope() { + return SearchEngine.createWorkspaceScope(); + } /* (non-Javadoc) * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getLimitTo() */ diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsInWorkingSetAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsInWorkingSetAction.java index ab182dbe088..15d7d9a4e90 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsInWorkingSetAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/FindRefsInWorkingSetAction.java @@ -17,6 +17,7 @@ import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.search.CSearchMessages; import org.eclipse.cdt.internal.ui.search.CSearchScopeFactory; import org.eclipse.cdt.internal.ui.search.CSearchUtil; +import org.eclipse.core.resources.IProject; import org.eclipse.ui.IWorkbenchSite; import org.eclipse.ui.IWorkingSet; @@ -77,7 +78,7 @@ public class FindRefsInWorkingSetAction extends FindAction { return scopeDescription; } /* (non-Javadoc) - * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope() + * @see org.eclipse.cdt.internal.ui.editor.selsearch.FindAction#getScope(org.eclipse.core.resources.IProject) */ protected ICSearchScope getScope() { IWorkingSet[] workingSets= null; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java index 610d16cc5b8..08a7bd45aaf 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/OpenDeclarationsAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2004 IBM Corporation and others. + * Copyright (c) 2000, 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 @@ -11,33 +11,28 @@ package org.eclipse.cdt.internal.ui.search.actions; -import org.eclipse.cdt.core.ICLogConstants; +import java.util.Iterator; +import java.util.Set; + +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CoreModel; +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.parser.IParser; -import org.eclipse.cdt.core.parser.ParseError; import org.eclipse.cdt.core.parser.ParserUtil; -import org.eclipse.cdt.core.parser.ast.ASTClassKind; -import org.eclipse.cdt.core.parser.ast.IASTClassSpecifier; -import org.eclipse.cdt.core.parser.ast.IASTEnumerationSpecifier; -import org.eclipse.cdt.core.parser.ast.IASTEnumerator; -import org.eclipse.cdt.core.parser.ast.IASTField; -import org.eclipse.cdt.core.parser.ast.IASTFunction; -import org.eclipse.cdt.core.parser.ast.IASTMethod; -import org.eclipse.cdt.core.parser.ast.IASTNamespaceDefinition; -import org.eclipse.cdt.core.parser.ast.IASTNode; -import org.eclipse.cdt.core.parser.ast.IASTOffsetableNamedElement; -import org.eclipse.cdt.core.parser.ast.IASTVariable; import org.eclipse.cdt.core.resources.FileStorage; +import org.eclipse.cdt.core.search.DOMSearchUtil; import org.eclipse.cdt.core.search.ICSearchConstants; import org.eclipse.cdt.core.search.IMatch; import org.eclipse.cdt.core.search.SearchEngine; -import org.eclipse.cdt.core.search.ICSearchConstants.SearchFor; +import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditorMessages; +import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput; import org.eclipse.cdt.internal.ui.util.EditorUtility; +import org.eclipse.cdt.internal.ui.util.ExternalEditorInput; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -53,12 +48,12 @@ import org.eclipse.jface.viewers.ISelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.PartInitException; +import org.eclipse.ui.texteditor.AbstractTextEditor; import org.eclipse.ui.texteditor.IUpdate; - public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate { - - //private String fDialogTitle; + public static final IASTName[] BLANK_NAME_ARRAY = new IASTName[0]; + //private String fDialogTitle; //private String fDialogMessage; SearchEngine searchEngine = null; @@ -94,44 +89,38 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd String projectName = ""; //$NON-NLS-1$ private static class Storage { - private IASTOffsetableNamedElement element; private IResource resource; private String fileName; + private int offset=0; + private int length=0; - public IASTOffsetableNamedElement getNamedElement() - { - return element; - } - /** - * @return Returns the fileName. - */ public final String getFileName() { return fileName; } - /** - * @param fileName The fileName to set. - */ - public final void setFileName(String fileName) { - this.fileName = fileName; + public Storage() { } - /** - * @return Returns the resource. - */ public final IResource getResource() { return resource; } - /** - * @param resource The resource to set. - */ - public final void setResource(IResource resource) { + public int getLength() { + return length; + } + public int getOffset() { + return offset; + } + public void setFileName(String fileName) { + this.fileName = fileName; + } + public void setLength(int length) { + this.length = length; + } + public void setOffset(int offset) { + this.offset = offset; + } + public void setResource(IResource resource) { this.resource = resource; } - /** - * @param element The element to set. - */ - public final void setElement(IASTOffsetableNamedElement element) { - this.element = element; - } + } /* (non-Javadoc) @@ -145,94 +134,130 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd } final Storage storage = new Storage(); - IRunnableWithProgress runnable = new IRunnableWithProgress() { + // steps: + // 1- parse and get the best selected name based on the offset/length into that TU + // 2- based on the IASTName selected, find the best declaration of it in the TU + // 3- if no IASTName is found for a declaration, then search the Index public void run(IProgressMonitor monitor) { - IFile resourceFile = fEditor.getInputFile(); - projectName = findProjectName(resourceFile); - - IParser parser = setupParser(resourceFile); - int selectionStart = selNode.selStart; - int selectionEnd = selNode.selEnd; + int selectionStart = selNode.selStart; + int selectionLength = selNode.selEnd - selNode.selStart; + + IFile resourceFile = null; + + IASTName[] selectedNames = BLANK_NAME_ARRAY; + if (fEditor.getEditorInput() instanceof ExternalEditorInput) { + if( fEditor.getEditorInput() instanceof ITranslationUnitEditorInput ) + { + ITranslationUnitEditorInput ip = (ITranslationUnitEditorInput) fEditor.getEditorInput(); + IResource r = ip.getTranslationUnit().getUnderlyingResource(); + if( r.getType() == IResource.FILE ) + resourceFile = (IFile) r; + else + { + operationNotAvailable(CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE); + return; - IParser.ISelectionParseResult result = null; - IASTOffsetableNamedElement node = null; - try{ - result = parser.parse(selectionStart,selectionEnd); - if( result != null ) - node = result.getOffsetableNamedElement(); - } - catch (ParseError er){} - catch ( VirtualMachineError vmErr){ - if (vmErr instanceof OutOfMemoryError){ - org.eclipse.cdt.internal.core.model.Util.log(null, "Open Declarations Out Of Memory error: " + vmErr.getMessage() + " on File: " + resourceFile.getName(), ICLogConstants.CDT); //$NON-NLS-1$ //$NON-NLS-2$ - } - } - catch (Exception ex){} - - finally{ - if (node == null){ - return; - } - } + } + } + } + else + resourceFile = fEditor.getInputFile(); + - storage.setFileName( result.getFilename() ); - storage.setElement( node ); - storage.setResource( ParserUtil.getResourceForFilename( result.getFilename() ) ); + if (resourceFile != null) + projectName = findProjectName(resourceFile); + + // step 1 starts here + if (resourceFile != null) + selectedNames = DOMSearchUtil.getSelectedNamesFrom(resourceFile, selectionStart, selectionLength); + + if (selectedNames.length > 0 && selectedNames[0] != null) { // just right, only one name selected + IASTName searchName = selectedNames[0]; + // step 2 starts here + IASTName[] domNames = DOMSearchUtil.getNamesFromDOM(searchName, ICSearchConstants.DECLARATIONS); + if (domNames != null && domNames.length > 0 && domNames[0] != null) { + String fileName=null; + int start=0; + int end=0; + + if ( domNames[0].getTranslationUnit() != null ) { + IASTFileLocation location = domNames[0].getTranslationUnit().flattenLocationsToFile( domNames[0].getNodeLocations() ); + fileName = location.getFileName(); + start = location.getNodeOffset(); + end = location.getNodeOffset() + location.getNodeLength(); + } + + if (fileName != null) { + storage.setFileName(fileName); + storage.setLength(end - start); + storage.setOffset(start); + storage.setResource(ParserUtil.getResourceForFilename( fileName )); + } + } else { + // step 3 starts here + ICElement[] scope = new ICElement[1]; + scope[0] = new CProject(null, fEditor.getInputFile().getProject()); + Set matches = DOMSearchUtil.getMatchesFromSearchEngine(SearchEngine.createCSearchScope(scope), searchName, ICSearchConstants.DECLARATIONS); + + if (matches != null) { + Iterator itr = matches.iterator(); + while(itr.hasNext()) { + Object match = itr.next(); + if (match instanceof IMatch) { + IMatch theMatch = (IMatch)match; + storage.setFileName(theMatch.getLocation().toOSString()); + storage.setLength(theMatch.getEndOffset() - theMatch.getStartOffset()); + storage.setOffset(theMatch.getStartOffset()); + storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString())); + break; + } + } + } + } + } else if (selectedNames.length == 0){ + operationNotAvailable(CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE); + return; + } else { + operationNotAvailable(CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE); + return; + } + return; } - - private String findProjectName(IFile resourceFile) { - if( resourceFile == null ) return ""; //$NON-NLS-1$ - IProject [] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); - for( int i = 0; i < projects.length; ++i ) - { - if( projects[i].contains(resourceFile) ) - return projects[i].getName(); - } - return ""; //$NON-NLS-1$ - } + + private String findProjectName(IFile resourceFile) { + if( resourceFile == null ) return ""; //$NON-NLS-1$ + IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); + for( int i = 0; i < projects.length; ++i ) + { + if( projects[i].contains(resourceFile) ) + return projects[i].getName(); + } + return ""; //$NON-NLS-1$ + } }; try { ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell()); progressMonitor.run(true, true, runnable); - IASTOffsetableNamedElement namedElement = storage.getNamedElement(); - if( namedElement == null ){ - operationNotAvailable(); - return; - } else { - clearStatusLine(); - } - + int nameOffset = storage.getOffset(); + int nameLength = storage.getLength(); if( storage.getResource() != null ) { - int nameOffset = 0; - int nameEndOffset = 0; - - nameOffset = namedElement.getNameOffset(); - nameEndOffset = namedElement.getNameEndOffset(); - - open( storage.getResource(), nameOffset, nameEndOffset - nameOffset ); + clearStatusLine(); + open( storage.getResource(), nameOffset, nameLength ); return; } - else - { - String fileName = null; - int nameOffset = 0; - int nameEndOffset = 0; - - fileName = storage.getFileName(); - nameOffset = namedElement.getNameOffset(); - nameEndOffset = namedElement.getNameEndOffset(); - - if (fileName != null){ - open( fileName,nameOffset, nameEndOffset - nameOffset); - } - } + String fileName = storage.getFileName(); + + if (fileName != null){ + clearStatusLine(); + open( fileName, nameOffset, nameLength); + } } catch(Exception x) { CUIPlugin.getDefault().log(x); @@ -252,15 +277,13 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd return true; } - ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName ); + ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName ); ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path); if (unit != null) { setSelectionAtOffset( EditorUtility.openInEditor(unit), offset, length ); return true; } - - - + FileStorage storage = new FileStorage(null, path); IEditorPart part = EditorUtility.openInEditor(storage); setSelectionAtOffset(part, offset, length); @@ -291,19 +314,12 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd * @param length TODO */ private void setSelectionAtOffset(IEditorPart part, int offset, int length) { - //int line = element.getStartOffset(); - //if(line > 0) line--; - if(part instanceof CEditor) { - CEditor ed = (CEditor)part; - - try { - //IDocument document= ed.getDocumentProvider().getDocument(ed.getEditorInput()); - //if(line > 3) { - // ed.selectAndReveal(document.getLineOffset(line - 3), 0); - //} - ed.selectAndReveal(offset, length); + if( part instanceof AbstractTextEditor ) + { + try { + ((AbstractTextEditor) part).selectAndReveal(offset, length); } catch (Exception e) {} - } + } } // /** // * Shows a dialog for resolving an ambigous C element. @@ -346,48 +362,6 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd public void update() { setEnabled(getSelectedStringFromEditor() != null); } - - private SearchFor getSearchForFromNode(IASTNode node){ - SearchFor searchFor = null; - - if (node instanceof IASTClassSpecifier){ - //Find out if class, struct, union - IASTClassSpecifier tempNode = (IASTClassSpecifier) node; - if(tempNode.getClassKind().equals(ASTClassKind.CLASS)){ - searchFor = ICSearchConstants.CLASS; - } - else if (tempNode.getClassKind().equals(ASTClassKind.STRUCT)){ - searchFor = ICSearchConstants.STRUCT; - } - else if (tempNode.getClassKind().equals(ASTClassKind.UNION)){ - searchFor = ICSearchConstants.UNION; - } - } - else if (node instanceof IASTMethod){ - searchFor = ICSearchConstants.METHOD; - } - else if (node instanceof IASTFunction){ - searchFor = ICSearchConstants.FUNCTION; - } - else if (node instanceof IASTField){ - searchFor = ICSearchConstants.FIELD; - } - else if (node instanceof IASTVariable){ - searchFor = ICSearchConstants.VAR; - } - else if (node instanceof IASTEnumerationSpecifier){ - searchFor = ICSearchConstants.ENUM; - } - else if (node instanceof IASTEnumerator){ - searchFor = ICSearchConstants.FIELD; - } - else if (node instanceof IASTNamespaceDefinition){ - searchFor = ICSearchConstants.NAMESPACE; - } - - return searchFor; - } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java index d4457a5686c..681d0c93f02 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/SelectionParseAction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004 IBM Corporation and others. + * Copyright (c) 2004,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 v0.5 * which accompanies this distribution, and is available at @@ -49,7 +49,10 @@ import org.eclipse.ui.texteditor.IDocumentProvider; * Created on Jun 2, 2004 */ public class SelectionParseAction extends Action { - + protected static final String CSEARCH_OPERATION_TOO_MANY_NAMES_MESSAGE = "CSearchOperation.tooManyNames.message"; //$NON-NLS-1$ + protected static final String CSEARCH_OPERATION_NO_NAMES_SELECTED_MESSAGE = "CSearchOperation.noNamesSelected.message"; //$NON-NLS-1$ + protected static final String CSEARCH_OPERATION_OPERATION_UNAVAILABLE_MESSAGE = "CSearchOperation.operationUnavailable.message"; //$NON-NLS-1$ + protected IWorkbenchSite fSite; protected CEditor fEditor; @@ -122,27 +125,45 @@ public class SelectionParseAction extends Action { return parser; } - protected void operationNotAvailable() { - IStatusLineManager statusManager = null; - if (fSite instanceof IViewSite){ - statusManager = ((IViewSite) fSite).getActionBars().getStatusLineManager(); - } - else if (fSite instanceof IEditorSite){ - statusManager = ((IEditorSite) fSite).getActionBars().getStatusLineManager(); - } - if( statusManager != null ) - statusManager.setErrorMessage(CSearchMessages.getString("CSearchOperation.operationUnavailable.message"));//$NON-NLS-1$ + protected void operationNotAvailable(final String message) { + // run the code to update the status line on the Display thread + // this way any other thread can invoke operationNotAvailable(String) + CUIPlugin.getStandardDisplay().asyncExec(new Runnable(){ + /* (non-Javadoc) + * @see java.lang.Runnable#run() + */ + public void run() { + IStatusLineManager statusManager = null; + if (fSite instanceof IViewSite){ + statusManager = ((IViewSite) fSite).getActionBars().getStatusLineManager(); + } + else if (fSite instanceof IEditorSite){ + statusManager = ((IEditorSite) fSite).getActionBars().getStatusLineManager(); + } + if( statusManager != null ) + statusManager.setErrorMessage(CSearchMessages.getString(message));//$NON-NLS-1$ + } + }); } protected void clearStatusLine() { - IStatusLineManager statusManager = null; - if (fSite instanceof IViewSite){ - statusManager = ((IViewSite) fSite).getActionBars().getStatusLineManager(); - } - else if (fSite instanceof IEditorSite){ - statusManager = ((IEditorSite) fSite).getActionBars().getStatusLineManager(); - } - if( statusManager != null ) - statusManager.setErrorMessage( "" ); //$NON-NLS-1$ + // run the code to update the status line on the Display thread + // this way any other thread can invoke clearStatusLine() + CUIPlugin.getStandardDisplay().asyncExec(new Runnable(){ + /* (non-Javadoc) + * @see java.lang.Runnable#run() + */ + public void run() { + IStatusLineManager statusManager = null; + if (fSite instanceof IViewSite){ + statusManager = ((IViewSite) fSite).getActionBars().getStatusLineManager(); + } + else if (fSite instanceof IEditorSite){ + statusManager = ((IEditorSite) fSite).getActionBars().getStatusLineManager(); + } + if( statusManager != null ) + statusManager.setErrorMessage( "" ); //$NON-NLS-1$ + } + }); } //TODO: Change this to work with qualified identifiers @@ -161,8 +182,12 @@ public class SelectionParseAction extends Action { try{ while (pos >= 0) { c= doc.getChar(pos); + + // TODO this logic needs to be improved + // ex: ~destr[cursor]uctors, p2->ope[cursor]rator=(zero), etc if (!Character.isJavaIdentifierPart(c)) - break; + break; + --pos; } fStartPos= pos + 1; @@ -188,7 +213,7 @@ public class SelectionParseAction extends Action { return sel; } - + /** * Return the selected string from the editor * @return The string currently selected, or null if there is no valid selection diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/WorkingSetFindAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/WorkingSetFindAction.java index abd04b2870f..fd3c4776c3c 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/WorkingSetFindAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/search/actions/WorkingSetFindAction.java @@ -9,6 +9,7 @@ package org.eclipse.cdt.internal.ui.search.actions; import org.eclipse.cdt.core.search.ICSearchScope; import org.eclipse.cdt.core.search.ICSearchConstants.LimitTo; import org.eclipse.cdt.internal.ui.editor.CEditor; +import org.eclipse.core.resources.IProject; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; @@ -43,7 +44,7 @@ public class WorkingSetFindAction extends FindAction { } /* (non-Javadoc) - * @see org.eclipse.cdt.internal.ui.search.actions.FindAction#getScope() + * @see org.eclipse.cdt.internal.ui.search.actions.FindAction#getScope(org.eclipse.core.resources.IProject) */ protected ICSearchScope getScope() { // TODO Auto-generated method stub diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CSearchResultLabelProvider.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CSearchResultLabelProvider.java index 37ab2730cb6..6a9be8c5e7a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CSearchResultLabelProvider.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CSearchResultLabelProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2003, 2004 IBM Corporation and others. + * Copyright (c) 2003, 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 @@ -138,6 +138,10 @@ public class CSearchResultLabelProvider extends LabelProvider { flags |= CElementImageDescriptor.TEMPLATE; break; } + + default: + imageDescriptor = CPluginImages.DESC_OBJS_UNKNOWN; + break; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java index f0b451948bc..be17cbd3160 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/CUIPlugin.java @@ -12,6 +12,7 @@ package org.eclipse.cdt.ui; +import java.io.File; import java.io.IOException; import java.text.MessageFormat; import java.util.ArrayList; @@ -54,6 +55,7 @@ import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdapterManager; import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status;