scilab: truncates too long identifier names (in addition to display warnings)
This commit is contained in:
parent
d421884a3b
commit
3ca185197c
4 changed files with 86 additions and 18 deletions
|
|
@ -14,6 +14,7 @@ top_builddir = ../@top_builddir@
|
|||
C_TEST_CASES += \
|
||||
scilab_consts \
|
||||
scilab_enums \
|
||||
scilab_identifier_name \
|
||||
|
||||
CPP_TEST_CASES += \
|
||||
inout \
|
||||
|
|
|
|||
19
Examples/test-suite/scilab/scilab_identifier_name_runme.sci
Normal file
19
Examples/test-suite/scilab/scilab_identifier_name_runme.sci
Normal 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);
|
||||
35
Examples/test-suite/scilab_identifier_name.i
Normal file
35
Examples/test-suite/scilab_identifier_name.i
Normal 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
|
||||
%}
|
||||
|
|
@ -15,6 +15,9 @@
|
|||
|
||||
/*#define SWIG_DEBUG*/
|
||||
|
||||
#define SCILAB_IDENTIFIER_CHAR_MAX 24
|
||||
|
||||
|
||||
static const char *usage = (char *) "\
|
||||
Scilab options (available with -scilab)\n\
|
||||
-addcflags <cflags> - Add compiler flags <cflags>\n\
|
||||
|
|
@ -289,8 +292,6 @@ public:
|
|||
SwigType *functionReturnType = Getattr(node, "type");
|
||||
ParmList *functionParamsList = Getattr(node, "parms");
|
||||
|
||||
checkIdentifierName(functionName);
|
||||
|
||||
int paramIndex = 0; // Used for loops over ParmsList
|
||||
Parm *param = NULL; // Used for loops over ParamsList
|
||||
|
||||
|
|
@ -483,14 +484,16 @@ public:
|
|||
/* Dump the function out */
|
||||
Wrapper_print(wrapper, wrappersSection);
|
||||
|
||||
String *scilabFunctionName = checkIdentifierName(functionName, SCILAB_IDENTIFIER_CHAR_MAX);
|
||||
|
||||
/* Update builder.sce contents */
|
||||
if (isLastOverloaded) {
|
||||
addFunctionToScilab(functionName, wrapperName);
|
||||
addFunctionToScilab(scilabFunctionName, wrapperName);
|
||||
dispatchFunction(node);
|
||||
}
|
||||
|
||||
if (!isOverloaded) {
|
||||
addFunctionToScilab(functionName, wrapperName);
|
||||
addFunctionToScilab(scilabFunctionName, wrapperName);
|
||||
}
|
||||
|
||||
/* tidy up */
|
||||
|
|
@ -552,11 +555,13 @@ public:
|
|||
String *origVariableName = Getattr(node, "name"); // Ex: Shape::nshapes
|
||||
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 */
|
||||
Wrapper *getFunctionWrapper = NewWrapper();
|
||||
String *getFunctionName = Swig_name_get(NSPACE_TODO, variableName);
|
||||
String *scilabGetFunctionName = Swig_name_get(NSPACE_TODO, scilabVariableName);
|
||||
|
||||
Setattr(node, "wrap:name", getFunctionName);
|
||||
Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
|
||||
|
|
@ -579,12 +584,13 @@ public:
|
|||
Wrapper_print(getFunctionWrapper, wrappersSection);
|
||||
|
||||
/* Add function to builder table */
|
||||
addFunctionToScilab(getFunctionName, getFunctionName);
|
||||
addFunctionToScilab(scilabGetFunctionName, getFunctionName);
|
||||
|
||||
/* Manage SET function */
|
||||
if (is_assignable(node)) {
|
||||
Wrapper *setFunctionWrapper = NewWrapper();
|
||||
String *setFunctionName = Swig_name_set(NSPACE_TODO, variableName);
|
||||
String *scilabSetFunctionName = Swig_name_set(NSPACE_TODO, scilabVariableName);
|
||||
|
||||
Setattr(node, "wrap:name", setFunctionName);
|
||||
Printv(setFunctionWrapper->def, "int ", setFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
|
||||
|
|
@ -605,7 +611,7 @@ public:
|
|||
Wrapper_print(setFunctionWrapper, wrappersSection);
|
||||
|
||||
/* Add function to builder table */
|
||||
addFunctionToScilab(setFunctionName, setFunctionName);
|
||||
addFunctionToScilab(scilabSetFunctionName, setFunctionName);
|
||||
}
|
||||
|
||||
return SWIG_OK;
|
||||
|
|
@ -625,8 +631,6 @@ public:
|
|||
String *constantValue = rawValue ? rawValue : Getattr(node, "value");
|
||||
String *constantTypemap = NULL;
|
||||
|
||||
checkIdentifierName(constantName);
|
||||
|
||||
// If feature scilab:const enabled, constants & enums are wrapped to Scilab variables
|
||||
if (GetFlag(node, "feature:scilab:const")) {
|
||||
bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING));
|
||||
|
|
@ -640,8 +644,10 @@ public:
|
|||
|
||||
constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0);
|
||||
if (constantTypemap != NULL) {
|
||||
String *scilabConstantName = checkIdentifierName(constantName, SCILAB_IDENTIFIER_CHAR_MAX);
|
||||
|
||||
Setattr(node, "wrap:name", constantName);
|
||||
Replaceall(constantTypemap, "$result", constantName);
|
||||
Replaceall(constantTypemap, "$result", scilabConstantName);
|
||||
Replaceall(constantTypemap, "$value", constantValue);
|
||||
|
||||
emit_action_code(node, variablesCode, constantTypemap);
|
||||
|
|
@ -660,9 +666,13 @@ public:
|
|||
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 */
|
||||
Wrapper *getFunctionWrapper = NewWrapper();
|
||||
String *getFunctionName = Swig_name_get(NSPACE_TODO, constantName);
|
||||
String *scilabGetFunctionName = Swig_name_get(NSPACE_TODO, scilabConstantName);
|
||||
Setattr(node, "wrap:name", getFunctionName);
|
||||
Printv(getFunctionWrapper->def, "int ", getFunctionName, "(SWIG_GatewayParameters) {\n", NIL);
|
||||
|
||||
|
|
@ -686,7 +696,7 @@ public:
|
|||
Wrapper_print(getFunctionWrapper, wrappersSection);
|
||||
|
||||
/* Add the function to Scilab */
|
||||
addFunctionToScilab(getFunctionName, getFunctionName);
|
||||
addFunctionToScilab(scilabGetFunctionName, getFunctionName);
|
||||
|
||||
DelWrapper(getFunctionWrapper);
|
||||
|
||||
|
|
@ -735,17 +745,20 @@ public:
|
|||
|
||||
/* -----------------------------------------------------------------------
|
||||
* checkIdentifierName()
|
||||
* Display a warning for too long generated identifier names
|
||||
* Scilab identifier name (functions, variables) can have 24 chars max
|
||||
* Truncates (and displays a warning) too long identifier names
|
||||
* (Scilab identifiers names are limited to 24 chars max)
|
||||
* ----------------------------------------------------------------------- */
|
||||
|
||||
void checkIdentifierName(String *name) {
|
||||
if (Len(name) > 24) {
|
||||
// Warning on too long identifiers
|
||||
String *checkIdentifierName(String *name, int char_size_max) {
|
||||
String *scilabFunctionName;
|
||||
if (Len(name) > char_size_max) {
|
||||
scilabFunctionName = DohNewStringWithSize(name, char_size_max);
|
||||
Swig_warning(WARN_SCILAB_TRUNCATED_NAME, input_file, line_number,
|
||||
"Identifier name '%s' exceeds 24 characters and has been truncated to '%s'.\n",
|
||||
name, DohNewStringWithSize(name, 24));
|
||||
}
|
||||
name, scilabFunctionName);
|
||||
} else
|
||||
scilabFunctionName = name;
|
||||
return scilabFunctionName;
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue