1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 19:25:38 +02:00

clean up GDBTypeParser

Reduce function complexity and comply with java coding styles.
Convert some larger ifs to switches for performance sake.

Change-Id: Ief2672141511a1373cdd8a98e0fa2eeb564816e1
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
This commit is contained in:
Matthew Khouzam 2014-12-10 10:38:33 -05:00
parent 74106910dd
commit bb8cc948c4

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2010, 2014 Wind River Systems and others. * Copyright (c) 2010, 2015 Wind River Systems 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
@ -9,6 +9,7 @@
* Wind River Systems - initial implementation * Wind River Systems - initial implementation
* Anders Dahlberg (Ericsson) - Need additional API to extend support for memory spaces (Bug 431627) * Anders Dahlberg (Ericsson) - Need additional API to extend support for memory spaces (Bug 431627)
* Alvaro Sanchez-Leon (Ericsson) - Need additional API to extend support for memory spaces (Bug 431627) * Alvaro Sanchez-Leon (Ericsson) - Need additional API to extend support for memory spaces (Bug 431627)
* Matthew Khouzam (Ericsson) - Minor code cleanup and privatization of variables
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.dsf.gdb; package org.eclipse.cdt.dsf.gdb;
@ -33,19 +34,19 @@ public class GDBTypeParser {
// name: ([a-zA-z][0-9])+ // name: ([a-zA-z][0-9])+
// integer ([0-9)+ // integer ([0-9)+
final static int EOF = -1; private static final int EOF = -1;
final static int NAME = 0; private static final int NAME = 0;
final static int PARENS = 1; private static final int PARENS = 1;
final static int BRACKETS = 2; private static final int BRACKETS = 2;
String line; private String line;
int index; private int index;
int tokenType; private int tokenType;
String token; private String token;
String dataType; private String dataType;
String name; private String name;
GDBDerivedType gdbDerivedType; private GDBDerivedType gdbDerivedType;
GDBType genericType; private GDBType genericType;
public GDBType getGDBType() { public GDBType getGDBType() {
if (gdbDerivedType != null) { if (gdbDerivedType != null) {
@ -58,11 +59,10 @@ public class GDBTypeParser {
return name; return name;
} }
public GDBType parse(String s) { public GDBType parse(String gdbTypeString) {
// Sanity. // Sanity.
if (s == null) { String s = (gdbTypeString == null) ? "" : gdbTypeString; //$NON-NLS-1$
s = new String();
}
s = Pattern.compile("\\bconst\\b").matcher(s).replaceAll(""); //$NON-NLS-1$//$NON-NLS-2$ s = Pattern.compile("\\bconst\\b").matcher(s).replaceAll(""); //$NON-NLS-1$//$NON-NLS-2$
s = Pattern.compile("\\bvolatile\\b").matcher(s).replaceAll(""); //$NON-NLS-1$//$NON-NLS-2$ s = Pattern.compile("\\bvolatile\\b").matcher(s).replaceAll(""); //$NON-NLS-1$//$NON-NLS-2$
s = s.trim(); s = s.trim();
@ -102,56 +102,72 @@ public class GDBTypeParser {
return getGDBType(); return getGDBType();
} }
public static String unParse (GDBType gdbType) { public static String unParse (GDBType gdbParentType) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
GDBType gdbType = gdbParentType;
// Fetch the datatype. // Fetch the datatype.
while (gdbType != null) { while (gdbType != null) {
GDBDerivedType derived = null;
int type = gdbType.getType();
if (gdbType instanceof GDBDerivedType) { if (gdbType instanceof GDBDerivedType) {
derived = (GDBDerivedType)gdbType; GDBDerivedType derived = (GDBDerivedType)gdbType;
int type = derived.getType();
gdbType = derived.getChild(); gdbType = derived.getChild();
// respect the precedence of operators. switch (type) {
if (type == GDBType.FUNCTION) { case GDBType.FUNCTION:
sb.append("()"); //$NON-NLS-1$ sb.append("()"); //$NON-NLS-1$
} else if (type == GDBType.ARRAY) { break;
case GDBType.ARRAY:
sb.append('[').append(derived.getDimension()).append(']'); sb.append('[').append(derived.getDimension()).append(']');
} else if (type == GDBType.POINTER) { break;
int childType = (gdbType != null) ? gdbType.getType() : GDBType.GENERIC; case GDBType.POINTER:
if (childType == GDBType.POINTER || childType == GDBType.REFERENCE) { handlePointer(gdbType, sb);
sb.append('*'); break;
} else if (childType == GDBType.GENERIC) { case GDBType.REFERENCE:
sb.insert(0, '*'); handleReference(gdbType, sb);
} else { break;
sb.insert(0, "(*").append(')'); //$NON-NLS-1$
}
} else if (type == GDBType.REFERENCE) {
int childType = (gdbType != null) ? gdbType.getType() : GDBType.GENERIC;
if (childType == GDBType.POINTER || childType == GDBType.REFERENCE) {
sb.append("&"); //$NON-NLS-1$
} else if (childType == GDBType.GENERIC) {
sb.insert(0, '&');
} else {
sb.insert(0, "(&").append(')'); //$NON-NLS-1$
}
} }
} else { } else {
sb.insert(0, ' '); sb.insert(0, ' ');
sb.insert(0, gdbType.getTypeName()); sb.insert(0, gdbType.getTypeName());
gdbType = null; break;
} }
} }
return sb.toString().trim(); return sb.toString().trim();
} }
private static void handleReference(GDBType gdbType, StringBuffer sb) {
handleReferenceOrPointer(gdbType, sb, '&');
}
private static void handlePointer(GDBType gdbType, StringBuffer sb) {
handleReferenceOrPointer(gdbType, sb, '*');
}
private static void handleReferenceOrPointer(GDBType gdbType, StringBuffer sb, char prefix) {
switch (getChildType(gdbType)) {
case GDBType.POINTER:
case GDBType.REFERENCE:
sb.append(prefix);
break;
case GDBType.GENERIC:
sb.insert(0, prefix);
break;
default:
sb.insert(0, "("+prefix).append(')'); //$NON-NLS-1$
break;
}
}
private static int getChildType(GDBType gdbType) {
return (gdbType != null) ? gdbType.getType() : GDBType.GENERIC;
}
public class GDBType { public class GDBType {
public final static int GENERIC = 0; public static final int GENERIC = 0;
public final static int POINTER = 1; public static final int POINTER = 1;
public final static int REFERENCE = 2; public static final int REFERENCE = 2;
public final static int ARRAY = 3; public static final int ARRAY = 3;
public final static int FUNCTION = 4; public static final int FUNCTION = 4;
String nameType; String nameType;
int type; int type;