scilab: truncates too long identifier names (in addition to display warnings)

This commit is contained in:
Simon Marchetto 2014-09-25 14:29:52 +02:00
commit 3ca185197c
4 changed files with 86 additions and 18 deletions

View file

@ -14,6 +14,7 @@ top_builddir = ../@top_builddir@
C_TEST_CASES += \ C_TEST_CASES += \
scilab_consts \ scilab_consts \
scilab_enums \ scilab_enums \
scilab_identifier_name \
CPP_TEST_CASES += \ CPP_TEST_CASES += \
inout \ inout \

View file

@ -0,0 +1,19 @@
exec("swigtest.start", -1);
// Not truncated identifier names
gvar_identifier_name_set(-101);
checkequal(gvar_identifier_name_get(), -101, "gvar_identifier_name_get()");
checkequal(CONS_IDENTIFIER_NAME_get(), -11, "CONS_IDENTIFIER_NAME_get()");
checkequal(function_identifier_name(), -21, "function_identifier_name()");
// Truncated identifier names
too_long_gvar_identi_set(101);
checkequal(too_long_gvar_identi_get(), 101, "too_long_variable_id_get()");
checkequal(TOO_LONG_CONST_IDENT_get(), 11, "TOO_LONG_CONST_IDENT_get()");
checkequal(too_long_function_identi(), 21, "too_long_function_identi()");
// Test identifier names in scilabconst mode
checkequal(SC_CONST_IDENTIFIER_NAME, int32(-12), "SC_TOO_LONG_IDENTIF");
checkequal(SC_TOO_LONG_CONST_IDENTI, int32(14), "SC_TOO_LONG_IDENTIF");
exec("swigtest.quit", -1);

View file

@ -0,0 +1,35 @@
%module scilab_identifier_name
// Test identifier name truncating
// (when variables, fonctions, constants names exceed 24 charaters long)
%inline %{
// These identifier names wont be truncated
int gvar_identifier_name = -1;
#define CONS_IDENTIFIER_NAME -11
int function_identifier_name() { return -21; };
// These identifier names will be truncated
int too_long_gvar_identifier_name_1 = 1;
int too_long_gvar_identifier_name_2 = 2;
#define TOO_LONG_CONST_IDENTIFIER_NAME_1 11
#define TOO_LONG_CONST_IDENTIFIER_NAME_2 12
int too_long_function_identifier_name_1() { return 21; };
int too_long_function_identifier_name_2() { return 22; };
%}
// Test when %scilabconst mode is activated
%scilabconst(1);
%inline %{
#define SC_CONST_IDENTIFIER_NAME -12;
#define SC_TOO_LONG_CONST_IDENTIFIER_NAME_1 13
#define SC_TOO_LONG_CONST_IDENTIFIER_NAME_2 14
%}

View file

@ -15,6 +15,9 @@
/*#define SWIG_DEBUG*/ /*#define SWIG_DEBUG*/
#define SCILAB_IDENTIFIER_CHAR_MAX 24
static const char *usage = (char *) "\ static const char *usage = (char *) "\
Scilab options (available with -scilab)\n\ Scilab options (available with -scilab)\n\
-addcflags <cflags> - Add compiler flags <cflags>\n\ -addcflags <cflags> - Add compiler flags <cflags>\n\
@ -289,8 +292,6 @@ public:
SwigType *functionReturnType = Getattr(node, "type"); SwigType *functionReturnType = Getattr(node, "type");
ParmList *functionParamsList = Getattr(node, "parms"); ParmList *functionParamsList = Getattr(node, "parms");
checkIdentifierName(functionName);
int paramIndex = 0; // Used for loops over ParmsList int paramIndex = 0; // Used for loops over ParmsList
Parm *param = NULL; // Used for loops over ParamsList Parm *param = NULL; // Used for loops over ParamsList
@ -483,14 +484,16 @@ public:
/* Dump the function out */ /* Dump the function out */
Wrapper_print(wrapper, wrappersSection); Wrapper_print(wrapper, wrappersSection);
String *scilabFunctionName = checkIdentifierName(functionName, SCILAB_IDENTIFIER_CHAR_MAX);
/* Update builder.sce contents */ /* Update builder.sce contents */
if (isLastOverloaded) { if (isLastOverloaded) {
addFunctionToScilab(functionName, wrapperName); addFunctionToScilab(scilabFunctionName, wrapperName);
dispatchFunction(node); dispatchFunction(node);
} }
if (!isOverloaded) { if (!isOverloaded) {
addFunctionToScilab(functionName, wrapperName); addFunctionToScilab(scilabFunctionName, wrapperName);
} }
/* tidy up */ /* tidy up */
@ -552,11 +555,13 @@ public:
String *origVariableName = Getattr(node, "name"); // Ex: Shape::nshapes String *origVariableName = Getattr(node, "name"); // Ex: Shape::nshapes
String *variableName = Getattr(node, "sym:name"); // Ex; Shape_nshapes (can be used for function names, ...) String *variableName = Getattr(node, "sym:name"); // Ex; Shape_nshapes (can be used for function names, ...)
checkIdentifierName(variableName); // Variable names can have SCILAB_IDENTIFIER_CHAR_MAX - 4 because of suffixes "_get" or "_set" added to function
String* scilabVariableName = checkIdentifierName(variableName, SCILAB_IDENTIFIER_CHAR_MAX - 4);
/* Manage GET function */ /* Manage GET function */
Wrapper *getFunctionWrapper = NewWrapper(); Wrapper *getFunctionWrapper = NewWrapper();
String *getFunctionName = Swig_name_get(NSPACE_TODO, variableName); String *getFunctionName = Swig_name_get(NSPACE_TODO, variableName);
String *scilabGetFunctionName = Swig_name_get(NSPACE_TODO, scilabVariableName);
Setattr(node, "wrap:name", getFunctionName); Setattr(node, "wrap:name", getFunctionName);
Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL); Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
@ -579,12 +584,13 @@ public:
Wrapper_print(getFunctionWrapper, wrappersSection); Wrapper_print(getFunctionWrapper, wrappersSection);
/* Add function to builder table */ /* Add function to builder table */
addFunctionToScilab(getFunctionName, getFunctionName); addFunctionToScilab(scilabGetFunctionName, getFunctionName);
/* Manage SET function */ /* Manage SET function */
if (is_assignable(node)) { if (is_assignable(node)) {
Wrapper *setFunctionWrapper = NewWrapper(); Wrapper *setFunctionWrapper = NewWrapper();
String *setFunctionName = Swig_name_set(NSPACE_TODO, variableName); String *setFunctionName = Swig_name_set(NSPACE_TODO, variableName);
String *scilabSetFunctionName = Swig_name_set(NSPACE_TODO, scilabVariableName);
Setattr(node, "wrap:name", setFunctionName); Setattr(node, "wrap:name", setFunctionName);
Printv(setFunctionWrapper->def, "int ", setFunctionName, "(SWIG_GatewayParameters) {\n", NIL); Printv(setFunctionWrapper->def, "int ", setFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
@ -605,7 +611,7 @@ public:
Wrapper_print(setFunctionWrapper, wrappersSection); Wrapper_print(setFunctionWrapper, wrappersSection);
/* Add function to builder table */ /* Add function to builder table */
addFunctionToScilab(setFunctionName, setFunctionName); addFunctionToScilab(scilabSetFunctionName, setFunctionName);
} }
return SWIG_OK; return SWIG_OK;
@ -625,8 +631,6 @@ public:
String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantValue = rawValue ? rawValue : Getattr(node, "value");
String *constantTypemap = NULL; String *constantTypemap = NULL;
checkIdentifierName(constantName);
// If feature scilab:const enabled, constants & enums are wrapped to Scilab variables // If feature scilab:const enabled, constants & enums are wrapped to Scilab variables
if (GetFlag(node, "feature:scilab:const")) { if (GetFlag(node, "feature:scilab:const")) {
bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)); bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING));
@ -640,8 +644,10 @@ public:
constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0); constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0);
if (constantTypemap != NULL) { if (constantTypemap != NULL) {
String *scilabConstantName = checkIdentifierName(constantName, SCILAB_IDENTIFIER_CHAR_MAX);
Setattr(node, "wrap:name", constantName); Setattr(node, "wrap:name", constantName);
Replaceall(constantTypemap, "$result", constantName); Replaceall(constantTypemap, "$result", scilabConstantName);
Replaceall(constantTypemap, "$value", constantValue); Replaceall(constantTypemap, "$value", constantValue);
emit_action_code(node, variablesCode, constantTypemap); emit_action_code(node, variablesCode, constantTypemap);
@ -660,9 +666,13 @@ public:
constantValue = wname; constantValue = wname;
} }
// Constant names can have SCILAB_IDENTIFIER_CHAR_MAX - 4 because of suffixes "_get" added to function
String *scilabConstantName = checkIdentifierName(constantName, SCILAB_IDENTIFIER_CHAR_MAX - 4);
/* Create GET function to get the constant value */ /* Create GET function to get the constant value */
Wrapper *getFunctionWrapper = NewWrapper(); Wrapper *getFunctionWrapper = NewWrapper();
String *getFunctionName = Swig_name_get(NSPACE_TODO, constantName); String *getFunctionName = Swig_name_get(NSPACE_TODO, constantName);
String *scilabGetFunctionName = Swig_name_get(NSPACE_TODO, scilabConstantName);
Setattr(node, "wrap:name", getFunctionName); Setattr(node, "wrap:name", getFunctionName);
Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL); Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
@ -686,7 +696,7 @@ public:
Wrapper_print(getFunctionWrapper, wrappersSection); Wrapper_print(getFunctionWrapper, wrappersSection);
/* Add the function to Scilab */ /* Add the function to Scilab */
addFunctionToScilab(getFunctionName, getFunctionName); addFunctionToScilab(scilabGetFunctionName, getFunctionName);
DelWrapper(getFunctionWrapper); DelWrapper(getFunctionWrapper);
@ -735,17 +745,20 @@ public:
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* checkIdentifierName() * checkIdentifierName()
* Display a warning for too long generated identifier names * Truncates (and displays a warning) too long identifier names
* Scilab identifier name (functions, variables) can have 24 chars max * (Scilab identifiers names are limited to 24 chars max)
* ----------------------------------------------------------------------- */ * ----------------------------------------------------------------------- */
void checkIdentifierName(String *name) { String *checkIdentifierName(String *name, int char_size_max) {
if (Len(name) > 24) { String *scilabFunctionName;
// Warning on too long identifiers if (Len(name) > char_size_max) {
scilabFunctionName = DohNewStringWithSize(name, char_size_max);
Swig_warning(WARN_SCILAB_TRUNCATED_NAME, input_file, line_number, Swig_warning(WARN_SCILAB_TRUNCATED_NAME, input_file, line_number,
"Identifier name '%s' exceeds 24 characters and has been truncated to '%s'.\n", "Identifier name '%s' exceeds 24 characters and has been truncated to '%s'.\n",
name, DohNewStringWithSize(name, 24)); name, scilabFunctionName);
} } else
scilabFunctionName = name;
return scilabFunctionName;
} }
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------