From f464b4d899e54308a0507916a2283d27ca15e751 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 8 Apr 2010 17:09:59 -0500 Subject: [PATCH] 2007-04-19: Reorganized cdefs, C++ guard and some formatting. --- src/dbHost/DBD/Menu.pm | 6 +++--- src/dbHost/DBD/Recordtype.pm | 12 +++++++----- src/dbHost/dbToRecordtypeH | 28 ++++++++++++++-------------- src/dbHost/test/Menu.pl | 7 ++++++- src/dbHost/test/Recfield.pl | 18 +++++++++--------- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/src/dbHost/DBD/Menu.pm b/src/dbHost/DBD/Menu.pm index f91edb661..61309f54d 100644 --- a/src/dbHost/DBD/Menu.pm +++ b/src/dbHost/DBD/Menu.pm @@ -45,7 +45,7 @@ sub toDeclaration { } $this->choices; return "typedef enum {\n" . join(",\n", @choices) . - "\n\t${name}_NUM_CHOICES\n" . + ",\n\t${name}_NUM_CHOICES\n" . "} $name;\n\n"; } @@ -55,12 +55,12 @@ sub toDefinition { my @strings = map { "\t\"" . escapeCstring(@{$_}[1]) . "\"" } $this->choices; - return "static const char * const ${name}ChoiceStrings = {\n" . + return "static const char * const ${name}ChoiceStrings[] = {\n" . join(",\n", @strings) . "\n};\n" . "const dbMenu ${name}MenuMetaData = {\n" . "\t\"" . escapeCstring($name) . "\",\n" . "\t${name}_NUM_CHOICES,\n" . - "\t${name}ChoiceStrings\n};\n"; + "\t${name}ChoiceStrings\n};\n\n"; } 1; diff --git a/src/dbHost/DBD/Recordtype.pm b/src/dbHost/DBD/Recordtype.pm index 0d0152d6d..f6571b7e0 100644 --- a/src/dbHost/DBD/Recordtype.pm +++ b/src/dbHost/DBD/Recordtype.pm @@ -64,8 +64,7 @@ sub add_device { } sub devices { - my $this = shift; - return @{$this->{DEVICE_LIST}}; + return @{shift->{DEVICE_LIST}}; } sub device { @@ -82,6 +81,10 @@ sub cdefs { return @{shift->{CDEFS}}; } +sub toCdefs { + return join("\n", shift->cdefs) . "\n\n"; +} + sub toDeclaration { my $this = shift; my @fields = map { @@ -89,10 +92,9 @@ sub toDeclaration { } $this->fields; my $name = $this->name; $name .= "Record" unless $name eq "dbCommon"; - my $cdefs = join("\n", $this->cdefs); - return "$cdefs\ntypedef struct $name {\n" . + return "typedef struct $name {\n" . join("\n", @fields) . - "\n} $name;\n"; + "\n} $name;\n\n"; } 1; diff --git a/src/dbHost/dbToRecordtypeH b/src/dbHost/dbToRecordtypeH index 3d5d5c0e1..190b669f4 100755 --- a/src/dbHost/dbToRecordtypeH +++ b/src/dbHost/dbToRecordtypeH @@ -48,20 +48,17 @@ if ($opt_D) { # Output dependencies only, to stdout open OUTFILE, ">$outfile" or die "$tool: Can't open $outfile: $!\n"; print OUTFILE "/* $outfile generated from $infile */\n\n", "#ifndef $guard_name\n", - "#define $guard_name\n\n", - "#include \"recDecls.h\"\n\n", - "#ifdef __cplusplus\n", - "extern \"C\" {\n", - "#endif\n\n"; + "#define $guard_name\n\n"; my ($rn, $rtyp) = each %{$rtypes}; + print OUTFILE $rtyp->toCdefs; + my @menu_fields = grep { $_->dbf_type eq 'DBF_MENU' } $rtyp->fields; my %menu_used; - # We don't need @menus_used any more, but the %menu_used hash is set here - my @menus_used = grep { + grep { !$menu_used{$_}++ } map { $_->attribute('menu') @@ -76,13 +73,12 @@ if ($opt_D) { # Output dependencies only, to stdout } } my @menus_external = keys %menu_used; - print OUTFILE "\n" if scalar %{$menus_defined}; print OUTFILE $rtyp->toDeclaration; unless ($rn eq 'dbCommon') { my $n=0; - print OUTFILE "\ntypedef enum {\n", + print OUTFILE "typedef enum {\n", join(",\n", map { "\t${rn}Record$_ = " . $n++ } $rtyp->field_names), "\n} ${rn}FieldIndex;\n\n"; print OUTFILE "#ifdef GEN_SIZE_OFFSET\n\n"; @@ -90,7 +86,7 @@ if ($opt_D) { # Output dependencies only, to stdout "extern const dbMenu ${_}MenuMetaData;\n" } @menus_external), "\n"; while (($name, $menu) = each %{$menus_defined}) { - print OUTFILE $menu->toDefinition, "\n"; + print OUTFILE $menu->toDefinition; } print OUTFILE (map { "static const char ${rn}FieldName$_\[] = \"$_\";\n" } @@ -142,7 +138,10 @@ if ($opt_D) { # Output dependencies only, to stdout " ${rn}RecordSortedFieldNames,\n", " ${rn}RecordSortedFieldIndex,\n", " \&${rn}RSET\n", - "};\n\n"; + "};\n\n", + "#ifdef __cplusplus\n", + "extern \"C\" {\n", + "#endif\n\n"; print OUTFILE "dbRecordType * epicsShareAPI ${rn}RecordRegistrar(dbBase *pbase, int nDevs)\n", "{\n", " dbRecordType *prt = dbCreateRecordtype(&${rn}RecordMetaData, nDevs);\n"; @@ -167,11 +166,12 @@ if ($opt_D) { # Output dependencies only, to stdout } $rtyp->fields; print OUTFILE " dbRegisterRecordtype(pbase, prt);\n"; print OUTFILE " return prt;\n}\n\n", + "#ifdef __cplusplus\n", + "} /* extern \"C\" */\n", + "#endif\n\n", "#endif /* GEN_SIZE_OFFSET */\n"; } - print OUTFILE "\n#ifdef __cplusplus\n", - "} /* extern \"C\" */\n", - "#endif\n\n", + print OUTFILE "\n", "#endif /* $guard_name */\n"; close OUTFILE; } diff --git a/src/dbHost/test/Menu.pl b/src/dbHost/test/Menu.pl index a2ea7f764..dc6db9841 100644 --- a/src/dbHost/test/Menu.pl +++ b/src/dbHost/test/Menu.pl @@ -21,4 +21,9 @@ is_deeply $menu->choice(1), ['ch2', 'Choice 2'], 'Second choice found'; ok !$menu->legal_choice('Choice 3'), 'Third choice not legal'; is_deeply $menu->choice(2), undef, 'Third choice undefined'; -is $menu->toEnum, "typedef enum {\n\tch1\t/* Choice 1 */,\n\tch2\t/* Choice 2 */\n} test;\n", 'enum'; +like $menu->toDeclaration, qr/ ^ + \s* typedef \s+ enum \s+ { + \s+ ch1 \s+ \/\* [^*]* \*\/, + \s+ ch2 \s+ \/\* [^*]* \*\/, + \s+ test_NUM_CHOICES ,? + \s+ } \s+ test; \s* $ /x, 'C declaration'; diff --git a/src/dbHost/test/Recfield.pl b/src/dbHost/test/Recfield.pl index d88d11a68..92833ecd3 100644 --- a/src/dbHost/test/Recfield.pl +++ b/src/dbHost/test/Recfield.pl @@ -14,7 +14,7 @@ is keys %{$fld_string->attributes}, 1, "Size set"; ok $fld_string->legal_value("Hello, world!"), 'Legal value'; ok !$fld_string->legal_value("x"x41), 'Illegal string'; $fld_string->check_valid; -is $fld_string->toDeclaration, "char str[41];", "C declaration"; +like $fld_string->toDeclaration, qr/^\s*char\s+str\[41\];\s*$/, "C declaration"; my $fld_char = DBD::Recfield->new('chr', 'DBF_CHAR'); isa_ok $fld_char, 'DBD::Recfield'; @@ -26,7 +26,7 @@ ok $fld_char->legal_value("-128"), 'Legal - value'; ok $fld_char->legal_value("127"), 'Legal + value'; ok !$fld_char->legal_value("0x80"), 'Illegal + hex value'; $fld_char->check_valid; -is $fld_char->toDeclaration, "signed char chr;", "C declaration"; +like $fld_char->toDeclaration, qr/^\s*signed\s+char\s+chr;\s*$/, "C declaration"; my $fld_uchar = DBD::Recfield->new('uchr', 'DBF_UCHAR'); isa_ok $fld_uchar, 'DBD::Recfield'; @@ -38,7 +38,7 @@ ok $fld_uchar->legal_value("0"), 'Legal 0 value'; ok $fld_uchar->legal_value("0377"), 'Legal + value'; ok !$fld_uchar->legal_value("0400"), 'Illegal + octal value'; $fld_uchar->check_valid; -is $fld_uchar->toDeclaration, "unsigned char uchr;", "C declaration"; +like $fld_uchar->toDeclaration, qr/^\s*unsigned\s+char\s+uchr;\s*$/, "C declaration"; my $fld_short = DBD::Recfield->new('shrt', 'DBF_SHORT'); isa_ok $fld_short, 'DBD::Recfield'; @@ -50,7 +50,7 @@ ok $fld_short->legal_value("-32768"), 'Legal - value'; ok $fld_short->legal_value("32767"), 'Legal + value'; ok !$fld_short->legal_value("0x8000"), 'Illegal + hex value'; $fld_short->check_valid; -is $fld_short->toDeclaration, "signed short shrt;", "C declaration"; +like $fld_short->toDeclaration, qr/^\s*short\s+shrt;\s*$/, "C declaration"; my $fld_ushort = DBD::Recfield->new('ushrt', 'DBF_USHORT'); isa_ok $fld_ushort, 'DBD::Recfield'; @@ -62,7 +62,7 @@ ok $fld_ushort->legal_value("0"), 'Legal 0 value'; ok $fld_ushort->legal_value("65535"), 'Legal + value'; ok !$fld_ushort->legal_value("0x10000"), 'Illegal + hex value'; $fld_ushort->check_valid; -is $fld_ushort->toDeclaration, "unsigned short ushrt;", "C declaration"; +like $fld_ushort->toDeclaration, qr/^\s*unsigned\s+short\s+ushrt;\s*$/, "C declaration"; my $fld_long = DBD::Recfield->new('lng', 'DBF_LONG'); isa_ok $fld_long, 'DBD::Recfield'; @@ -73,7 +73,7 @@ ok $fld_long->legal_value("-12345678"), 'Legal - value'; ok $fld_long->legal_value("0x12345678"), 'Legal + value'; ok !$fld_long->legal_value("0xfigure"), 'Illegal value'; $fld_long->check_valid; -is $fld_long->toDeclaration, "epicsInt32 lng;", "C declaration"; +like $fld_long->toDeclaration, qr/^\s*epicsInt32\s+lng;\s*$/, "C declaration"; my $fld_ulong = DBD::Recfield->new('ulng', 'DBF_ULONG'); isa_ok $fld_ulong, 'DBD::Recfield'; @@ -85,7 +85,7 @@ ok $fld_ulong->legal_value("00"), 'Legal 0 value'; ok $fld_ulong->legal_value("0xffffffff"), 'Legal + value'; ok !$fld_ulong->legal_value("0xfacepaint"), 'Illegal value'; $fld_ulong->check_valid; -is $fld_ulong->toDeclaration, "unsigned long ulng;", "C declaration"; +like $fld_ulong->toDeclaration, qr/^\s*unsigned\s+long\s+ulng;\s*$/, "C declaration"; my $fld_float = DBD::Recfield->new('flt', 'DBF_FLOAT'); isa_ok $fld_float, 'DBD::Recfield'; @@ -96,7 +96,7 @@ ok $fld_float->legal_value("-1.2345678e9"), 'Legal - value'; ok $fld_float->legal_value("0.12345678e9"), 'Legal + value'; ok !$fld_float->legal_value("0x1.5"), 'Illegal value'; $fld_float->check_valid; -is $fld_float->toDeclaration, "float flt;", "C declaration"; +like $fld_float->toDeclaration, qr/^\s*float\s+flt;\s*$/, "C declaration"; my $fld_double = DBD::Recfield->new('dbl', 'DBF_DOUBLE'); isa_ok $fld_double, 'DBD::Recfield'; @@ -107,5 +107,5 @@ ok $fld_double->legal_value("-12345e-67"), 'Legal - value'; ok $fld_double->legal_value("12345678e+9"), 'Legal + value'; ok !$fld_double->legal_value("e5"), 'Illegal value'; $fld_double->check_valid; -is $fld_double->toDeclaration, "double dbl;", "C declaration"; +like $fld_double->toDeclaration, qr/^\s*double\s+dbl;\s*$/, "C declaration";