OSDN Git Service

ae146babb877d24f435151db70e7cc01c1db056f
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / testsuite_abi.cc
1 // -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 3, or (at
8 // your option) any later version.
9
10 // This library is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING3.  If not see
17 // <http://www.gnu.org/licenses/>.
18
19
20 // Benjamin Kosnik  <bkoz@redhat.com>
21
22 #include "testsuite_abi.h"
23 #include <cstdlib>
24 #include <sstream>
25 #include <fstream>
26 #include <iostream>
27 #include <vector>
28 #include <algorithm>
29
30 using namespace std;
31
32 void 
33 symbol::init(string& data)
34 {
35   const char delim = ':';
36   const char version_delim = '@';
37   const string::size_type npos = string::npos;
38   string::size_type n = 0;
39
40   // Set the type.
41   if (data.find("FUNC") == 0)
42     type = symbol::function;
43   else if (data.find("OBJECT") == 0)
44     type = symbol::object;
45
46   n = data.find_first_of(delim);
47   if (n != npos)
48     data.erase(data.begin(), data.begin() + n + 1);
49
50   // Iff object, get size info.
51   if (type == symbol::object)
52     {
53       n = data.find_first_of(delim);
54       if (n != npos)
55         {
56           string objectsize(data.begin(), data.begin() + n);
57           istringstream iss(objectsize);
58           int x;
59           iss >> x;
60           if (!iss.fail())
61             size = x;
62           data.erase(data.begin(), data.begin() + n + 1);
63         }
64     }
65
66   // Set the name and raw_name.
67   raw_name = string(data.begin(), data.end());
68   n = data.find_first_of(version_delim);
69   if (n != npos)
70     {
71       // Found version string.
72       name = string(data.begin(), data.begin() + n);
73       n = data.find_last_of(version_delim);
74       data.erase(data.begin(), data.begin() + n + 1);
75
76       // Set version name.
77       version_name = data;
78     }
79   else
80     {
81       // No versioning info.
82       name = string(data.begin(), data.end());
83       version_status = symbol::none;
84     }
85
86   // Set the demangled name.
87   demangled_name = demangle(name);
88 }
89
90 void
91 symbol::print() const
92 {
93   const char tab = '\t';
94   cout << name << endl;
95
96   if (demangled_name != name)
97     cout << demangled_name << endl;
98
99   string vers;
100   switch (version_status)
101     {
102     case none:
103       vers = "none";
104       break;
105     case compatible:
106       vers = "compatible";
107       break;
108     case incompatible:
109       vers = "incompatible";
110       break;
111      case unversioned:
112       vers = "unversioned";
113       break;
114    default:
115       vers = "<default>";
116     }
117   cout << "version status: " << vers << endl;
118
119   if (version_name.size() 
120       && (version_status == compatible || version_status == incompatible))
121     cout << version_name << endl;  
122
123   string type_string;
124   switch (type)
125     {
126     case function:
127       type_string = "function";
128       break;
129     case object:
130       type_string = "object";
131       break;
132     case uncategorized:
133       type_string = "uncategorized";
134       break;
135     default:
136       type_string = "<default>";
137     }
138   cout << "type: " << type_string << endl;
139   
140   if (type == object)
141     cout << "type size: " << size << endl;
142
143   string status_string;
144   switch (status)
145     {
146     case added:
147       status_string = "added";
148       break;
149     case subtracted:
150       status_string = "subtracted";
151       break;
152     case undesignated:
153       status_string = "undesignated";
154       break;
155     default:
156       status_string = "<default>";
157     }
158   cout << "status: " << status_string << endl;
159
160   cout << endl;
161 }
162
163
164 bool
165 check_version(symbol& test, bool added)
166 {
167   // Construct list of compatible versions.
168   typedef std::vector<std::string> compat_list;
169   static compat_list known_versions;
170   if (known_versions.empty())
171     {
172       // NB: First version here must be the default version for this
173       // version of DT_SONAME.
174       known_versions.push_back("GLIBCXX_3.4");
175       known_versions.push_back("GLIBCXX_3.4.1");
176       known_versions.push_back("GLIBCXX_3.4.2");
177       known_versions.push_back("GLIBCXX_3.4.3");
178       known_versions.push_back("GLIBCXX_3.4.4"); 
179       known_versions.push_back("GLIBCXX_3.4.5");
180       known_versions.push_back("GLIBCXX_3.4.6");
181       known_versions.push_back("GLIBCXX_3.4.7");
182       known_versions.push_back("GLIBCXX_3.4.8");
183       known_versions.push_back("GLIBCXX_3.4.9");
184       known_versions.push_back("GLIBCXX_3.4.10");
185       known_versions.push_back("GLIBCXX_3.4.11");
186       known_versions.push_back("GLIBCXX_3.4.12");
187       known_versions.push_back("GLIBCXX_3.4.13");
188       known_versions.push_back("GLIBCXX_LDBL_3.4");
189       known_versions.push_back("GLIBCXX_LDBL_3.4.7");
190       known_versions.push_back("GLIBCXX_LDBL_3.4.10");
191       known_versions.push_back("CXXABI_1.3");
192       known_versions.push_back("CXXABI_1.3.1");
193       known_versions.push_back("CXXABI_1.3.2");
194       known_versions.push_back("CXXABI_1.3.3");
195       known_versions.push_back("CXXABI_LDBL_1.3");
196     }
197   compat_list::iterator begin = known_versions.begin();
198   compat_list::iterator end = known_versions.end();
199
200   // Check for compatible version.
201   if (test.version_name.size())
202     {
203       compat_list::iterator it1 = find(begin, end, test.version_name);
204       compat_list::iterator it2 = find(begin, end, test.name);
205       if (it1 != end)
206         test.version_status = symbol::compatible;
207       else
208         test.version_status = symbol::incompatible;
209       
210       // Check that added symbols aren't added in the base version.
211       if (added && test.version_name == known_versions[0])
212         test.version_status = symbol::incompatible;
213       
214       // Check that long double compatibility symbols demangled as
215       // __float128 are put into some _LDBL_ version name.
216       if (added && test.demangled_name.find("__float128") != std::string::npos)
217         {
218           // Has to be in _LDBL_ version name.
219           if (test.version_name.find("_LDBL_") == std::string::npos)
220             test.version_status = symbol::incompatible;
221         }
222
223       // Check for weak label.
224       if (it1 == end && it2 == end)
225         test.version_status = symbol::incompatible;
226       
227       // Check that 
228       // GLIBCXX_3.4
229       // GLIBCXX_3.4.5
230       // version as compatible
231       // XXX
232     }
233   else
234     {
235       if (added)
236         {
237           // New version labels are ok. The rest are not.
238           compat_list::iterator it2 = find(begin, end, test.name);
239           if (it2 != end)
240             test.version_status = symbol::compatible;
241           else
242             test.version_status = symbol::incompatible;
243         }
244     }
245   return test.version_status == symbol::compatible;
246 }
247
248 bool 
249 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
250 {
251   bool ret = true;
252   const char tab = '\t';
253
254   // Check to see if symbol_objects are compatible.
255   if (lhs.type != rhs.type)
256     {
257       ret = false;
258       if (verbose)
259         cout << tab << "incompatible types" << endl;
260     }
261   
262   if (lhs.name != rhs.name)
263     {
264       ret = false;
265       if (verbose)
266         cout << tab << "incompatible names" << endl;
267     }
268
269   if (lhs.size != rhs.size)
270     {
271       ret = false;
272       if (verbose)
273         {
274           cout << tab << "incompatible sizes" << endl;
275           cout << tab << lhs.size << endl;
276           cout << tab << rhs.size << endl;
277         }
278     }
279
280   if (lhs.version_name != rhs.version_name 
281       && !check_version(lhs) && !check_version(rhs))
282     {
283       ret = false;
284       if (verbose)
285         {
286           cout << tab << "incompatible versions" << endl;
287           cout << tab << lhs.version_name << endl;
288           cout << tab << rhs.version_name << endl;
289         }
290     }
291
292   if (verbose)
293     cout << endl;
294
295   return ret;
296 }
297
298
299 inline bool
300 has_symbol(const string& name, const symbols& s) throw()
301 { return s.find(name) != s.end(); }
302
303 const symbol&
304 get_symbol(const string& name, const symbols& s)
305 {
306   symbols::const_iterator i = s.find(name);
307   if (i != s.end())
308     {
309       return i->second;
310     }
311   else
312     {
313       ostringstream os;
314       os << "get_symbol failed for symbol " << name;
315       __throw_logic_error(os.str().c_str());
316     }
317 }
318
319 void 
320 examine_symbol(const char* name, const char* file)
321 {
322   try
323     {
324       symbols s = create_symbols(file);
325       const symbol& sym = get_symbol(name, s);
326       sym.print();
327     }
328   catch(...)
329     { __throw_exception_again; }
330 }
331
332 int
333 compare_symbols(const char* baseline_file, const char* test_file, 
334                 bool verbose)
335 {
336   // Input both lists of symbols into container.
337   symbols baseline = create_symbols(baseline_file);
338   symbols test = create_symbols(test_file);
339
340   //  Sanity check results.
341   if (!baseline.size() || !test.size())
342     {
343       cerr << "Problems parsing the list of exported symbols." << endl;
344       exit(2);
345     }
346
347   // Check to see if any long double compatibility symbols are produced.
348   bool ld_version_found(false);
349   symbols::iterator li(test.begin());
350   while (!ld_version_found && li != test.end())
351     {
352       if (li->second.version_name.find("_LDBL_") != std::string::npos)
353         ld_version_found = true;
354       ++li;
355     }
356
357   // Sort out names.
358   // Assuming all baseline names and test names are both unique w/ no
359   // duplicates.
360   //
361   // The names added to missing_names are baseline names not found in
362   // test names 
363   // -> symbols that have been deleted.
364   //
365   // The names added to added_names are test names not in
366   // baseline names
367   // -> symbols that have been added.
368   typedef std::vector<std::string> symbol_names;
369   symbol_names shared_names;
370   symbol_names missing_names;
371   symbol_names added_names;
372   for (li = test.begin(); li != test.end(); ++li)
373     added_names.push_back(li->first);
374
375   for (symbols::iterator i = baseline.begin(); i != baseline.end(); ++i)
376     {
377       string name(i->first);
378       symbol_names::iterator end = added_names.end();
379       symbol_names::iterator it = find(added_names.begin(), end, name);
380       if (it != end)
381         {
382           // Found.
383           shared_names.push_back(name);
384           added_names.erase(it);
385         }
386        else
387         {
388           // Iff no test long double compatibility symbols at all and the symbol
389           // missing is a baseline long double compatibility symbol, skip.
390           string version_name(i->second.version_name);
391           bool base_ld(version_name.find("_LDBL_") != std::string::npos);
392           if (!base_ld || base_ld && ld_version_found)
393             missing_names.push_back(name);
394         }
395     }
396
397   // Fill out list of incompatible symbols.
398   typedef pair<symbol, symbol> symbol_pair;
399   vector<symbol_pair> incompatible;
400
401   // Check missing names for compatibility.
402   for (size_t j = 0; j < missing_names.size(); ++j)
403     {
404       symbol& sbase = baseline[missing_names[j]];
405       sbase.status = symbol::subtracted;
406       incompatible.push_back(symbol_pair(sbase, sbase));
407     }
408
409   // Check shared names for compatibility.
410   const symbol_names::size_type shared_size = shared_names.size();
411   for (size_t k = 0; k < shared_size; ++k)
412     {
413       symbol& sbase = baseline[shared_names[k]];
414       symbol& stest = test[shared_names[k]];
415       stest.status = symbol::existing;
416       if (!check_compatible(sbase, stest))
417         incompatible.push_back(symbol_pair(sbase, stest));
418     }
419
420   // Check added names for compatibility.
421   const symbol_names::size_type added_size = added_names.size();
422   for (size_t l = 0; l < added_size; ++l)
423     {
424       symbol& stest = test[added_names[l]];
425       stest.status = symbol::added;
426       if (!check_version(stest, true))
427         incompatible.push_back(symbol_pair(stest, stest));
428     }
429
430   // Report results.
431   if (verbose && added_names.size())
432     {
433       cout << endl << added_names.size() << " added symbols " << endl;
434       for (size_t j = 0; j < added_names.size() ; ++j)
435         {
436           cout << j << endl;
437           test[added_names[j]].print();
438         }
439     }
440   
441   if (verbose && missing_names.size())
442     {
443       cout << endl << missing_names.size() << " missing symbols " << endl;
444       for (size_t j = 0; j < missing_names.size() ; ++j)
445         {
446           cout << j << endl;
447           baseline[missing_names[j]].print();
448         }
449     }
450   
451   if (verbose && incompatible.size())
452     {
453       cout << endl << incompatible.size() << " incompatible symbols " << endl;
454       for (size_t j = 0; j < incompatible.size() ; ++j)
455         {
456           // First, print index.
457           cout << j << endl;
458
459           // Second, report name.
460           symbol& sbase = incompatible[j].first;
461           symbol& stest = incompatible[j].second;
462           stest.print();
463           
464           // Second, report reason or reasons incompatible.
465           check_compatible(sbase, stest, true);
466         }
467     }
468   
469   cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
470   cout << endl;
471   cout << "# of added symbols:\t\t " << added_names.size() << endl;
472   cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
473   cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
474   cout << endl;
475   cout << "using: " << baseline_file << endl;
476
477   return !(missing_names.size() || incompatible.size());
478 }
479
480
481 symbols
482 create_symbols(const char* file)
483 {
484   symbols s;
485   ifstream ifs(file);
486   if (ifs.is_open())
487     {
488       // Organize file data into an associated container (symbols) of symbol
489       // objects mapped to mangled names without versioning
490       // information.
491       const string empty;
492       string line = empty;
493       while (getline(ifs, line).good())
494         {
495           symbol tmp;
496           tmp.init(line);
497           s[tmp.name] = tmp;
498           line = empty;
499         }
500     }
501   else
502     {
503       ostringstream os;
504       os << "create_symbols failed for file " << file;
505       __throw_runtime_error(os.str().c_str());
506     }
507   return s;
508 }
509
510
511 const char*
512 demangle(const std::string& mangled)
513 {
514   const char* name;
515   if (mangled[0] != '_' || mangled[1] != 'Z')
516     {
517       // This is not a mangled symbol, thus has "C" linkage.
518       name = mangled.c_str();
519     }
520   else
521     {
522       // Use __cxa_demangle to demangle.
523       int status = 0;
524       name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
525       if (!name)
526         {
527           switch (status)
528             {
529             case 0:
530               name = "error code = 0: success";
531               break;
532             case -1:
533               name = "error code = -1: memory allocation failure";
534               break;
535             case -2:
536               name = "error code = -2: invalid mangled name";
537               break;
538             case -3:
539               name = "error code = -3: invalid arguments";
540               break;
541             default:
542               name = "error code unknown - who knows what happened";
543             }
544         }
545     }
546   return name;
547 }
548