File: unanon

package info (click to toggle)
sudo 1.9.17p2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,624 kB
  • sloc: ansic: 114,008; sh: 13,621; makefile: 9,805; yacc: 2,608; lex: 1,574; perl: 366; python: 362; sed: 265
file content (50 lines) | stat: -rwxr-xr-x 1,214 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env perl
#
# Post-process files generated by protoc-c to remove anonymous unions.
# Works on the generated files but probably little else.

use warnings;
use strict;

sub unanon {
    my $hfile = shift;
    my $cfile = shift;
    my ($fh, $content);
    my %members;

    open $fh, "<", $hfile or die $!;
    local $/; # enable localized slurp mode
    $content = <$fh>;
    close $fh;

    # Detect and replace anonymous unions in .h file.
    # Assumes there is only one anonymous union in scope.
    while ($content =~ s/^(struct\s+(\w+)[^}]+)(union\s+{([^}]+)}\s*);/$1$3 u;/sm) {
	my $s = $2;
	my $u = $4;
	$u =~ s:/\*((?!\*/).)*\*/::sg;
	foreach (split(/\n+/, $u)) {
	    if (/^.*\s+\**([^;]+);/) {
		$members{$1} = $s;
	    }
	}
    }
    open $fh, ">", $hfile or die $!;
    print $fh $content;
    close $fh;

    # Replace anonymous union access in generated .c file.
    open $fh, "<", $cfile or die $!;
    $content = <$fh>;
    close $fh;

    while (my ($key, $val) = each %members) {
	# We only support access via offsetof()
	$content =~ s/offsetof\($val, $key\)/offsetof($val, u.$key)/g;
    }
    open $fh, ">", $cfile or die $!;
    print $fh $content;
    close $fh;
}

unanon(@ARGV);