1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-05 16:56:04 +02:00

Static var in source included by other source, bug 265821.

This commit is contained in:
Markus Schorn 2009-02-26 17:32:19 +00:00
parent 35f45ec13f
commit 00d1af4bc5
14 changed files with 123 additions and 28 deletions

View file

@ -496,11 +496,11 @@ public class LocationMapTests extends BaseTestCase {
assertEquals(FN, fLocationMap.getCurrentFilePath());
fLocationMap.encounteredComment(0,2,true);
// number: [6,15)[25,26)
ILocationCtx i1= fLocationMap.pushInclusion(0, 2, 4, 6, "b1b2b3b4b5".toCharArray(), "pre1", "pre1".toCharArray(), false, false);
ILocationCtx i1= fLocationMap.pushInclusion(0, 2, 4, 6, "b1b2b3b4b5".toCharArray(), "pre1", "pre1".toCharArray(), false, false, false);
assertEquals("pre1", fLocationMap.getCurrentFilePath());
fLocationMap.encounteredComment(2,4,true);
// number: [15,25)
ILocationCtx i2= fLocationMap.pushInclusion(6, 7, 8, 9, "c1c2c3c4c5".toCharArray(), "pre11", "pre11".toCharArray(), false, false);
ILocationCtx i2= fLocationMap.pushInclusion(6, 7, 8, 9, "c1c2c3c4c5".toCharArray(), "pre11", "pre11".toCharArray(), false, false, false);
assertEquals("pre11", fLocationMap.getCurrentFilePath());
fLocationMap.encounteredComment(2,6,true);
fLocationMap.popContext(i2);
@ -513,7 +513,7 @@ public class LocationMapTests extends BaseTestCase {
fLocationMap.popContext(pre2);
assertEquals(FN, fLocationMap.getCurrentFilePath());
// number [36, 46)
ILocationCtx i3= fLocationMap.pushInclusion(0, 2, 4, 6, "d1d2d3d4d5".toCharArray(), "pre2", "pre2".toCharArray(), false, false);
ILocationCtx i3= fLocationMap.pushInclusion(0, 2, 4, 6, "d1d2d3d4d5".toCharArray(), "pre2", "pre2".toCharArray(), false, false, false);
assertEquals("pre2", fLocationMap.getCurrentFilePath());
fLocationMap.encounteredComment(0,2,true);
fLocationMap.popContext(i3);

View file

@ -1651,4 +1651,28 @@ public class IndexBugsTests extends BaseTestCase {
fIndex.releaseReadLock();
}
}
// #include "B.cpp"
// static int STATIC;
// void ref() {STATIC=1;}
public void testStaticVarInSourceIncluded_Bug265821() throws Exception {
String[] contents= getContentsForTest(2);
final IIndexManager indexManager = CCorePlugin.getIndexManager();
IFile a= TestSourceReader.createFile(fCProject.getProject(), "A.cpp", contents[0]);
IFile b= TestSourceReader.createFile(fCProject.getProject(), "B.cpp", contents[1]);
indexManager.reindex(fCProject);
waitForIndexer();
ITranslationUnit tu= (ITranslationUnit) CoreModel.getDefault().create(b);
fIndex.acquireReadLock();
try {
IASTTranslationUnit ast= tu.getAST(fIndex, ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
IBinding var= ((IASTSimpleDeclaration) ast.getDeclarations()[0]).getDeclarators()[0].getName().resolveBinding();
IIndexBinding adapted = fIndex.adaptBinding(var);
assertNotNull(adapted);
} finally {
fIndex.releaseReadLock();
}
}
}

View file

@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.dom.parser;
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.IFunction;
import org.eclipse.cdt.core.dom.ast.IScope;
@ -121,18 +120,16 @@ public class ASTInternal {
}
private static String isPartOfSource(String filePath, IASTNode decl) {
IASTTranslationUnit tu;
if (filePath == null) {
tu= decl.getTranslationUnit();
if (tu.isHeaderUnit()) {
return null;
if (decl instanceof ASTNode) {
if (((ASTNode) decl).isPartOfSourceFile()) {
if (filePath == null)
return decl.getContainingFilename();
if (filePath.equals(decl.getContainingFilename()))
return filePath;
}
filePath= tu.getFilePath();
}
if (!filePath.equals(decl.getContainingFilename())) {
return null;
}
return filePath;
return null;
}
public static String getDeclaredInOneFileOnly(IBinding binding) {

View file

@ -216,6 +216,17 @@ public abstract class ASTNode implements IASTNode {
return false;
}
public boolean isPartOfSourceFile() {
IASTTranslationUnit ast = getTranslationUnit();
if (ast != null) {
ILocationResolver lr= (ILocationResolver) ast.getAdapter(ILocationResolver.class);
if (lr != null) {
return lr.isPartOfSourceFile(offset);
}
}
return false;
}
public IASTTranslationUnit getTranslationUnit() {
return parent != null ? parent.getTranslationUnit() : null;
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 QNX Software Systems and others.
* Copyright (c) 2005, 2009 QNX Software Systems 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
@ -167,7 +167,9 @@ public final class IndexBasedCodeReaderFactory extends AbstractCodeReaderFactory
try {
CodeReader codeReader= InternalParserUtil.createCodeReader(ifl);
if (codeReader != null) {
return new IncludeFileContent(codeReader);
IncludeFileContent ifc= new IncludeFileContent(codeReader);
ifc.setIsSource(fPathResolver.isSource(path));
return ifc;
}
} catch (CoreException e) {
CCorePlugin.log(e);

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
* Copyright (c) 2007, 2009 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
@ -52,6 +52,11 @@ public class StandaloneIndexerInputAdapter extends IndexerInputAdapter {
public boolean isSourceUnit(Object tu) {
return isValidSourceUnitName((String) tu);
}
@Override
public boolean isSource(String filename) {
return isValidSourceUnitName(filename);
}
@Override
public IIndexFileLocation resolveFile(Object tu) {

View file

@ -1166,7 +1166,7 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable {
if (reader != null && !isCircularInclusion(path)) {
reported= true;
fAllIncludedFiles.add(path);
ILocationCtx ctx= fLocationMap.pushInclusion(poundOffset, nameOffsets[0], nameOffsets[1], condEndOffset, reader.buffer, path, headerName, userInclude, isHeuristic);
ILocationCtx ctx= fLocationMap.pushInclusion(poundOffset, nameOffsets[0], nameOffsets[1], condEndOffset, reader.buffer, path, headerName, userInclude, isHeuristic, fi.isSource());
ScannerContext fctx= new ScannerContext(ctx, fCurrentContext, new Lexer(reader.buffer, fLexOptions, this, this));
fCurrentContext= fctx;
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2008 IBM Corporation and others.
* Copyright (c) 2004, 2009 IBM Corporation 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
@ -151,6 +151,12 @@ public interface ILocationResolver {
*/
boolean isPartOfTranslationUnitFile(int sequenceNumber);
/**
* Returns whether the specified sequence number points into a file that is considered a
* source file (even if it is included by some other file).
*/
boolean isPartOfSourceFile(int sequenceNumber);
/**
* Same as {@link #getMappedFileLocation(int, int)} for the given array of consecutive node locations.
*/

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Wind River Systems, Inc. and others.
* Copyright (c) 2008, 2009 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
@ -44,6 +44,7 @@ public class IncludeFileContent {
private final List<ICPPUsingDirective> fUsingDirectives;
private final String fFileLocation;
private boolean fHeuristic;
private boolean fIsSource= false;
private List<IIndexFile> fFiles;
/**
@ -156,4 +157,12 @@ public class IncludeFileContent {
public void setFoundByHeuristics(boolean val) {
fHeuristic= val;
}
public boolean isSource() {
return fIsSource;
}
public void setIsSource(boolean isSource) {
fIsSource= isSource;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
* Copyright (c) 2007, 2009 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
@ -143,4 +143,10 @@ abstract class LocationCtx implements ILocationCtx {
public Collection<LocationCtx> getChildren() {
return Collections.emptySet();
}
public boolean isSourceFile() {
if (fParent == null)
return false;
return fParent.isSourceFile();
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007 Wind River Systems, Inc. and others.
* Copyright (c) 2007, 2009 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
@ -22,11 +22,15 @@ import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
class LocationCtxFile extends LocationCtxContainer {
private final String fFilename;
private final ASTInclusionStatement fASTInclude;
private final boolean fIsSource;
public LocationCtxFile(LocationCtxContainer parent, String filename, char[] source, int parentOffset, int parentEndOffset, int sequenceNumber, ASTInclusionStatement inclusionStatement) {
public LocationCtxFile(LocationCtxContainer parent, String filename, char[] source, int parentOffset,
int parentEndOffset, int sequenceNumber, ASTInclusionStatement inclusionStatement,
boolean isSource) {
super(parent, source, parentOffset, parentEndOffset, sequenceNumber);
fFilename= new String(filename);
fASTInclude= inclusionStatement;
fIsSource= isSource;
}
@Override
@ -120,4 +124,9 @@ class LocationCtxFile extends LocationCtxContainer {
}
}
}
@Override
public boolean isSourceFile() {
return fIsSource;
}
}

View file

@ -96,7 +96,7 @@ public class LocationMap implements ILocationResolver {
public ILocationCtx pushTranslationUnit(String filename, char[] buffer) {
assert fCurrentContext == null;
fTranslationUnitPath= filename;
fCurrentContext= fRootContext= new LocationCtxFile(null, filename, buffer, 0, 0, 0, null);
fCurrentContext= fRootContext= new LocationCtxFile(null, filename, buffer, 0, 0, 0, null, true);
fLastChildInsertionOffset= 0;
return fCurrentContext;
}
@ -126,7 +126,7 @@ public class LocationMap implements ILocationResolver {
* @param userInclude <code>true</code> when specified with double-quotes.
*/
public ILocationCtx pushInclusion(int startOffset, int nameOffset, int nameEndOffset, int endOffset,
char[] buffer, String filename, char[] name, boolean userInclude, boolean heuristic) {
char[] buffer, String filename, char[] name, boolean userInclude, boolean heuristic, boolean isSource) {
assert fCurrentContext instanceof LocationCtxContainer;
int startNumber= getSequenceNumberForOffset(startOffset);
int nameNumber= getSequenceNumberForOffset(nameOffset);
@ -135,7 +135,7 @@ public class LocationMap implements ILocationResolver {
final ASTInclusionStatement inclusionStatement=
new ASTInclusionStatement(fTranslationUnit, startNumber, nameNumber, nameEndNumber, endNumber, name, filename, userInclude, true, heuristic);
fDirectives.add(inclusionStatement);
fCurrentContext= new LocationCtxFile((LocationCtxContainer) fCurrentContext, filename, buffer, startOffset, endOffset, endNumber, inclusionStatement);
fCurrentContext= new LocationCtxFile((LocationCtxContainer) fCurrentContext, filename, buffer, startOffset, endOffset, endNumber, inclusionStatement, isSource);
fLastChildInsertionOffset= 0;
return fCurrentContext;
}
@ -398,6 +398,14 @@ public class LocationMap implements ILocationResolver {
return new String(ctx.getFilePath());
}
public boolean isPartOfSourceFile(int sequenceNumber) {
LocationCtx ctx= fRootContext.findSurroundingContext(sequenceNumber, 1);
if (ctx == fRootContext && fTranslationUnit != null)
return !fTranslationUnit.isHeaderUnit();
return ctx.isSourceFile();
}
public ASTFileLocation getMappedFileLocation(int sequenceNumber, int length) {
return fRootContext.findMappedFileLocation(sequenceNumber, length);
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
* Copyright (c) 2007, 2009 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
@ -40,4 +40,9 @@ public abstract class ASTFilePathResolver {
* Convert an index file location to the path as it will be stored in the AST.
*/
public abstract String getASTPath(IIndexFileLocation ifl);
/**
* Answers whether this file is considered to be a source file (vs. a header file).
*/
public abstract boolean isSource(String astFilePath);
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2007, 2008 Wind River Systems, Inc. and others.
* Copyright (c) 2007, 2009 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
@ -33,6 +33,7 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.content.IContentType;
/**
* Provides information about translation-units.
@ -175,6 +176,18 @@ public class ProjectIndexerInputAdapter extends IndexerInputAdapter {
ITranslationUnit tu= (ITranslationUnit) tuo;
return tu.isSourceUnit();
}
@Override
public boolean isSource(String filename) {
IContentType ct= CCorePlugin.getContentType(fCProject.getProject(), filename);
if (ct != null) {
String id = ct.getId();
if (CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id) || CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(id)) {
return true;
}
}
return false;
}
@Override
public IIndexFileLocation resolveFile(Object tuo) {