From 67b68db542173cd3f521f20cbe2a5cf0238f29f4 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 9 Oct 2008 11:21:52 +0000 Subject: [PATCH] Enforce pragma once semantics for headers shown in editor, bug 249884. --- .../internal/index/tests/IndexBugsTests.java | 47 +++++++++++++++++++ .../index/IndexBasedCodeReaderFactory.java | 5 ++ .../core/parser/scanner/CPreprocessor.java | 7 ++- .../scanner/IIndexBasedCodeReaderFactory.java | 7 ++- 4 files changed, 64 insertions(+), 2 deletions(-) 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 fdb2c2fbe52..1585f6269a8 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 @@ -26,6 +26,7 @@ import org.eclipse.cdt.core.dom.IName; import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.dom.ast.ASTTypeUtil; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; @@ -1423,4 +1424,50 @@ public class IndexBugsTests extends BaseTestCase { fIndex.releaseReadLock(); } } + + + // #ifndef B_H + // #include "b.h" + // #endif + // + // #ifndef A_H_ + // #define A_H_ + // int aOK; + // #endif /* A_H_ */ + + // #ifndef A_H_ + // #include "a.h" + // #endif + // + // #ifndef B_H_ + // #define B_H_ + // int bOK; + // #endif + + // #include "a.h" + public void testStrangeIncludeStrategy_Bug249884() throws Exception { + String[] contents= getContentsForTest(3); + final IIndexManager indexManager = CCorePlugin.getIndexManager(); + IFile ah= TestSourceReader.createFile(fCProject.getProject(), "a.h", contents[0]); + TestSourceReader.createFile(fCProject.getProject(), "b.h", contents[1]); + TestSourceReader.createFile(fCProject.getProject(), "source.cpp", contents[2]); + indexManager.reindex(fCProject); + waitForIndexer(); + ITranslationUnit tu= (ITranslationUnit) CoreModel.getDefault().create(ah); + fIndex.acquireReadLock(); + try { + IIndexBinding[] bindings= fIndex.findBindings("aOK".toCharArray(), IndexFilter.ALL_DECLARED, new NullProgressMonitor()); + assertEquals(1, bindings.length); + fIndex.findBindings("bOK".toCharArray(), IndexFilter.ALL_DECLARED, new NullProgressMonitor()); + assertEquals(1, bindings.length); + IASTTranslationUnit ast= tu.getAST(fIndex, ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS); + final IASTDeclaration[] decls = ast.getDeclarations(); + assertEquals(1, decls.length); + IASTSimpleDeclaration decl= (IASTSimpleDeclaration) decls[0]; + assertEquals("aOK", decl.getDeclarators()[0].getName().toString()); + } finally { + fIndex.releaseReadLock(); + } + } + } \ No newline at end of file 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 60903447ad6..37864dfa152 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 @@ -115,6 +115,11 @@ public final class IndexBasedCodeReaderFactory extends AbstractCodeReaderFactory return fPathResolver.doesIncludeFileExist(path); } + public void reportTranslationUnitFile(String path) { + IIndexFileLocation ifl= fPathResolver.resolveASTPath(path); + fIncludedFiles.add(ifl); + } + public boolean hasFileBeenIncludedInCurrentTranslationUnit(String path) { IIndexFileLocation ifl= fPathResolver.resolveASTPath(path); return fIncludedFiles.contains(ifl); 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 5d252cef56e..7ffea6ca118 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 @@ -180,7 +180,9 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { final String filePath= new String(reader.filename); fAllIncludedFiles.add(filePath); - ILocationCtx ctx= fLocationMap.pushTranslationUnit(filePath, reader.buffer); + ILocationCtx ctx= fLocationMap.pushTranslationUnit(filePath, reader.buffer); + fCodeReaderFactory.reportTranslationUnitFile(filePath); + fAllIncludedFiles.add(filePath); fRootLexer= new Lexer(reader.buffer, fLexOptions, this, this); fRootContext= fCurrentContext= new ScannerContext(ctx, null, fRootLexer); if (info instanceof IExtendedScannerInfo) { @@ -207,6 +209,9 @@ public class CPreprocessor implements ILexerLog, IScanner, IAdaptable { } return null; } + public void reportTranslationUnitFile(String path) { + fAllIncludedFiles.add(path); + } public boolean hasFileBeenIncludedInCurrentTranslationUnit(String path) { return fAllIncludedFiles.contains(path); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IIndexBasedCodeReaderFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IIndexBasedCodeReaderFactory.java index 0f58e303555..a50dd02004c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IIndexBasedCodeReaderFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner/IIndexBasedCodeReaderFactory.java @@ -24,7 +24,12 @@ public interface IIndexBasedCodeReaderFactory extends ICodeReaderFactory { * Returns whether or not the file has been included. */ boolean hasFileBeenIncludedInCurrentTranslationUnit(String path); - + + /** + * Reports the path of the translation unit, such that it is known as included. + */ + void reportTranslationUnitFile(String path); + /** * Create an InclusionContent object for the given location. * return an inclusion content or null if the location does not exist.