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:
parent
0fd3f6cfb2
commit
10c51f51bb
3 changed files with 72 additions and 55 deletions
|
@ -3005,7 +3005,7 @@ public class AST2Tests extends AST2BaseTest {
|
|||
assertFalse(col.getName(i).resolveBinding() instanceof IProblemBinding);
|
||||
|
||||
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();
|
||||
tu.accept(col);
|
||||
for (int i = 0; i < col.size(); ++i)
|
||||
|
|
|
@ -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.c.CASTVisitor;
|
||||
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.IASTInternalScope;
|
||||
|
||||
public abstract class CASTAmbiguity extends CASTNode {
|
||||
|
||||
|
@ -55,51 +55,66 @@ public abstract class CASTAmbiguity extends CASTNode {
|
|||
|
||||
@Override
|
||||
public boolean accept(ASTVisitor visitor) {
|
||||
IASTNode [] nodez = getNodes();
|
||||
int [] issues = new int[ nodez.length ];
|
||||
for( int i = 0; i < nodez.length; ++i )
|
||||
{
|
||||
IASTNode s = nodez[i];
|
||||
s.accept(visitor);
|
||||
CASTNameCollector resolver = new CASTNameCollector();
|
||||
s.accept( resolver );
|
||||
IASTName [] names = resolver.getNames();
|
||||
for( int j = 0; j < names.length; ++j )
|
||||
{
|
||||
try
|
||||
{
|
||||
IBinding b = names[j].resolveBinding();
|
||||
if( b == null || b instanceof IProblemBinding )
|
||||
++issues[i];
|
||||
IScope scope = CVisitor.getContainingScope( names[j] );
|
||||
if( scope != null )
|
||||
{
|
||||
try {
|
||||
ASTInternal.flushCache(scope);
|
||||
} catch (DOMException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( Exception t )
|
||||
{
|
||||
++issues[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
int bestIndex = 0;
|
||||
int bestValue = issues[0];
|
||||
for( int i = 1; i < issues.length; ++i )
|
||||
{
|
||||
if( issues[i] < bestValue )
|
||||
{
|
||||
bestIndex = i;
|
||||
bestValue = issues[i];
|
||||
}
|
||||
}
|
||||
IScope scope= CVisitor.getContainingScope(this);
|
||||
|
||||
IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent();
|
||||
owner.replace(this, nodez[bestIndex]);
|
||||
return true;
|
||||
}
|
||||
IASTNode[] nodez = getNodes();
|
||||
int bestIndex = 0;
|
||||
int bestValue = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < nodez.length; ++i) {
|
||||
final IASTNode s = nodez[i];
|
||||
s.accept(visitor);
|
||||
|
||||
int issues= 0;
|
||||
final CASTNameCollector resolver = new CASTNameCollector();
|
||||
s.accept(resolver);
|
||||
final IASTName[] names= resolver.getNames();
|
||||
for (IASTName name : names) {
|
||||
try {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
if (issues < bestValue) {
|
||||
bestValue= issues;
|
||||
bestIndex= i;
|
||||
if (issues == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IASTAmbiguityParent owner = (IASTAmbiguityParent) getParent();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,10 +11,6 @@
|
|||
* Bryan Wilkinson (QNX)
|
||||
* Andrew Ferguson (Symbian)
|
||||
*******************************************************************************/
|
||||
|
||||
/*
|
||||
* Created on Nov 25, 2004
|
||||
*/
|
||||
package org.eclipse.cdt.internal.core.dom.parser.c;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
|
@ -415,28 +411,34 @@ public class CScope implements ICScope, IASTInternalScope {
|
|||
|
||||
public void flushCache() {
|
||||
CharArrayObjectMap map= mapsToNameOrBinding[0];
|
||||
CharArrayObjectMap builtins= new CharArrayObjectMap(map.size());
|
||||
CharArrayObjectMap builtins= null;
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
Object obj= map.getAt(i);
|
||||
if (obj instanceof IASTName) {
|
||||
((IASTName) obj).setBinding(null);
|
||||
} else if (obj instanceof IBinding) {
|
||||
if (builtins == null) {
|
||||
builtins= new CharArrayObjectMap(2);
|
||||
}
|
||||
builtins.put(((IBinding) obj).getNameCharArray(), obj);
|
||||
}
|
||||
}
|
||||
mapsToNameOrBinding[0]= map;
|
||||
mapsToNameOrBinding[0]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
|
||||
|
||||
map= mapsToNameOrBinding[1];
|
||||
builtins= new CharArrayObjectMap(map.size());
|
||||
builtins= null;
|
||||
for (int i = 0; i < map.size(); i++) {
|
||||
Object obj= map.getAt(i);
|
||||
if (obj instanceof IASTName) {
|
||||
((IASTName) obj).setBinding(null);
|
||||
} else if (obj instanceof IBinding) {
|
||||
if (builtins == null) {
|
||||
builtins= new CharArrayObjectMap(2);
|
||||
}
|
||||
builtins.put(((IBinding) obj).getNameCharArray(), obj);
|
||||
}
|
||||
}
|
||||
mapsToNameOrBinding[1]= map;
|
||||
mapsToNameOrBinding[1]= builtins == null ? CharArrayObjectMap.EMPTY_MAP : builtins;
|
||||
reuseBindings= null;
|
||||
isFullyCached = false;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue