From 00d1af4bc53685388ee7ede9cc96b8461ca4ca0b Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 26 Feb 2009 17:32:19 +0000 Subject: [PATCH] Static var in source included by other source, bug 265821. --- .../tests/scanner/LocationMapTests.java | 6 ++--- .../internal/index/tests/IndexBugsTests.java | 24 +++++++++++++++++++ .../internal/core/dom/parser/ASTInternal.java | 19 +++++++-------- .../cdt/internal/core/dom/parser/ASTNode.java | 11 +++++++++ .../index/IndexBasedCodeReaderFactory.java | 6 +++-- .../StandaloneIndexerInputAdapter.java | 7 +++++- .../core/parser/scanner/CPreprocessor.java | 2 +- .../parser/scanner/ILocationResolver.java | 8 ++++++- .../parser/scanner/IncludeFileContent.java | 11 ++++++++- .../core/parser/scanner/LocationCtx.java | 8 ++++++- .../core/parser/scanner/LocationCtxFile.java | 13 ++++++++-- .../core/parser/scanner/LocationMap.java | 14 ++++++++--- .../core/pdom/ASTFilePathResolver.java | 7 +++++- .../indexer/ProjectIndexerInputAdapter.java | 15 +++++++++++- 14 files changed, 123 insertions(+), 28 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/LocationMapTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/LocationMapTests.java index 82db7095093..ea9f47aadf5 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/LocationMapTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner/LocationMapTests.java @@ -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); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java index 8d2936d361c..daea1485b70 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBugsTests.java @@ -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(); + } + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java index fd066fc9719..508c780774d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTInternal.java @@ -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) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java index 38691d7a75b..0117daefe1f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/ASTNode.java @@ -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; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java index ceda8eddacf..dacdae119c6 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/index/IndexBasedCodeReaderFactory.java @@ -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); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java index 63dd9d9c41a..c7e6da60296 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/indexer/StandaloneIndexerInputAdapter.java @@ -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) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java index 727211d9f8f..c04effa3abb 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/CPreprocessor.java @@ -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; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ILocationResolver.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ILocationResolver.java index 5518b4822b6..b7e4936c9af 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ILocationResolver.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/ILocationResolver.java @@ -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. */ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IncludeFileContent.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IncludeFileContent.java index 8d104520c2a..c32d7b1aaec 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IncludeFileContent.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IncludeFileContent.java @@ -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 fUsingDirectives; private final String fFileLocation; private boolean fHeuristic; + private boolean fIsSource= false; private List 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; + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtx.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtx.java index b25e540854e..007796b302d 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtx.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtx.java @@ -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 getChildren() { return Collections.emptySet(); } + + public boolean isSourceFile() { + if (fParent == null) + return false; + return fParent.isSourceFile(); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxFile.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxFile.java index b3110958bba..e8976352a75 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxFile.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationCtxFile.java @@ -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; + } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java index 2c221bcfc04..0de289e3c6b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/LocationMap.java @@ -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 true 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); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/ASTFilePathResolver.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/ASTFilePathResolver.java index fbd2dbb1649..24931afc44b 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/ASTFilePathResolver.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/ASTFilePathResolver.java @@ -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); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java index 9a6bd3c4cba..8cf4acdfc6f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/indexer/ProjectIndexerInputAdapter.java @@ -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) {