From: Stan Cox <scox@redhat.com>
To: Andrew Cagney <cagney@redhat.com>
Cc: Frysk List <frysk@sourceware.org>
Subject: Re: generating type tests
Date: Tue, 16 Oct 2007 19:47:00 -0000 [thread overview]
Message-ID: <1192563628.2947.213.camel@multics.rdu.redhat.com> (raw)
In-Reply-To: <47139A01.8070404@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 856 bytes --]
On Mon, 2007-10-15 at 12:49 -0400, Andrew Cagney wrote:
> if they only need to read/edit a
> simple text (.c) file to add additional type tests. The expected
> results could be marked with comments:
> // LTYPE: struct bar {
> // LTYPE: struct foo *f;
> // LTYPE: struct bar *b;
> // LTYPE: }
> // VALUE: { 0x.*, 0x.* }
> // STYPE: struct bar
> // TEST: bp
>
> and a page of AWK could translate that into the body of a test.
>
> For the easy cases, can the generator produce a file like that;
I interpreted this as "create a generator that produces output like the
above from C source. This python script pretty much does that from an
input file of C source. I think it is reasonably solid, but as per the
meeting discussion if the preference is to have a tightly defined input
file syntax instead then that is okay too.
[-- Attachment #2: gen-typetests.py --]
[-- Type: text/x-python, Size: 3178 bytes --]
#!/usr/bin/python
import os,posix,sys
import re
from subprocess import *
from os.path import *
def usage ():
print "Usage: " + sys.argv[0] + " {-help} File"
sys.exit(1)
def get_dcl ():
get_a_line = True
dcl = ""
semi_count = 0
while (get_a_line):
line = d_file.readline()
if (line == ""):
break
if (line.lstrip().find("//") >= 0):
continue
dcl = dcl + line
for c in line:
if (c == '{'):
semi_count += 1
elif (c == '}'):
semi_count -= 1
if (semi_count == 0 and re.compile("^.*; *$").match(line)):
break
return dcl
if (len (sys.argv) == 1):
usage()
if (sys.argv[1] == "-help"):
print "The input file contains C declarations that are converted to tests."
print "The following declaration forms are recognized:"
print "basic_type string = initial;"
print "type string [N]... = {initial};"
print "type (* string) [N]... = initial;"
print "struct string {initial};"
print "struct string string = {initial};"
print "enum string {initial};"
usage()
d_file = open(sys.argv[1], 'r')
if (d_file == 0):
usage()
dcl = get_dcl()
types = {'char': "char", 'short': "short", 'int': "int", 'long': "long",
'long long': "long long", 'float': "float", 'double': "double"}
while (dcl != ""):
dcl = dcl.rstrip()
initial = ""
if (dcl.find("=") == -1 or dcl.find("enum") >= 0):
dcl_end = len(dcl)
else:
dcl_end = dcl.find("=")-1
print "\nDcl: " + dcl.rstrip()[0:dcl_end]
have_arr_ptr = re.compile("\( *\*.*\)").search(dcl)
# struct bar b = { 0, 0 };
have_struct_type = re.compile("^\s*(struct|union).*{.*};").search(dcl) and dcl.find("=") == -1
have_typedef = False
have_initial = dcl.find("=") >= 0 and dcl.find("enum") == -1
dcl = dcl.rstrip(';')
if (have_struct_type):
tokens = dcl.split()
name = tokens[0] + " " + tokens[1]
dcl = re.compile("\s*struct\s*\w+\s+").sub("",dcl,1)
elif (have_arr_ptr):
name = have_arr_ptr.group(0)
name = name[2:len(name)-1].strip()
dcl = re.compile("\s" + name + "\s").sub(" ",dcl,1)
else:
tokens = dcl.split()
previous = ""
name = tokens[len(tokens)-1]
if (have_initial):
for t in tokens:
if (t == "="):
name = previous
initial = dcl[dcl.find("="):]
break
previous = t
if (tokens[0] == "typedef"):
have_typedef = True
if name.find('[') >= 0:
name = name[0:name.find('[')]
dcl = re.compile("\s+" + name + "[^\w]").sub(" ",dcl,1)
print "Name: " + name
print "Initial: " + initial.lstrip("=").strip().strip(";")
dcl = dcl.replace(initial,"").strip()
dcl = re.compile("\s*(static|extern|typedef)\s*").sub("",dcl,1)
print "Type: " + dcl
if (have_struct_type or have_typedef):
types[name] = dcl
try:
print "Expanded Type:" + types[dcl]
except KeyError:
True
dcl = get_dcl()
[-- Attachment #3: sample-types.txt --]
[-- Type: text/plain, Size: 14362 bytes --]
struct foo { int i; } f = { 1 };
struct bar { struct foo* f; struct bar *b;};
struct bar b = { 0, 0 };
struct bar bp = { &f, &b };
// char
static char char_var = 176;
static const char const_char_var = 41;
static volatile char volatile_char_var = 41;
static char * ptr_char_var = &char_var;
static const char * ptr_const_char_var = &char_var;
static char * const char_ptr_const_var = &char_var;
static volatile char * ptr_volatile_char_var = &char_var;
static char * volatile char_ptr_volatile_var = &char_var;
// short int
static short int short_int_var = 32767;
static const short int const_short_int_var = -32767-1;
static volatile short int volatile_short_int_var = -32767-1;
static short int * ptr_short_int_var = &short_int_var;
static const short int * ptr_const_short_int_var = &short_int_var;
static short int * const short_int_ptr_const_var = &short_int_var;
static volatile short int * ptr_volatile_short_int_var = &short_int_var;
static short int * volatile short_int_ptr_volatile_var = &short_int_var;
// int
static int int_var = 2147483647;
static const int const_int_var = -2147483647-1;
static volatile int volatile_int_var = -2147483647-1;
static int * ptr_int_var = &int_var;
static const int * ptr_const_int_var = &int_var;
static int * const int_ptr_const_var = &int_var;
static volatile int * ptr_volatile_int_var = &int_var;
static int * volatile int_ptr_volatile_var = &int_var;
// long int
static long int long_int_var = 2147483647L;
static const long int const_long_int_var = -2147483647L-1;
static volatile long int volatile_long_int_var = -2147483647L-1;
static long int * ptr_long_int_var = &long_int_var;
static const long int * ptr_const_long_int_var = &long_int_var;
static long int * const long_int_ptr_const_var = &long_int_var;
static volatile long int * ptr_volatile_long_int_var = &long_int_var;
static long int * volatile long_int_ptr_volatile_var = &long_int_var;
// long long int
static long long int long_long_int_var = 9223372036854775807LL;
static const long long int const_long_long_int_var = -9223372036854775807LL-1;
static volatile long long int volatile_long_long_int_var = -9223372036854775807LL-1;
static long long int * ptr_long_long_int_var = &long_long_int_var;
static const long long int * ptr_const_long_long_int_var = &long_long_int_var;
static long long int * const long_long_int_ptr_const_var = &long_long_int_var;
static volatile long long int * ptr_volatile_long_long_int_var = &long_long_int_var;
static long long int * volatile long_long_int_ptr_volatile_var = &long_long_int_var;
// float
static float float_var = 3.402823E+38;
static const float const_float_var = 1.175494E-38;
static volatile float volatile_float_var = 1.175494E-38;
static float * ptr_float_var = &float_var;
static const float * ptr_const_float_var = &float_var;
static float * const float_ptr_const_var = &float_var;
static volatile float * ptr_volatile_float_var = &float_var;
static float * volatile float_ptr_volatile_var = &float_var;
// double
static double double_var = 1.797693E+308;
static const double const_double_var = 2.225074E-308;
static volatile double volatile_double_var = 2.225074E-308;
static double * ptr_double_var = &double_var;
static const double * ptr_const_double_var = &double_var;
static double * const double_ptr_const_var = &double_var;
static volatile double * ptr_volatile_double_var = &double_var;
static double * volatile double_ptr_volatile_var = &double_var;
// array of char
static char arr_char[2] = {41,176};
static char arr_arr_char[2][2] = {{41,176},{41,176}};
static char arr_arr_arr_char[2][2][2] = {{{41,176},{41,176}},{{41,176},{41,176}}};
static char * arr_ptr_arr_arr_char[2] = {arr_arr_char[0],arr_arr_char[1]};
static char (* ptr_arr_char)[2] = &arr_char;
// array of short int
static short int arr_short_int[2] = {-32767-1,32767};
static short int arr_arr_short_int[2][2] = {{-32767-1,32767},{-32767-1,32767}};
static short int arr_arr_arr_short_int[2][2][2] = {{{-32767-1,32767},{-32767-1,32767}},{{-32767-1,32767},{-32767-1,32767}}};
static short int * arr_ptr_arr_arr_short_int[2] = {arr_arr_short_int[0],arr_arr_short_int[1]};
static short int (* ptr_arr_short_int)[2] = &arr_short_int;
// array of int
static int arr_int[2] = {-2147483647-1,2147483647};
static int arr_arr_int[2][2] = {{-2147483647-1,2147483647},{-2147483647-1,2147483647}};
static int arr_arr_arr_int[2][2][2] = {{{-2147483647-1,2147483647},{-2147483647-1,2147483647}},{{-2147483647-1,2147483647},{-2147483647-1,2147483647}}};
static int * arr_ptr_arr_arr_int[2] = {arr_arr_int[0],arr_arr_int[1]};
static int (* ptr_arr_int)[2] = &arr_int;
// array of long int
static long int arr_long_int[2] = {-2147483647L-1,2147483647L};
static long int arr_arr_long_int[2][2] = {{-2147483647L-1,2147483647L},{-2147483647L-1,2147483647L}};
static long int arr_arr_arr_long_int[2][2][2] = {{{-2147483647L-1,2147483647L},{-2147483647L-1,2147483647L}},{{-2147483647L-1,2147483647L},{-2147483647L-1,2147483647L}}};
static long int * arr_ptr_arr_arr_long_int[2] = {arr_arr_long_int[0],arr_arr_long_int[1]};
static long int (* ptr_arr_long_int)[2] = &arr_long_int;
// array of long long int
static long long int arr_long_long_int[2] = {-9223372036854775807LL-1,9223372036854775807LL};
static long long int arr_arr_long_long_int[2][2] = {{-9223372036854775807LL-1,9223372036854775807LL},{-9223372036854775807LL-1,9223372036854775807LL}};
static long long int arr_arr_arr_long_long_int[2][2][2] = {{{-9223372036854775807LL-1,9223372036854775807LL},{-9223372036854775807LL-1,9223372036854775807LL}},{{-9223372036854775807LL-1,9223372036854775807LL},{-9223372036854775807LL-1,9223372036854775807LL}}};
static long long int * arr_ptr_arr_arr_long_long_int[2] = {arr_arr_long_long_int[0],arr_arr_long_long_int[1]};
static long long int (* ptr_arr_long_long_int)[2] = &arr_long_long_int;
// array of float
static float arr_float[2] = {1.175494E-38,3.402823E+38};
static float arr_arr_float[2][2] = {{1.175494E-38,3.402823E+38},{1.175494E-38,3.402823E+38}};
static float arr_arr_arr_float[2][2][2] = {{{1.175494E-38,3.402823E+38},{1.175494E-38,3.402823E+38}},{{1.175494E-38,3.402823E+38},{1.175494E-38,3.402823E+38}}};
static float * arr_ptr_arr_arr_float[2] = {arr_arr_float[0],arr_arr_float[1]};
static float (* ptr_arr_float)[2] = &arr_float;
// array of double
static double arr_double[2] = {2.225074E-308,1.797693E+308};
static double arr_arr_double[2][2] = {{2.225074E-308,1.797693E+308},{2.225074E-308,1.797693E+308}};
static double arr_arr_arr_double[2][2][2] = {{{2.225074E-308,1.797693E+308},{2.225074E-308,1.797693E+308}},{{2.225074E-308,1.797693E+308},{2.225074E-308,1.797693E+308}}};
static double * arr_ptr_arr_arr_double[2] = {arr_arr_double[0],arr_arr_double[1]};
static double (* ptr_arr_double)[2] = &arr_double;
static int one = 1, two = 2, three = 3, four = 4;
static struct {
int int_var;
} arr_struct[2] = {{1},{2}};
static union {
int int_var;
float float_var;
} arr_union[2] = {{1},{2}};
static struct {
int int_var;
} arr_arr_struct[2][2] = {{{1},{2}},{{3},{4}}};
static union {
int int_var;
float float_var;
} arr_arr_union[2][2] = {{{1},{2}},{{3},{4}}};
static int * arr_arr_ptr[2][2] = {{&one,&two},{&three,&four}};
static struct {
int arr_int[2];
} arr_struct_arr_int[2] = {{{1, 2}}, {{3, 4}}};
static struct {
struct {
int int_var;
} struct_a;
} arr_struct_struct[2] = {{{1}},{{2}}};
static struct {
union {
int int_var;
float float_var;
} struct_a;
} arr_struct_union[2] = {{{1}},{{2}}};
static struct {
int * ptr;
} arr_struct_ptr[2] = {{&one},{&two}};
static union {
int arr_int[2];
float arr_float[2];
} arr_union_arr_int[2] = {{{1, 2}}, {{3, 4}}};
static union {
struct {
int int_var;
} struct_a;
} arr_union_struct[2] = {{{1}}, {{2}}};
static union {
union {
int int_var;
} union_a;
} arr_union_union[2] = {{{1}}, {{2}}};
static union {
int * ptr;
} arr_union_ptr[2] = {{&one}, {&two}};
static struct {
int int_var;
} * arr_ptr_struct[2] ;
static union {
int int_var;
float float_var;
} * arr_ptr_union[2] ;
static int * * arr_ptr_ptr[2] ;
static struct {
int int_var;
} (* ptr_arr_struct)[2] ;
static union {
int int_var;
} (* ptr_arr_union)[2] ;
static int * (* ptr_arr_ptr)[2] ;
typedef struct {
char char_var;
short short_var;
int int_var;
long long_var;
float float_var;
double double_var;
char arr_char[4];
} type_struct;
typedef struct {
type_struct type_struct_min;
type_struct type_struct_max;
} type_type_struct;
type_type_struct type_minmax_struct =
{{41, -32767-1, -2147483647-1, -2147483647L-1, 1.175494E-38, 2.225074E-308, "ABC"},
{176, 32767, 2147483647, 2147483647L, 3.402823E+38, 1.797693E+308, "XYZ"}
};
static struct {
unsigned int bit1_0:1;
unsigned int bit1_1:1;
char char_2;
unsigned int bit1_6:1;
unsigned int bit1_7:1;
char char_8;
unsigned int bit1_9:1;
unsigned int bit1_10:1;
} bitfields_small_var = {1, 0, 0x7f, 1, 0, 0x7f, 1, 0};
static struct {
unsigned char char_0;
int bit1_4:1;
unsigned int bit1_5:1;
int bit2_6:2;
unsigned int bit2_8:2;
int bit3_10:3;
unsigned int bit3_13:3;
int bit9_16:9;
unsigned int bit9_25:9;
char char_34;
} bitfields_bit_var = {0x7f, 1, 1, 1, 3, 3, 7, UINT8_MAX, 511, 0x7f};
static struct {
short int arr_short[2];
} struct_arr_short_var = {{1, 2}};
static struct {
struct {
int int_var;
} struct_a;
} struct_struct_var = {{1}};
static struct {
union {
int int_var;
} union_a;
} struct_union_var = {{1}};
static struct {
int * ptr_int;
} struct_ptr_var = {&one};
static union {
int arr_int[4];
float arr_float[4];
} union_arr_var = {{1, 2, 3, 4}};
static union {
struct {
int int_var;
} struct_a;
} union_struct_var = {{1}};
static union {
union {
int int_var;
float float_var;
} union_a;
} union_union_var = {{1}};
static union {
int * ptr_int;
} union_ptr_var = {&one};
static struct {
int int_var;
} * ptr_struct_var ;
static union {
int int_var;
} * ptr_union_var ;
static struct {
int arr_arr_int[2][2];
} struct_arr_arr_int_var = {{{1, 2}, {3, 4}}};
static struct {
struct {
int int_var;
} arr_struct[2];
} struct_arr_struct_var = {{{1}, {2}}};
static struct {
union {
int int_var;
} arr_union[4];
} struct_arr_union_var = {{{1}, {2}}};
static struct {
int * arr_ptr[2];
} struct_arr_ptr_var = {{&one, &two}};
static struct {
struct {
int arr_int[2];
} struct_arr;
} struct_struct_arr_int_var = {{{1, 2}}};
static struct {
struct {
struct {
int int_var;
} struct_a;
} struct_struct;
} struct_struct_struct_var = {{{1}}};
static struct {
struct {
union {
char int_var;
} union_a;
} struct_union;
} struct_struct_union_var = {{{1}}};
static struct {
struct {
int * ptr_int;
} sp;
} struct_struct_ptr_var = {{&three}};
static struct {
union {
int arr_int[4];
} union_arr_int;
} struct_union_arr_var = {{{1, 2, 3, 4}}};
static struct {
union {
struct {
int int_var;
} struct_a;
} union_struct;
} struct_union_struct_var = {{{1}}};
static struct {
union {
union {
long int int_var;
float float_var;
} union_a;
} union_union;
} struct_union_union_var = {{{1.0}}};
static struct {
union {
int * ptr_int;
} union_ptr;
} struct_union_ptr_var = {{&four}};
static struct {
int (* ptr_arr)[4];
} struct_ptr_arr_var ;
static struct {
struct {
int int_var;
} * ptr_struct;
} struct_ptr_struct_var ;
static struct {
union {
int int_var;
} * ptr_union;
} struct_ptr_union_var ;
static struct {
int * * ptr_ptr;
} struct_ptr_ptr_var ;
static union {
int arr_int[2][2];
float arr_float[2][2];
} union_arr_int_var = {{{1, 2}, {3, 4}}};
static union {
struct {
int int_var;
} arr_struct[2];
} union_arr_struct_var = {{{1}}};
static union {
union {
int int_var;
float float_var;
} arr_union[2];
} union_arr_union_var = {{{1.1}}};
static union {
int * arr_ptr[4];
} union_arr_ptr_var = {{&one}};
static union {
struct {
int arr_int[4];
} struct_arr;
} union_struct_arr_int_var = {{{1, 2, 3, 4}}};
static union {
struct {
struct {
int int_var;
} struct_a;
} struct_b;
} union_struct_struct_var = {{{1}}};
static union {
struct {
union {
int int_var;
float float_var;
} union_a;
} struct_a;
} union_struct_union_var = {{{1.1}}};
static union {
struct {
int * ptr_int;
} struct_ptr;
} union_struct_ptr_var = {{&one}};
static union {
union {
long long int arr_int[4];
} union_arr;
} union_union_arr_int_var = {{{1, 2, 3, 4}}};
static union {
union {
struct {
long long int int_var;
} struct_a;
} union_a;
} union_union_struct_var = {{{1}}};
static union {
union {
union {
int int_var;
float float_var;
} union_a;
} union_b;
} union_union_union_var = {{{1.1}}};
static union {
union {
int * ptr_int;
float * ptr_float;
} union_ptr;
} union_union_ptr_var = {{&one}};
static union {
int (* ptr_arr)[4];
} union_ptr_arr_var ;
static union {
struct {
int int_var;
} * ptr_struct;
} union_ptr_struct_var ;
static union {
union {
int int_var;
} * ptr_union;
} union_ptr_union_var ;
static union {
int * * ptr_ptr;
} union_ptr_ptr_var ;
static struct {
int arr_int[4];
} * ptr_struct_arr_int_var ;
static struct {
struct {
int int_var;
} struct_a;
} * ptr_struct_struct_var ;
static struct {
union {
int int_var;
} union_a;
} * ptr_struct_union_var ;
static struct {
int * ptr_int;
} * ptr_struct_ptr_var ;
static union {
int arr_int[4];
} * ptr_union_arr_int_var ;
static union {
struct {
int int_var;
} struct_a;
} * ptr_union_struct_var ;
static union {
union {
int int_var;
} union_a;
} * ptr_union_union_var ;
static union {
int * ptr_int;
} * ptr_union_ptr_var ;
static struct {
int int_var;
} * * ptr_ptr_struct_var ;
static union {
int int_var;
} * * ptr_ptr_union_var ;
static enum {
red = 0,
green = 1,
blue = 2
} primary_colors_var ;
static enum colors {
orange = 0,
yellow = 1,
violet = 2,
indigo = 3
} rainbow_colors_var ;
static enum {
chevy = 33,
dodge = 44,
ford = 55
} usa_cars_var ;
static enum cars {
bmw = 0,
mercedes = 1,
porsche = 2
} european_cars_var ;
next prev parent reply other threads:[~2007-10-16 19:47 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-12 21:18 Stan Cox
2007-10-12 22:54 ` Andrew Cagney
2007-10-15 2:10 ` Stan Cox
2007-10-15 16:50 ` Andrew Cagney
2007-10-16 19:47 ` Stan Cox [this message]
2007-10-17 11:12 ` Stan Cox
2007-10-15 16:56 ` Andrew Cagney
2007-10-15 19:13 ` Stan Cox
2007-10-18 16:16 ` Stan Cox
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1192563628.2947.213.camel@multics.rdu.redhat.com \
--to=scox@redhat.com \
--cc=cagney@redhat.com \
--cc=frysk@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).