Skip to content

Commit 1b08688

Browse files
ctf: make CTF1 parsing more rigid
Do not accept double typealiases with the same value. Change-Id: I2affb15f6faf83cd0292a7310febbad8ae657513 Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
1 parent e687b36 commit 1b08688

3 files changed

Lines changed: 53 additions & 25 deletions

File tree

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/metadata/DeclarationScope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public void registerType(String name, IDeclaration declaration)
178178
}
179179
IDeclaration originalDeclaration = fTypes.get(name);
180180
if (originalDeclaration != null && !Objects.equals(declaration, originalDeclaration)) {
181-
throw new ParseException("Type has already been defined:" + name); //$NON-NLS-1$
181+
throw new ParseException("Type has already been defined: " + name); //$NON-NLS-1$
182182
}
183183

184184
/* Add it to the register. */

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/TypeAliasParser.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,50 +141,64 @@ public IDeclaration parse(ICTFMetadataNode typealias, ICommonTreeParserParameter
141141
}
142142
}
143143
if (fieldClass != null) {
144-
if (type.isEmpty()) {
145-
if (fieldClass.isJsonObject()) {
146-
type = fieldClass.get(JsonMetadataStrings.TYPE).getAsString();
147-
}
144+
if (type.isEmpty() && fieldClass.isJsonObject()) {
145+
type = fieldClass.get(JsonMetadataStrings.TYPE).getAsString();
148146
}
149-
if (JsonMetadataStrings.FIXED_UNSIGNED_INTEGER_FIELD.equals(type)) {
147+
switch (type) {
148+
case JsonMetadataStrings.FIXED_UNSIGNED_INTEGER_FIELD:
150149
fieldClass.addProperty(SIGNED, false);
151150
fieldClass.addProperty(VARINT, false);
152151
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
153-
} else if (JsonMetadataStrings.FIXED_SIGNED_INTEGER_FIELD.equals(type)) {
152+
break;
153+
case JsonMetadataStrings.FIXED_SIGNED_INTEGER_FIELD:
154154
fieldClass.addProperty(SIGNED, true);
155155
fieldClass.addProperty(VARINT, false);
156156
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
157-
} else if (JsonMetadataStrings.VARIABLE_UNSIGNED_INTEGER_FIELD.equals(type)) {
157+
break;
158+
case JsonMetadataStrings.VARIABLE_UNSIGNED_INTEGER_FIELD:
158159
fieldClass.addProperty(SIGNED, false);
159160
fieldClass.addProperty(VARINT, true);
160161
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
161-
} else if (JsonMetadataStrings.VARIABLE_SIGNED_INTEGER_FIELD.equals(type)) {
162+
break;
163+
case JsonMetadataStrings.VARIABLE_SIGNED_INTEGER_FIELD:
162164
fieldClass.addProperty(SIGNED, true);
163165
fieldClass.addProperty(VARINT, true);
164166
targetDeclaration = IntegerDeclarationParser.INSTANCE.parse(typealias, new IntegerDeclarationParser.Param(trace));
165-
} else if (JsonMetadataStrings.STATIC_LENGTH_BLOB.equals(type)) {
167+
break;
168+
case JsonMetadataStrings.STATIC_LENGTH_BLOB:
166169
targetDeclaration = BlobDeclarationParser.INSTANCE.parse(typealias, null);
167-
} else if (JsonMetadataStrings.NULL_TERMINATED_STRING.equals(type)) {
170+
break;
171+
case JsonMetadataStrings.NULL_TERMINATED_STRING:
168172
targetDeclaration = StringDeclarationParser.INSTANCE.parse(typealias, null);
169-
} else if (JsonMetadataStrings.VARIANT.equals(type)) {
173+
break;
174+
case JsonMetadataStrings.VARIANT:
170175
targetDeclaration = VariantParser.INSTANCE.parse(typealias, new VariantParser.Param(trace, scope));
171-
} else if (JsonMetadataStrings.FIXED_UNSIGNED_ENUMERATION.equals(type)) {
176+
break;
177+
case JsonMetadataStrings.FIXED_UNSIGNED_ENUMERATION:
172178
targetDeclaration = EnumParser.INSTANCE.parse(typealias, new EnumParser.Param(trace, scope));
173-
} else if (JsonMetadataStrings.DYNAMIC_LENGTH_STRING.equals(type)) {
179+
break;
180+
case JsonMetadataStrings.DYNAMIC_LENGTH_STRING:
174181
targetDeclaration = DynamicLengthStringParser.INSTANCE.parse(typealias, new DynamicLengthStringParser.Param(trace));
175-
} else if (JsonMetadataStrings.STATIC_LENGTH_STRING.equals(type)) {
182+
break;
183+
case JsonMetadataStrings.STATIC_LENGTH_STRING:
176184
targetDeclaration = StaticLengthStringParser.INSTANCE.parse(typealias, new StaticLengthStringParser.Param(trace));
177-
} else if (JsonMetadataStrings.STATIC_LENGTH_ARRAY.equals(type)) {
185+
break;
186+
case JsonMetadataStrings.STATIC_LENGTH_ARRAY:
178187
targetDeclaration = StaticLengthArrayParser.INSTANCE.parse(typealias, new StaticLengthArrayParser.Param(trace, scope));
179-
} else if (JsonMetadataStrings.DYNAMIC_LENGTH_ARRAY.equals(type)) {
188+
break;
189+
case JsonMetadataStrings.DYNAMIC_LENGTH_ARRAY:
180190
targetDeclaration = DynamicLengthArrayParser.INSTANCE.parse(typealias, new DynamicLengthArrayParser.Param(trace, scope));
181-
} else if (JsonMetadataStrings.STRUCTURE.equals(type)) {
191+
break;
192+
case JsonMetadataStrings.STRUCTURE:
182193
targetDeclaration = StructParser.INSTANCE.parse(typealias, new StructParser.Param(trace, null, scope));
183-
} else if (JsonMetadataStrings.FIXED_LENGTH_FLOATING_POINT.equals(type)) {
194+
break;
195+
case JsonMetadataStrings.FIXED_LENGTH_FLOATING_POINT:
184196
targetDeclaration = FloatDeclarationParser.INSTANCE.parse(typealias, new FloatDeclarationParser.Param(trace));
185-
} else if (JsonMetadataStrings.VARIABLE_LENGTH_FLOATING_POINT.equals(type)) {
197+
break;
198+
case JsonMetadataStrings.VARIABLE_LENGTH_FLOATING_POINT:
186199
targetDeclaration = FloatDeclarationParser.INSTANCE.parse(typealias, new FloatDeclarationParser.Param(trace));
187-
} else {
200+
break;
201+
default:
188202
throw new ParseException("Invalid field class: " + type); //$NON-NLS-1$
189203
}
190204
} else {
@@ -210,6 +224,9 @@ public IDeclaration parse(ICTFMetadataNode typealias, ICommonTreeParserParameter
210224
}
211225

212226
aliasString = TypeAliasAliasParser.INSTANCE.parse(alias, null);
227+
if (scope.lookupType(aliasString)!= null) {
228+
throw new ParseException("Type has already been defined: " + aliasString); //$NON-NLS-1$
229+
}
213230
}
214231

215232
scope.registerType(aliasString, targetDeclaration);

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/tsdl/struct/StructDeclarationParser.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeDeclaratorParser;
2727
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
2828

29+
import com.google.common.base.Objects;
30+
2931
/**
3032
* Structures follow the ISO/C standard for structures
3133
*
@@ -92,8 +94,8 @@ public StructDeclaration parse(ICTFMetadataNode declaration, ICommonTreeParserPa
9294
StringBuilder identifierSB = new StringBuilder();
9395
IDeclaration decl = null;
9496
String fieldName = null;
95-
96-
if (declaration instanceof CTFAntlrMetadataNode) {
97+
boolean isCtf1 = declaration instanceof CTFAntlrMetadataNode;
98+
if (isCtf1) {
9799
/* Get the type specifier list node */
98100
ICTFMetadataNode typeSpecifierListNode = declaration.getFirstChildWithType(CTFParser.tokenNames[CTFParser.TYPE_SPECIFIER_LIST]);
99101

@@ -119,6 +121,11 @@ public StructDeclaration parse(ICTFMetadataNode declaration, ICommonTreeParserPa
119121
decl = TypeDeclaratorParser.INSTANCE.parse(typeDeclaratorNode, new TypeDeclaratorParser.Param(trace, typeSpecifierListNode, scope, identifierSB));
120122
}
121123
fieldName = identifierSB.toString();
124+
125+
if (struct.getField(fieldName) != null) {
126+
throw new ParseException("struct: duplicate field " //$NON-NLS-1$
127+
+ fieldName);
128+
}
122129
} else {
123130
decl = TypeAliasParser.INSTANCE.parse(declaration, new TypeAliasParser.Param(trace, scope));
124131
fieldName = ((JsonStructureFieldMemberMetadataNode) declaration).getName();
@@ -129,12 +136,16 @@ public StructDeclaration parse(ICTFMetadataNode declaration, ICommonTreeParserPa
129136

130137
scope.registerIdentifier(fieldName, decl);
131138
IDeclaration current = struct.getField(fieldName);
132-
if (decl != null && current != null && !decl.equals(current)) {
139+
if (decl == null) {
140+
throw new ParseException("struct: Cannot add null field " + fieldName); //$NON-NLS-1$
141+
}
142+
143+
if (current != null && !Objects.equal(decl, current)) {
133144
throw new ParseException("struct: duplicate field " //$NON-NLS-1$
134145
+ fieldName);
135146
}
136147

137-
if (fieldName != null && decl != null) {
148+
if (fieldName != null) {
138149
struct.addField(fieldName, decl);
139150
}
140151

0 commit comments

Comments
 (0)