2005-12-18 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_abi.cc
blobd0e65afcf597b1f1f6a8f46822e8f5727afd06f6
1 // -*- C++ -*-
3 // Copyright (C) 2004, 2005 Free Software Foundation, Inc.
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 2, or (at
8 // your option) any later version.
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.
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING. If not, write to
17 // the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
20 // As a special exception, you may use this file as part of a free
21 // software library without restriction. Specifically, if other files
22 // instantiate templates or use macros or inline functions from this
23 // file, or you compile this file and link it with other files to
24 // produce an executable, this file does not by itself cause the
25 // resulting executable to be covered by the GNU General Public
26 // License. This exception does not however invalidate any other
27 // reasons why the executable file might be covered by the GNU General
28 // Public License.
30 // Benjamin Kosnik <bkoz@redhat.com>
32 #include "testsuite_abi.h"
33 #include <sstream>
34 #include <fstream>
35 #include <iostream>
37 using namespace std;
39 void
40 symbol::init(string& data)
42 const char delim = ':';
43 const char version_delim = '@';
44 const string::size_type npos = string::npos;
45 string::size_type n = 0;
47 // Set the type.
48 if (data.find("FUNC") == 0)
49 type = symbol::function;
50 else if (data.find("OBJECT") == 0)
51 type = symbol::object;
53 n = data.find_first_of(delim);
54 if (n != npos)
55 data.erase(data.begin(), data.begin() + n + 1);
57 // Iff object, get size info.
58 if (type == symbol::object)
60 n = data.find_first_of(delim);
61 if (n != npos)
63 string size(data.begin(), data.begin() + n);
64 istringstream iss(size);
65 int x;
66 iss >> x;
67 if (!iss.fail())
68 size = x;
69 data.erase(data.begin(), data.begin() + n + 1);
73 // Set the name and raw_name.
74 raw_name = string(data.begin(), data.end());
75 n = data.find_first_of(version_delim);
76 if (n != npos)
78 // Found version string.
79 name = string(data.begin(), data.begin() + n);
80 n = data.find_last_of(version_delim);
81 data.erase(data.begin(), data.begin() + n + 1);
83 // Set version name.
84 version_name = data;
86 else
88 // No versioning info.
89 name = string(data.begin(), data.end());
90 version_status = symbol::none;
93 // Set the demangled name.
94 demangled_name = demangle(name);
97 void
98 symbol::print() const
100 const char tab = '\t';
101 cout << name << endl;
103 if (demangled_name != name)
104 cout << demangled_name << endl;
106 string vers;
107 switch (version_status)
109 case none:
110 vers = "none";
111 break;
112 case compatible:
113 vers = "compatible";
114 break;
115 case incompatible:
116 vers = "incompatible";
117 break;
118 case unversioned:
119 vers = "unversioned";
120 break;
121 default:
122 vers = "<default>";
124 cout << "version status: " << vers << endl;
126 if (version_name.size()
127 && (version_status == compatible || version_status == incompatible))
128 cout << version_name << endl;
130 string type_string;
131 switch (type)
133 case function:
134 type_string = "function";
135 break;
136 case object:
137 type_string = "object";
138 break;
139 case uncategorized:
140 type_string = "uncategorized";
141 break;
142 default:
143 type_string = "<default>";
145 cout << "type: " << type_string << endl;
147 if (type == object)
148 cout << "type size: " << size << endl;
150 string status_string;
151 switch (status)
153 case added:
154 status_string = "added";
155 break;
156 case subtracted:
157 status_string = "subtracted";
158 break;
159 case undesignated:
160 status_string = "undesignated";
161 break;
162 default:
163 status_string = "<default>";
165 cout << "status: " << status_string << endl;
167 cout << endl;
171 bool
172 check_version(symbol& test, bool added)
174 // Construct list of compatible versions.
175 typedef std::vector<std::string> compat_list;
176 static compat_list known_versions;
177 if (known_versions.empty())
179 // NB: First version here must be the default version for this
180 // version of DT_SONAME.
181 known_versions.push_back("GLIBCXX_3.4");
182 known_versions.push_back("GLIBCXX_3.4.1");
183 known_versions.push_back("GLIBCXX_3.4.2");
184 known_versions.push_back("GLIBCXX_3.4.3");
185 known_versions.push_back("GLIBCXX_3.4.4");
186 known_versions.push_back("GLIBCXX_3.4.5");
187 known_versions.push_back("GLIBCXX_3.4.6");
188 known_versions.push_back("GLIBCXX_3.4.7");
189 known_versions.push_back("CXXABI_1.3");
190 known_versions.push_back("CXXABI_1.3.1");
192 compat_list::iterator begin = known_versions.begin();
193 compat_list::iterator end = known_versions.end();
195 // Check for compatible version.
196 if (test.version_name.size())
198 compat_list::iterator it1 = find(begin, end, test.version_name);
199 compat_list::iterator it2 = find(begin, end, test.name);
200 if (it1 != end)
201 test.version_status = symbol::compatible;
202 else
203 test.version_status = symbol::incompatible;
205 // Check that added symbols aren't added in the base version.
206 if (added && test.version_name == known_versions[0])
207 test.version_status = symbol::incompatible;
209 // Check for weak label.
210 if (it1 == end && it2 == end)
211 test.version_status = symbol::incompatible;
213 // Check that
214 // GLIBCXX_3.4
215 // GLIBCXX_3.4.5
216 // version as compatible
217 // XXX
219 else
221 if (added)
223 // New version labels are ok. The rest are not.
224 compat_list::iterator it2 = find(begin, end, test.name);
225 if (it2 != end)
227 test.version_status = symbol::compatible;
229 else
230 test.version_status = symbol::incompatible;
233 return test.version_status == symbol::compatible;
236 bool
237 check_compatible(symbol& lhs, symbol& rhs, bool verbose)
239 bool ret = true;
240 const char tab = '\t';
242 // Check to see if symbol_objects are compatible.
243 if (lhs.type != rhs.type)
245 ret = false;
246 if (verbose)
247 cout << tab << "incompatible types" << endl;
250 if (lhs.name != rhs.name)
252 ret = false;
253 if (verbose)
254 cout << tab << "incompatible names" << endl;
257 if (lhs.size != rhs.size)
259 ret = false;
260 if (verbose)
262 cout << tab << "incompatible sizes" << endl;
263 cout << tab << lhs.size << endl;
264 cout << tab << rhs.size << endl;
268 if (lhs.version_name != rhs.version_name
269 && !check_version(lhs) && !check_version(rhs))
271 ret = false;
272 if (verbose)
274 cout << tab << "incompatible versions" << endl;
275 cout << tab << lhs.version_name << endl;
276 cout << tab << rhs.version_name << endl;
280 if (verbose)
281 cout << endl;
283 return ret;
287 bool
288 has_symbol(const string& mangled, const symbols& s) throw()
290 const symbol_names& names = s.first;
291 symbol_names::const_iterator i = find(names.begin(), names.end(), mangled);
292 return i != names.end();
295 symbol&
296 get_symbol(const string& mangled, const symbols& s)
298 const symbol_names& names = s.first;
299 symbol_names::const_iterator i = find(names.begin(), names.end(), mangled);
300 if (i != names.end())
302 symbol_objects objects = s.second;
303 return objects[mangled];
305 else
307 ostringstream os;
308 os << "get_symbol failed for symbol " << mangled;
309 __throw_logic_error(os.str().c_str());
313 void
314 examine_symbol(const char* name, const char* file)
318 symbols s = create_symbols(file);
319 symbol& sym = get_symbol(name, s);
320 sym.print();
322 catch(...)
323 { __throw_exception_again; }
327 compare_symbols(const char* baseline_file, const char* test_file,
328 bool verbose)
330 // Input both lists of symbols into container.
331 symbols baseline = create_symbols(baseline_file);
332 symbols test = create_symbols(test_file);
333 symbol_names& baseline_names = baseline.first;
334 symbol_objects& baseline_objects = baseline.second;
335 symbol_names& test_names = test.first;
336 symbol_objects& test_objects = test.second;
338 // Sanity check results.
339 const symbol_names::size_type baseline_size = baseline_names.size();
340 const symbol_names::size_type test_size = test_names.size();
341 if (!baseline_size || !test_size)
343 cerr << "Problems parsing the list of exported symbols." << endl;
344 exit(2);
347 // Sort out names.
348 // Assuming baseline_names, test_names are both unique w/ no duplicates.
350 // The names added to missing_names are baseline_names not found in
351 // test_names
352 // -> symbols that have been deleted.
354 // The names added to added_names are test_names not in
355 // baseline_names
356 // -> symbols that have been added.
357 symbol_names shared_names;
358 symbol_names missing_names;
359 symbol_names added_names = test_names;
360 for (size_t i = 0; i < baseline_size; ++i)
362 string what(baseline_names[i]);
363 symbol_names::iterator end = added_names.end();
364 symbol_names::iterator it = find(added_names.begin(), end, what);
365 if (it != end)
367 // Found.
368 shared_names.push_back(what);
369 added_names.erase(it);
371 else
372 missing_names.push_back(what);
375 // Check missing names for compatibility.
376 typedef pair<symbol, symbol> symbol_pair;
377 vector<symbol_pair> incompatible;
378 for (size_t j = 0; j < missing_names.size(); ++j)
380 symbol& base = baseline_objects[missing_names[j]];
381 base.status = symbol::subtracted;
382 incompatible.push_back(symbol_pair(base, base));
385 // Check shared names for compatibility.
386 for (size_t k = 0; k < shared_names.size(); ++k)
388 symbol& base = baseline_objects[shared_names[k]];
389 symbol& test = test_objects[shared_names[k]];
390 test.status = symbol::existing;
391 if (!check_compatible(base, test))
392 incompatible.push_back(symbol_pair(base, test));
395 // Check added names for compatibility.
396 for (size_t l = 0; l < added_names.size(); ++l)
398 symbol& test = test_objects[added_names[l]];
399 test.status = symbol::added;
400 if (!check_version(test, true))
401 incompatible.push_back(symbol_pair(test, test));
404 // Report results.
405 if (verbose && added_names.size())
407 cout << endl << added_names.size() << " added symbols " << endl;
408 for (size_t j = 0; j < added_names.size() ; ++j)
410 cout << j << endl;
411 test_objects[added_names[j]].print();
415 if (verbose && missing_names.size())
417 cout << endl << missing_names.size() << " missing symbols " << endl;
418 for (size_t j = 0; j < missing_names.size() ; ++j)
420 cout << j << endl;
421 baseline_objects[missing_names[j]].print();
425 if (verbose && incompatible.size())
427 cout << endl << incompatible.size() << " incompatible symbols " << endl;
428 for (size_t j = 0; j < incompatible.size() ; ++j)
430 // First, print index.
431 cout << j << endl;
433 // Second, report name.
434 symbol& base = incompatible[j].first;
435 symbol& test = incompatible[j].second;
436 test.print();
438 // Second, report reason or reasons incompatible.
439 check_compatible(base, test, true);
443 cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
444 cout << endl;
445 cout << "# of added symbols:\t\t " << added_names.size() << endl;
446 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
447 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
448 cout << endl;
449 cout << "using: " << baseline_file << endl;
451 return !(missing_names.size() || incompatible.size());
455 symbols
456 create_symbols(const char* file)
458 symbols s;
459 ifstream ifs(file);
460 if (ifs.is_open())
462 // Organize file data into container of symbol objects.
463 symbol_names& names = s.first;
464 symbol_objects& objects = s.second;
465 const string empty;
466 string line = empty;
467 while (getline(ifs, line).good())
469 symbol tmp;
470 tmp.init(line);
471 objects[tmp.raw_name] = tmp;
472 names.push_back(tmp.raw_name);
473 line = empty;
476 else
478 ostringstream os;
479 os << "create_symbols failed for file " << file;
480 __throw_runtime_error(os.str().c_str());
482 return s;
486 const char*
487 demangle(const std::string& mangled)
489 const char* name;
490 if (mangled[0] != '_' || mangled[1] != 'Z')
492 // This is not a mangled symbol, thus has "C" linkage.
493 name = mangled.c_str();
495 else
497 // Use __cxa_demangle to demangle.
498 int status = 0;
499 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
500 if (!name)
502 switch (status)
504 case 0:
505 name = "error code = 0: success";
506 break;
507 case -1:
508 name = "error code = -1: memory allocation failure";
509 break;
510 case -2:
511 name = "error code = -2: invalid mangled name";
512 break;
513 case -3:
514 name = "error code = -3: invalid arguments";
515 break;
516 default:
517 name = "error code unknown - who knows what happened";
521 return name;