slist: fix compile breakage
[smatch.git] / smatch_data / db / load_db_type_value.pl
blobefc0686315ea1357e66b2120a2909271a08a74fd
1 #!/usr/bin/perl -w
3 use strict;
4 use warnings;
5 use bigint;
6 use DBI;
7 use Data::Dumper;
10 my $project = shift;
11 my $warns = shift;
12 my $db_file = shift;
13 my $db = DBI->connect("dbi:SQLite:$db_file", "", "", {AutoCommit => 0});
15 sub text_to_int($)
17 my $text = shift;
19 if ($text =~ /s64min/) {
20 return -(2**63);
21 } elsif ($text =~/s32min/) {
22 return -(2**31);
23 } elsif ($text =~ /s16min/) {
24 return -(2**15);
25 } elsif ($text =~ /s64max/) {
26 return 2**63 - 1;
27 } elsif ($text =~ /s32max/) {
28 return 2**31 - 1;
29 } elsif ($text =~ /s16max/) {
30 return 2**15 - 1;
31 } elsif ($text =~ /u64max/) {
32 return 2**64 - 1;
33 } elsif ($text =~ /u32max/) {
34 return 2**32 - 1;
35 } elsif ($text =~ /u16max/) {
36 return 2**16 - 1;
38 if ($text =~ /\((.*?)\)/) {
39 $text = $1;
41 if (!($text =~ /^[-0123456789]/)) {
42 return "NaN";
45 return int($text);
48 sub add_range($$$)
50 my $union = shift;
51 my $min = shift;
52 my $max = shift;
53 my %range;
54 my @return_union;
55 my $added = 0;
56 my $check_next = 0;
58 $range{min} = $min;
59 $range{max} = $max;
61 foreach my $tmp (@$union) {
62 if ($added) {
63 push @return_union, $tmp;
64 next;
67 if ($range{max} < $tmp->{min}) {
68 push @return_union, \%range;
69 push @return_union, $tmp;
70 $added = 1;
71 } elsif ($range{min} <= $tmp->{min}) {
72 if ($range{max} <= $tmp->{max}) {
73 $range{max} = $tmp->{max};
74 push @return_union, \%range;
75 $added = 1;
77 } elsif ($range{min} <= $tmp->{max}) {
78 if ($range{max} <= $tmp->{max}) {
79 push @return_union, $tmp;
80 $added = 1;
81 } else {
82 $range{min} = $tmp->{min};
84 } else {
85 push @return_union, $tmp;
89 if (!$added) {
90 push @return_union, \%range;
93 return \@return_union;
96 sub print_num($)
98 my $num = shift;
100 if ($num < 0) {
101 return "(" . $num . ")";
102 } else {
103 return $num;
107 sub print_range($)
109 my $range = shift;
111 if ($range->{min} == $range->{max}) {
112 return print_num($range->{min});
113 } else {
114 return print_num($range->{min}) . "-" . print_num($range->{max});
118 sub print_info($$)
120 my $type = shift;
121 my $union = shift;
122 my $printed_range = "";
123 my $i = 0;
125 foreach my $range (@$union) {
126 if ($i) {
127 $printed_range = $printed_range . ",";
129 $i++;
130 $printed_range = $printed_range . print_range($range);
132 my $sql = "insert into type_value values ('$type', '$printed_range');";
133 $db->do($sql);
137 $db->do("PRAGMA synchronous = OFF");
138 $db->do("PRAGMA cache_size = 800000");
139 $db->do("PRAGMA journal_mode = OFF");
140 $db->do("PRAGMA count_changes = OFF");
141 $db->do("PRAGMA temp_store = MEMORY");
142 $db->do("PRAGMA locking = EXCLUSIVE");
144 my ($sth, @row, $cur_type, $type, @ranges, $range_txt, %range, $min, $max, $union_array, $skip);
146 $sth = $db->prepare('select type, value from function_type_value order by type');
147 $sth->execute();
149 $skip = 0;
150 $cur_type = "";
151 while (@row = $sth->fetchrow_array()) {
152 $type = $row[0];
154 if ($cur_type ne "$type") {
155 if ($cur_type ne "" && $skip == 0) {
156 print_info($cur_type, $union_array);
158 $cur_type = $type;
159 $union_array = ();
160 $skip = 0;
163 if ($skip == 1) {
164 next;
167 @ranges = split(/,/, $row[1]);
168 foreach $range_txt (@ranges) {
169 if ($range_txt =~ /ignore/) {
170 next;
172 if ($range_txt =~ /(.*[^(])-(.*)/) {
173 $min = text_to_int($1);
174 $max = text_to_int($2);
175 } else {
176 $min = text_to_int($range_txt);
177 $max = $min;
179 if ($min =~ /NaN/ || $max =~ /NaN/) {
180 $skip = 1;
181 last;
183 $union_array = add_range($union_array, $min, $max);
186 if ($skip == 0) {
187 print_info($cur_type, $union_array);
190 $db->commit();
191 $db->disconnect();