/** \brief Function for selecting the correct switch value for a given * union object */ static c_value sd_unionDetermineSwitchValue( c_type switchType, c_object object) { c_value switchValue; switch (c_baseObject(switchType)->kind) { case M_PRIMITIVE: switch (c_primitive(switchType)->kind) { #define __CASE__(prim, type) case prim: switchValue = type##Value(*((type *)object)); break; __CASE__(P_ADDRESS,c_address) __CASE__(P_BOOLEAN,c_bool) __CASE__(P_CHAR,c_char) __CASE__(P_SHORT,c_short) __CASE__(P_USHORT,c_ushort) __CASE__(P_LONG,c_long) __CASE__(P_ULONG,c_ulong) __CASE__(P_LONGLONG,c_longlong) __CASE__(P_ULONGLONG,c_ulonglong) #undef __CASE__ default: switchValue = c_undefinedValue(); SD_CONFIDENCE(FALSE); break; } break; case M_ENUMERATION: switchValue = c_longValue(*(c_long *)object); break; default: switchValue = c_undefinedValue(); SD_CONFIDENCE(FALSE); break; } return switchValue; /* QAC EXPECT 5101; cyclomatic complexity is high because of switch statement */ }
static void c_deepwalkUnion( c_union v_union, c_object *objectPtr, c_deepwalkFunc action, void *actionArg) { c_type switchType; c_type caseType; c_unionCase deflt; c_unionCase activeCase; c_unionCase currentCase; c_object unionData; c_value switchValue; c_literal label; int i,j, nCases, nLabels; c_long dataOffset; /* action for the union itself */ /* No action, but separate actions for the switch and the data */ /* action(c_type(v_union), objectPtr, actionArg); */ /* action for the switch */ c_deepwalkType(v_union->switchType, objectPtr, action, actionArg); switchType = c_typeActualType(v_union->switchType); /* Determine value of the switch field */ switch (c_baseObject(switchType)->kind) { case M_PRIMITIVE: switch (c_primitive(switchType)->kind) { #define __CASE__(prim, type) \ case prim: switchValue = type##Value(*((type *)*objectPtr)); break; __CASE__(P_BOOLEAN,c_bool) __CASE__(P_CHAR,c_char) __CASE__(P_SHORT,c_short) __CASE__(P_USHORT,c_ushort) __CASE__(P_LONG,c_long) __CASE__(P_ULONG,c_ulong) __CASE__(P_LONGLONG,c_longlong) __CASE__(P_ULONGLONG,c_ulonglong) #undef __CASE__ default: switchValue = c_undefinedValue(); assert(FALSE); break; } break; case M_ENUMERATION: switchValue = c_longValue(*(c_long *)*objectPtr); break; default: switchValue = c_undefinedValue(); assert(FALSE); break; } /* Determine the label corresponding to this field */ activeCase = NULL; deflt = NULL; nCases = c_arraySize(v_union->cases); for (i=0; (i<nCases) && !activeCase; i++) { currentCase = c_unionCase(v_union->cases[i]); nLabels = c_arraySize(currentCase->labels); if (nLabels > 0) { for (j=0; (j<nLabels) && !activeCase; j++) { label = c_literal(currentCase->labels[j]); if (c_valueCompare(switchValue, label->value) == C_EQ) { activeCase = currentCase; } } } else { deflt = currentCase; } } if (!activeCase) { activeCase = deflt; } assert(activeCase); if (activeCase) { caseType = c_specifier(activeCase)->type; if (c_type(v_union)->alignment >= v_union->switchType->size) { dataOffset = c_type(v_union)->alignment; } else { dataOffset = v_union->switchType->size; } unionData = C_DISPLACE(*objectPtr, (c_address)dataOffset); c_deepwalkType(caseType, &unionData, action, actionArg); } }
/* * Check if each usage of a char array as a key has a corresponding * "#pragma cats" declaration. */ static c_bool idl_checkCatsUsage( c_base base, const char* filename) { char errorBuffer [IDL_MAX_ERRORSIZE]; idl_keyDef keyDef = idl_keyDefDefGet(); c_long keyMapIdx; idl_keyMap keyMap; c_type type; c_structure structure; c_iter keysList; os_uint32 keysListSize; os_uint32 keyIdx; c_char* keyName; os_uint32 i; c_iter keyNameList; os_uint32 keyNameListSize; c_structure tmpStructure; c_specifier sp; c_type subType; c_string typeName; c_type spType; if (keyDef != NULL) { /* check all key definition list elements */ for (keyMapIdx = 0; keyMapIdx < c_iterLength(keyDef->keyList); keyMapIdx++) { keyMap = c_iterObject(keyDef->keyList, keyMapIdx); /* if a keylist is defined for the type */ if (keyMap->keyList && strlen(keyMap->keyList) > 0) { /* find meteobject for the type */ type = c_type(c_metaResolveType(keyMap->scope, keyMap->typeName)); if (!type) { snprintf(errorBuffer, IDL_MAX_ERRORSIZE-1, errorText[idl_UndeclaredIdentifier], keyMap->typeName); idl_printError(filename, errorBuffer); return OS_FALSE; } /* type can be a typedef. Determine the actual type. */ type = c_typeActualType(type); /* type should be a structure */ if (c_baseObject(type)->kind != M_STRUCTURE) { snprintf(errorBuffer, IDL_MAX_ERRORSIZE-1, errorText[idl_IllegalKeyFields]); idl_printError(filename, errorBuffer); return OS_FALSE; } structure = c_structure(type); /* for each key in keyList, check if type is a char array */ keysList = c_splitString(keyMap->keyList, ","); keysListSize = c_iterLength(keysList); for(keyIdx = 0; keyIdx < keysListSize; keyIdx++) { keyName = c_iterTakeFirst(keysList); /* We might be dealing with a field of a field definition in * the keylist, so let's split this up */ keyNameList = c_splitString(keyName, "."); keyNameListSize = c_iterLength(keyNameList); tmpStructure = structure; for(i = 0; i < keyNameListSize; i++) { keyName = c_iterTakeFirst(keyNameList); /* Now get the actual member defined by the name */ sp = c_specifier(c_metaFindByName( c_metaObject(tmpStructure), keyName, CQ_FIXEDSCOPE | CQ_MEMBER | CQ_CASEINSENSITIVE)); if(sp) { spType = c_typeActualType(sp->type); /* If the member is a structure, we need to * recurse deeper. */ if(c_baseObject(spType)->kind == M_STRUCTURE) { tmpStructure = c_structure(spType); } /* If the member is a collection then we need to * ensure it is not a character array, but if it * is we need to ensure a corresponding CATS pragma * can be located */ else if(c_baseObject(spType)->kind == M_COLLECTION && c_collectionType(spType)->kind == C_ARRAY) { subType = c_typeActualType(c_collectionType(spType)->subType); if(c_baseObject(subType)->kind == M_PRIMITIVE && c_primitive(subType)->kind == P_CHAR) { typeName = c_metaName(c_metaObject(tmpStructure)); /* check if there is corresponding catsDef */ if (!idl_isCatsDefFor(c_metaObject(tmpStructure)->definedIn, typeName, keyName)) { snprintf( errorBuffer, IDL_MAX_ERRORSIZE-1, errorText[idl_NoCorrespondingCats], c_metaObject(structure)->name, keyName); idl_printError(filename, errorBuffer); return OS_FALSE; } c_free(typeName); } } } } } } } } return OS_TRUE; }
c_char * idl_genJavaLiteralValueImage( c_value literal, c_type type) { c_char * valueImg = NULL; c_char *val2; int i; if (c_baseObject(type)->kind != M_ENUMERATION) { switch (literal.kind) { case V_OCTET: valueImg = os_malloc (40); snprintf(valueImg, 40, "%d", literal.is.Octet); break; case V_FLOAT: case V_DOUBLE: val2 = os_malloc(45); valueImg = os_malloc(45); snprintf(val2, 45, "%40.17g", literal.is.Double); i = 0; while (val2[i] == ' ') { i++; } os_strncpy(valueImg, &val2[i], 40); os_free(val2); if ((strchr(valueImg, '.') == NULL) && (strchr(valueImg, 'E') == NULL)) { strcat(valueImg, ".0"); } break; case V_STRING: valueImg = os_malloc(strlen(literal.is.String)+3); snprintf(valueImg, strlen(literal.is.String)+3, "\"%s\"", literal.is.String); break; case V_BOOLEAN: valueImg = os_malloc(40); if (literal.is.Boolean) { snprintf(valueImg, 40, "true"); } else { snprintf (valueImg, 40, "false"); } break; case V_LONGLONG: valueImg = os_malloc(40); switch (c_primitive(type)->kind) { case P_SHORT: /* Apply unsigned version, since sign will be applied later as additional minus operand. */ snprintf(valueImg, 40, "%hu", (c_short)literal.is.LongLong); break; case P_USHORT: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ snprintf(valueImg, 40, "%hd", (c_short)literal.is.LongLong); break; case P_LONG: /* Apply unsigned version, since sign will be applied later as additional minus operand. */ snprintf(valueImg, 40, "%u", (c_long)literal.is.LongLong); break; case P_ULONG: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ snprintf(valueImg, 40, "%d", (c_long)literal.is.LongLong); break; case P_LONGLONG: /* Apply unsigned version, since sign will be applied later as additional minus operand. */ snprintf(valueImg, 40, "%"PA_PRIu64"L", (c_longlong)literal.is.LongLong); break; case P_ULONGLONG: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ snprintf(valueImg, 40, "%"PA_PRId64"L", (c_longlong)literal.is.LongLong); break; case P_CHAR: snprintf(valueImg, 40, "%d", (unsigned char)literal.is.LongLong); break; case P_OCTET: snprintf(valueImg, 40, "%d", (unsigned char)literal.is.LongLong); break; case P_ADDRESS: snprintf(valueImg, 40, PA_ADDRFMT, (PA_ADDRCAST)literal.is.LongLong); break; case P_UNDEFINED: case P_BOOLEAN: case P_WCHAR: case P_FLOAT: case P_DOUBLE: case P_VOIDP: case P_MUTEX: case P_LOCK: case P_COND: case P_COUNT: case P_PA_UINT32: case P_PA_UINTPTR: case P_PA_VOIDP: /* Do nothing */ break; } break; case V_SHORT: /* Apply unsigned version, since sign will be applied later as additional minus operand. */ valueImg = os_malloc(40); snprintf(valueImg, 40, "%hu", literal.is.Short); break; case V_LONG: /* Apply unsigned version, since sign will be applied later as additional minus operand. */ valueImg = os_malloc(40); snprintf(valueImg, 40, "%u", (c_long)literal.is.Long); break; case V_USHORT: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ valueImg = os_malloc(40); snprintf(valueImg, 40, "%hd", (c_short)literal.is.UShort); break; case V_ULONG: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ valueImg = os_malloc(40); snprintf(valueImg, 40, "%d", (c_long)literal.is.ULong); break; case V_ULONGLONG: /* Apply signed version, since Java has no unsigned types and therefore needs to * overflow into the negative range. */ valueImg = os_malloc(40); snprintf(valueImg, 40, "%" PA_PRId64, (c_longlong)literal.is.ULongLong); break; case V_ADDRESS: valueImg = os_malloc(40); snprintf(valueImg, 40, PA_ADDRFMT, (PA_ADDRCAST)literal.is.Address); break; case V_CHAR: valueImg = os_malloc(40); snprintf(valueImg, 40, "%u", (unsigned char)literal.is.Char); break; case V_UNDEFINED: case V_WCHAR: case V_WSTRING: case V_FIXED: case V_VOIDP: case V_OBJECT: case V_COUNT: /* Invalid types for literal constants*/ /* FALL THROUGH */ default: valueImg = NULL; break; } } else { const char *ENUM_TEMPLATE = "%s.%s"; char *javaEnumTp = idl_javaId(c_metaObject(type)->name); char *javaEnumLabel = idl_javaId(c_metaObject(c_enumeration(type)->elements[literal.is.Long])->name); size_t valLen = strlen(javaEnumTp) + strlen(javaEnumLabel) + strlen(ENUM_TEMPLATE) + 1; valueImg = os_malloc(valLen); snprintf(valueImg, valLen, ENUM_TEMPLATE, javaEnumTp, javaEnumLabel); os_free(javaEnumTp); os_free(javaEnumLabel); } return valueImg; }