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

Fix NPE bug 36976.

This commit is contained in:
Alain Magloire 2003-04-26 23:39:33 +00:00
parent 452d55abef
commit c8cfea5fba

View file

@ -11,7 +11,6 @@
package org.eclipse.cdt.internal.ui.compare; package org.eclipse.cdt.internal.ui.compare;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -26,6 +25,7 @@ import org.eclipse.cdt.internal.core.dom.EnumerationSpecifier;
import org.eclipse.cdt.internal.core.dom.IOffsetable; import org.eclipse.cdt.internal.core.dom.IOffsetable;
import org.eclipse.cdt.internal.core.dom.Inclusion; import org.eclipse.cdt.internal.core.dom.Inclusion;
import org.eclipse.cdt.internal.core.dom.Macro; import org.eclipse.cdt.internal.core.dom.Macro;
import org.eclipse.cdt.internal.core.dom.Name;
import org.eclipse.cdt.internal.core.dom.NamespaceDefinition; import org.eclipse.cdt.internal.core.dom.NamespaceDefinition;
import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause; import org.eclipse.cdt.internal.core.dom.ParameterDeclarationClause;
import org.eclipse.cdt.internal.core.dom.SimpleDeclaration; import org.eclipse.cdt.internal.core.dom.SimpleDeclaration;
@ -35,7 +35,6 @@ import org.eclipse.cdt.internal.core.dom.TypeSpecifier;
import org.eclipse.cdt.internal.core.parser.Parser; import org.eclipse.cdt.internal.core.parser.Parser;
import org.eclipse.cdt.internal.parser.IStructurizerCallback; import org.eclipse.cdt.internal.parser.IStructurizerCallback;
/** /**
* @author alain * @author alain
* TODO: this should be remove when the new parser provides proper callbacks. * TODO: this should be remove when the new parser provides proper callbacks.
@ -46,7 +45,6 @@ public class ComparatorModelBuilder {
IStructurizerCallback callback; IStructurizerCallback callback;
String code; String code;
/** /**
*/ */
public ComparatorModelBuilder(IStructurizerCallback cb, String buffer) { public ComparatorModelBuilder(IStructurizerCallback cb, String buffer) {
@ -66,36 +64,35 @@ public class ComparatorModelBuilder {
generateModelElements(domBuilder.getTranslationUnit()); generateModelElements(domBuilder.getTranslationUnit());
} }
protected void generateModelElements(TranslationUnit tu){ protected void generateModelElements(TranslationUnit tu) {
Iterator i = tu.iterateOffsetableElements(); Iterator i = tu.iterateOffsetableElements();
while (i.hasNext()){ while (i.hasNext()) {
IOffsetable offsetable = (IOffsetable)i.next(); IOffsetable offsetable = (IOffsetable) i.next();
if (offsetable instanceof Inclusion){ if (offsetable instanceof Inclusion) {
createInclusion((Inclusion) offsetable); createInclusion((Inclusion) offsetable);
} } else if (offsetable instanceof Macro) {
else if (offsetable instanceof Macro){
createMacro((Macro) offsetable); createMacro((Macro) offsetable);
}else if(offsetable instanceof Declaration){ } else if (offsetable instanceof Declaration) {
generateModelElements ((Declaration) offsetable); generateModelElements((Declaration) offsetable);
} }
} }
} }
protected void generateModelElements (Declaration declaration){ protected void generateModelElements(Declaration declaration) {
// Namespace Definition // Namespace Definition
if (declaration instanceof NamespaceDefinition){ if (declaration instanceof NamespaceDefinition) {
NamespaceDefinition nsDef = (NamespaceDefinition) declaration; NamespaceDefinition nsDef = (NamespaceDefinition) declaration;
createNamespace(nsDef); createNamespace(nsDef);
List nsDeclarations = nsDef.getDeclarations(); List nsDeclarations = nsDef.getDeclarations();
Iterator nsDecls = nsDeclarations.iterator(); Iterator nsDecls = nsDeclarations.iterator();
while (nsDecls.hasNext()){ while (nsDecls.hasNext()) {
Declaration subNsDeclaration = (Declaration) nsDecls.next(); Declaration subNsDeclaration = (Declaration) nsDecls.next();
generateModelElements(subNsDeclaration); generateModelElements(subNsDeclaration);
} }
}// end Namespace Definition } // end Namespace Definition
// Simple Declaration // Simple Declaration
if (declaration instanceof SimpleDeclaration){ if (declaration instanceof SimpleDeclaration) {
SimpleDeclaration simpleDeclaration = (SimpleDeclaration) declaration; SimpleDeclaration simpleDeclaration = (SimpleDeclaration) declaration;
/*------------------------------------------- /*-------------------------------------------
@ -103,17 +100,17 @@ public class ComparatorModelBuilder {
*-------------------------------------------*/ *-------------------------------------------*/
TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier(); TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier();
// Enumeration // Enumeration
if (typeSpec instanceof EnumerationSpecifier){ if (typeSpec instanceof EnumerationSpecifier) {
EnumerationSpecifier enumSpecifier = (EnumerationSpecifier) typeSpec; EnumerationSpecifier enumSpecifier = (EnumerationSpecifier) typeSpec;
createEnumeration (enumSpecifier); createEnumeration(enumSpecifier);
} else if (typeSpec instanceof ClassSpecifier){ // Structure } else if (typeSpec instanceof ClassSpecifier) { // Structure
ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec; ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec;
createClass (simpleDeclaration, classSpecifier, false); createClass(simpleDeclaration, classSpecifier, false);
// create the sub declarations // create the sub declarations
List declarations = classSpecifier.getDeclarations(); List declarations = classSpecifier.getDeclarations();
Iterator j = declarations.iterator(); Iterator j = declarations.iterator();
while (j.hasNext()){ while (j.hasNext()) {
Declaration subDeclaration = (Declaration)j.next(); Declaration subDeclaration = (Declaration) j.next();
generateModelElements(subDeclaration); generateModelElements(subDeclaration);
} // end while j } // end while j
} }
@ -122,140 +119,181 @@ public class ComparatorModelBuilder {
* ----------------------------------------*/ * ----------------------------------------*/
List declarators = simpleDeclaration.getDeclarators(); List declarators = simpleDeclaration.getDeclarators();
Iterator d = declarators.iterator(); Iterator d = declarators.iterator();
while (d.hasNext()){ while (d.hasNext()) {
Declarator declarator = (Declarator)d.next(); Declarator declarator = (Declarator) d.next();
createElement(simpleDeclaration, declarator); createElement(simpleDeclaration, declarator);
} // end while d } // end while d
} // end if SimpleDeclaration } // end if SimpleDeclaration
// Template Declaration // Template Declaration
if(declaration instanceof TemplateDeclaration){ if (declaration instanceof TemplateDeclaration) {
TemplateDeclaration templateDeclaration = (TemplateDeclaration)declaration; TemplateDeclaration templateDeclaration = (TemplateDeclaration) declaration;
SimpleDeclaration simpleDeclaration = (SimpleDeclaration)templateDeclaration.getDeclarations().get(0); SimpleDeclaration simpleDeclaration = (SimpleDeclaration) templateDeclaration.getDeclarations().get(0);
TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier(); TypeSpecifier typeSpec = simpleDeclaration.getTypeSpecifier();
if (typeSpec instanceof ClassSpecifier){ if (typeSpec instanceof ClassSpecifier) {
ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec; ClassSpecifier classSpecifier = (ClassSpecifier) typeSpec;
createClass(simpleDeclaration, classSpecifier, true); createClass(simpleDeclaration, classSpecifier, true);
// create the sub declarations // create the sub declarations
List declarations = classSpecifier.getDeclarations(); List declarations = classSpecifier.getDeclarations();
Iterator j = declarations.iterator(); Iterator j = declarations.iterator();
while (j.hasNext()){ while (j.hasNext()) {
Declaration subDeclaration = (Declaration)j.next(); Declaration subDeclaration = (Declaration) j.next();
generateModelElements(subDeclaration); generateModelElements(subDeclaration);
} // end while j } // end while j
} }
List declarators = simpleDeclaration.getDeclarators(); List declarators = simpleDeclaration.getDeclarators();
Iterator d = declarators.iterator(); Iterator d = declarators.iterator();
while (d.hasNext()){ while (d.hasNext()) {
Declarator declarator = (Declarator)d.next(); Declarator declarator = (Declarator) d.next();
createTemplateElement(templateDeclaration, simpleDeclaration, declarator); createTemplateElement(templateDeclaration, simpleDeclaration, declarator);
} // end while d } // end while d
}// end Template Declaration } // end Template Declaration
} }
private void createElement(SimpleDeclaration simpleDeclaration, Declarator declarator){ private void createElement(SimpleDeclaration simpleDeclaration, Declarator declarator) {
// typedef // typedef
if (simpleDeclaration.getDeclSpecifier().isTypedef()){ if (simpleDeclaration.getDeclSpecifier().isTypedef()) {
createTypeDef(declarator, simpleDeclaration); createTypeDef(declarator, simpleDeclaration);
} else { } else {
ParameterDeclarationClause pdc = declarator.getParms(); ParameterDeclarationClause pdc = declarator.getParms();
if (pdc == null){ if (pdc == null) {
createVariableSpecification(simpleDeclaration, declarator); createVariableSpecification(simpleDeclaration, declarator);
} } else {
else{
createFunctionSpecification(simpleDeclaration, declarator, pdc, false); createFunctionSpecification(simpleDeclaration, declarator, pdc, false);
} }
} }
} }
private void createTemplateElement(TemplateDeclaration templateDeclaration, SimpleDeclaration simpleDeclaration, Declarator declarator){ private void createTemplateElement(
TemplateDeclaration templateDeclaration,
SimpleDeclaration simpleDeclaration,
Declarator declarator) {
// TODO: no template in the old parser // TODO: no template in the old parser
ParameterDeclarationClause pdc = declarator.getParms(); ParameterDeclarationClause pdc = declarator.getParms();
if (pdc != null){ if (pdc != null) {
createFunctionSpecification(simpleDeclaration, declarator, pdc, true); createFunctionSpecification(simpleDeclaration, declarator, pdc, true);
} }
} }
private void createInclusion(Inclusion inclusion){ private void createInclusion(Inclusion inclusion) {
callback.includeDecl(inclusion.getName(), inclusion.getStartingOffset(), inclusion.getTotalLength(),0, 0); callback.includeDecl(inclusion.getName(), inclusion.getStartingOffset(), inclusion.getTotalLength(), 0, 0);
} }
private void createMacro(Macro macro){ private void createMacro(Macro macro) {
callback.defineDecl(macro.getName(), macro.getStartingOffset(), macro.getTotalLength(), 0, 0); callback.defineDecl(macro.getName(), macro.getStartingOffset(), macro.getTotalLength(), 0, 0);
} }
private void createNamespace(NamespaceDefinition nsDef){ private void createNamespace(NamespaceDefinition nsDef) {
// TODO: the old parser callback does not know about namespace. // TODO: the old parser callback does not know about namespace.
} }
private void createEnumeration(EnumerationSpecifier enumSpecifier){ private void createEnumeration(EnumerationSpecifier enumSpecifier) {
callback.structDeclBegin(enumSpecifier.getName().toString(), ICElement.C_ENUMERATION, callback.structDeclBegin(
enumSpecifier.getName().getStartOffset(), enumSpecifier.getName().length(), enumSpecifier.getName().toString(),
enumSpecifier.getStartingOffset(), 0, 0); ICElement.C_ENUMERATION,
enumSpecifier.getName().getStartOffset(),
enumSpecifier.getName().length(),
enumSpecifier.getStartingOffset(),
0,
0);
callback.structDeclEnd(enumSpecifier.getTotalLength(), 0); callback.structDeclEnd(enumSpecifier.getTotalLength(), 0);
} }
private void createClass(SimpleDeclaration simpleDeclaration, ClassSpecifier classSpecifier, boolean isTemplate){ private void createClass(SimpleDeclaration simpleDeclaration, ClassSpecifier classSpecifier, boolean isTemplate) {
// create element // create element
String className = (classSpecifier.getName() == null ) ? "" : classSpecifier.getName().toString(); String className = (classSpecifier.getName() == null) ? "" : classSpecifier.getName().toString();
int kind; int kind;
switch( classSpecifier.getClassKey() ) switch (classSpecifier.getClassKey()) {
{ case ClassKey.t_class :
case ClassKey.t_class:
kind = ICElement.C_CLASS; kind = ICElement.C_CLASS;
break; break;
case ClassKey.t_struct: case ClassKey.t_struct :
kind = ICElement.C_STRUCT; kind = ICElement.C_STRUCT;
break; break;
default: default :
kind = ICElement.C_UNION; kind = ICElement.C_UNION;
break; break;
} }
// set element position // set element position
if( classSpecifier.getName() != null ) if (classSpecifier.getName() != null) {
{ callback.structDeclBegin(
callback.structDeclBegin(className, kind, className,
classSpecifier.getName().getStartOffset(), classSpecifier.getName().length(), kind,
classSpecifier.getStartingOffset(), 0, 0); classSpecifier.getName().getStartOffset(),
} classSpecifier.getName().length(),
else classSpecifier.getStartingOffset(),
{ 0,
callback.structDeclBegin(className, kind, 0);
classSpecifier.getClassKeyToken().getOffset(), classSpecifier.getClassKeyToken().getLength(), } else {
classSpecifier.getStartingOffset(), 0, 0); callback.structDeclBegin(
className,
kind,
classSpecifier.getClassKeyToken().getOffset(),
classSpecifier.getClassKeyToken().getLength(),
classSpecifier.getStartingOffset(),
0,
0);
} }
callback.structDeclBegin(className, kind, callback.structDeclBegin(
classSpecifier.getClassKeyToken().getOffset(), classSpecifier.getClassKeyToken().getLength(), className,
classSpecifier.getStartingOffset(), 0, 0); kind,
classSpecifier.getClassKeyToken().getOffset(),
classSpecifier.getClassKeyToken().getLength(),
classSpecifier.getStartingOffset(),
0,
0);
callback.structDeclEnd(classSpecifier.getTotalLength(), 0); callback.structDeclEnd(classSpecifier.getTotalLength(), 0);
} }
private void createTypeDef(Declarator declarator, SimpleDeclaration simpleDeclaration){ private void createTypeDef(Declarator declarator, SimpleDeclaration simpleDeclaration) {
// TODO:No typedef in the old callback // TODO:No typedef in the old callback
String declaratorName = declarator.getName().toString(); Name domName = (declarator.getDeclarator() != null) ? declarator.getDeclarator().getName() : declarator.getName();
callback.fieldDecl(declaratorName, String declaratorName = domName.toString();
declarator.getName().getStartOffset(), declarator.getName().length(), callback.fieldDecl(
simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength(), declaratorName,
0, 0, 0); domName.getStartOffset(),
domName.length(),
simpleDeclaration.getStartingOffset(),
simpleDeclaration.getTotalLength(),
0,
0,
0);
} }
private void createVariableSpecification(SimpleDeclaration simpleDeclaration, Declarator declarator){ private void createVariableSpecification(SimpleDeclaration simpleDeclaration, Declarator declarator) {
String declaratorName = declarator.getName().toString(); Name domName = (declarator.getDeclarator() != null) ? declarator.getDeclarator().getName() : declarator.getName();
callback.fieldDecl(declaratorName, declarator.getName().getStartOffset(), declarator.getName().length(), String declaratorName = domName.toString();
simpleDeclaration.getStartingOffset(), simpleDeclaration.getTotalLength(), 0, 0, 0); callback.fieldDecl(
declaratorName,
domName.getStartOffset(),
domName.length(),
simpleDeclaration.getStartingOffset(),
simpleDeclaration.getTotalLength(),
0,
0,
0);
} }
private void createFunctionSpecification(SimpleDeclaration simpleDeclaration, Declarator declarator, ParameterDeclarationClause pdc, boolean isTemplate){ private void createFunctionSpecification(
String declaratorName = declarator.getName().toString(); SimpleDeclaration simpleDeclaration,
callback.functionDeclBegin(declaratorName, Declarator declarator,
declarator.getName().getStartOffset(), declarator.getName().length(), ParameterDeclarationClause pdc,
simpleDeclaration.getStartingOffset(), 0, 0, 0); boolean isTemplate) {
Name domName = (declarator.getDeclarator() != null) ? declarator.getDeclarator().getName() : declarator.getName();
String declaratorName = domName.toString();
callback.functionDeclBegin(
declaratorName,
domName.getStartOffset(),
domName.length(),
simpleDeclaration.getStartingOffset(),
0,
0,
0);
callback.functionDeclEnd(simpleDeclaration.getTotalLength(), 0, simpleDeclaration.isFunctionDefinition()); callback.functionDeclEnd(simpleDeclaration.getTotalLength(), 0, simpleDeclaration.isFunctionDefinition());
} }
} }