mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-03 22:35:43 +02:00
Must not use ast visitor before ambiguity resolution, follow up for bug 253690.
This commit is contained in:
parent
0087933aa5
commit
7fb63c36d6
6 changed files with 102 additions and 76 deletions
|
@ -0,0 +1,63 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2008 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
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Markus Schorn - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.core.dom.parser;
|
||||
|
||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
||||
|
||||
/**
|
||||
* Base class for {@link CVisitor} and {@link CPPVisitor}
|
||||
*/
|
||||
public class ASTQueries {
|
||||
/**
|
||||
* Tests whether the given expression can contain ast-names, suitable to be used before ambiguity
|
||||
* resolution.
|
||||
*/
|
||||
public static boolean canContainName(IASTExpression expr) {
|
||||
if (expr == null || expr instanceof IASTLiteralExpression)
|
||||
return false;
|
||||
|
||||
if (expr instanceof IASTAmbiguousExpression)
|
||||
return true;
|
||||
if (expr instanceof IASTIdExpression)
|
||||
return true;
|
||||
|
||||
if (expr instanceof IASTUnaryExpression) {
|
||||
IASTUnaryExpression uexpr= (IASTUnaryExpression) expr;
|
||||
return canContainName(uexpr.getOperand());
|
||||
}
|
||||
if (expr instanceof IASTBinaryExpression) {
|
||||
IASTBinaryExpression bexpr= (IASTBinaryExpression) expr;
|
||||
return canContainName(bexpr.getOperand1()) || canContainName(bexpr.getOperand2());
|
||||
}
|
||||
if (expr instanceof IASTConditionalExpression) {
|
||||
IASTConditionalExpression cexpr= (IASTConditionalExpression) expr;
|
||||
return canContainName(cexpr.getLogicalConditionExpression()) ||
|
||||
canContainName(cexpr.getPositiveResultExpression()) || canContainName(cexpr.getNegativeResultExpression());
|
||||
}
|
||||
if (expr instanceof IASTExpressionList) {
|
||||
IASTExpressionList lexpr= (IASTExpressionList) expr;
|
||||
IASTExpression[] subexprs= lexpr.getExpressions();
|
||||
for (IASTExpression subexpr : subexprs) {
|
||||
if (canContainName(subexpr))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -63,7 +63,6 @@ import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
|
|||
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
|
||||
import org.eclipse.cdt.core.dom.parser.IBuiltinBindingsProvider;
|
||||
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
|
||||
|
@ -108,24 +107,6 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
|
|||
}
|
||||
}
|
||||
|
||||
protected static class NameChecker extends ASTVisitor {
|
||||
private boolean fFound;
|
||||
protected NameChecker() {
|
||||
shouldVisitNames= true;
|
||||
}
|
||||
@Override
|
||||
public int visit(IASTName name) {
|
||||
fFound= true;
|
||||
return PROCESS_ABORT;
|
||||
}
|
||||
public boolean containsName(IASTNode node) {
|
||||
fFound= false;
|
||||
node.accept(this);
|
||||
return fFound;
|
||||
}
|
||||
}
|
||||
protected NameChecker NAME_CHECKER= new NameChecker();
|
||||
|
||||
protected static final int DEFAULT_DESIGNATOR_LIST_SIZE = 4;
|
||||
protected static int parseCount = 0;
|
||||
|
||||
|
@ -1225,8 +1206,8 @@ public abstract class AbstractGNUSourceCodeParser implements ISourceCodeParser {
|
|||
case IASTLiteralExpression.lk_char_constant:
|
||||
case IASTLiteralExpression.lk_float_constant:
|
||||
case IASTLiteralExpression.lk_integer_constant:
|
||||
case ICPPASTLiteralExpression.lk_true:
|
||||
case ICPPASTLiteralExpression.lk_false:
|
||||
case IASTLiteralExpression.lk_true:
|
||||
case IASTLiteralExpression.lk_false:
|
||||
throwBacktrack(operatorToken);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -111,6 +111,7 @@ import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
|||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.BacktrackException;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions;
|
||||
|
@ -192,21 +193,22 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
|
||||
IASTInitializer initializer = cInitializerClause(newDesignators, true);
|
||||
|
||||
if (newDesignators.isEmpty()) {
|
||||
// depending on value of skipTrivialItemsInCompoundInitializers initializer may be null
|
||||
if (initializer != null)
|
||||
// depending on value of skipTrivialItemsInCompoundInitializers initializer may be null
|
||||
if (initializer != null) {
|
||||
if (newDesignators.isEmpty()) {
|
||||
result.addInitializer(initializer);
|
||||
} else {
|
||||
ICASTDesignatedInitializer desigInitializer = createDesignatorInitializer();
|
||||
((ASTNode) desigInitializer).setOffsetAndLength(
|
||||
((ASTNode) newDesignators.get(0)).getOffset(),
|
||||
((ASTNode)initializer).getOffset() + ((ASTNode)initializer).getLength() - ((ASTNode) newDesignators.get(0)).getOffset());
|
||||
for (int i = 0; i < newDesignators.size(); ++i) {
|
||||
ICASTDesignator d = (ICASTDesignator) newDesignators.get(i);
|
||||
desigInitializer.addDesignator(d);
|
||||
}
|
||||
desigInitializer.setOperandInitializer(initializer);
|
||||
result.addInitializer(desigInitializer);
|
||||
} else {
|
||||
ICASTDesignatedInitializer desigInitializer = createDesignatorInitializer();
|
||||
((ASTNode) desigInitializer).setOffsetAndLength(
|
||||
((ASTNode) newDesignators.get(0)).getOffset(),
|
||||
((ASTNode)initializer).getOffset() + ((ASTNode)initializer).getLength() - ((ASTNode) newDesignators.get(0)).getOffset());
|
||||
for (int i = 0; i < newDesignators.size(); ++i) {
|
||||
ICASTDesignator d = (ICASTDesignator) newDesignators.get(i);
|
||||
desigInitializer.addDesignator(d);
|
||||
}
|
||||
desigInitializer.setOperandInitializer(initializer);
|
||||
result.addInitializer(desigInitializer);
|
||||
}
|
||||
}
|
||||
// can end with ", }" or "}"
|
||||
if (LT(1) == IToken.tCOMMA)
|
||||
|
@ -235,7 +237,7 @@ public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
|
|||
// assignmentExpression
|
||||
IASTExpression assignmentExpression = assignmentExpression();
|
||||
if (inAggregateInitializer && skipTrivialExpressionsInAggregateInitializers) {
|
||||
if (!NAME_CHECKER.containsName(assignmentExpression))
|
||||
if (!ASTQueries.canContainName(assignmentExpression))
|
||||
return null;
|
||||
}
|
||||
IASTInitializerExpression result = createInitializerExpression();
|
||||
|
|
|
@ -146,6 +146,7 @@ import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
|||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.BacktrackException;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions;
|
||||
|
@ -1371,7 +1372,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
protected IASTExpression primaryExpression() throws EndOfFileException,
|
||||
BacktrackException {
|
||||
IToken t = null;
|
||||
ICPPASTLiteralExpression literalExpression = null;
|
||||
IASTLiteralExpression literalExpression = null;
|
||||
switch (LT(1)) {
|
||||
// TO DO: we need more literals...
|
||||
case IToken.tINTEGER:
|
||||
|
@ -1407,14 +1408,14 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
case IToken.t_false:
|
||||
t = consume();
|
||||
literalExpression = createLiteralExpression();
|
||||
literalExpression.setKind(ICPPASTLiteralExpression.lk_false);
|
||||
literalExpression.setKind(IASTLiteralExpression.lk_false);
|
||||
literalExpression.setValue(t.getImage());
|
||||
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
|
||||
return literalExpression;
|
||||
case IToken.t_true:
|
||||
t = consume();
|
||||
literalExpression = createLiteralExpression();
|
||||
literalExpression.setKind(ICPPASTLiteralExpression.lk_true);
|
||||
literalExpression.setKind(IASTLiteralExpression.lk_true);
|
||||
literalExpression.setValue(t.getImage());
|
||||
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
|
||||
return literalExpression;
|
||||
|
@ -1422,7 +1423,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
case IToken.t_this:
|
||||
t = consume();
|
||||
literalExpression = createLiteralExpression();
|
||||
literalExpression.setKind(ICPPASTLiteralExpression.lk_this);
|
||||
literalExpression.setKind(IASTLiteralExpression.lk_this);
|
||||
literalExpression.setValue(t.getImage());
|
||||
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
|
||||
return literalExpression;
|
||||
|
@ -3290,7 +3291,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
|
|||
// assignmentExpression
|
||||
IASTExpression assignmentExpression = assignmentExpression();
|
||||
if (inAggregateInitializer && skipTrivialExpressionsInAggregateInitializers) {
|
||||
if (!NAME_CHECKER.containsName(assignmentExpression))
|
||||
if (!ASTQueries.canContainName(assignmentExpression))
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -95,7 +95,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
|
||||
|
@ -145,6 +144,7 @@ import org.eclipse.cdt.core.index.IIndexBinding;
|
|||
import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
|
||||
|
@ -184,7 +184,7 @@ import org.eclipse.cdt.internal.core.index.IIndexScope;
|
|||
/**
|
||||
* @author aniefer
|
||||
*/
|
||||
public class CPPVisitor {
|
||||
public class CPPVisitor extends ASTQueries {
|
||||
public static final String SIZE_T = "size_t"; //$NON-NLS-1$
|
||||
public static final String PTRDIFF_T = "ptrdiff_t"; //$NON-NLS-1$
|
||||
public static final String STD = "std"; //$NON-NLS-1$
|
||||
|
@ -1784,15 +1784,15 @@ public class CPPVisitor {
|
|||
IASTTypeId id = ((IASTCastExpression)expression).getTypeId();
|
||||
IType type = createType(id.getDeclSpecifier());
|
||||
return createType(type, id.getAbstractDeclarator());
|
||||
} else if (expression instanceof ICPPASTLiteralExpression) {
|
||||
ICPPASTLiteralExpression lit= (ICPPASTLiteralExpression) expression;
|
||||
} else if (expression instanceof IASTLiteralExpression) {
|
||||
IASTLiteralExpression lit= (IASTLiteralExpression) expression;
|
||||
switch(lit.getKind()) {
|
||||
case ICPPASTLiteralExpression.lk_this: {
|
||||
case IASTLiteralExpression.lk_this: {
|
||||
IScope scope = getContainingScope(expression);
|
||||
return getThisType(scope);
|
||||
}
|
||||
case ICPPASTLiteralExpression.lk_true:
|
||||
case ICPPASTLiteralExpression.lk_false:
|
||||
case IASTLiteralExpression.lk_true:
|
||||
case IASTLiteralExpression.lk_false:
|
||||
return new CPPBasicType(ICPPBasicType.t_bool, 0, expression);
|
||||
case IASTLiteralExpression.lk_char_constant:
|
||||
return new CPPBasicType(IBasicType.t_char, 0, expression);
|
||||
|
@ -2096,12 +2096,12 @@ public class CPPVisitor {
|
|||
}
|
||||
|
||||
private static IType classifyTypeOfFloatLiteral(final IASTLiteralExpression expr) {
|
||||
final String lit= expr.toString();
|
||||
final int len= lit.length();
|
||||
final char[] lit= expr.getValue();
|
||||
final int len= lit.length;
|
||||
int kind= IBasicType.t_double;
|
||||
int flags= 0;
|
||||
if (len > 0) {
|
||||
switch(lit.charAt(len-1)) {
|
||||
switch(lit[len-1]) {
|
||||
case 'f': case 'F':
|
||||
kind= IBasicType.t_float;
|
||||
break;
|
||||
|
@ -2117,13 +2117,13 @@ public class CPPVisitor {
|
|||
int makelong= 0;
|
||||
boolean unsigned= false;
|
||||
|
||||
final String lit= expression.toString();
|
||||
for (int i=lit.length()-1; i >=0; i--) {
|
||||
final char c= lit.charAt(i);
|
||||
final char[] lit= expression.getValue();
|
||||
for (int i=lit.length-1; i >=0; i--) {
|
||||
final char c= lit[i];
|
||||
if (!(c > 'f' && c <= 'z') && !(c > 'F' && c <= 'Z')) {
|
||||
break;
|
||||
}
|
||||
switch (lit.charAt(i)) {
|
||||
switch (c) {
|
||||
case 'u':
|
||||
case 'U':
|
||||
unsigned = true;
|
||||
|
|
|
@ -333,46 +333,25 @@ public class CallHierarchyBugs extends CallHierarchyBaseTest {
|
|||
// CALL(0);
|
||||
// }
|
||||
public void testMacrosHidingCall_249801() throws Exception {
|
||||
long t= System.currentTimeMillis();
|
||||
String content= getContentsForTest(1)[0].toString();
|
||||
t= printTime("contents", t);
|
||||
IFile file= createFile(getProject(), "file249801.cpp", content);
|
||||
t= printTime("file", t);
|
||||
waitForIndexer(fIndex, file, CallHierarchyBaseTest.INDEXER_WAIT_TIME);
|
||||
t= printTime("indexer", t);
|
||||
|
||||
final CHViewPart ch= (CHViewPart) activateView(CUIPlugin.ID_CALL_HIERARCHY);
|
||||
t= printTime("view", t);
|
||||
|
||||
// open editor, check outline
|
||||
CEditor editor= openEditor(file);
|
||||
t= printTime("editor", t);
|
||||
int idx = content.indexOf("MACRO(Test");
|
||||
editor.selectAndReveal(idx, 0);
|
||||
openCallHierarchy(editor, false);
|
||||
t= printTime("ch1", t);
|
||||
|
||||
Tree chTree= checkTreeNode(ch, 0, "PREFIX_Test(char *, char *)").getParent();
|
||||
TreeItem ti= checkTreeNode(chTree, 0, 0, "call(int)");
|
||||
t= printTime("checked", t);
|
||||
|
||||
idx = content.indexOf("CALL(0");
|
||||
editor.selectAndReveal(idx+4, 0);
|
||||
openCallHierarchy(editor, true);
|
||||
t= printTime("ch2",t );
|
||||
chTree= checkTreeNode(ch, 0, "call(int)").getParent();
|
||||
ti= checkTreeNode(chTree, 0, 0, "PREFIX_Test(char *, char *)");
|
||||
t= printTime("checked", t);
|
||||
}
|
||||
|
||||
/**
|
||||
* mstodo
|
||||
* @param string
|
||||
* @return
|
||||
*/
|
||||
private long printTime(String string, long off) {
|
||||
long t= System.currentTimeMillis();
|
||||
System.out.println(string + (t-off));
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue