1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

API to check whether a declaration is from a file actually included by a translation unit, bug 254844.

This commit is contained in:
Markus Schorn 2009-03-02 15:50:43 +00:00
parent 41109da4be
commit 4e149fbde7
6 changed files with 92 additions and 11 deletions

View file

@ -50,6 +50,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.index.IIndexMacro;
import org.eclipse.cdt.core.index.IIndexManager;
@ -1675,4 +1676,43 @@ public class IndexBugsTests extends BaseTestCase {
fIndex.releaseReadLock();
}
}
// int a;
// #include "a.h"
// void test() {a=0;}
public void testDeclarationForBinding_Bug254844() throws Exception {
String[] contents= getContentsForTest(2);
final IIndexManager indexManager = CCorePlugin.getIndexManager();
IFile a= TestSourceReader.createFile(fCProject.getProject(), "a.h", contents[0]);
IFile b= TestSourceReader.createFile(fCProject.getProject(), "b.h", contents[0]);
IFile source= TestSourceReader.createFile(fCProject.getProject(), "source.cpp", contents[1]);
indexManager.reindex(fCProject);
waitForIndexer();
ITranslationUnit tu= (ITranslationUnit) CoreModel.getDefault().create(source);
fIndex.acquireReadLock();
try {
IASTTranslationUnit ast= tu.getAST(fIndex, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
IIndexFileSet fileset= ast.getIndexFileSet();
IBinding var= getBindingFromASTName(ast, contents[1], "a=", 1, IBinding.class);
IName[] decls= ast.getDeclarations(var);
assertEquals(2, decls.length);
int check= 0;
for (int i = 0; i < decls.length; i++) {
IName name = decls[i];
assert name instanceof IIndexName;
IIndexName iName= (IIndexName) name;
if (iName.getFileLocation().getFileName().endsWith("a.h")) {
check |= 1;
assertTrue(fileset.contains(iName.getFile()));
} else {
check |= 2;
assertFalse(fileset.contains(iName.getFile()));
}
}
assertEquals(3, check);
} finally {
fIndex.releaseReadLock();
}
}
}

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.core.dom.ast;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.parser.ParserLanguage;
import org.eclipse.core.runtime.IAdaptable;
@ -244,11 +245,13 @@ public interface IASTTranslationUnit extends IASTNode, IASTDeclarationListOwner,
public IIndex getIndex();
/**
* Set the Index to be used for this translation unit.
*
* @param index
* Return the set of files that have been skipped because they have been part of the index
* prior to creating this AST, or <code>null</code> if not available.
* Applies only, if AST was created with an index and the option to skip headers found in the
* index.
* @since 5.1
*/
public void setIndex(IIndex index);
IIndexFileSet getIndexFileSet();
/**
* In case the ast was created in a way that supports comment parsing,
@ -271,11 +274,6 @@ public interface IASTTranslationUnit extends IASTNode, IASTDeclarationListOwner,
*/
public boolean isHeaderUnit();
/**
* Sets whether this ast represents a header file.
*/
public void setIsHeaderUnit(boolean headerUnit);
/**
* Returns the node factory that was used to build the AST.
*
@ -284,6 +282,18 @@ public interface IASTTranslationUnit extends IASTNode, IASTDeclarationListOwner,
*/
public INodeFactory getASTNodeFactory();
/**
* Set the Index to be used for this translation unit.
* @noreference This method is not intended to be referenced by clients.
*/
public void setIndex(IIndex index);
/**
* Sets whether this ast represents a header file.
* @noreference This method is not intended to be referenced by clients.
*/
public void setIsHeaderUnit(boolean headerUnit);
/**
* Causes this node and all the nodes rooted at this node to become immutable.
* Once the AST is frozen any calls to set or add methods on any of the nodes

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.core.index;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.internal.core.index.IndexFileSet;
import org.eclipse.core.runtime.CoreException;
/**
* File set for index files. Can be used to filter file-local bindings.
@ -22,6 +23,12 @@ import org.eclipse.cdt.internal.core.index.IndexFileSet;
public interface IIndexFileSet {
IIndexFileSet EMPTY = new IndexFileSet();
/**
* Returns whether the given file is part of this file set.
* @since 5.1
*/
boolean contains(IIndexFile file) throws CoreException;
/**
* Returns an array of bindings where all local bindings that are not part of this file-set
* have been removed.
@ -30,7 +37,7 @@ public interface IIndexFileSet {
/**
* Adds a file to this set.
* @param indexFile
* @noreference This method is not intended to be referenced by clients.
*/
void add(IIndexFile indexFile);
}

View file

@ -24,4 +24,9 @@ public interface IIndexFragmentFileSet {
* Adds the fragment file to the file-set.
*/
void add(IIndexFragmentFile fragFile);
/**
* Returns whether the file set contains the given file.
*/
boolean contains(IIndexFragmentFile file) throws CoreException;
}

View file

@ -79,4 +79,16 @@ public class IndexFileSet implements IIndexFileSet {
}
return result;
}
public boolean contains(IIndexFile file) throws CoreException {
if (!(file instanceof IIndexFragmentFile))
return false;
IIndexFragmentFile ifile= (IIndexFragmentFile) file;
IIndexFragmentFileSet subSet= fSubSets.get(ifile.getIndexFragment());
if (subSet != null) {
return subSet.contains(ifile);
}
return false;
}
}

View file

@ -32,4 +32,11 @@ public class PDOMFileSet implements IIndexFragmentFileSet {
PDOMBinding pdomBinding= (PDOMBinding) fb;
return fFileIDs.contains(pdomBinding.getLocalToFileRec());
}
public boolean contains(IIndexFragmentFile file) throws CoreException {
if (file instanceof PDOMFile) {
return fFileIDs.contains(((PDOMFile) file).getRecord());
}
return false;
}
}