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

Fixes ambiguity resolution for plain C, bug 232612.

This commit is contained in:
Markus Schorn 2008-05-19 16:55:16 +00:00
parent 0fd3f6cfb2
commit 10c51f51bb
3 changed files with 72 additions and 55 deletions

View file

@ -3005,7 +3005,7 @@ public class AST2Tests extends AST2BaseTest {
assertFalse(col.getName(i).resolveBinding() instanceof IProblemBinding); assertFalse(col.getName(i).resolveBinding() instanceof IProblemBinding);
tu = parse( tu = parse(
"void f() { typedef int x; int y; x * y; }", ParserLanguage.C); //$NON-NLS-1$ "int y; void f() { typedef int x; x * y; }", ParserLanguage.C); //$NON-NLS-1$
col = new CNameCollector(); col = new CNameCollector();
tu.accept(col); tu.accept(col);
for (int i = 0; i < col.size(); ++i) for (int i = 0; i < col.size(); ++i)

View file

@ -20,8 +20,8 @@ import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor; import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent; import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
public abstract class CASTAmbiguity extends CASTNode { public abstract class CASTAmbiguity extends CASTNode {
@ -55,50 +55,65 @@ public abstract class CASTAmbiguity extends CASTNode {
@Override @Override
public boolean accept(ASTVisitor visitor) { public boolean accept(ASTVisitor visitor) {
IScope scope= CVisitor.getContainingScope(this);
IASTNode[] nodez = getNodes(); IASTNode[] nodez = getNodes();
int [] issues = new int[ nodez.length ]; int bestIndex = 0;
for( int i = 0; i < nodez.length; ++i ) int bestValue = Integer.MAX_VALUE;
{ for (int i = 0; i < nodez.length; ++i) {
IASTNode s = nodez[i]; final IASTNode s = nodez[i];
s.accept(visitor); s.accept(visitor);
CASTNameCollector resolver = new CASTNameCollector();
int issues= 0;
final CASTNameCollector resolver = new CASTNameCollector();
s.accept(resolver); s.accept(resolver);
IASTName [] names = resolver.getNames(); final IASTName[] names= resolver.getNames();
for( int j = 0; j < names.length; ++j ) for (IASTName name : names) {
{
try
{
IBinding b = names[j].resolveBinding();
if( b == null || b instanceof IProblemBinding )
++issues[i];
IScope scope = CVisitor.getContainingScope( names[j] );
if( scope != null )
{
try { try {
ASTInternal.flushCache(scope); IBinding b = name.resolveBinding();
if (b instanceof IProblemBinding) {
issues++;
}
} catch (Exception t) {
issues++;
}
if (issues == bestValue) {
break;
}
}
if (scope instanceof IASTInternalScope) {
final IASTInternalScope internalScope = (IASTInternalScope) scope;
try {
internalScope.flushCache();
} catch (DOMException e) { } catch (DOMException e) {
} }
} }
} if (issues < bestValue) {
catch( Exception t ) bestValue= issues;
{
++issues[i];
}
}
}
int bestIndex = 0;
int bestValue = issues[0];
for( int i = 1; i < issues.length; ++i )
{
if( issues[i] < bestValue )
{
bestIndex= i; bestIndex= i;
bestValue = issues[i]; if (issues == 0) {
break;
}
} }
} }
IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent(); IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent();
owner.replace(this, nodez[bestIndex]); owner.replace(this, nodez[bestIndex]);
if (scope instanceof IASTInternalScope) {
try {
IASTNode node= ((IASTInternalScope) scope).getPhysicalNode();
if (node != null) {
CASTNameCollector nc = new CASTNameCollector();
node.accept(nc);
final IASTName[] names= nc.getNames();
for (IASTName name : names) {
name.setBinding(null);
}
}
} catch (DOMException e) {
}
}
return true; return true;
} }

View file

@ -11,10 +11,6 @@
* Bryan Wilkinson (QNX) * Bryan Wilkinson (QNX)
* Andrew Ferguson (Symbian) * Andrew Ferguson (Symbian)
*******************************************************************************/ *******************************************************************************/
/*
* Created on Nov 25, 2004
*/
package org.eclipse.cdt.internal.core.dom.parser.c; package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
@ -415,28 +411,34 @@ public class CScope implements ICScope, IASTInternalScope {
public void flushCache() { public void flushCache() {
CharArrayObjectMap map= mapsToNameOrBinding[0]; CharArrayObjectMap map= mapsToNameOrBinding[0];
CharArrayObjectMap builtins= new CharArrayObjectMap(map.size()); CharArrayObjectMap builtins= null;
for (int i = 0; i < map.size(); i++) { for (int i = 0; i < map.size(); i++) {
Object obj= map.getAt(i); Object obj= map.getAt(i);
if (obj instanceof IASTName) { if (obj instanceof IASTName) {
((IASTName) obj).setBinding(null); ((IASTName) obj).setBinding(null);
} else if (obj instanceof IBinding) { } else if (obj instanceof IBinding) {
if (builtins == null) {
builtins= new CharArrayObjectMap(2);
}
builtins.put(((IBinding) obj).getNameCharArray(), obj); builtins.put(((IBinding) obj).getNameCharArray(), obj);
} }
} }
mapsToNameOrBinding[0]= map; mapsToNameOrBinding[0]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
map= mapsToNameOrBinding[1]; map= mapsToNameOrBinding[1];
builtins= new CharArrayObjectMap(map.size()); builtins= null;
for (int i = 0; i < map.size(); i++) { for (int i = 0; i < map.size(); i++) {
Object obj= map.getAt(i); Object obj= map.getAt(i);
if (obj instanceof IASTName) { if (obj instanceof IASTName) {
((IASTName) obj).setBinding(null); ((IASTName) obj).setBinding(null);
} else if (obj instanceof IBinding) { } else if (obj instanceof IBinding) {
if (builtins == null) {
builtins= new CharArrayObjectMap(2);
}
builtins.put(((IBinding) obj).getNameCharArray(), obj); builtins.put(((IBinding) obj).getNameCharArray(), obj);
} }
} }
mapsToNameOrBinding[1]= map; mapsToNameOrBinding[1]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
reuseBindings= null; reuseBindings= null;
isFullyCached = false; isFullyCached = false;
} }