1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-05 15:25:49 +02:00

Bug 432701 - Move code that determines the value of an initializer to SemanticUtil

Change-Id: I0fcbad27155d875b420ce99cd9e9ba202448cc59
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
Reviewed-on: https://git.eclipse.org/r/26418
Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
Nathan Ridge 2014-05-14 03:15:34 -04:00 committed by Sergey Prigogin
parent ab5c43e46a
commit 6cbdabfbec
4 changed files with 48 additions and 36 deletions

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2010 IBM Corporation and others. * Copyright (c) 2004, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* Andrew Niefer (IBM Corporation) - initial API and implementation * Andrew Niefer (IBM Corporation) - initial API and implementation
* Markus Schorn (Wind River Systems) * Markus Schorn (Wind River Systems)
* Nathan Ridge
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
@ -35,6 +36,7 @@ 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.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.core.runtime.PlatformObject; import org.eclipse.core.runtime.PlatformObject;
/** /**
@ -220,7 +222,7 @@ public class CPPParameter extends PlatformObject implements ICPPParameter, ICPPI
return false; return false;
} }
public IASTInitializer getDefaultValue() { public IASTInitializer getInitializer() {
if (fDeclarations == null) if (fDeclarations == null)
return null; return null;
for (int i = 0; i < fDeclarations.length && fDeclarations[i] != null; i++) { for (int i = 0; i < fDeclarations.length && fDeclarations[i] != null; i++) {
@ -236,7 +238,7 @@ public class CPPParameter extends PlatformObject implements ICPPParameter, ICPPI
@Override @Override
public boolean hasDefaultValue() { public boolean hasDefaultValue() {
return getDefaultValue() != null; return getInitializer() != null;
} }
@Override @Override

View file

@ -18,23 +18,16 @@ import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer; import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IArrayType; import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue; import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerList;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable; import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
@ -340,30 +333,7 @@ public class CPPVariable extends PlatformObject implements ICPPVariable, ICPPInt
if (dtor != null) { if (dtor != null) {
IASTInitializer init= dtor.getInitializer(); IASTInitializer init= dtor.getInitializer();
if (init != null) { if (init != null) {
IASTInitializerClause clause= null; return SemanticUtil.getValueOfInitializer(init, getType(), maxDepth);
if (init instanceof IASTEqualsInitializer) {
clause= ((IASTEqualsInitializer) init).getInitializerClause();
} else if (init instanceof ICPPASTConstructorInitializer) {
IASTInitializerClause[] args= ((ICPPASTConstructorInitializer) init).getArguments();
if (args.length == 1 && args[0] instanceof IASTExpression) {
IType type= SemanticUtil.getUltimateTypeUptoPointers(getType());
if (type instanceof IPointerType || type instanceof IBasicType) {
clause= args[0];
}
}
} else if (init instanceof ICPPASTInitializerList) {
ICPPASTInitializerList list= (ICPPASTInitializerList) init;
switch (list.getSize()) {
case 0:
return Value.create(0);
case 1:
clause= list.getClauses()[0];
}
}
if (clause instanceof IASTExpression) {
return Value.create((IASTExpression) clause, maxDepth);
}
return Value.UNKNOWN;
} }
} }
return null; return null;

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2004, 2013 IBM Corporation and others. * Copyright (c) 2004, 2014 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -28,11 +28,15 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IArrayType; import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind; import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IEnumeration; import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator; import org.eclipse.cdt.core.dom.ast.IEnumerator;
@ -44,6 +48,8 @@ import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IValue; import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerList;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
@ -61,6 +67,7 @@ import org.eclipse.cdt.core.parser.util.CharArraySet;
import org.eclipse.cdt.core.parser.util.CharArrayUtils; import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.ObjectSet; import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer; import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClosureType; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClosureType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunctionType; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunctionType;
@ -755,4 +762,37 @@ public class SemanticUtil {
} }
return -1; return -1;
} }
/**
* Get the value of the initializer of a variable.
* @param init the initializer's AST node
* @param type the type of the variable
* @param maxDepth maximum recursion depth
*/
public static IValue getValueOfInitializer(IASTInitializer init, IType type, int maxDepth) {
IASTInitializerClause clause= null;
if (init instanceof IASTEqualsInitializer) {
clause= ((IASTEqualsInitializer) init).getInitializerClause();
} else if (init instanceof ICPPASTConstructorInitializer) {
IASTInitializerClause[] args= ((ICPPASTConstructorInitializer) init).getArguments();
if (args.length == 1 && args[0] instanceof IASTExpression) {
IType typeUpToPointers= SemanticUtil.getUltimateTypeUptoPointers(type);
if (typeUpToPointers instanceof IPointerType || typeUpToPointers instanceof IBasicType) {
clause= args[0];
}
}
} else if (init instanceof ICPPASTInitializerList) {
ICPPASTInitializerList list= (ICPPASTInitializerList) init;
switch (list.getSize()) {
case 0:
return Value.create(0);
case 1:
clause= list.getClauses()[0];
}
}
if (clause instanceof IASTExpression) {
return Value.create((IASTExpression) clause, maxDepth);
}
return Value.UNKNOWN;
}
} }

View file

@ -207,7 +207,7 @@ public class QPropertyAttributeProposal {
String defValue = null; String defValue = null;
if (param instanceof CPPParameter) { if (param instanceof CPPParameter) {
CPPParameter cppParam = (CPPParameter) param; CPPParameter cppParam = (CPPParameter) param;
IASTInitializer defaultValue = cppParam.getDefaultValue(); IASTInitializer defaultValue = cppParam.getInitializer();
if (defaultValue instanceof IASTEqualsInitializer) { if (defaultValue instanceof IASTEqualsInitializer) {
IASTInitializerClause clause = ((IASTEqualsInitializer) defaultValue).getInitializerClause(); IASTInitializerClause clause = ((IASTEqualsInitializer) defaultValue).getInitializerClause();
defValue = clause.toString(); defValue = clause.toString();