1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 367753: Correct propagating of internal modifications to the macro dictionary.

This commit is contained in:
Markus Schorn 2012-01-03 14:32:31 +01:00
parent 3bd21a03c4
commit 8afad54183
5 changed files with 126 additions and 25 deletions

View file

@ -84,6 +84,8 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
protected IASTName findName(String section, int len) { protected IASTName findName(String section, int len) {
if (len == 0)
len= section.length();
for (int i = 0; i < strategy.getAstCount(); i++) { for (int i = 0; i < strategy.getAstCount(); i++) {
IASTTranslationUnit ast = strategy.getAst(i); IASTTranslationUnit ast = strategy.getAst(i);
final IASTNodeSelector nodeSelector = ast.getNodeSelector(null); final IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
@ -260,30 +262,36 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
this.cpp = cpp; this.cpp = cpp;
} }
@Override
public ICProject getCProject() { public ICProject getCProject() {
return cproject; return cproject;
} }
@Override
public StringBuilder[] getTestData() { public StringBuilder[] getTestData() {
return testData; return testData;
} }
@Override
public int getAstCount() { public int getAstCount() {
return 1; return 1;
} }
@Override
public IASTTranslationUnit getAst(int index) { public IASTTranslationUnit getAst(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return ast; return ast;
} }
@Override
public StringBuilder getAstSource(int index) { public StringBuilder getAstSource(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return testData[1]; return testData[1];
} }
@Override
public void setUp() throws Exception { public void setUp() throws Exception {
cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER)
: CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); : CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
@ -308,6 +316,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile); ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile);
} }
@Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (index != null) { if (index != null) {
index.releaseReadLock(); index.releaseReadLock();
@ -317,10 +326,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
@Override
public IIndex getIndex() { public IIndex getIndex() {
return index; return index;
} }
@Override
public boolean isCompositeIndex() { public boolean isCompositeIndex() {
return false; return false;
} }
@ -337,30 +348,36 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
this.cpp = cpp; this.cpp = cpp;
} }
@Override
public ICProject getCProject() { public ICProject getCProject() {
return cproject; return cproject;
} }
@Override
public StringBuilder[] getTestData() { public StringBuilder[] getTestData() {
return testData; return testData;
} }
@Override
public int getAstCount() { public int getAstCount() {
return 1; return 1;
} }
@Override
public IASTTranslationUnit getAst(int index) { public IASTTranslationUnit getAst(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return ast; return ast;
} }
@Override
public StringBuilder getAstSource(int index) { public StringBuilder getAstSource(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return testData[1]; return testData[1];
} }
@Override
public void setUp() throws Exception { public void setUp() throws Exception {
cproject = cpp ? CProjectHelper.createCCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) cproject = cpp ? CProjectHelper.createCCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER)
: CProjectHelper.createCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); : CProjectHelper.createCProject(getName()+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
@ -385,6 +402,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile); ast = TestSourceReader.createIndexBasedAST(index, cproject, cppfile);
} }
@Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (index != null) { if (index != null) {
index.releaseReadLock(); index.releaseReadLock();
@ -394,10 +412,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
@Override
public IIndex getIndex() { public IIndex getIndex() {
return index; return index;
} }
@Override
public boolean isCompositeIndex() { public boolean isCompositeIndex() {
return false; return false;
} }
@ -425,26 +445,32 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
asts = new ArrayList<IASTTranslationUnit>(); asts = new ArrayList<IASTTranslationUnit>();
} }
@Override
public ICProject getCProject() { public ICProject getCProject() {
return cproject; return cproject;
} }
@Override
public StringBuilder[] getTestData() { public StringBuilder[] getTestData() {
return testData; return testData;
} }
@Override
public int getAstCount() { public int getAstCount() {
return asts.size(); return asts.size();
} }
@Override
public IASTTranslationUnit getAst(int index) { public IASTTranslationUnit getAst(int index) {
return asts.get(index); return asts.get(index);
} }
@Override
public StringBuilder getAstSource(int index) { public StringBuilder getAstSource(int index) {
return astSources.get(index); return astSources.get(index);
} }
@Override
public void setUp() throws Exception { public void setUp() throws Exception {
cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) cproject = cpp ? CProjectHelper.createCCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER)
: CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); : CProjectHelper.createCProject(getName() + System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
@ -487,6 +513,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
@Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (index != null) { if (index != null) {
index.releaseReadLock(); index.releaseReadLock();
@ -496,10 +523,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
@Override
public IIndex getIndex() { public IIndex getIndex() {
return index; return index;
} }
@Override
public boolean isCompositeIndex() { public boolean isCompositeIndex() {
return false; return false;
} }
@ -516,10 +545,12 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
this.cpp = cpp; this.cpp = cpp;
} }
@Override
public ICProject getCProject() { public ICProject getCProject() {
return cproject; return cproject;
} }
@Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (index != null) { if (index != null) {
index.releaseReadLock(); index.releaseReadLock();
@ -532,6 +563,7 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
} }
} }
@Override
public void setUp() throws Exception { public void setUp() throws Exception {
cproject= cpp ? CProjectHelper.createCCProject("OnlineContent"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER) cproject= cpp ? CProjectHelper.createCCProject("OnlineContent"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER)
: CProjectHelper.createCProject("OnlineContent"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER); : CProjectHelper.createCProject("OnlineContent"+System.currentTimeMillis(), "bin", IPDOMManager.ID_NO_INDEXER);
@ -580,30 +612,36 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
return referenced; return referenced;
} }
@Override
public int getAstCount() { public int getAstCount() {
return 1; return 1;
} }
@Override
public IASTTranslationUnit getAst(int index) { public IASTTranslationUnit getAst(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return ast; return ast;
} }
@Override
public StringBuilder getAstSource(int index) { public StringBuilder getAstSource(int index) {
if (index != 0) if (index != 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
return testData[1]; return testData[1];
} }
@Override
public IIndex getIndex() { public IIndex getIndex() {
return index; return index;
} }
@Override
public StringBuilder[] getTestData() { public StringBuilder[] getTestData() {
return testData; return testData;
} }
@Override
public boolean isCompositeIndex() { public boolean isCompositeIndex() {
return true; return true;
} }

View file

@ -76,13 +76,6 @@ public class IndexCPPTemplateResolutionTest extends IndexBindingResolutionTestBa
public ProjectWithDepProj() {setStrategy(new ReferencedProject(true));} public ProjectWithDepProj() {setStrategy(new ReferencedProject(true));}
public static TestSuite suite() {return suite(ProjectWithDepProj.class);} public static TestSuite suite() {return suite(ProjectWithDepProj.class);}
// template <typename T= int> class XT;
// #include "header.h"
// template <typename T> class XT {};
// void test() {
// XT<> x;
// };
@Override @Override
public void testDefaultTemplateArgInHeader_264988() throws Exception { public void testDefaultTemplateArgInHeader_264988() throws Exception {
// Not supported across projects (the composite index does not merge // Not supported across projects (the composite index does not merge

View file

@ -12,6 +12,8 @@ package org.eclipse.cdt.internal.index.tests;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
@ -136,4 +138,50 @@ public class IndexMultiVariantHeaderTest extends IndexBindingResolutionTestBase
getBindingFromASTName("y = 0", 1, ICPPVariable.class); getBindingFromASTName("y = 0", 1, ICPPVariable.class);
getBindingFromASTName("z = 0", 1, ICPPVariable.class); getBindingFromASTName("z = 0", 1, ICPPVariable.class);
} }
// b.h
// #ifndef _B
// #define _B
// #define SIG // This internal modification is not propagated
// #endif
// a.h
// #include "b.h"
// #ifdef SIG // Not significant, because it is defined in "b.h"
// #endif
// a.cpp *
// #include "a.h"
public void testSignificantMacroDetection_367753a() throws Exception {
IASTName includeName= findName("a.h", 0);
IASTPreprocessorIncludeStatement inc= (IASTPreprocessorIncludeStatement) includeName.getParent();
assertTrue(inc.isResolved());
assertEquals("{}", inc.getSignificantMacros().toString());
assertNotNull(inc.getImportedIndexFile());
}
// c.h
// #define SIG // This internal modification is not propagated
// b.h
// #ifndef _B
// #define _B
// #include "c.h"
// #endif
// a.h
// #include "b.h"
// #ifdef SIG // Not significant, because it is defined in "c.h"
// #endif
// a.cpp *
// #include "a.h"
public void testSignificantMacroDetection_367753b() throws Exception {
IASTName includeName= findName("a.h", 0);
IASTPreprocessorIncludeStatement inc= (IASTPreprocessorIncludeStatement) includeName.getParent();
assertTrue(inc.isResolved());
assertEquals("{}", inc.getSignificantMacros().toString());
assertNotNull(inc.getImportedIndexFile());
}
} }

View file

@ -105,9 +105,12 @@ public class TestSourceReader {
contents.remove(0); contents.remove(0);
content = new StringBuilder(); content = new StringBuilder();
} }
int idx= line.indexOf(testName); if (line.length() > 0 && !contents.isEmpty()) {
if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) { int idx= line.indexOf(testName);
return contents.toArray(new StringBuilder[contents.size()]); if (idx != -1 && !Character.isJavaIdentifierPart(line.charAt(idx + testName.length()))) {
return contents.toArray(new StringBuilder[contents.size()]);
}
contents.clear();
} }
} }
} }
@ -229,6 +232,7 @@ public class TestSourceReader {
final IWorkspace ws = ResourcesPlugin.getWorkspace(); final IWorkspace ws = ResourcesPlugin.getWorkspace();
final IFile result[] = new IFile[1]; final IFile result[] = new IFile[1];
ws.run(new IWorkspaceRunnable() { ws.run(new IWorkspaceRunnable() {
@Override
public void run(IProgressMonitor monitor) throws CoreException { public void run(IProgressMonitor monitor) throws CoreException {
//Obtain file handle //Obtain file handle
IFile file = container.getFile(filePath); IFile file = container.getFile(filePath);

View file

@ -330,8 +330,21 @@ final class ScannerContext {
} }
public void internalModification(char[] macroName) { public void internalModification(char[] macroName) {
if (fInternalModifications != null) final CharArraySet collector = findModificationCollector();
fInternalModifications.put(macroName); if (collector != null)
collector.put(macroName);
}
private CharArraySet findModificationCollector() {
ScannerContext ctx= this;
do {
final CharArraySet collector = ctx.fInternalModifications;
if (collector != null)
return collector;
ctx= ctx.getParent();
} while (ctx != null);
return null;
} }
public void significantMacro(IMacroBinding macro) { public void significantMacro(IMacroBinding macro) {
@ -372,23 +385,28 @@ final class ScannerContext {
return; return;
if (fParent != null) { if (fParent != null) {
final CharArraySet local = fParent.fInternalModifications; CharArraySet collector = fParent.findModificationCollector();
if (local != null) { if (collector != null) {
final CharArrayObjectMap<char[]> significant = fParent.fSignificantMacros; // Propagate internal modifications to first interested parent.
for (int i=0; i<fSignificantMacros.size(); i++) { collector.addAll(fInternalModifications);
final char[] name = fSignificantMacros.keyAt(i);
if (!local.containsKey(name)) { // Propagate significant macros to direct parent, if it is interested.
final char[] value= fSignificantMacros.getAt(i); if (collector == fParent.fInternalModifications) {
if (value == SignificantMacros.DEFINED) { final CharArrayObjectMap<char[]> significant = fParent.fSignificantMacros;
if (!local.containsKey(name)) { for (int i=0; i<fSignificantMacros.size(); i++) {
fParent.addSignificantMacroDefined(name); final char[] name = fSignificantMacros.keyAt(i);
if (!collector.containsKey(name)) {
final char[] value= fSignificantMacros.getAt(i);
if (value == SignificantMacros.DEFINED) {
if (!collector.containsKey(name)) {
fParent.addSignificantMacroDefined(name);
}
} else {
significant.put(name, value);
} }
} else {
significant.put(name, value);
} }
} }
} }
local.addAll(fInternalModifications);
} }
} }
fInternalModifications= null; fInternalModifications= null;