Skip to content

Commit 5d3dd22

Browse files
ctf: add static-length-array
Change-Id: Ic10d79e4e72a043e9116d554f5dffacd34013fa6 Signed-off-by: Matthew Khouzam <[email protected]>
1 parent f7786ce commit 5d3dd22

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.dynamicstring.DynamicLengthStringParser;
2828
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.enumeration.EnumParser;
2929
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.integer.IntegerDeclarationParser;
30+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.staticarray.StaticLengthArrayParser;
3031
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.staticstring.StaticLengthStringParser;
3132
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.string.StringDeclarationParser;
3233
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.variant.VariantParser;
@@ -165,6 +166,8 @@ public IDeclaration parse(ICTFMetadataNode typealias, ICommonTreeParserParameter
165166
targetDeclaration = DynamicLengthStringParser.INSTANCE.parse(typealias, new DynamicLengthStringParser.Param(trace));
166167
} else if (JsonMetadataStrings.STATIC_LENGTH_STRING.equals(type)) {
167168
targetDeclaration = StaticLengthStringParser.INSTANCE.parse(typealias, new StaticLengthStringParser.Param(trace));
169+
} else if (JsonMetadataStrings.STATIC_LENGTH_ARRAY.equals(type)) {
170+
targetDeclaration = StaticLengthArrayParser.INSTANCE.parse(typealias, new StaticLengthArrayParser.Param(trace, scope));
168171
} else {
169172
throw new ParseException("Invalid field class: " + type); //$NON-NLS-1$
170173
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Ericsson
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.staticarray;
12+
13+
import org.eclipse.jdt.annotation.NonNullByDefault;
14+
import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
15+
import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
16+
import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
17+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
18+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.JsonStructureFieldMemberMetadataNode;
19+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
20+
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
21+
import org.eclipse.tracecompass.internal.ctf.core.event.types.ArrayDeclaration;
22+
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
23+
import org.eclipse.tracecompass.internal.ctf.core.utils.JsonMetadataStrings;
24+
25+
import com.google.gson.JsonElement;
26+
import com.google.gson.JsonObject;
27+
28+
/**
29+
* A static-length array field class describes static-length array fields.
30+
*
31+
* @author Matthew Khouzam
32+
*/
33+
public final class StaticLengthArrayParser extends AbstractScopedCommonTreeParser {
34+
35+
/**
36+
* Instance
37+
*/
38+
public static final StaticLengthArrayParser INSTANCE = new StaticLengthArrayParser();
39+
40+
private StaticLengthArrayParser() {
41+
}
42+
43+
@Override
44+
public IDeclaration parse(ICTFMetadataNode node, ICommonTreeParserParameter param) throws ParseException {
45+
if (!(node instanceof JsonStructureFieldMemberMetadataNode)) {
46+
throw new ParseException("Static-length array only supported in JSON metadata"); //$NON-NLS-1$
47+
}
48+
if (!(param instanceof Param)) {
49+
throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
50+
}
51+
52+
JsonStructureFieldMemberMetadataNode member = (JsonStructureFieldMemberMetadataNode) node;
53+
JsonObject fieldClass = member.getFieldClass().getAsJsonObject();
54+
55+
JsonElement lengthField = fieldClass.get(JsonMetadataStrings.LENGTH);
56+
if (lengthField == null) {
57+
throw new ParseException("Static-length array requires length property"); //$NON-NLS-1$
58+
}
59+
60+
JsonElement elementFieldClass = fieldClass.get(JsonMetadataStrings.ELEMENT_FIELD_CLASS);
61+
if (elementFieldClass == null) {
62+
throw new ParseException("Static-length array requires element-field-class property"); //$NON-NLS-1$
63+
}
64+
65+
int length = lengthField.getAsInt();
66+
CTFTrace trace = ((Param) param).getTrace();
67+
DeclarationScope scope = ((Param) param).getScope();
68+
69+
JsonStructureFieldMemberMetadataNode elementNode = new JsonStructureFieldMemberMetadataNode(node, "", "", "", elementFieldClass.getAsJsonObject()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
70+
IDeclaration elementDeclaration = TypeAliasParser.INSTANCE.parse(elementNode, new TypeAliasParser.Param(trace, scope));
71+
72+
return new ArrayDeclaration(length, elementDeclaration);
73+
}
74+
75+
/**
76+
* Parameters for the static-length array parser
77+
*/
78+
@NonNullByDefault
79+
public static final class Param implements ICommonTreeParserParameter {
80+
private final CTFTrace fTrace;
81+
private final DeclarationScope fScope;
82+
83+
/**
84+
* Parameter constructor
85+
*
86+
* @param trace the trace
87+
* @param scope the declaration scope
88+
*/
89+
public Param(CTFTrace trace, DeclarationScope scope) {
90+
fTrace = trace;
91+
fScope = scope;
92+
}
93+
94+
/**
95+
* Get the trace
96+
*
97+
* @return the trace
98+
*/
99+
public CTFTrace getTrace() {
100+
return fTrace;
101+
}
102+
103+
/**
104+
* Get the scope
105+
*
106+
* @return the scope
107+
*/
108+
public DeclarationScope getScope() {
109+
return fScope;
110+
}
111+
}
112+
}

ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/utils/JsonMetadataStrings.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ private JsonMetadataStrings() {
201201
*/
202202
public static final String STRUCTURE = "structure"; //$NON-NLS-1$
203203

204+
/**
205+
* Encodings map
206+
*/
204207
public static final Map<String, Charset> ENCODINGS = Map.of("utf-8",StandardCharsets.UTF_8, //$NON-NLS-1$
205208
"utf-16be",StandardCharsets.UTF_16BE,"utf-16le",StandardCharsets.UTF_16LE, //$NON-NLS-1$ //$NON-NLS-2$
206209
"utf-32be", Charset.forName("UTF-32BE"),"utf-32le", Charset.forName("UTF-32LE")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
@@ -214,4 +217,14 @@ private JsonMetadataStrings() {
214217
* Static length string
215218
*/
216219
public static final String STATIC_LENGTH_STRING = "static-length-string"; //$NON-NLS-1$
220+
221+
/**
222+
* Static length array
223+
*/
224+
public static final String STATIC_LENGTH_ARRAY = "static-length-array"; //$NON-NLS-1$
225+
226+
/**
227+
* Element field class
228+
*/
229+
public static final String ELEMENT_FIELD_CLASS = "element-field-class"; //$NON-NLS-1$
217230
}

0 commit comments

Comments
 (0)