1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-01 06:05:24 +02:00

Include Browser: using offsets from index

This commit is contained in:
Markus Schorn 2006-11-08 08:39:12 +00:00
parent 9c3e30e9a0
commit cc1407df9d
10 changed files with 134 additions and 185 deletions

View file

@ -114,7 +114,7 @@ public class IndexIncludeTest extends IndexTestBase {
private void checkContext() throws Exception {
final long timestamp= System.currentTimeMillis();
final IFile file= (IFile) fProject.getProject().findMember(new Path("included.h"));
assertNotNull(file);
assertNotNull("Can't find included.h", file);
waitForIndexer();
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
@ -122,12 +122,13 @@ public class IndexIncludeTest extends IndexTestBase {
file.setContents(new ByteArrayInputStream( "int included; int CONTEXT;\n".getBytes()), false, false, NPM);
}
}, NPM);
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, 1000);
assertTrue("Timestamp was not increased", file.getLocalTimeStamp() >= timestamp);
TestSourceReader.waitUntilFileIsIndexed(fIndex, file, 4000);
fIndex.acquireReadLock();
try {
IIndexFile ifile= fIndex.getFile(file.getLocation());
assertNotNull(ifile);
assertTrue(ifile.getTimestamp() >= timestamp);
assertNotNull("Can't find " + file.getLocation(), ifile);
assertTrue("timestamp not ok", ifile.getTimestamp() >= timestamp);
IIndexBinding[] result= fIndex.findBindings(Pattern.compile("testInclude_cpp"), true, IndexFilter.ALL, NPM);
assertEquals(1, result.length);
@ -140,7 +141,6 @@ public class IndexIncludeTest extends IndexTestBase {
// {source20061107}
// #include "user20061107.h"
// #include <system20061107.h>
public void testIncludeProperties() throws Exception {
waitForIndexer();
TestScannerProvider.sIncludes= new String[]{fProject.getProject().getLocation().toOSString()};
@ -169,6 +169,35 @@ public class IndexIncludeTest extends IndexTestBase {
TestScannerProvider.sIncludes= null;
}
}
public void testIncludeProperties_2() throws Exception {
TestScannerProvider.sIncludes= new String[]{fProject.getProject().getLocation().toOSString()};
try {
TestSourceReader.createFile(fProject.getProject(), "header20061107.h", "");
String content = "// comment \n#include \"header20061107.h\"\n";
IFile file= TestSourceReader.createFile(fProject.getProject(), "intermed20061107.h", content);
TestSourceReader.createFile(fProject.getProject(), "source20061107.cpp", "#include \"intermed20061107.h\"\n");
CCoreInternals.getPDOMManager().getIndexer(fProject).reindex();
waitForIndexer();
fIndex.acquireReadLock();
try {
IIndexFile ifile= fIndex.getFile(file.getLocation());
assertNotNull(ifile);
IIndexInclude[] includes= ifile.getIncludes();
assertEquals(1, includes.length);
checkInclude(includes[0], content, "header20061107.h", false);
}
finally {
fIndex.releaseReadLock();
}
}
finally {
TestScannerProvider.sIncludes= null;
}
}
private void checkInclude(IIndexInclude include, String content, String includeName, boolean isSystem) throws CoreException {
int offset= content.indexOf(includeName);
@ -176,4 +205,5 @@ public class IndexIncludeTest extends IndexTestBase {
assertEquals(includeName.length(), include.getNameLength());
assertEquals(isSystem, include.isSystemInclude());
}
}

View file

@ -7,6 +7,7 @@
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.model;
@ -189,7 +190,7 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
return CoreModelMessages.getFormattedString("status.ParserError"); //$NON-NLS-1$
case ELEMENT_DOES_NOT_EXIST :
return CoreModelMessages.getFormattedString("element.doesNotExist", getFirstElementName()); //$NON-NLS-1$
return CoreModelMessages.getFormattedString("status.elementDoesNotExist", getFirstElementName()); //$NON-NLS-1$
case EVALUATION_ERROR :
return CoreModelMessages.getFormattedString("status.evaluationError", getString()); //$NON-NLS-1$
@ -250,7 +251,7 @@ public class CModelStatus extends Status implements ICModelStatus, ICModelStatus
if (fString != null) {
return fString;
}
return CoreModelMessages.getFormattedString("status.nameCollision", sb.toString()); //$NON-NLS-1$ //$NON-NLS-2$
return CoreModelMessages.getFormattedString("status.nameCollision", sb.toString()); //$NON-NLS-1$
case NO_ELEMENTS_TO_PROCESS :
return CoreModelMessages.getFormattedString("operation.needElements"); //$NON-NLS-1$

View file

@ -46,6 +46,7 @@ workingCopy.commit = Committing working copy...
### status
status.cannotUseDeviceOnPath = Operation requires a path with no device. Path specified was: {0}
status.elementDoesNotExist = Element {0} does not exist.
status.coreException = Core exception.
status.defaultPackageReadOnly = Default package is read-only.
status.evaluationError = Evaluation error: {0}.

View file

@ -141,7 +141,9 @@ public class DOMScanner extends BaseScanner {
char[] filenamePath, boolean local, int startOffset,
int startingLineNumber, int nameOffset, int nameEndOffset,
int nameLine, int endOffset, int endLine, boolean isForced) {
return new DOMInclusion(filenamePath, getGlobalOffset(startOffset), nameOffset, nameEndOffset, fileName, !local);
return new DOMInclusion(filenamePath, getGlobalOffset(startOffset),
getGlobalOffset(nameOffset), getGlobalOffset(nameEndOffset),
fileName, !local);
}
/*

View file

@ -192,13 +192,13 @@ public class AsyncViewerTest extends BaseUITestCase {
// + a
// + b
dlg.fViewer.setInput(root); runEventQueue(0);
dlg.fViewer.setInput(root); runEventQueue(50);
assertEquals(2, countVisibleItems(dlg.fViewer));
// - a
// - ...
// + b
dlg.fViewer.setExpandedState(a, true); runEventQueue(0);
dlg.fViewer.setExpandedState(a, true); runEventQueue(50);
assertEquals("...", dlg.fViewer.getTree().getItem(0).getItem(0).getText());
assertEquals(3, countVisibleItems(dlg.fViewer));
@ -211,10 +211,10 @@ public class AsyncViewerTest extends BaseUITestCase {
// + a
// + b
dlg.fViewer.setInput(null);
dlg.fViewer.setInput(root); runEventQueue(0);
dlg.fViewer.setInput(root); runEventQueue(50);
// expand async with two children
dlg.fViewer.setExpandedState(b, true); runEventQueue(0);
dlg.fViewer.setExpandedState(b, true); runEventQueue(50);
// + a
// - b
// - ...
@ -231,7 +231,7 @@ public class AsyncViewerTest extends BaseUITestCase {
// + a
// + b
dlg.fViewer.setInput(null);
dlg.fViewer.setInput(root); runEventQueue(0);
dlg.fViewer.setInput(root); runEventQueue(50);
// wait until children are computed (for the sake of the +-sign)
runEventQueue(800);
@ -255,7 +255,7 @@ public class AsyncViewerTest extends BaseUITestCase {
}, 150)
}, 0);
dlg.fViewer.setInput(root); runEventQueue(0);
dlg.fViewer.setInput(root); runEventQueue(50);
assertEquals(2, countVisibleItems(dlg.fViewer));
dlg.fContentProvider.recompute();

View file

@ -13,18 +13,21 @@ package org.eclipse.cdt.internal.ui.includebrowser;
import java.util.ArrayList;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.widgets.Display;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.ui.missingapi.CIndexQueries;
import org.eclipse.cdt.internal.ui.missingapi.CIndexIncludeRelation;
import org.eclipse.cdt.internal.ui.viewsupport.AsyncTreeContentProvider;
/**
@ -53,7 +56,7 @@ public class IBContentProvider extends AsyncTreeContentProvider {
protected Object[] syncronouslyComputeChildren(Object parentElement) {
if (parentElement instanceof ITranslationUnit) {
ITranslationUnit tu = (ITranslationUnit) parentElement;
return new Object[] { new IBNode(null, new IBFile(tu), null, null, 0, 0) };
return new Object[] { new IBNode(null, new IBFile(tu), null, null, 0, 0, 0) };
}
if (parentElement instanceof IBNode) {
IBNode node = (IBNode) parentElement;
@ -69,51 +72,64 @@ public class IBContentProvider extends AsyncTreeContentProvider {
if (parentElement instanceof IBNode) {
IBNode node = (IBNode) parentElement;
ITranslationUnit tu= node.getRepresentedTranslationUnit();
if (tu != null) {
CIndexIncludeRelation[] includes;
if (tu == null) {
return NO_CHILDREN;
}
IIndex index;
try {
index = CCorePlugin.getIndexManager().getIndex(tu.getCProject(),
fComputeIncludedBy ? IIndexManager.ADD_DEPENDENT : IIndexManager.ADD_DEPENDENCIES);
index.acquireReadLock();
} catch (CoreException e) {
CUIPlugin.getDefault().log(e);
return NO_CHILDREN;
} catch (InterruptedException e) {
return NO_CHILDREN;
}
try {
IBFile directiveFile= null;
IBFile targetFile= null;
IIndexInclude[] includes;
if (fComputeIncludedBy) {
ICProject[] projects;
try {
projects = CoreModel.getDefault().getCModel().getCProjects();
} catch (CModelException e) {
CUIPlugin.getDefault().log(e);
return NO_CHILDREN;
}
includes= CIndexQueries.getInstance().findIncludedBy(projects, tu, NPM);
includes= findIncludedBy(index, tu, NPM);
}
else {
includes= CIndexQueries.getInstance().findIncludesTo(tu, NPM);
includes= findIncludesTo(index, tu, NPM);
directiveFile= node.getRepresentedFile();
}
if (includes.length > 0) {
ArrayList result= new ArrayList(includes.length);
for (int i = 0; i < includes.length; i++) {
CIndexIncludeRelation include = includes[i];
IIndexInclude include = includes[i];
try {
Path includesPath = new Path(include.getIncludesLocation());
if (fComputeIncludedBy) {
directiveFile= targetFile= new IBFile(tu.getCProject(), include.getIncludedBy());
directiveFile= targetFile= new IBFile(tu.getCProject(), new Path(include.getIncludedByLocation()));
}
else {
targetFile= new IBFile(tu.getCProject(), include.getIncludes());
targetFile= new IBFile(tu.getCProject(), includesPath);
}
} catch (CModelException e) {
CUIPlugin.getDefault().log(e);
targetFile= null;
}
if (targetFile != null) {
IBNode newnode= new IBNode(node, targetFile, directiveFile,
include.getName(), include.getOffset(), include.getTimestamp());
newnode.setIsActiveCode(include.isActiveCode());
includesPath.lastSegment(), include.getNameOffset(),
include.getNameLength(),
include.getIncludedBy().getTimestamp());
newnode.setIsActiveCode(true);
newnode.setIsSystemInclude(include.isSystemInclude());
result.add(newnode);
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
}
return result.toArray();
}
}
finally {
index.releaseReadLock();
}
}
return NO_CHILDREN;
}
@ -127,4 +143,37 @@ public class IBContentProvider extends AsyncTreeContentProvider {
public boolean getComputeIncludedBy() {
return fComputeIncludedBy;
}
private IIndexInclude[] findIncludedBy(IIndex index, ITranslationUnit tu, IProgressMonitor pm) {
try {
IPath location= tu.getLocation();
if (location != null) {
IIndexFile file= index.getFile(location);
if (file != null) {
return index.findIncludedBy(file);
}
}
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
return new IIndexInclude[0];
}
public IIndexInclude[] findIncludesTo(IIndex index, ITranslationUnit tu, IProgressMonitor pm) {
try {
IPath location= tu.getLocation();
if (location != null) {
IIndexFile file= index.getFile(location);
if (file != null) {
return index.findIncludes(file);
}
}
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
return new IIndexInclude[0];
}
}

View file

@ -30,6 +30,7 @@ public class IBNode implements IAdaptable {
private IBFile fDirectiveFile;
private String fDirectiveName;
private int fDirectiveCharacterOffset;
private int fDirectiveLength;
private int fHashCode;
private boolean fIsSystemInclude= false;
@ -40,13 +41,14 @@ public class IBNode implements IAdaptable {
/**
* Creates a new node for the include browser
*/
public IBNode(IBNode parent, IBFile represents, IBFile fileOfDirective, String nameOfDirective,
int charOffset, long timestamp) {
public IBNode(IBNode parent, IBFile represents, IBFile fileOfDirective,
String nameOfDirective, int charOffset, int length, long timestamp) {
fParent= parent;
fRepresentedFile= represents;
fDirectiveFile= fileOfDirective;
fDirectiveName= nameOfDirective;
fDirectiveCharacterOffset= charOffset;
fDirectiveLength= length;
fIsRecursive= computeIsRecursive(fParent, represents.getLocation());
fHashCode= computeHashCode();
fTimestamp= timestamp;
@ -148,6 +150,10 @@ public class IBNode implements IAdaptable {
public int getDirectiveCharacterOffset() {
return fDirectiveCharacterOffset;
}
public int getDirectiveLength() {
return fDirectiveLength;
}
public IBFile getDirectiveFile() {
return fDirectiveFile;

View file

@ -659,7 +659,7 @@ public class IBViewPart extends ViewPart
IWorkbenchPage page= getSite().getPage();
IBFile ibf= node.getDirectiveFile();
if (ibf != null) {
IRegion region= new Region(node.getDirectiveCharacterOffset(), node.getDirectiveName().length() + 2);
IRegion region= new Region(node.getDirectiveCharacterOffset(), node.getDirectiveLength());
long timestamp= node.getTimestamp();
IFile f= ibf.getResource();

View file

@ -1,53 +0,0 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.missingapi;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.cdt.core.index.IIndexInclude;
/**
* Represents an include relation found in the index.
* @since 4.0
*/
public class CIndexIncludeRelation {
private IPath fIncludedBy;
private IPath fIncludes;
CIndexIncludeRelation(IIndexInclude include) throws CoreException {
fIncludedBy= Path.fromOSString(include.getIncludedByLocation());
fIncludes= Path.fromOSString(include.getIncludesLocation());
}
public boolean isSystemInclude() {
return false;
}
public boolean isActiveCode() {
return true;
}
public IPath getIncludedBy() {
return fIncludedBy;
}
public IPath getIncludes() {
return fIncludes;
}
public String getName() {
return fIncludes.lastSegment();
}
public int getOffset() {
return 9;
}
public long getTimestamp() {
return 0;
}
}

View file

@ -12,8 +12,6 @@
package org.eclipse.cdt.internal.ui.missingapi;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
@ -34,9 +32,6 @@ import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICExternalBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
@ -57,7 +52,6 @@ import org.eclipse.cdt.internal.corext.util.CModelUtil;
public class CIndexQueries {
private static final int ASTTU_OPTIONS = ITranslationUnit.AST_SKIP_INDEXED_HEADERS;
private static final ICElement[] EMPTY_ELEMENTS = new ICElement[0];
private static final CIndexIncludeRelation[] EMPTY_INCLUDES = new CIndexIncludeRelation[0];
private static final CIndexQueries sInstance= new CIndexQueries();
public static CIndexQueries getInstance() {
@ -99,87 +93,6 @@ public class CIndexQueries {
return false;
}
/**
* Searches for all include-relations that include the given translation unit.
* @param scope the projects to be searched.
* @param tu a translation unit
* @param pm a monitor for reporting progress.
* @return an array of include relations.
* @since 4.0
*/
public CIndexIncludeRelation[] findIncludedBy(ICProject[] scope, ITranslationUnit tu, IProgressMonitor pm) {
HashMap result= new HashMap();
try {
IIndex index= CCorePlugin.getIndexManager().getIndex(scope);
index.acquireReadLock();
try {
IPath location= tu.getLocation();
if (location != null) {
IIndexFile file= index.getFile(location);
if (file != null) {
IIndexInclude[] includes= index.findIncludedBy(file);
for (int i = 0; i < includes.length; i++) {
IIndexInclude include = includes[i];
CIndexIncludeRelation rel= new CIndexIncludeRelation(include);
result.put(rel.getIncludedBy(), rel);
}
}
}
}
finally {
index.releaseReadLock();
}
}
catch (InterruptedException e) {
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
Collection includes= result.values();
return (CIndexIncludeRelation[]) includes.toArray(new CIndexIncludeRelation[includes.size()]);
}
/**
* Searches for all include-relations defined in the given translation unit.
* @param tu a translation unit.
* @param pm a monitor to report progress.
* @return an array of include relations.
* @since 4.0
*/
public CIndexIncludeRelation[] findIncludesTo(ITranslationUnit tu, IProgressMonitor pm) {
try {
ICProject cproject= tu.getCProject();
if (cproject != null) {
IIndex index= CCorePlugin.getIndexManager().getIndex(cproject, IIndexManager.ADD_DEPENDENCIES);
index.acquireReadLock();
try {
IPath location= tu.getLocation();
if (location != null) {
IIndexFile file= index.getFile(location);
if (file != null) {
IIndexInclude includes[]= index.findIncludes(file);
ArrayList result= new ArrayList();
for (int i = 0; i < includes.length; i++) {
IIndexInclude include = includes[i];
result.add(new CIndexIncludeRelation(include));
}
return (CIndexIncludeRelation[]) result.toArray(new CIndexIncludeRelation[result.size()]);
}
}
}
finally {
index.releaseReadLock();
}
}
}
catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
catch (InterruptedException e) {
}
return EMPTY_INCLUDES;
}
/**
* Searches for functions and methods that call a given element.