Debian Codegraph @

perllib/Debian/PkgKde/SymbolsHelper/Symbol.pm

raw
1 # Copyright (C) 2008-2010 Modestas Vainius <[email protected]>
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>
15
16 package Debian::PkgKde::SymbolsHelper::Symbol;
17
18 use strict;
19 use warnings;
20 use base 'Dpkg::Shlibs::Symbol';
21
22 use Dpkg::Gettext;
23 use Dpkg::Shlibs::Cppfilt;
24 use Dpkg::Arch qw(get_valid_arches);
25 use Dpkg::ErrorHandling;
26 use Debian::PkgKde::SymbolsHelper::Substs;
27 use Debian::PkgKde::SymbolsHelper::String;
28
29 sub get_h_name {
30 my $self = shift;
31 if (!exists $self->{h_name}) {
32 $self->{h_name} = Debian::PkgKde::SymbolsHelper::String->new(
33 $self->get_symbolname()
34 );
35 if (exists $self->{substs}) {
36 # We need to recreate string2 from the templ
37 $self->{h_name}->init_string2_by_re($self->get_symboltempl(),
38 qr/\{([^}]+)\}/, $self->{substs});
39 if ($self->{h_name}->get_string2() ne $self->get_symboltempl()) {
40 internerr("unsupported substitutions/alterations in the symbol template '%s'. " .
41 "Parsed as '%s'. Cannot continue.",
42 $self->get_symboltempl(), $self->{h_name}->get_string2());
43 }
44 }
45 }
46 return $self->{h_name};
47 }
48
49 sub reset_h_name {
50 my ($self, $new_h_name) = @_;
51 if (defined $new_h_name) {
52 $self->{h_name} = $new_h_name;
53 } else {
54 delete $self->{h_name};
55 }
56 }
57
58 sub resync_name_with_h_name {
59 my $self = shift;
60 if (exists $self->{h_name}) {
61 my $h_name = $self->{h_name};
62 $self->set_symbolname($h_name->get_string(), $h_name->get_string2());
63 }
64 }
65
66 # Needed for h_name above
67 sub get_substs {
68 my $self = shift;
69 return $self->{substs};
70 }
71
72 sub is_trunk_symbol {
73 my $self = shift;
74 my $name = shift || $self->get_symbolname();
75 # Based on: binutils/libiberty/cp-demangle.c:d_special_name()
76 # c - DEMANGLE_COMPONENT_COVARIANT_THUNK: covariant return thunk to
77 # C - DEMANGLE_COMPONENT_CONSTRUCTION_VTABLE: construction vtable for
78 # h - DEMANGLE_COMPONENT_THUNK: non-virtual thunk to
79 # v - DEMANGLE_COMPONENT_VIRTUAL_THUNK: virtual thunk to
80 return $name =~ /^_ZT[Cchv]/;
81 }
82
83 sub initialize {
84 my ($self, %opts) = @_;
85
86 # Expand substvars
87 if ($self->has_tag('subst')) {
88 # Expand substitutions in the symbol name. See below.
89 if ($self->expand_substitutions(%opts) == 0) {
90 # Redundant subst tag. Warn.
91 warning(g_("%s: no valid substitutions, 'subst' tag is redundant"),
92 $self->get_symbolname());
93 }
94 }
95
96 return $self->SUPER::initialize(%opts);
97 }
98
99 sub expand_substitutions {
100 my ($self, %opts) = @_;
101 my $symbol = $self->get_symbolname();
102 my %substs;
103
104 # Collect substitutions in the symbol name
105 while ($symbol =~ /\{(([^}=]+)(?:=([^}]+))?)\}/g) {
106 my $subst = $1;
107 my $name = $2;
108 my $val = $3;
109 unless (exists $substs{$name}) {
110 my $substobj = $SUBSTS{$name};
111 if (defined $subst) {
112 $substs{$subst} = $substobj->expand($opts{arch}, $val);
113 if (!defined $substs{$subst}) {
114 error(g_("%s: unable to expand symbol substitution '%s'"), $symbol, $subst);
115 }
116 } # If not defined, silently ignore.
117 }
118 }
119
120 # Expand substitutions
121 for my $subst (keys %substs) {
122 $symbol =~ s/\Q{$subst}\E/$substs{$subst}/g;
123 }
124
125 $self->{symbol} = $symbol;
126 $self->{substs} = \%substs;
127 return keys %substs;
128 }
129
130 sub get_cppname {
131 my $self = shift;
132 unless (exists $self->{cppname}) {
133 $self->{cppname} = ($self->get_symbolname() =~ /^_Z/) ?
134 cppfilt_demangle_cpp($self->get_symbolname()) :
135 undef;
136 }
137 return $self->{cppname};
138 }
139
140 sub detect_cpp_templinst() {
141 my $self = shift;
142
143 my $cppname = $self->get_cppname();
144 if (defined $cppname) {
145 # Prepare for tokenizing: wipe out unnecessary spaces
146 $cppname =~ s/([,<>()])\s+/$1/g;
147 $cppname =~ s/\s+([,<>()])/$1/g;
148 $cppname =~ s/\s*((?:(?:un)?signed|volatile|restrict|const|long)[*&]*)\s*/$1/g;
149 if (my @tokens = split(/\s+/, $cppname)) {
150 my $func;
151 if ($tokens[0] =~ /[(]/) {
152 $func = $tokens[0];
153 } elsif ($#tokens >= 1 && $tokens[1] =~ /[(]/) {
154 # The first token was return type, try the second
155 $func = $tokens[1];
156 }
157 if (defined $func && $func =~ /<[^>]+>[^(]*[(]/) {
158 return 1;
159 }
160 }
161 }
162 return 0;
163 }
164
165 # Typically template instantiations are not useful public symbols
166 sub mark_cpp_templinst_as_optional {
167 my ($self, @tag) = @_;
168 @tag = ("optional", "templinst") unless @tag;
169 if (!$self->is_optional() && $self->detect_cpp_templinst()) {
170 $self->add_tag(@tag);
171 }
172 }
173
174 # Converts symbol template to c++ alias converting substitutions as well.
175 # Returns converted template string or undef in case of failure.
176 sub convert_templ_to_cpp_alias {
177 my ($self, $templ) = @_;
178 $templ = $self->get_symboltempl() unless defined $templ;
179 my $result;
180
181 return undef unless $templ =~ /^_Z/;
182
183 if (! $self->has_tag('subst')) {
184 $result = cppfilt_demangle_cpp($templ);
185 } else {
186 my (%mangled, @possible_substs);
187
188 # Collect possible symbol variants by expanding on all valid arches
189 foreach my $arch (get_valid_arches()) {
190 $self->{symbol} = $templ;
191 @possible_substs = $self->expand_substitutions(arch => $arch);
192 push @{$mangled{$self->get_symbolname()}}, $arch;
193 }
194
195 # Prepare for checking of demangled symbols
196 my (@demangled, @arches);
197 foreach my $mangled (keys %mangled) {
198 my $d = cppfilt_demangle_cpp($mangled);
199 # Fail immediatelly if couldn't demangle a variant
200 return undef unless defined $d;
201
202 # Tokenize
203 push @demangled, [ split(/\b/, $d) ];
204 push @arches, $mangled{$mangled};
205 }
206
207 # Create a subst expansion result map for $main_arch
208 my $main_arch = $arches[0][0];
209 my %cppmap;
210 foreach my $subst (@possible_substs) {
211 my $name = "c++:$subst";
212 # Can't handle a subst which does not have a c++ replacement
213 if (exists $SUBSTS{$name}) {
214 my $cppsubst = $SUBSTS{$name};
215 push @{$cppmap{$cppsubst->expand($main_arch)}}, $cppsubst;
216 } else {
217 return undef;
218 }
219 }
220
221 # Now do detection
222 my @result;
223 my @expanded_size = map { 0 } @demangled;
224 while (@{$demangled[0]} > 0) {
225 # Check if the token is not the same in all symbols (i.e. no subst here)
226 my $token = shift @{$demangled[0]};
227 my $ok = 1;
228 for (my $i = 1; $i < @demangled; $i++) {
229 if ($token ne $demangled[$i][0]) {
230 $ok = 0;
231 last;
232 }
233 }
234 if ($ok) {
235 # Tokens match. Get next token and push to @result
236 for (my $i = 1; $i < @demangled; $i++) {
237 shift @{$demangled[$i]};
238 }
239 push @result, $token;
240 } else {
241 # Tokens do not match. We need to guess a subst
242 my $found_subst;
243
244 # Determine a set of candidate substs by expansion result
245 # for $main_arch
246 while (!exists $cppmap{$token}) {
247 my $next = shift @{$demangled[0]};
248 if (!defined $next) {
249 return undef;
250 }
251 # Add up next token to this one and check again
252 $token .= $next;
253 }
254
255 # If we are here, a set of candidate substs has been found
256 my $cand_substs = $cppmap{$token};
257
258 # Now we need to pick a right candidate from candidates
259 next_candidate: for my $cand_subst (@$cand_substs) {
260 # Expansion must be the same on index 0 arches
261 foreach my $arch (@{$arches[0]}) {
262 if ($cand_subst->expand($arch) ne $token) {
263 next next_candidate;
264 }
265 }
266
267 # On 1+ arches, $subst->expand() and our $demangled value must match
268 for (my $i = 1; $i < @arches; $i++) {
269 my $archset = $arches[$i];
270 my $expanded = $cand_subst->expand($archset->[0]);
271
272 # Check if expansion is the same on all arches in the current set
273 foreach my $arch (@$archset) {
274 if ($expanded ne $cand_subst->expand($arch)) {
275 next next_candidate;
276 }
277 }
278
279 # Now actually check if $expanded matches what was demangled
280 $expanded_size[$i] = scalar(my @s = split(/\b/, $expanded));
281 if (join("", @{$demangled[$i]}[0..($expanded_size[$i]-1)]) ne $expanded) {
282 next next_candidate;
283 }
284 }
285
286 # If we are here, candidate has been confirmed
287 $found_subst = $cand_subst;
288 last;
289 }
290
291 if (defined $found_subst) {
292 for (my $i = 1; $i < @demangled; $i++) {
293 splice @{$demangled[$i]}, 0, ($expanded_size[$i]);
294 }
295 push @result, '{' . $found_subst->get_name() . '}';
296 } else {
297 # Unable to find an appropriate subst
298 return undef;
299 }
300 }
301 }
302
303 foreach my $demangled (@demangled) {
304 # Fail if demangling was not complete
305 return undef if @$demangled > 0;
306 }
307
308 $result = join("", @result);
309 }
310 return $result;
311 }
312
313 sub upgrade_virtual_table_symbol {
314 my ($self, $arch) = @_;
315 if ($self->is_trunk_symbol($self->get_symboltempl())) {
316 my $newtempl = $self->convert_templ_to_cpp_alias();
317 if (defined $newtempl) {
318 $self->set_symbolname($newtempl, $newtempl);
319 $self->add_tag("c++");
320 # Finally, reinitialize
321 $self->initialize(arch => $arch);
322 }
323 return $newtempl;
324 }
325 return undef;
326 }
327
328 sub set_min_version {
329 my ($self, $version, %opts) = @_;
330
331 $self->{minver} = $version
332 if ($opts{with_deprecated} || !$self->{deprecated});
333 }
334
335 sub normalize_min_version {
336 my ($self, %opts) = @_;
337
338 if ($opts{with_deprecated} || !$self->{deprecated}) {
339 my $minver = $self->{minver};
340 if ($minver =~ m/-.*[^~]$/) {
341 unless($minver =~ s/-[01](?:$|[^\d-][^-]*$)//) {
342 $minver =~ s/([^~])$/$1~/;
343 }
344 $self->{minver} = $minver;
345 }
346 }
347 }
348
349 sub handle_min_version {
350 my ($self, $version, %opts) = @_;
351
352 if (defined $version) {
353 if ($version) {
354 return $self->set_min_version($version, %opts);
355 } else {
356 return $self->normalize_min_version(%opts);
357 }
358 }
359 }
360
361 1;