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