Add a New Comment (Rich Markup)
Comment Type & Canned Response: None No canned response available
Hi, I've adding a two new tests, test_types_tabs and test_types_tabs_c, which use the source code from test_types.cc and test_types_c.c, respectively (so there is no new source code for these tests).
It's a good job that I did, because I also discovered that my previous patch only half-worked when generating C source code. Integer and boolean variables also don't want to be passed though outputcvar_gen_class, which is for strings. So, for example, the following was generated for integers:
fprintf (stream, "%s", " cout << \"this is an int: \" << "); fprintf (stream, "%d", (record->myint ? record->myint : ""));
instead of:
fprintf (stream, "%s", " cout << \"this is an int: \" << "); fprintf (stream, "%d", record->myint);
which caused a compilation error.
So, the new patch: - fixes the original problem (passing non-strings to generate_string()) - adds test cases for types when used with --expand-tabs - fixes the above issue (using non-string types with outputcvar_gen_class)
(file #27468)
It would be better to have a specific test for that :)
Hi,
Sorry for the slow response...
Yes, all tests pass. That is to say, running "make check", "make my-check" and "make check-valgrind" are all OK.
I haven't written a test for this, no. I can add one though, if you like?
Thanks Tim for the patch! Did you check whether the tests still run? Did you write a test for this issue as well?
cheers Lorenzo
When run with --expand-tabs, skel variables are passed to generate_string() in the generated code, regardless of their type. For string variables, this is fine. For non-string variables, this causes a compilation error for generated C++ code, and a runtime segfault for generated C code.
For example, take the following skel file:
@array_name@[@array_index:int@]
When run like this:
$ gengen --expand-tabs < skel
The following code is generated:
void generate_gengen(ostream &stream, unsigned int indent = 0) { string indent_str (indent, ' '); indent = 0;
generate_string (array_name, stream, indent + indent_str.length ()); stream << "["; generate_string (array_index, stream, indent + indent_str.length ()); stream << "]"; stream << "\n"; stream << indent_str; }
You can see array_name and array_index are both passed to generate_string(), even though generate_string() takes a const string & as its first parameter, and not an int. Passing array_index causes a compiler error.
gengen --expand-tabs --output-format=c < skel
void generate_gengen(FILE stream, struct gengen_gen_struct record, unsigned int indent) { char *indent_str; unsigned int i;
indent_str = (char *) malloc (indent + 1); for (i = 0; i < indent; ++i) indent_str[i] = ' '; indent_str[indent] = '\0'; indent = 0;
generate_string ((record->array_name ? record->array_name : ""), stream, indent + strlen (indent_str)); fprintf (stream, "%s", "["); generate_string ((record->array_index ? record->array_index : ""), stream, indent + strlen (indent_str)); fprintf (stream, "%s", "]"); fprintf (stream, "%s", "\n"); fprintf (stream, "%s", indent_str);
free (indent_str); }
Again, you can see record->array_name and record->array_index both being passed to generate_string(), even though generate_string() takes a const char * as its first paramater, and not an int. This creates a compiler warning and segfaults at runtime (the int is treated as a pointer).
I have attached a patch for src/skelstruct_cpp.cc and src/skelstruct_c.cpp that works, in both cases, as follows:
Previously, the generation logic worked like this:
if expand-tabs send the variable through generate_string() else just output the variable
The patch changes this to:
if variable-type is string and expand-tabs send the variable through generate_string() else just output the variable
This should work OK, because non-string type variables (bools and ints) can not contain newlines anyway, and do not need to be indented, so generate_string() would have no effect on them.
(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)
Attach Files: Comment:
Depends on the following items: None found
Items that depend on this one: None found
There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.
Only logged-in users can vote.
Please enter the title of George Orwell's famous dystopian book (it's a date):
Follow 3 latest changes.
Copyright © 2023 Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. The Levitating, Meditating, Flute-playing Gnu logo is a GNU GPL'ed image provided by the Nevrax Design Team. Source Code
Powered by Savane 3.9