ctf: preserve byte order if explicitly set in a typedef
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 1 Dec 2015 17:06:33 +0000 (12:06 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 2 Dec 2015 21:33:12 +0000 (16:33 -0500)
Change-Id: I904cd931da955f17b7cafa285f48d60c0c0358a1
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/61684
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Reviewed-by: Hudson CI
ctf/org.eclipse.tracecompass.ctf.core.tests/src/org/eclipse/tracecompass/ctf/core/tests/trace/MetadataTest.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/EnumDeclaration.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/FloatDeclaration.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/ISimpleDatatypeDeclaration.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/event/types/IntegerDeclaration.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/event/metadata/IOStructGen.java

index b1e16219b37f39d44a01c7ab420d9f5f99abf8d0..b6005352bebb74c47701d2701e7e724d7c923af2 100644 (file)
@@ -21,8 +21,11 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.UUID;
 
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.ctf.core.CTFException;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.types.ISimpleDatatypeDeclaration;
 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTraceUtils;
 import org.eclipse.tracecompass.ctf.core.trace.CTFStream;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
@@ -31,6 +34,8 @@ import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace;
 import org.junit.Before;
 import org.junit.Test;
 
+import com.google.common.collect.Iterables;
+
 /**
  * The class <code>MetadataTest</code> contains tests for the class
  * <code>{@link Metadata}</code>.
@@ -162,6 +167,62 @@ public class MetadataTest {
             "        };\n" +
             "    };";
 
+    private static final String ENDIAN_CHANGE_L_B =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = le; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+    private static final String ENDIAN_CHANGE_L_N =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = le; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+    private static final String ENDIAN_CHANGE_L_L =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16; byte_order = le; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = le; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+
+    private static final String ENDIAN_CHANGE_B_L =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16;  byte_order = le; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = be; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+    private static final String ENDIAN_CHANGE_B_N =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = be; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+    private static final String ENDIAN_CHANGE_B_B =
+            "/* ctf 1.8 */"
+            + "typealias integer { size = 32; align = 16; byte_order = be; signed = true; base = dec; } := INT;"
+            + "trace { byte_order = be; };"
+            + "event { "
+            + " name = \"bob\"; "
+            + " fields := struct field { INT data ; };"
+            + "};";
+
+
     private Metadata fixture;
 
     /**
@@ -237,6 +298,42 @@ public class MetadataTest {
         assertNotNull(result);
     }
 
+    /**
+     * Test a changing endian event field
+     *
+     * @throws CTFException
+     *             won't happen
+     */
+    @Test
+    public void testEndian() throws CTFException {
+        testEndianess(ENDIAN_CHANGE_L_B, ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN);
+        testEndianess(ENDIAN_CHANGE_L_N, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+        testEndianess(ENDIAN_CHANGE_L_L, ByteOrder.LITTLE_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+        testEndianess(ENDIAN_CHANGE_B_L, ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN);
+        testEndianess(ENDIAN_CHANGE_B_N, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
+        testEndianess(ENDIAN_CHANGE_B_B, ByteOrder.BIG_ENDIAN, ByteOrder.BIG_ENDIAN);
+    }
+
+    private void testEndianess(String tsdl, ByteOrder traceEndian, ByteOrder fieldEndian) throws CTFException {
+        fixture = new Metadata();
+        CTFTrace trace = fixture.getTrace();
+        fixture.parseText(tsdl);
+        assertEquals(traceEndian, trace.getByteOrder());
+        final Iterable<IEventDeclaration> eventDeclarations = trace.getEventDeclarations(0L);
+        assertNotNull(eventDeclarations);
+        IEventDeclaration event = Iterables.getFirst(eventDeclarations, null);
+        assertNotNull(event);
+        assertNotNull(event.getFields());
+        final @Nullable IDeclaration field = event.getFields().getField("data");
+        assertNotNull(field);
+        if (field instanceof ISimpleDatatypeDeclaration) {
+            ISimpleDatatypeDeclaration declaration = (ISimpleDatatypeDeclaration) field;
+            assertEquals(fieldEndian, declaration.getByteOrder());
+        } else {
+            fail("data is not the right type");
+        }
+    }
+
     /**
      * Run the void parse() method test.
      *
index f79961e077d722b8766b54161568809a617a110c..ffa0b8bf84822bf081c3265cd8252b2f94d2bbdb 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.ctf.core.event.types;
 
+import java.nio.ByteOrder;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -114,6 +115,22 @@ public final class EnumDeclaration extends Declaration implements ISimpleDatatyp
         return fContainerType.getMaximumSize();
     }
 
+    /**
+     * @since 2.0
+     */
+    @Override
+    public boolean isByteOrderSet() {
+        return fContainerType.isByteOrderSet();
+    }
+
+    /**
+     * @since 2.0
+     */
+    @Override
+    public ByteOrder getByteOrder() {
+        return fContainerType.getByteOrder();
+    }
+
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
index af56d7672821925b13eae48d74733de1bd8f7b5a..3defc7a4d5d415bbd5f1b090ed755227a9c35ae4 100644 (file)
@@ -36,6 +36,7 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty
 
     private final int fMantissa;
     private final int fExponent;
+    private final boolean fIsByteOrderSet;
     private final ByteOrder fByteOrder;
     private final long fAlignement;
 
@@ -59,6 +60,7 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty
             long alignment) {
         fMantissa = mantissa;
         fExponent = exponent;
+        fIsByteOrderSet = byteOrder != null;
         fByteOrder = (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder;
         fAlignement = Math.max(alignment, 1);
 
@@ -69,22 +71,28 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty
     // ------------------------------------------------------------------------
 
     /**
-     * @return the mant
+     * @return the mantissa
      */
     public int getMantissa() {
         return fMantissa;
     }
 
     /**
-     * @return the exp
+     * @return the exponent
      */
     public int getExponent() {
         return fExponent;
     }
 
     /**
-     * @return the byteOrder
+     * @since 2.0
      */
+    @Override
+    public boolean isByteOrderSet() {
+        return fIsByteOrderSet;
+    }
+
+    @Override
     public ByteOrder getByteOrder() {
         return fByteOrder;
     }
@@ -177,7 +185,11 @@ public final class FloatDeclaration extends Declaration implements ISimpleDataty
         final int prime = 31;
         int result = 1;
         result = prime * result + (int) (fAlignement ^ (fAlignement >>> 32));
-        result = prime * result + fByteOrder.toString().hashCode(); // don't evaluate object but string
+        result = prime * result + fByteOrder.toString().hashCode(); // don't
+                                                                    // evaluate
+                                                                    // object
+                                                                    // but
+                                                                    // string
         result = prime * result + fExponent;
         result = prime * result + fMantissa;
         return result;
index f8f93fe9f62376989de514701810d0faa70d9747..30730af2d07fdf5de757ac131c1c2c2fa807849f 100644 (file)
@@ -12,6 +12,8 @@
 
 package org.eclipse.tracecompass.ctf.core.event.types;
 
+import java.nio.ByteOrder;
+
 /**
  * Common interface for simple CTF data types (which do not contain sub-fields).
  *
@@ -19,4 +21,21 @@ package org.eclipse.tracecompass.ctf.core.event.types;
  */
 public interface ISimpleDatatypeDeclaration {
 
+    /**
+     * Is the byte order set
+     *
+     * @return If the byte order was set
+     * @since 2.0
+     */
+    public boolean isByteOrderSet();
+
+    /**
+     * Get the byte order
+     *
+     * @return the byte order, or @link {@link ByteOrder#nativeOrder()} if not
+     *         set
+     * @since 2.0
+     */
+    public ByteOrder getByteOrder();
+
 }
index df33768aa822f2c3cc7c8bbe600d935667064fdb..84e8c45211ad8794d1d7b70a36872a0b86cb6406 100644 (file)
@@ -120,6 +120,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
     private final int fLength;
     private final boolean fSigned;
     private final int fBase;
+    private final boolean fIsByteOrderSet;
     private final ByteOrder fByteOrder;
     private final Encoding fEncoding;
     private final long fAlignment;
@@ -243,6 +244,7 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
         fLength = len;
         fSigned = signed;
         fBase = base;
+        fIsByteOrderSet = byteOrder != null;
         fByteOrder = (byteOrder == null) ? ByteOrder.nativeOrder() : byteOrder;
         fEncoding = encoding;
         fClock = clock;
@@ -276,10 +278,14 @@ public final class IntegerDeclaration extends Declaration implements ISimpleData
     }
 
     /**
-     * Get the byte order
-     *
-     * @return the byte order
+     * @since 2.0
      */
+    @Override
+    public boolean isByteOrderSet() {
+        return fIsByteOrderSet;
+    }
+
+    @Override
     public ByteOrder getByteOrder() {
         return fByteOrder;
     }
index 1076181b9453f8fe3fcd30a23b5be5d1086bcd2d..e94d57ffcbd9ccf38d878d875c3ad462a79f49d0 100644 (file)
@@ -443,7 +443,7 @@ public class IOStructGen {
             final DeclarationScope parentScope, String name,
             IntegerDeclaration decl) throws ParseException {
 
-        if (decl.getByteOrder() != byteOrder) {
+        if (!decl.isByteOrderSet()) {
             IntegerDeclaration newI;
             newI = IntegerDeclaration.createDeclaration(decl.getLength(), decl.isSigned(),
                     decl.getBase(), byteOrder, decl.getEncoding(),
@@ -454,7 +454,7 @@ public class IOStructGen {
 
     private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, EnumDeclaration decl) throws ParseException {
         final IntegerDeclaration containerType = decl.getContainerType();
-        if (containerType.getByteOrder() != byteOrder) {
+        if (!decl.isByteOrderSet()) {
             EnumDeclaration newEnum = new EnumDeclaration(IntegerDeclaration.createDeclaration(containerType.getLength(), containerType.isSigned(),
                     containerType.getBase(), byteOrder, containerType.getEncoding(),
                     containerType.getClock(), containerType.getAlignment()));
@@ -467,7 +467,7 @@ public class IOStructGen {
     }
 
     private static void addByteOrder(ByteOrder byteOrder, DeclarationScope parentScope, String name, FloatDeclaration decl) throws ParseException {
-        if (decl.getByteOrder() != byteOrder) {
+        if (!decl.isByteOrderSet()) {
             FloatDeclaration newFloat = new FloatDeclaration(decl.getExponent(), decl.getMantissa(), byteOrder, decl.getAlignment());
             parentScope.replaceType(name, newFloat);
         }
This page took 0.031625 seconds and 5 git commands to generate.